]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/wxILPDwithImage.cxx
Avoid warnings
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / wxILPDwithImage.cxx
1 /*=========================================================================
2
3   Program:   wxMaracas
4   Module:    $RCSfile: wxILPDwithImage.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/10/31 16:32:09 $
7   Version:   $Revision: 1.1 $
8
9   Copyright: (c) 2002, 2003
10   License:
11   
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notice for more information.
15
16 =========================================================================*/
17
18
19 #include "wxILPDwithImage.h"
20
21
22 #include "ILPD/src/controlerInterface/ILPDViewListPatient.h"
23 #include "ILPD/src/wxInterface/ILPDViewWxWindow.h"
24
25 #include <gdcm.h> 
26 #include <gdcmFile.h> 
27 #include "vtkGdcmReader.h"
28
29 #include "marImageData.h"
30 #include "marGdcmDicom.h"
31
32 #include <vtkImageData.h>
33
34
35
36 //----------------------------------------------------------------------------
37 //----------------------------------------------------------------------------
38 //----------------------------------------------------------------------------
39
40
41 BEGIN_EVENT_TABLE(wxILPDwithImage , wxPanel)
42       EVT_MENU(20001, wxILPDwithImage::OnSelectPatientStudySerieImage)
43 END_EVENT_TABLE()
44
45
46 //----------------------------------------------------------------------------
47
48 wxILPDwithImage::wxILPDwithImage(wxWindow *parent)
49        : wxPanel(parent, -1)
50 {
51
52         _wxvtk2Dbaseview                        = NULL;
53         _vtkbasedata                            = NULL; 
54
55
56         wxPanel *panel=this; 
57         panel->SetAutoLayout(true);
58
59
60         wxBoxSizer                      *topsizer                                       = new wxBoxSizer(wxVERTICAL  );
61     _pnlSplitter_ViewImage_wxILPD                                       = new wxSplitterWindow( panel , -1);
62     _wxILPD                                                                                     = new wxILPD( _pnlSplitter_ViewImage_wxILPD );
63         ILPDViewListPatient*    _ilpdViewListPatient    = new ILPDViewListPatient( new ILPDViewWxWindow(_wxILPD) );
64         wxPanel                                 *viewPanel                              = CreatePanel_ViewImagePanel_WxILPD( _pnlSplitter_ViewImage_wxILPD );
65
66         topsizer -> Add( _pnlSplitter_ViewImage_wxILPD ,1,wxGROW  ,0);
67
68         int ww,hh;
69         wxWindow *pp=this;
70         while (pp->GetParent()!=NULL) pp=pp->GetParent();
71         pp->GetSize(&ww,&hh);
72         GetSize(&ww,&hh);
73
74 //EEDxx2.4
75 //      pnlSplitter                     -> SplitHorizontally( viewPanel , _wxILPD, (int)(hh*0.45) );
76         _pnlSplitter_ViewImage_wxILPD                   -> SplitVertically(   _wxILPD , viewPanel,300);
77         _pnlSplitter_ViewImage_wxILPD           -> SetMinimumPaneSize( 10 );
78
79 //EEDxx2.4
80 //      panel->FitInside();
81
82
83         panel->SetSizer( topsizer );      // use the sizer for layout
84         panel->Layout(); 
85
86 }
87
88 //----------------------------------------------------------------------------
89 wxILPDwithImage::~wxILPDwithImage()
90 {
91         if (_vtkbasedata!=NULL)         {       delete  _vtkbasedata;           }
92     if (_wxvtk2Dbaseview!=NULL) {       delete  _wxvtk2Dbaseview;       }
93 }
94
95 //----------------------------------------------------------------------------
96 wxPanel *wxILPDwithImage::CreatePanel_ViewImagePanel_WxILPD( wxWindow *parent )
97 {
98         wxPanel *panel =new wxPanel( parent, -1 ); 
99
100         wxBoxSizer                                              *topsizer               = new wxBoxSizer(wxHORIZONTAL  );
101                                  _pnlSplitter_Image_DicomList           = new wxSplitterWindow( panel , -1);
102
103         _panelImage                                                     = new wxPanel(_pnlSplitter_Image_DicomList,-1);
104         _panelImage->SetBackgroundColour( wxColour(0,0,0) );
105         wxBoxSizer *sizer                                       = new wxBoxSizer(wxHORIZONTAL  );
106 //      _panelImage->SetAutoLayout(true);
107         _panelImage->SetSizer( sizer );      // use the sizer for layout
108 //      _panelImage->Layout(); 
109 //      _panelImage->FitInside();
110
111         int ww,hh;
112         wxWindow *pp=this;
113         while (pp->GetParent()!=NULL) pp=pp->GetParent();
114         pp->GetSize(&ww,&hh);
115
116
117         _dicomPanelListView                     =       new wxListView( _pnlSplitter_Image_DicomList, -1  );
118         _pnlSplitter_Image_DicomList->SetMinimumPaneSize( 200 );
119
120 //EEDxx2.4
121 //      pnlSplitter                     -> SplitVertically( _panelImage, _dicomPanelListView, (int)(ww*0.45) );
122         _pnlSplitter_Image_DicomList                    -> SplitHorizontally( _panelImage, _dicomPanelListView, 300);
123
124         topsizer                        -> Add( _pnlSplitter_Image_DicomList ,1,wxGROW  ,0);
125
126         panel->SetAutoLayout(true);
127         panel->SetSizer( topsizer );      // use the sizer for layout
128         panel->Layout(); 
129
130 //EEDxx2.4
131 //      panel->FitInside();
132
133
134         return panel;
135 }
136
137
138 //----------------------------------------------------------------------------
139
140 void wxILPDwithImage::SelectedSerie()
141 {
142         if (_wxILPD->GetTypeOfSelection() == 3)
143         {
144                 vector <string*> lstStringFileName;
145                 _wxILPD->GetLstImageFiles(&lstStringFileName);
146
147 //EED 4 oct 2006
148 //              marGdcmDicomILPD *margdcmdicomILPD = (marGdcmDicomILPD*)_mar->_dicom;
149 //              margdcmdicomILPD->SetListImages( &lstStringFileName );
150
151                 if (_backStringFirstFile==*(lstStringFileName[0]))
152                 {
153 //EED 14Avril2008 OJO
154 //                      _noteBook->SetPageImage(1, 4);
155                 } else {
156 //EED 14Avril2008 OJO
157 //                      _noteBook->SetPageImage(1, 3);
158                 }
159
160                 // Show Image
161                 int i,sizeLstNameFiles=lstStringFileName.size();
162                 i=sizeLstNameFiles/2;
163
164                 ShowDicomImage( lstStringFileName[i] );
165
166                 // Show Number of Images
167                 _dicomPanelListView->InsertColumn(0, _T("Number of Images")              , wxLIST_FORMAT_LEFT, 100);
168                 wxString text;
169                 text.Printf(_T("%d"),sizeLstNameFiles);
170                 SetItem(_dicomPanelListView,0,0, text );
171
172
173         } else {
174 //EED 14Avril2008 OJO
175 //              _noteBook->SetPageImage(1, 5);
176         }
177
178 }
179
180 //----------------------------------------------------------------------------
181
182 void wxILPDwithImage::ShowDicomInformation(string *fileImageName)
183 {
184
185
186         GDCM_NAME_SPACE::File *gdcmFile = GDCM_NAME_SPACE::File::New();
187         gdcmFile->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);
188         gdcmFile->SetFileName( fileImageName->c_str() );
189         bool ok = gdcmFile->Load();
190
191         // Filling lines
192         if (gdcmFile->IsEmpty()==true)
193         {
194                 _dicomPanelListView->InsertColumn(0, _T("Error")        , wxLIST_FORMAT_LEFT, 80);
195                 SetItem(_dicomPanelListView,0,0, _T("File not found...") );
196         } else {
197
198                 // Creating Columns
199                 _dicomPanelListView->InsertColumn(0, _T("Tag")          , wxLIST_FORMAT_LEFT, 80);
200                 _dicomPanelListView->InsertColumn(1, _T("VR")           , wxLIST_FORMAT_LEFT, 40);
201                 _dicomPanelListView->InsertColumn(2, _T("Name")         , wxLIST_FORMAT_LEFT, 150);
202                 _dicomPanelListView->InsertColumn(3, _T("Value")        , wxLIST_FORMAT_LEFT, 400);
203
204
205
206                 int line=0;
207                 GDCM_NAME_SPACE::DataEntry *entry;
208                 entry = (GDCM_NAME_SPACE::DataEntry*)gdcmFile->GetFirstEntry();
209                 while (entry!=NULL)
210                 {
211                         string groupelem = GDCM_NAME_SPACE::Util::Format("%04x | %04x" , entry->GetGroup() , entry->GetElement());
212                         SetItem(_dicomPanelListView,line,0, wxString(groupelem.c_str(), wxConvUTF8) );
213
214                         string vr =entry->GetVR().str();
215                         SetItem(_dicomPanelListView,line,1, wxString(vr.c_str(), wxConvUTF8) );
216                         SetItem(_dicomPanelListView,line,2, wxString(entry->GetName().c_str(), wxConvUTF8) );
217
218                         if (entry->GetBinArea())
219                         {
220                                 // Print Value
221                                 string value;
222                                 if( vr == "US" || vr == "SS" || vr == "UL" || vr == "SL" ||  vr == "FL"  ||  vr == "FD")
223                                 {
224                                         value   = entry->GetString();
225                                 } else  {
226                                         value   =  GDCM_NAME_SPACE::Util::CreateCleanString( entry->GetBinArea(),entry->GetLength()  );
227                                 }
228                                 value = "[" + value + "]" ;
229                                 SetItem(_dicomPanelListView,line,3, wxString(value.c_str(), wxConvUTF8) );
230                         } else {
231                                 SetItem(_dicomPanelListView,line,3, _T("-  NOT LOADED  -") );
232                         }
233                         entry = (GDCM_NAME_SPACE::DataEntry*)gdcmFile->GetNextEntry();
234                         line++;
235                 } // while
236         } //IsEmpty
237         gdcmFile->Delete();
238 }
239
240 //----------------------------------------------------------------------------
241
242 void wxILPDwithImage::ShowDicomImage(string *fileImageName)
243 {
244
245         bool            reconfigure=false;
246         double          colorwindow;
247         double          colorlevel;
248
249
250         FILE *ff = fopen( fileImageName->c_str(),"r");
251         if (ff==NULL)
252         {
253 //              string msgText= "File not found: \n\n";
254 //              msgText.append(fileImageName->c_str());
255 //              wxMessageDialog msgBox(this, msgText.c_str(),"Read file ERROR", wxOK );
256 //              msgBox.ShowModal();
257         } else {
258                 fclose(ff);
259                 if (_wxvtk2Dbaseview!=NULL)
260                 {
261                         reconfigure=true;
262                         colorwindow = _wxvtk2Dbaseview->_imageViewer2XYZ->GetVtkImageViewer2()->GetColorWindow();
263                         colorlevel      = _wxvtk2Dbaseview->_imageViewer2XYZ->GetVtkImageViewer2()->GetColorLevel();
264                 }
265
266
267                 // Read header file
268                 GDCM_NAME_SPACE::File *gdcmFile = GDCM_NAME_SPACE::File::New();
269                 gdcmFile->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOW);
270                 gdcmFile->SetFileName( fileImageName->c_str() );
271                 bool ok = gdcmFile->Load();
272                 long int offset = gdcmFile->GetPixelOffset(); 
273                 int dim[3];
274                 dim[0]=gdcmFile->GetXSize();
275                 dim[1]=gdcmFile->GetYSize();
276                 dim[2]=1; 
277
278
279                 // Memory allocation
280                 vtkImageData *imageData =       vtkImageData::New();
281                 imageData->SetDimensions ( dim );
282                 imageData->SetScalarTypeToUnsignedShort();
283                 imageData->AllocateScalars();    
284                 imageData->Update();  
285
286                 marImageData *marimagedata = new marImageData(imageData);
287                 
288                 // Read Image
289                 marGdcmDicom margdcmdicom;
290                 void *p_vol = imageData->GetScalarPointer(0,0,0);
291                 margdcmdicom.loadFileDicom( p_vol , dim , (char *)fileImageName->c_str() );
292
293 // EED 17 Oct 2007 Before
294 //              if ( _wxvtk2Dbaseview   )       { delete _wxvtk2Dbaseview;      } 
295 //              _wxvtk2Dbaseview        = new wxVtk2DBaseView( _panelImage );
296
297
298 // EED 17 Oct 2007 After
299                 if ( _wxvtk2Dbaseview==NULL     )       
300                 { 
301                         _wxvtk2Dbaseview        = new wxVtk2DBaseView( _panelImage );
302                         wxVTKRenderWindowInteractor *imagePanel = _wxvtk2Dbaseview->GetWxVTKRenderWindowInteractor();       
303                         wxSizer *sizer=_panelImage->GetSizer();
304                         sizer-> Add( imagePanel ,1,wxGROW  ,0);
305                         _panelImage->SetAutoLayout(true);
306                         _panelImage->Layout();  
307                 } 
308
309
310                 if ( _vtkbasedata               )       { delete _vtkbasedata;          } 
311 //              if ( _widgetMesure              )       { delete _widgetMesure;         } 
312
313
314 //_widgetMesure                         = new wxWidgetMesure2D(_panelImage);
315
316 //_wxvtk2Dbaseview                      = new wxVtk2DBaseView( _widgetMesure );
317
318
319
320                 _vtkbasedata            = new vtkBaseData(); 
321                 _vtkbasedata->SetMarImageData( marimagedata );
322                 _wxvtk2Dbaseview->SetVtkBaseData( _vtkbasedata );
323                 _wxvtk2Dbaseview->Configure(true);
324
325 //_widgetMesure->ConfigureA(_wxvtk2Dbaseview);
326 //_widgetMesure ->      SetMesureScale( gdcmFile->GetXSpacing() );
327
328                 gdcmFile->Delete();
329                 
330 //sizer-> Add( _widgetMesure ,1,wxGROW  ,0);
331
332
333                 _wxvtk2Dbaseview->Refresh();
334
335                 if (reconfigure==true)
336                 {
337                         _wxvtk2Dbaseview->_imageViewer2XYZ->GetVtkImageViewer2()->SetColorWindow( colorwindow );
338                         _wxvtk2Dbaseview->_imageViewer2XYZ->GetVtkImageViewer2()->SetColorLevel( colorlevel );
339                 }
340
341         }
342
343 }
344
345
346 //----------------------------------------------------------------------------
347 void wxILPDwithImage::ResetwxListView()
348 {
349         // Reset wxListView
350         _dicomPanelListView->DeleteAllItems();
351         int i,size=_dicomPanelListView->GetColumnCount();
352         for (i=0;i<size;i++)
353         {
354                 _dicomPanelListView->DeleteColumn(0);
355         }
356 }
357
358 //----------------------------------------------------------------------------
359
360 void wxILPDwithImage::SelectedImage()
361 {
362         if (_wxILPD->GetTypeOfSelection() == 4)
363         {
364                 string fileImageName;
365                 _wxILPD->GetFileImageName( &fileImageName );
366                 ShowDicomInformation( &fileImageName );
367                 ShowDicomImage(&fileImageName);
368         }
369 }
370
371 //----------------------------------------------------------------------------
372
373 void wxILPDwithImage::SelectedPatient()
374 {
375         if (_wxILPD->GetTypeOfSelection() == 1)
376         {
377                 _dicomPanelListView->InsertColumn(0, _T("Number of Series")              , wxLIST_FORMAT_LEFT, 80);
378                 int numberOfSeries = _wxILPD->GetNumberOfSeries();
379                 wxString text;
380                 text.Printf(_T("%d"),numberOfSeries);
381                 SetItem(_dicomPanelListView,0,0, text );
382         }
383 }
384
385 //----------------------------------------------------------------------------
386
387 void wxILPDwithImage::OnSelectPatientStudySerieImage(wxCommandEvent& event)
388 {
389         ResetwxListView();
390         SelectedPatient();
391
392         SelectedSerie();
393         SelectedImage();
394
395         // Reset Image if there is no selection
396         if ((_wxILPD->GetTypeOfSelection() == 4) || (_wxILPD->GetTypeOfSelection() == 3))
397         {
398         } else {
399                 if (_vtkbasedata)               
400                 { 
401                         delete _vtkbasedata;     
402                         _vtkbasedata=NULL;
403                 } 
404                 if (_wxvtk2Dbaseview)   
405                 { 
406 //                      delete _wxvtk2Dbaseview; 
407 //                      _wxvtk2Dbaseview=NULL;
408                 } 
409         }
410 }
411
412
413 //----------------------------------------------------------------------------
414 void wxILPDwithImage::SetItem( wxListView *wxlistview, int line, int col, wxString text )  
415 {
416         if (wxlistview->GetItemCount() <= line)
417         {
418                 wxlistview->InsertItem(line,_T(" "),1);
419         } 
420         wxListItem item;
421         item.m_itemId = line;
422         item.SetTextColour(*wxBLACK);
423         item.SetFont(*wxNORMAL_FONT);   // or wxNORMAL_FONT, wxSMALL_FONT, wxSWISS_FONT
424         item.SetBackgroundColour(wxColour(255,255,255));
425         wxlistview->SetItem( item );
426         wxlistview->SetItem(line,col,text);
427 }
428
429
430
431
432
433 //----------------------------------------------------------------------------
434
435 wxILPD  *wxILPDwithImage::GetWxILPD()
436 {
437         return _wxILPD;
438 }
439
440
441
442
443
444
445
446 //----------------------------------------------------------------------------
447 void wxILPDwithImage::Configure()
448 {
449         _pnlSplitter_ViewImage_wxILPD   -> SetSashPosition(850);
450         _pnlSplitter_Image_DicomList    -> SetSashPosition(400);
451 }
452