]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/HistogramDialogComboBox.cxx
#2846 creaMaracasVisu Bug New Normal - HistogramDialogComboBox not finding good...
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / pPlotter / HistogramDialogComboBox.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 #include "HistogramDialogComboBox.h"
27 #include <vector>
28
29 #include "Edit.xpm"
30
31 #include "creaSystem.h"
32 #include "HistogramDialog.h"
33 //-------------------------
34 //Constructor
35 //-------------------------
36 HistogramDialogComboBox::HistogramDialogComboBox(wxWindow* parent)
37 : wxPanel(parent)
38 {
39         _img                            = NULL;
40         _bitmapcombo            = NULL;
41         _slidercolor            = NULL;
42         _sliderwindowlevel      = NULL;
43         _ckboxActive            = NULL;
44         colorBar_Bitmap         = NULL;
45         _bitmapsizer            = NULL;
46         InitCurrentPathColorsFuntion();
47         this->SetSizer(getBitmapCombo());
48         _img                            = NULL;
49         _currentitem            = -1;    
50         _maxgreyvalue           = 0;
51 }
52
53
54 HistogramDialogComboBox::~HistogramDialogComboBox()
55 {
56 }
57
58 void HistogramDialogComboBox::SetColors(std::vector<double> greyvect, std::vector<double> redvect, std::vector<double> greenvect, std::vector<double> bluevect)
59 {
60         _greyvect       = greyvect;
61         _redvect        = redvect;
62         _greenvect      = greenvect;
63         _bluevect       = bluevect;
64 }
65
66 wxSizer* HistogramDialogComboBox::getBitmapCombo()
67 {
68         //_bitmapsizer = new wxBoxSizer(wxVERTICAL);
69         _bitmapsizer = new wxBoxSizer(wxVERTICAL);
70
71         wxBoxSizer* comboeditsizer = new wxBoxSizer(wxHORIZONTAL);
72         _bitmapcombo = getBitmapComboElements();
73     BitmapComboItemSelect(0);
74         wxBitmap bitmap1(Edit_xpm);
75         wxBitmapButton* edit = new wxBitmapButton(this, -1, bitmap1,wxDefaultPosition,wxSize(30,30));
76         Connect(edit->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&HistogramDialogComboBox::OnEditBitmapCombo);
77         _ckboxActive= new wxCheckBox( this, 
78                               -1, 
79                               _T(" "),
80                               wxDefaultPosition, 
81                               //wxSize(sizeX,sizeY),
82                               wxDefaultSize, 
83                               wxCHK_2STATE | wxALIGN_RIGHT);      
84         _ckboxActive->SetValue(false);
85
86     Connect( _ckboxActive->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) (void (wxPanel::*)(wxScrollEvent&))  &HistogramDialogComboBox::OnChkBoxActive);
87
88         comboeditsizer->Add(_ckboxActive,wxSizerFlags().Center().FixedMinSize());
89         comboeditsizer->Add(_bitmapcombo,wxSizerFlags().Center().FixedMinSize());
90         comboeditsizer->Add(edit,wxSizerFlags().Center());
91         _bitmapsizer->Add(comboeditsizer,wxSizerFlags().FixedMinSize().Center());
92         _bitmapsizer->AddSpacer(5);
93         _bitmapsizer->Add(getSlidersWlCo(),wxSizerFlags().Expand().Center());
94         return _bitmapsizer;
95 }
96
97 wxSizer* HistogramDialogComboBox::getSlidersWlCo()
98 {
99         wxBoxSizer* sizersliders        = new wxBoxSizer(wxVERTICAL);
100         _slidercolor                            = new wxSlider(this, -1,1,0,1,wxDefaultPosition,wxDefaultSize,wxSL_LABELS);
101         _sliderwindowlevel                      = new wxSlider(this, -1,1,0,1,wxDefaultPosition,wxDefaultSize,wxSL_LABELS);
102         sizersliders->Add(_slidercolor,wxSizerFlags().Expand().Center());
103         sizersliders->Add(_sliderwindowlevel,wxSizerFlags().Expand().Center());
104     Connect(_slidercolor->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&HistogramDialogComboBox::OnColorLevelChanged);
105         Connect(_sliderwindowlevel->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&HistogramDialogComboBox::OnWindowLevelChanged);
106         return sizersliders;
107 }
108
109 double HistogramDialogComboBox::GetWindowLevel()
110 {
111    double val(0.0);
112    if(_sliderwindowlevel != NULL)
113         {
114       val= _sliderwindowlevel->GetValue();
115         }
116    return val; // JPR
117 }
118
119 double HistogramDialogComboBox::GetColorLevel()
120 {
121    double val(0.0);
122    if(_slidercolor != NULL)
123         {
124       val = _slidercolor->GetValue();
125         }
126    return val; // JPR
127 }
128
129
130 bool HistogramDialogComboBox::GetActive()
131 {
132         bool result = false;
133         if (_ckboxActive!=NULL) 
134         {
135                 result = _ckboxActive->GetValue();
136         }
137         return result;
138 }
139
140
141 void HistogramDialogComboBox::OnChkBoxActive(wxCommandEvent& event)
142 {
143    wxCommandEvent newevent(wxEVT_COMMAND_COMBOBOX_SELECTED,this->GetId());
144    ProcessEvent(newevent);
145 }
146
147
148 void HistogramDialogComboBox::OnColorLevelChanged(wxCommandEvent& event)
149 {
150    _slidercolor->GetValue();
151    wxCommandEvent newevent(wxEVT_SCROLL_THUMBTRACK,this->GetId());
152    ProcessEvent(newevent);
153 }
154
155 void HistogramDialogComboBox::OnWindowLevelChanged(wxCommandEvent& event)
156 {
157    _sliderwindowlevel->GetValue();
158    wxCommandEvent newevent(wxEVT_SCROLL_THUMBTRACK,this->GetId());
159    ProcessEvent(newevent);
160 }
161
162 void HistogramDialogComboBox::setImageData(vtkImageData* img)
163 {
164    _maxgreyvalue = img->GetScalarRange()[1];
165    _img = img;
166    setSlidersValue();
167 }
168
169 void HistogramDialogComboBox::setSlidersValue()
170 {
171         if(_img!=NULL){
172                 if(_slidercolor!=NULL){
173                         _slidercolor->SetRange(_img->GetScalarRange()[0],_img->GetScalarRange()[1]);
174                         _slidercolor->SetValue((_img->GetScalarRange()[0]+_img->GetScalarRange()[1])/2);
175                 }
176                 if(_sliderwindowlevel!=NULL){
177                         _sliderwindowlevel->SetRange(_img->GetScalarRange()[0],_img->GetScalarRange()[1]);
178                         _sliderwindowlevel->SetValue((_img->GetScalarRange()[0]+_img->GetScalarRange()[1])/2);
179                 }
180         }
181 }
182
183 void HistogramDialogComboBox::OnEditBitmapCombo(wxCommandEvent& event)
184 {
185         if(_img!=NULL)
186         {
187                 bool createaddandremovecontrols = true;
188                 HistogramDialog* dialog = new HistogramDialog(this,_T("Color Configuration"),createaddandremovecontrols);
189                 dialog->initializeHistogram(_img);
190                 dialog->setInfoPanExtraControls(_bitmapsitems);
191
192                 if(dialog->ShowModal()==wxID_OK)
193                 {
194                         dialog->UpdateCurrentComboElement();
195                         _bitmapsitems = dialog->getComboBoxItems();
196
197                         std::vector<double> redvect,greenvect,bluevect;
198                         std::vector<double> greyvect;
199
200                         saveCurrentConfiguration(_bitmapsitems, GetFileNameOfColorsFunction() );
201                         wxBitmapComboBox* tempbitmapcombo = _bitmapcombo;
202                         for(int i = 0; i < _bitmapsitems.size();i++)
203                         {
204                                 delete _bitmapsitems[i];
205                         }
206                         _bitmapsitems.clear();
207                         _bitmapcombo = getBitmapComboElements();
208                         
209 // EED Eraseme                  
210 //                      if(_bitmapsizer->Replace(tempbitmapcombo,_bitmapcombo))
211 //                      {
212 //                              tempbitmapcombo->Destroy();
213 //                      } else {
214 //                              printf ("EED ERROR HistogramDialogComboBox::OnEditBitmapCombo. Can't replace bitmapcombo \n");
215 //                      }
216                         
217                         this->Layout();
218                 }
219                 dialog->Destroy();
220         }
221 }
222
223
224 void HistogramDialogComboBox::saveCurrentConfiguration(std::vector<HistogramDialogComboBoxItem*>& itembitmaps,std::string filename)
225 {
226         std::ofstream file;
227         file.open(  (const char*) (filename.c_str()) );
228         double gv=-1,red=-1,gr=-1,bl=-1;
229         int in=-1;
230
231         if(file.is_open())
232         {
233                 file << "<ComboBoxData>" << std::endl;
234                 for(int i = 0; i < itembitmaps.size();i++)
235                 {
236                         HistogramDialogComboBoxItem* item = itembitmaps[i];
237                         std::vector<double> vecttransfer = item->getGreyVectorTransfer();
238                         std::vector<double> value = item->getValueVector();
239
240                         file << " <ComboBoxItem>"<< std::endl;
241                         for(int j = 0; j < vecttransfer.size();j++)
242                         {
243                                 if(j==0){
244                                         file << " <TransferenceFunction>"<< std::endl;
245                                 }
246                                 file << "               <Transferencepoint>" << std::endl;
247                                 file << "                       <greyValue>" << vecttransfer[j] << "</greyValue>" <<std::endl;
248                                 file << "                       <intensity>" << value[j] << "</intensity> "<<std::endl;
249                                 file << "               </Transferencepoint>" << std::endl;
250                                 if(j==vecttransfer.size()-1){
251                     file << " </TransferenceFunction>"<< std::endl;
252                                 }
253                         }
254                         std::vector<double> greyv = item->getGreyVector();
255                         std::vector<double> red = item->getRedVector();
256                         std::vector<double> green = item->getGreenVector();
257                         std::vector<double> blue = item->getBlueVector();
258                         for(int j = 0; j < greyv.size();j++)
259                         {
260                                 if(j==0){
261                                         file << "       <Colors>" << std::endl;
262                                 }
263                                 file << "               <greyValueRGBpoint>" << std::endl;
264                                 file << "                       <RGBgreyValue>" << greyv[j] << "</RGBgreyValue>" <<std::endl;
265                                 file << "                       <red>" << red[j] << "</red>" <<std::endl;
266                                 file << "                       <green>" << green[j] << "</green>" <<std::endl;
267                                 file << "                       <blue>" << blue[j] << "</blue>" <<std::endl;
268                                 file << "               </greyValueRGBpoint>" << std::endl;
269                                 if(j==greyv.size()-1){
270                                         file << "       </Colors>" << std::endl;
271                                 }
272                         }
273                         file << "       </ComboBoxItem>"<< std::endl;
274                 }
275                 file << "</ComboBoxData>" << std::endl;
276         }
277         file.close();
278 }
279
280 void HistogramDialogComboBox::InitCurrentPathColorsFuntion()
281 {
282       _currentpathColorsFuntion = "";
283 #ifdef WIN32
284         _currentpathColorsFuntion = crea::System::GetDllAppPath("bbcreaMaracasVisu");
285         _currentpathColorsFuntion.append("/data/");
286 #endif
287 #ifdef LINUX
288         _currentpathColorsFuntion = crea::System::GetExecutablePath();
289         _currentpathColorsFuntion.append("/../share/creaMaracasVisu/data/");
290 #endif
291         crea::System::createDirectory(_currentpathColorsFuntion.c_str());
292 }
293
294
295
296 std::string HistogramDialogComboBox::GetFileNameOfColorsFunction()
297 {
298    std::string filename;
299    filename = _currentpathColorsFuntion;                                                       
300    filename.append(COLORSFUNCTIONFILENAME);                                                                                                                       
301    return filename;
302 }
303
304
305 wxBitmapComboBox* HistogramDialogComboBox::getBitmapComboElements()
306 {
307         std::vector<HistogramDialogComboBoxItem*> bitmapsitems;
308         OnLoadComboBoxData(bitmapsitems, GetFileNameOfColorsFunction() );
309         _bitmapsitems = bitmapsitems;
310         wxString* choices;
311         choices = new wxString[bitmapsitems.size()];
312         for(int i = 0; i < bitmapsitems.size();i++)
313         {
314                 choices[i] = _T("Color Function");
315         }
316         
317         wxBitmapComboBox* bitmapcombo;
318 //EED   wxBitmapComboBox* bitmapcombo = new wxBitmapComboBox(this, -1, _T(""), wxDefaultPosition, wxDefaultSize, bitmapsitems.size(),choices);
319         if (_bitmapcombo!=NULL)
320         {
321                 _bitmapcombo->Clear();
322                 bitmapcombo=_bitmapcombo;
323         } else {
324                 bitmapcombo = new wxBitmapComboBox(this, -1, _T(""));
325         }
326 //      wxBitmapComboBox* bitmapcombo = new wxBitmapComboBox(this, -1, _T(""));
327         bitmapcombo->SetSize(165,30);
328
329         Connect(bitmapcombo->GetId(), wxEVT_COMMAND_COMBOBOX_SELECTED, (wxObjectEventFunction)&HistogramDialogComboBox::OnBitmapComboItemSelected);
330
331         for(int i = 0; i < bitmapsitems.size(); i++)
332         {
333                 bitmapcombo->Append(_T("ColorFunction"), bitmapsitems[i]->GetBitmap() );
334 //        bitmapcombo->SetItemBitmap(i, bitmapsitems[i]->GetBitmap());
335         }
336         BitmapComboItemSelect(0);
337         return  bitmapcombo;
338 }
339
340
341 void HistogramDialogComboBox::BitmapComboItemSelect(int value)
342 {
343         HistogramDialogComboBoxItem* currentconfig;
344         if(_bitmapcombo!=NULL)
345         {
346                 _currentitem = value;
347                 _bitmapcombo->Select(value);
348                 if(_bitmapsitems.size()>_currentitem)
349                 {
350                         currentconfig           = _bitmapsitems[_currentitem];
351                         _greyvect                       = currentconfig->getGreyVector();
352                         _redvect                        = currentconfig->getRedVector();
353                         _greenvect                      = currentconfig->getGreenVector();
354                         _bluevect                       = currentconfig->getBlueVector();
355                         _greyvecttransfer       = currentconfig->getGreyVectorTransfer();
356                         _value                          = currentconfig->getValueVector();
357                         wxCommandEvent newevent(wxEVT_COMMAND_COMBOBOX_SELECTED,this->GetId());
358             ProcessEvent(newevent);
359                 } // if size
360         } // _bitmapcombo
361 }
362
363
364 void HistogramDialogComboBox::OnBitmapComboItemSelected(wxCommandEvent& event)
365 {
366         BitmapComboItemSelect(  event.GetInt() );
367 }
368
369 void HistogramDialogComboBox::OnLoadComboBoxData(std::vector<HistogramDialogComboBoxItem*>& itembitmaps, std::string filename)
370 {
371         std::vector<double> redvect, greenvect, bluevect;
372         std::vector<double> greyvect, greyvecttransfunct, value;
373
374         redvect.clear();
375         greenvect.clear();
376         bluevect.clear();
377         greyvect.clear();
378         greyvecttransfunct.clear();
379         value.clear();
380
381         std::ifstream file;
382         file.open(  (const char*) (filename.c_str()) );
383         double gv=-1,gvtransfer=-1,intensity=-1,red=-1,gr=-1,bl=-1;
384         int in=-1;
385
386         //std::cout<<filename<<std::endl;
387         itembitmaps.clear();
388
389         if(file.is_open())
390         {
391                 bool add = false;
392                 HistogramDialogComboBoxItem* item=NULL;
393                 while(!file.eof())
394                 {
395                         std::string line;
396                         std::getline(file,line);
397                         //std::cout<<line<<std::endl;
398             if( (int)(line.find("<ComboBoxItem>")!=-1))
399                 {
400                         item = new HistogramDialogComboBoxItem();
401                 }else if((int)(line.find("<greyValue>"))!=-1) {
402             int pos1=line.find(">");
403                         int pos2=line.find("<",pos1+1);
404                         std::string x=line.substr(pos1+1,pos2-pos1-1);
405                         gvtransfer=atof(x.c_str());
406                         greyvecttransfunct.push_back(gvtransfer);
407                 }else if((int)(line.find("<intensity>"))!=-1) {
408             int pos1=line.find(">");
409                         int pos2=line.find("<",pos1+1);
410                         std::string x=line.substr(pos1+1,pos2-pos1-1);
411                         intensity=atof(x.c_str());
412                         value.push_back(intensity);
413                 }
414                 else if( (int)(line.find("<RGBgreyValue>"))!=-1)
415                 {
416                         int pos1=line.find(">");
417                         int pos2=line.find("<",pos1+1);
418                         std::string x=line.substr(pos1+1,pos2-pos1-1);
419                         gv=atof(x.c_str());
420                         greyvect.push_back(gv);
421                 }
422                 else if( (int)(line.find("<red>"))!=-1)
423                 {
424                         int pos1=line.find(">");
425                         int pos2=line.find("<",pos1+1);
426                         std::string x=line.substr(pos1+1,pos2-pos1-1);
427                         red=atof(x.c_str());
428                         redvect.push_back(red);
429                 }
430                 else if( (int)(line.find("<green>"))!=-1)
431                 {
432                         int pos1=line.find(">");
433                         int pos2=line.find("<",pos1+1);
434                         std::string x=line.substr(pos1+1,pos2-pos1-1);
435                         gr=atof(x.c_str());
436                         greenvect.push_back(gr);
437                 }
438                 else if( (int)(line.find("<blue>"))!=-1 )
439                 {
440                         int pos1=line.find(">");
441                         int pos2=line.find("<",pos1+1);
442                         std::string x=line.substr(pos1+1,pos2-pos1-1);
443                         bl=atof(x.c_str());
444                         bluevect.push_back(bl);
445
446                 }else if( (int)(line.find("</ComboBoxItem>"))!=-1 ) {
447
448                         item->SetColors(greyvect,redvect,greenvect,bluevect);
449                         item->SetTransferFunction(greyvecttransfunct,value);
450                         /*for(int i = 0; i < greyvecttransfunct.size();i++){
451                                         std::cout<<"HistogramDialogComboBox::OnLoadComboBoxData("<<greyvecttransfunct[i]<<std::endl;
452                                         std::cout<<value[i]<<std::endl;
453                         }
454                         for(int i = 0; i < greyvect.size();i++){
455                                         std::cout<<"HistogramDialogComboBox::OnLoadComboBoxData("<<greyvect[i]<<std::endl;
456                                         std::cout<<redvect[i]<<std::endl;
457                                         std::cout<<greenvect[i]<<std::endl;
458                                         std::cout<<bluevect[i]<<std::endl;
459                         }*/
460
461                         itembitmaps.push_back(item);
462
463                         greyvecttransfunct.clear();
464                         value.clear();
465                         greyvect.clear();
466                         redvect.clear();
467                         greenvect.clear();
468                         bluevect.clear();
469                 }
470                 line.clear();
471         }
472         file.close();
473         //std::cout<<itembitmaps.size()<<std::endl;
474      }
475 }
476
477 /**
478 **      Returns two vectors, the grey level of the point and its value, the value is between [0,1]
479 **/
480 void HistogramDialogComboBox::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value)
481 {
482         for(int i = 0; i < _greyvecttransfer.size();i++) 
483         {
484                 greylevel.push_back(_greyvecttransfer[i]*_maxgreyvalue);
485                 value.push_back(_value[i]);
486         } // for i
487 }
488
489 /**
490 **      Returns two vectors, the grey level of the point and its value, the red, green
491 **      and blue value is between [0,1]
492 **/
493 void HistogramDialogComboBox::GetValuesColorPointsFunction(std::vector<double>& greylevel,
494                                                                 std::vector<double>& red,
495                                                                 std::vector<double>& green,
496                                                                 std::vector<double>& blue)
497 {
498         for(int i = 0; i < _greyvect.size();i++) 
499         {
500                 greylevel.push_back(_greyvect[i]*_maxgreyvalue);
501                 red.push_back(_redvect[i]);
502                 green.push_back(_greenvect[i]);
503                 blue.push_back(_bluevect[i]);
504         } // for i
505 }
506