]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/HistogramDialogComboBox.cxx
#2844 creaMaracasVisu Feature New Normal - ManualPaint_model box, Refresh TransferF...
[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 #endif
286 #ifdef LINUX
287         _currentpathColorsFuntion = crea::System::GetExecutablePath();
288         _currentpathColorsFuntion.append("/../share/creaMaracasVisu/data/");
289 #endif
290         crea::System::createDirectory(_currentpathColorsFuntion.c_str());
291 }
292
293
294
295 std::string HistogramDialogComboBox::GetFileNameOfColorsFunction()
296 {
297    std::string filename;
298    filename = _currentpathColorsFuntion;                                                       
299    filename.append(COLORSFUNCTIONFILENAME);                                                                                                                       
300    return filename;
301 }
302
303
304 wxBitmapComboBox* HistogramDialogComboBox::getBitmapComboElements()
305 {
306         std::vector<HistogramDialogComboBoxItem*> bitmapsitems;
307         OnLoadComboBoxData(bitmapsitems, GetFileNameOfColorsFunction() );
308         _bitmapsitems = bitmapsitems;
309         wxString* choices;
310         choices = new wxString[bitmapsitems.size()];
311         for(int i = 0; i < bitmapsitems.size();i++)
312         {
313                 choices[i] = _T("Color Function");
314         }
315         
316         wxBitmapComboBox* bitmapcombo;
317 //EED   wxBitmapComboBox* bitmapcombo = new wxBitmapComboBox(this, -1, _T(""), wxDefaultPosition, wxDefaultSize, bitmapsitems.size(),choices);
318         if (_bitmapcombo!=NULL)
319         {
320                 _bitmapcombo->Clear();
321                 bitmapcombo=_bitmapcombo;
322         } else {
323                 bitmapcombo = new wxBitmapComboBox(this, -1, _T(""));
324         }
325 //      wxBitmapComboBox* bitmapcombo = new wxBitmapComboBox(this, -1, _T(""));
326         bitmapcombo->SetSize(165,30);
327
328         Connect(bitmapcombo->GetId(), wxEVT_COMMAND_COMBOBOX_SELECTED, (wxObjectEventFunction)&HistogramDialogComboBox::OnBitmapComboItemSelected);
329
330         for(int i = 0; i < bitmapsitems.size(); i++)
331         {
332                 bitmapcombo->Append(_T("ColorFunction"), bitmapsitems[i]->GetBitmap() );
333 //        bitmapcombo->SetItemBitmap(i, bitmapsitems[i]->GetBitmap());
334         }
335         BitmapComboItemSelect(0);
336         return  bitmapcombo;
337 }
338
339
340 void HistogramDialogComboBox::BitmapComboItemSelect(int value)
341 {
342         HistogramDialogComboBoxItem* currentconfig;
343         if(_bitmapcombo!=NULL)
344         {
345                 _currentitem = value;
346                 _bitmapcombo->Select(value);
347                 if(_bitmapsitems.size()>_currentitem)
348                 {
349                         currentconfig           = _bitmapsitems[_currentitem];
350                         _greyvect                       = currentconfig->getGreyVector();
351                         _redvect                        = currentconfig->getRedVector();
352                         _greenvect                      = currentconfig->getGreenVector();
353                         _bluevect                       = currentconfig->getBlueVector();
354                         _greyvecttransfer       = currentconfig->getGreyVectorTransfer();
355                         _value                          = currentconfig->getValueVector();
356                         wxCommandEvent newevent(wxEVT_COMMAND_COMBOBOX_SELECTED,this->GetId());
357             ProcessEvent(newevent);
358                 } // if size
359         } // _bitmapcombo
360 }
361
362
363 void HistogramDialogComboBox::OnBitmapComboItemSelected(wxCommandEvent& event)
364 {
365         BitmapComboItemSelect(  event.GetInt() );
366 }
367
368 void HistogramDialogComboBox::OnLoadComboBoxData(std::vector<HistogramDialogComboBoxItem*>& itembitmaps, std::string filename)
369 {
370         std::vector<double> redvect, greenvect, bluevect;
371         std::vector<double> greyvect, greyvecttransfunct, value;
372
373         redvect.clear();
374         greenvect.clear();
375         bluevect.clear();
376         greyvect.clear();
377         greyvecttransfunct.clear();
378         value.clear();
379
380         std::ifstream file;
381         file.open(  (const char*) (filename.c_str()) );
382         double gv=-1,gvtransfer=-1,intensity=-1,red=-1,gr=-1,bl=-1;
383         int in=-1;
384
385         //std::cout<<filename<<std::endl;
386         itembitmaps.clear();
387
388         if(file.is_open())
389         {
390                 bool add = false;
391                 HistogramDialogComboBoxItem* item=NULL;
392                 while(!file.eof())
393                 {
394                         std::string line;
395                         std::getline(file,line);
396                         //std::cout<<line<<std::endl;
397             if( (int)(line.find("<ComboBoxItem>")!=-1))
398                 {
399                         item = new HistogramDialogComboBoxItem();
400                 }else if((int)(line.find("<greyValue>"))!=-1) {
401                         int pos1=line.find(">");
402                         int pos2=line.find("<",pos1+1);
403                         std::string x=line.substr(pos1+1,pos2-pos1-1);
404                         gvtransfer=atof(x.c_str());
405                         greyvecttransfunct.push_back(gvtransfer);
406                 }else if((int)(line.find("<intensity>"))!=-1) {
407                         int pos1=line.find(">");
408                         int pos2=line.find("<",pos1+1);
409                         std::string x=line.substr(pos1+1,pos2-pos1-1);
410                         intensity=atof(x.c_str());
411                         value.push_back(intensity);
412                 }
413                 else if( (int)(line.find("<RGBgreyValue>"))!=-1)
414                 {
415                         int pos1=line.find(">");
416                         int pos2=line.find("<",pos1+1);
417                         std::string x=line.substr(pos1+1,pos2-pos1-1);
418                         gv=atof(x.c_str());
419                         greyvect.push_back(gv);
420                 }
421                 else if( (int)(line.find("<red>"))!=-1)
422                 {
423                         int pos1=line.find(">");
424                         int pos2=line.find("<",pos1+1);
425                         std::string x=line.substr(pos1+1,pos2-pos1-1);
426                         red=atof(x.c_str());
427                         redvect.push_back(red);
428                 }
429                 else if( (int)(line.find("<green>"))!=-1)
430                 {
431                         int pos1=line.find(">");
432                         int pos2=line.find("<",pos1+1);
433                         std::string x=line.substr(pos1+1,pos2-pos1-1);
434                         gr=atof(x.c_str());
435                         greenvect.push_back(gr);
436                 }
437                 else if( (int)(line.find("<blue>"))!=-1 )
438                 {
439                         int pos1=line.find(">");
440                         int pos2=line.find("<",pos1+1);
441                         std::string x=line.substr(pos1+1,pos2-pos1-1);
442                         bl=atof(x.c_str());
443                         bluevect.push_back(bl);
444
445                 }else if( (int)(line.find("</ComboBoxItem>"))!=-1 ) {
446
447                         item->SetColors(greyvect,redvect,greenvect,bluevect);
448                         item->SetTransferFunction(greyvecttransfunct,value);
449                         /*for(int i = 0; i < greyvecttransfunct.size();i++){
450                                         std::cout<<"HistogramDialogComboBox::OnLoadComboBoxData("<<greyvecttransfunct[i]<<std::endl;
451                                         std::cout<<value[i]<<std::endl;
452                         }
453                         for(int i = 0; i < greyvect.size();i++){
454                                         std::cout<<"HistogramDialogComboBox::OnLoadComboBoxData("<<greyvect[i]<<std::endl;
455                                         std::cout<<redvect[i]<<std::endl;
456                                         std::cout<<greenvect[i]<<std::endl;
457                                         std::cout<<bluevect[i]<<std::endl;
458                         }*/
459
460                         itembitmaps.push_back(item);
461
462                         greyvecttransfunct.clear();
463                         value.clear();
464                         greyvect.clear();
465                         redvect.clear();
466                         greenvect.clear();
467                         bluevect.clear();
468                 }
469                 line.clear();
470         }
471         file.close();
472         //std::cout<<itembitmaps.size()<<std::endl;
473      }
474 }
475
476 /**
477 **      Returns two vectors, the grey level of the point and its value, the value is between [0,1]
478 **/
479 void HistogramDialogComboBox::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value)
480 {
481         for(int i = 0; i < _greyvecttransfer.size();i++) {
482                 greylevel.push_back(_greyvecttransfer[i]*_maxgreyvalue);
483                 value.push_back(_value[i]);
484         }
485 }
486
487 /**
488 **      Returns two vectors, the grey level of the point and its value, the red, green
489 **      and blue value is between [0,1]
490 **/
491 void HistogramDialogComboBox::GetValuesColorPointsFunction(std::vector<double>& greylevel,
492                                                                 std::vector<double>& red,
493                                                                 std::vector<double>& green,
494                                                                 std::vector<double>& blue)
495 {
496         for(int i = 0; i < _greyvect.size();i++) {
497                 greylevel.push_back(_greyvect[i]*_maxgreyvalue);
498                 red.push_back(_redvect[i]);
499                 green.push_back(_greenvect[i]);
500                 blue.push_back(_bluevect[i]);
501         }
502 }