]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/ColorLayerImageViewPanel.cxx
92d9fcbf5b693b598c845454072ac566c49a3954
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / ThresholdImageView / ColorLayerImageViewPanel.cxx
1 /**
2  *  \file
3  *  \brief ClassThresholdImageViewPanel .
4  */
5
6
7 #include "ColorLayerImageViewPanel.h"
8 #include "OpenImageDialog.h"
9
10
11 //---------------------------------------------------------
12 //---------------------------------------------------------
13 //---------------------------------------------------------
14
15 wxDlgTransformByDimensionBySpacingByPixel::wxDlgTransformByDimensionBySpacingByPixel()
16 {
17 }
18
19 wxDlgTransformByDimensionBySpacingByPixel::~wxDlgTransformByDimensionBySpacingByPixel()
20 {
21 }
22
23
24 void wxDlgTransformByDimensionBySpacingByPixel::GetTransformType(wxWindow *parent , wxString message,int &typeOfTransformation,bool &dlgWxOK)
25 {
26         wxDialog* dial          = new wxDialog (parent,-1,_T("Tools"),wxDefaultPosition, wxSize(590,190));
27         wxSizer* buttonsSizer   = dial->CreateSeparatedButtonSizer(wxOK|wxCANCEL);
28         wxBoxSizer *dialSizer   = new wxBoxSizer(wxVERTICAL);
29
30         wxString lstOptOperation[3];
31         lstOptOperation[0]=_T("By Dimension");
32         lstOptOperation[1]=_T("By Spacing");
33         lstOptOperation[2]=_T("By Pixel");
34         wxRadioBox * radioOpts = new wxRadioBox(dial, -1, _T("Type of Transformation"), wxDefaultPosition, wxSize(270,45), 3 , lstOptOperation,  3, wxRA_SPECIFY_COLS);
35
36         dialSizer->Add( new wxStaticText(dial,-1,message)  , 0, wxGROW );
37         //      dialSizer->Add( new wxStaticText(dial,-1,_T("selected Roi file. Do you want to tranform it?"))  , 0, wxGROW );
38         dialSizer->Add( new wxStaticText(dial,-1,_T(" "))  , 0, wxGROW );
39         dialSizer->Add(radioOpts,0,wxGROW);
40         dialSizer->Add( new wxStaticText(dial,-1,_T(" "))  , 0, wxGROW );
41         dialSizer->Add(buttonsSizer,0,wxGROW);
42         dial->SetSizer(dialSizer, true);
43         dial->Layout();
44         dial->ShowModal();
45
46         typeOfTransformation    = -1;
47         dlgWxOK                 = false;
48
49         if (dial->GetReturnCode() == wxID_OK)
50         {
51                 typeOfTransformation    = radioOpts->GetSelection() + 1;
52                 dlgWxOK                 = true;
53         }
54 }
55
56
57 //---------------------------------------------------------
58 //---------------------------------------------------------
59 //---------------------------------------------------------
60
61
62 /**
63  ** Begin of the threshold panel
64  **/
65 ColorLayerImageViewPanel::ColorLayerImageViewPanel(wxWindow* parent, int min, int max, int type)
66 : wxPanel(parent, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SUNKEN)
67 {
68         printf("EED ColorLayerImageViewPanel::ColorLayerImageViewPanel start \n");
69
70         _spcBase[0] = _spcBase[1] = _spcBase[2] = 0;
71         _dimBase[0] = _dimBase[1] = _dimBase[2] = 0;
72
73         _colorLayerImageView            = new ColorLayerImageView();
74         _btn_ReadImage                  = new wxButton(this, wxID_ANY, _T("Read Image") );
75         _thresholdGo                    = true;
76         _cb_ShowHide                    = new wxCheckBox(this, wxID_ANY, _T("Show/Hide Layer") );
77         _cb_ShowHide->SetValue(_thresholdGo);
78
79         _cb_SliceFixDinamic = new wxCheckBox(this, wxID_ANY, _T("Fixed/Dynamic Slice") );
80         _cb_SliceFixDinamic->SetValue(true);
81
82         _sl_SliceImage = new wxSlider(this, wxID_ANY, 0, 1000, 1000, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
83         _sl_SliceImage->Enable(false);
84
85         _interpolationCheckBox = new wxCheckBox(this, -1, _T("Image Interpolation") );
86         _interpolationCheckBox->SetValue(true);
87         _opacity = new wxSlider(this, wxID_ANY, 6, 1, 10, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
88
89         if (type==0)
90         {
91         }
92
93         if (type==1)
94         {
95         }
96
97
98         Connect( _btn_ReadImage->GetId(), wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &ColorLayerImageViewPanel::onReadImage );
99         Connect( _cb_ShowHide->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED , (wxObjectEventFunction) &ColorLayerImageViewPanel::onThresholdShow );
100         Connect( _interpolationCheckBox->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) &ColorLayerImageViewPanel::onThresholdInterpolation );
101         Connect( _opacity->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction) &ColorLayerImageViewPanel::onChangeOpacity );
102         Connect( _cb_SliceFixDinamic->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceFixDinamic );
103         Connect( _sl_SliceImage->GetId(), wxEVT_SCROLL_THUMBTRACK       , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
104         Connect( _sl_SliceImage->GetId(), wxEVT_SCROLL_LINEUP           , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
105         Connect( _sl_SliceImage->GetId(), wxEVT_SCROLL_LINEDOWN         , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
106         Connect( _sl_SliceImage->GetId(), wxEVT_SCROLL_PAGEUP           , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
107         Connect( _sl_SliceImage->GetId(), wxEVT_SCROLL_PAGEDOWN         , (wxObjectEventFunction) &ColorLayerImageViewPanel::onSliceImage );
108
109
110         wxFlexGridSizer * sizer         = new wxFlexGridSizer(1);
111         if (type==1)
112         {
113                 sizer -> Add( new wxStaticText(this,-1,_T("Color Image Layer"))  , 1, wxGROW );
114                 sizer -> Add( _btn_ReadImage, 1, wxGROW );
115                 sizer -> Add( new wxStaticText(this,-1,_T(" "))  , 1, wxGROW );
116         }
117         sizer -> Add( new wxStaticText(this,-1,_T("Opacity Level"))  , 1, wxGROW );
118         sizer -> Add( _opacity, 1, wxGROW );
119         sizer -> Add( new wxStaticText(this,-1,_T(" "))  , 1, wxGROW );
120         sizer -> Add( _cb_ShowHide, 1, wxGROW );
121         sizer -> Add( new wxStaticText(this,-1,_T(" "))  , 1, wxGROW );
122         sizer -> Add( _interpolationCheckBox, 1, wxGROW );
123
124         sizer -> Add( new wxStaticText(this,-1,_T(" "))  , 1, wxGROW );
125         sizer -> Add( new wxStaticText(this,-1,_T("Slice "))  , 1, wxGROW );
126         sizer -> Add( _sl_SliceImage, 1, wxGROW );
127         sizer -> Add( _cb_SliceFixDinamic, 1, wxGROW );
128
129         this->SetSizer( sizer );
130         this->SetAutoLayout( true );
131         this->Layout();
132      
133         //CM Sets the default fitting mode to Pixel mode.
134         _fitting_mode = 3;
135         // EO CM
136 }
137
138 //----------------------------------------------------------------------------
139 ColorLayerImageViewPanel::~ColorLayerImageViewPanel()
140 {
141 }
142
143
144 //----------------------------------------------------------------------------
145 void ColorLayerImageViewPanel::SetColorLayerImageView(ColorLayerImageView* colorLayerImageView)
146 {
147         _colorLayerImageView = colorLayerImageView;
148 }
149
150 //----------------------------------------------------------------------------
151 ColorLayerImageView* ColorLayerImageViewPanel::GetColorLayerImageView()
152 {
153         return _colorLayerImageView;
154 }
155
156
157 // This is the new spacing of the background image  (1,1,1)
158 void ColorLayerImageViewPanel::SetBaseSpacing(double spc[3])
159 {
160         _spcBase[0] = spc[0];
161         _spcBase[1] = spc[1];
162         _spcBase[2] = spc[2];
163
164         _spcBase[0] = 1;
165         _spcBase[1] = 1;
166         _spcBase[2] = 1;
167 }
168
169 // This is the Original spacing of the background image
170 void ColorLayerImageViewPanel::SetOriginalSpacing(double spc[3])
171 {
172         _spcOrg[0] = spc[0];
173         _spcOrg[1] = spc[1];
174         _spcOrg[2] = spc[2];
175 }
176
177 // This is the size in pixels of the background image
178 void ColorLayerImageViewPanel::SetBaseDimension(int dim[3])
179 {
180         _dimBase[0] = dim[0];
181         _dimBase[1] = dim[1];
182         _dimBase[2] = dim[2];
183 }
184
185 //----------------------------------------------------------------------------
186 void ColorLayerImageViewPanel::SetImage(vtkImageData *img)
187 {
188     if (img!=NULL)
189     {
190         _colorLayerImageView->SetImage( img );
191
192         double spc[3];
193         img->GetSpacing(spc);
194
195         int dim[3];
196         int ext[6];
197         img->GetWholeExtent(ext);
198         dim[0] = ext[1]-ext[0];
199         dim[1] = ext[3]-ext[2];
200         dim[2] = ext[5]-ext[4];
201         _sl_SliceImage->SetRange( 0 , dim[2] );
202
203         if ( (spc[0]!=_spcBase[0]) || (spc[1]!=_spcBase[1]) || (spc[2]!=_spcBase[2]) ||
204              (dim[0]!=_dimBase[0]) || (spc[1]!=_spcBase[1]) || (spc[2]!=_spcBase[2])
205             )
206         {
207           // CM
208           int typeOfTransformation;     
209           // In case of the option is set to create a dialog box.
210           if (_fitting_mode == -1)
211             {
212               bool transformOkDlg;
213               wxString msg = _T("The image resolution of both images are not compatible. Do you want to tranform it? ");
214               wxDlgTransformByDimensionBySpacingByPixel dlg;
215               dlg.GetTransformType(this,msg,typeOfTransformation,transformOkDlg);
216               // CM Reset the default value (Pixel i.e. 3) if the dialog box returned an inappropriate value.
217               if (transformOkDlg == false)
218                 typeOfTransformation = 3;
219             }
220           else
221             typeOfTransformation = _fitting_mode;
222           //EO CM
223
224           printf("EED ColorLayerImageViewPanel::onReadImage call dialog spc size,dim...???\n");
225
226             if (typeOfTransformation==1)  // make dimensions equals
227             {
228                 printf("EED ColorLayerImageViewPanel::onReadImage ...WARNNING.... dif size image spc*dim ofnew image ...???\n");
229                 spc[0]=_spcBase[0]*_dimBase[0]/dim[0];
230                 spc[1]=_spcBase[1]*_dimBase[1]/dim[1];
231                 spc[2] = 1;
232             }
233
234             if (typeOfTransformation==2)  // make spacing equals
235             {
236                 spc[0] = ( _spcBase[0]/spc[0] ) * _spcOrg[0];
237                 spc[1] = ( _spcBase[1]/spc[1] ) * _spcOrg[1];
238                 spc[2] = ( _spcBase[2]/spc[2] ) * _spcOrg[2];
239             }
240
241             if (typeOfTransformation==3)  // make spacing = 1
242             {
243                 spc[0] = 1;
244                 spc[1] = 1;
245                 spc[2] = 1;
246             }
247
248             img->SetSpacing(spc);
249         } // spc !_spcBase   dim!__dimBase
250
251         if (_colorLayerImageView!=NULL)
252         {
253             _colorLayerImageView->onThreshold();
254             _colorLayerImageView->Refresh();
255         }
256
257
258     }
259 }
260
261 //----------------------------------------------------------------------------
262 void ColorLayerImageViewPanel::SetFittingMode(int fitting_mode)
263 {
264   // CM Sets the fitting mode if an appropriate value is provided.
265   if (fitting_mode == -1 || fitting_mode == 1 || fitting_mode == 2 || fitting_mode == 3)
266     {
267       _fitting_mode = fitting_mode;
268     }
269   //Otherwise, an exception should be thrown (value 0 is fine, it corresponds to an empty field i.e. the default value is kept).
270   else if (fitting_mode != 0)
271     std::cout << "CM ERROR!!! The fitting mode provided has an inappropriate value. It should be an int between -1 ; 1 ; 2 ; 3, but its value is :" << fitting_mode << ". The default value will be set instead, i.e. Pixel transformation (3)." <<  std::endl;
272   //EO CM
273 }
274
275 //----------------------------------------------------------------------------
276 int ColorLayerImageViewPanel::GetFittingMode()
277 {
278   return _fitting_mode;
279 }
280
281 //----------------------------------------------------------------------------
282 void ColorLayerImageViewPanel::onReadImage(wxCommandEvent& event)
283 {
284 // EED ???
285 //      creaImageIO::WxSimpleDlg w(0,_T("Select your image"),"creaContours_Descriptor.dscp","creatisContours DB");
286 //      w.ShowModal();
287         printf("EED ColorLayerImageViewPanel::onReadImage ....WARNING... Read dlg. all images creaImageIO ... ???\n");
288
289         creaMaracasVisuKernel::OpenImageDialog diag = creaMaracasVisuKernel::OpenImageDialog();
290         SetImage( diag.getImageData() );
291 }
292
293 //----------------------------------------------------------------------------
294 void ColorLayerImageViewPanel::onThresholdChange(wxCommandEvent& event)
295 {
296         if (_colorLayerImageView!=NULL)
297         {
298         if (_thresholdGo)
299         {
300             _colorLayerImageView->onThresholdChange();
301             _colorLayerImageView->Refresh();
302             //std::cout<< "Valor Min: " << minVal << " & Valor Max: " << maxVal  << std::endl;
303         } // _threshold
304         }//_colorLayerImageView
305 }
306
307 //----------------------------------------------------------------------------
308 void ColorLayerImageViewPanel::onThresholdShow(wxCommandEvent& event)
309 {
310         if (_colorLayerImageView!=NULL)
311         {
312         _thresholdGo = _cb_ShowHide->GetValue();
313         if (_thresholdGo)
314         {
315             _colorLayerImageView->onThreshold();
316         }else{
317             _colorLayerImageView->onThresholdRemove( );
318         }
319         _colorLayerImageView->Refresh();
320         }//_colorLayerImageView
321 }
322
323 //----------------------------------------------------------------------------
324 void ColorLayerImageViewPanel::onThresholdStop()
325 {
326         if (_colorLayerImageView!=NULL)
327         {
328         if (_thresholdGo)
329         {
330             _colorLayerImageView->onThresholdRemove( );
331             _thresholdGo=false;
332         }
333         }//_colorLayerImageView
334 }
335
336 //----------------------------------------------------------------------------
337 void ColorLayerImageViewPanel::onThresholdInterpolation(wxCommandEvent& event)
338 {
339         if (_colorLayerImageView!=NULL)
340         {
341         _colorLayerImageView->onThresholdInterpolation(_interpolationCheckBox->GetValue());
342         _colorLayerImageView->Refresh();
343         }//_colorLayerImageView
344 }
345
346 //----------------------------------------------------------------------------
347 void ColorLayerImageViewPanel::onSliceFixDinamic(wxCommandEvent& event)
348 {
349         if (_colorLayerImageView!=NULL)
350         {
351         bool fixdin = _cb_SliceFixDinamic->GetValue();
352         _colorLayerImageView->SetSliceFixDynamic( fixdin );
353         if (fixdin==false)
354         {
355             _colorLayerImageView->SetZ2( _sl_SliceImage->GetValue() );
356         }
357         _sl_SliceImage->Enable(!fixdin);
358         _colorLayerImageView->onThreshold();
359         _colorLayerImageView->Refresh();
360         }//_colorLayerImageView
361 }
362
363 //----------------------------------------------------------------------------
364 void ColorLayerImageViewPanel::onSliceImage(wxScrollEvent& event)
365 {
366         if (_colorLayerImageView!=NULL)
367         {
368         _colorLayerImageView->SetZ2( _sl_SliceImage->GetValue() );
369         _colorLayerImageView->onThreshold();
370         _colorLayerImageView->Refresh();
371         }//_colorLayerImageView
372 }
373
374 //----------------------------------------------------------------------------
375 void ColorLayerImageViewPanel::onChangeOpacity(wxScrollEvent& event)
376 {
377         if (_colorLayerImageView!=NULL)
378         {
379         int opacity = _opacity->GetValue();
380         _colorLayerImageView->onThresholdChangeOpacity(opacity);
381         _colorLayerImageView->Refresh();
382         }//_colorLayerImageView
383 }
384
385
386 //----------------------------------------------------------------------------
387 bool ColorLayerImageViewPanel::IsVisible()
388 {
389         return _thresholdGo;
390 }
391
392
393
394 // EOF
395