]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/HistogramDialogComboBox.cxx
e57059a3fed44f08493276d8d7f8684a49800434
[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         colorBar_Bitmap         = NULL;
44         _bitmapsizer            = NULL;
45
46         InitCurrentPathColorsFuntion();
47         this->SetSizer(getBitmapCombo());
48         _img                    = NULL;
49         _currentitem            = -1;    
50         _maxgreyvalue           = 0;
51
52
53 }
54
55
56
57
58 HistogramDialogComboBox::~HistogramDialogComboBox()
59 {
60 }
61
62 void HistogramDialogComboBox::SetColors(std::vector<double> greyvect, std::vector<double> redvect, std::vector<double> greenvect, std::vector<double> bluevect)
63 {
64         _greyvect       = greyvect;
65         _redvect        = redvect;
66         _greenvect      = greenvect;
67         _bluevect       = bluevect;
68 }
69
70 wxSizer* HistogramDialogComboBox::getBitmapCombo()
71 {
72         //_bitmapsizer = new wxBoxSizer(wxVERTICAL);
73         _bitmapsizer = new wxBoxSizer(wxVERTICAL);
74
75         wxBoxSizer* comboeditsizer = new wxBoxSizer(wxHORIZONTAL);
76         _bitmapcombo = getBitmapComboElements();
77         BitmapComboItemSelect(0);
78
79         wxBitmap bitmap1(Edit_xpm);
80         wxBitmapButton* edit = new wxBitmapButton(this, -1, bitmap1,wxDefaultPosition,wxSize(30,30));
81         Connect(edit->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&HistogramDialogComboBox::OnEditBitmapCombo);
82
83         comboeditsizer->Add(_bitmapcombo,wxSizerFlags().Center().FixedMinSize());
84         comboeditsizer->Add(edit,wxSizerFlags().Center());
85
86         _bitmapsizer->Add(comboeditsizer,wxSizerFlags().FixedMinSize().Center());
87         _bitmapsizer->AddSpacer(5);
88         _bitmapsizer->Add(getSlidersWlCo(),wxSizerFlags().Expand().Center());
89
90         return _bitmapsizer;
91 }
92
93 wxSizer* HistogramDialogComboBox::getSlidersWlCo()
94 {
95         wxBoxSizer* sizersliders = new wxBoxSizer(wxVERTICAL);
96
97         _slidercolor = new wxSlider(this, -1,1,0,1,wxDefaultPosition,wxDefaultSize,wxSL_LABELS);
98         _sliderwindowlevel = new wxSlider(this, -1,1,0,1,wxDefaultPosition,wxDefaultSize,wxSL_LABELS);
99
100         sizersliders->Add(_slidercolor,wxSizerFlags().Expand().Center());
101         sizersliders->Add(_sliderwindowlevel,wxSizerFlags().Expand().Center());
102
103         Connect(_slidercolor->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&HistogramDialogComboBox::OnColorLevelChanged);
104         Connect(_sliderwindowlevel->GetId(), wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&HistogramDialogComboBox::OnWindowLevelChanged);
105
106         return sizersliders;
107 }
108
109 double HistogramDialogComboBox::GetWindowLevel()
110 {
111    double val(0.0);
112    if(_sliderwindowlevel != NULL)
113       val= _sliderwindowlevel->GetValue();
114    return val; // JPR
115 }
116
117 double HistogramDialogComboBox::GetColorLevel()
118 {
119    double val(0.0);
120    if(_slidercolor != NULL)
121       val = _slidercolor->GetValue();
122    return val; // JPR
123 }
124
125 void HistogramDialogComboBox::OnColorLevelChanged(wxCommandEvent& event)
126 {
127    _slidercolor->GetValue();
128    wxCommandEvent newevent(wxEVT_SCROLL_THUMBTRACK,this->GetId());
129    ProcessEvent(newevent);
130 }
131
132 void HistogramDialogComboBox::OnWindowLevelChanged(wxCommandEvent& event)
133 {
134    _sliderwindowlevel->GetValue();
135    wxCommandEvent newevent(wxEVT_SCROLL_THUMBTRACK,this->GetId());
136    ProcessEvent(newevent);
137 }
138
139 void HistogramDialogComboBox::setImageData(vtkImageData* img)
140 {
141    _maxgreyvalue = img->GetScalarRange()[1];
142    _img = img;
143    setSlidersValue();
144 }
145
146 void HistogramDialogComboBox::setSlidersValue()
147 {
148         if(_img!=NULL){
149                 if(_slidercolor!=NULL){
150                         _slidercolor->SetRange(_img->GetScalarRange()[0],_img->GetScalarRange()[1]);
151                         _slidercolor->SetValue((_img->GetScalarRange()[0]+_img->GetScalarRange()[1])/2);
152                 }
153                 if(_sliderwindowlevel!=NULL){
154                         _sliderwindowlevel->SetRange(_img->GetScalarRange()[0],_img->GetScalarRange()[1]);
155                         _sliderwindowlevel->SetValue((_img->GetScalarRange()[0]+_img->GetScalarRange()[1])/2);
156                 }
157         }
158 }
159
160 void HistogramDialogComboBox::OnEditBitmapCombo(wxCommandEvent& event)
161 {
162         if(_img!=NULL)
163         {
164                 bool createaddandremovecontrols = true;
165                 HistogramDialog* dialog = new HistogramDialog(this,_T("Color Configuration"),createaddandremovecontrols);
166                 dialog->initializeHistogram(_img);
167                 dialog->setInfoPanExtraControls(_bitmapsitems);
168
169                 if(dialog->ShowModal()==wxID_OK)
170                 {
171                         dialog->UpdateCurrentComboElement();
172                         _bitmapsitems = dialog->getComboBoxItems();
173
174                         std::vector<double> redvect,greenvect,bluevect;
175                         std::vector<double> greyvect;
176
177                         saveCurrentConfiguration(_bitmapsitems, GetFileNameOfColorsFunction() );
178                         wxBitmapComboBox* tempbitmapcombo = _bitmapcombo;
179                         for(int i = 0; i < _bitmapsitems.size();i++)
180                         {
181                                 delete _bitmapsitems[i];
182                         }
183                         _bitmapsitems.clear();
184                         _bitmapcombo = getBitmapComboElements();
185                         
186 // EED Eraseme                  
187 //                      if(_bitmapsizer->Replace(tempbitmapcombo,_bitmapcombo))
188 //                      {
189 //                              tempbitmapcombo->Destroy();
190 //                      } else {
191 //                              printf ("EED ERROR HistogramDialogComboBox::OnEditBitmapCombo. Can't replace bitmapcombo \n");
192 //                      }
193                         
194                         this->Layout();
195                 }
196                 dialog->Destroy();
197         }
198 }
199
200
201 void HistogramDialogComboBox::saveCurrentConfiguration(std::vector<HistogramDialogComboBoxItem*>& itembitmaps,std::string filename)
202 {
203         std::ofstream file;
204         file.open(  (const char*) (filename.c_str()) );
205         double gv=-1,red=-1,gr=-1,bl=-1;
206         int in=-1;
207
208         if(file.is_open())
209         {
210                 file << "<ComboBoxData>" << std::endl;
211                 for(int i = 0; i < itembitmaps.size();i++)
212                 {
213                         HistogramDialogComboBoxItem* item = itembitmaps[i];
214                         std::vector<double> vecttransfer = item->getGreyVectorTransfer();
215                         std::vector<double> value = item->getValueVector();
216
217                         file << " <ComboBoxItem>"<< std::endl;
218                         for(int j = 0; j < vecttransfer.size();j++)
219                         {
220                                 if(j==0){
221                                         file << " <TransferenceFunction>"<< std::endl;
222                                 }
223                                 file << "               <Transferencepoint>" << std::endl;
224                                 file << "                       <greyValue>" << vecttransfer[j] << "</greyValue>" <<std::endl;
225                                 file << "                       <intensity>" << value[j] << "</intensity> "<<std::endl;
226                                 file << "               </Transferencepoint>" << std::endl;
227                                 if(j==vecttransfer.size()-1){
228                     file << " </TransferenceFunction>"<< std::endl;
229                                 }
230                         }
231                         std::vector<double> greyv = item->getGreyVector();
232                         std::vector<double> red = item->getRedVector();
233                         std::vector<double> green = item->getGreenVector();
234                         std::vector<double> blue = item->getBlueVector();
235                         for(int j = 0; j < greyv.size();j++)
236                         {
237                                 if(j==0){
238                                         file << "       <Colors>" << std::endl;
239                                 }
240                                 file << "               <greyValueRGBpoint>" << std::endl;
241                                 file << "                       <RGBgreyValue>" << greyv[j] << "</RGBgreyValue>" <<std::endl;
242                                 file << "                       <red>" << red[j] << "</red>" <<std::endl;
243                                 file << "                       <green>" << green[j] << "</green>" <<std::endl;
244                                 file << "                       <blue>" << blue[j] << "</blue>" <<std::endl;
245                                 file << "               </greyValueRGBpoint>" << std::endl;
246                                 if(j==greyv.size()-1){
247                                         file << "       </Colors>" << std::endl;
248                                 }
249                         }
250                         file << "       </ComboBoxItem>"<< std::endl;
251                 }
252                 file << "</ComboBoxData>" << std::endl;
253         }
254         file.close();
255 }
256
257 void HistogramDialogComboBox::InitCurrentPathColorsFuntion()
258 {
259       _currentpathColorsFuntion = "";
260 #ifdef WIN32
261         _currentpathColorsFuntion = crea::System::GetDllAppPath("bbcreaMaracasVisu");
262 #endif
263 #ifdef LINUX
264         _currentpathColorsFuntion = crea::System::GetExecutablePath();
265         _currentpathColorsFuntion.append("/../share/creaMaracasVisu/data/");
266 #endif
267         crea::System::createDirectory(_currentpathColorsFuntion.c_str());
268 }
269
270
271
272 std::string HistogramDialogComboBox::GetFileNameOfColorsFunction()
273 {
274    std::string filename;
275    filename = _currentpathColorsFuntion;                                                       
276    filename.append(COLORSFUNCTIONFILENAME);                                                                                                                       
277    return filename;
278 }
279
280
281 wxBitmapComboBox* HistogramDialogComboBox::getBitmapComboElements()
282 {
283         std::vector<HistogramDialogComboBoxItem*> bitmapsitems;
284         OnLoadComboBoxData(bitmapsitems, GetFileNameOfColorsFunction() );
285         _bitmapsitems = bitmapsitems;
286         wxString* choices;
287         choices = new wxString[bitmapsitems.size()];
288         for(int i = 0; i < bitmapsitems.size();i++)
289         {
290                 choices[i] = _T("Color Function");
291         }
292         
293         wxBitmapComboBox* bitmapcombo;
294 //EED   wxBitmapComboBox* bitmapcombo = new wxBitmapComboBox(this, -1, _T(""), wxDefaultPosition, wxDefaultSize, bitmapsitems.size(),choices);
295         if (_bitmapcombo!=NULL)
296         {
297                 _bitmapcombo->Clear();
298                 bitmapcombo=_bitmapcombo;
299         } else {
300                 bitmapcombo = new wxBitmapComboBox(this, -1, _T(""));
301         }
302 //      wxBitmapComboBox* bitmapcombo = new wxBitmapComboBox(this, -1, _T(""));
303         bitmapcombo->SetSize(165,30);
304
305         Connect(bitmapcombo->GetId(), wxEVT_COMMAND_COMBOBOX_SELECTED, (wxObjectEventFunction)&HistogramDialogComboBox::OnBitmapComboItemSelected);
306
307         for(int i = 0; i < bitmapsitems.size(); i++)
308         {
309                 bitmapcombo->Append(_T("ColorFunction"), bitmapsitems[i]->GetBitmap() );
310 //        bitmapcombo->SetItemBitmap(i, bitmapsitems[i]->GetBitmap());
311         }
312         BitmapComboItemSelect(0);
313         return  bitmapcombo;
314 }
315
316
317 void HistogramDialogComboBox::BitmapComboItemSelect(int value)
318 {
319         HistogramDialogComboBoxItem* currentconfig;
320         if(_bitmapcombo!=NULL)
321         {
322                 _currentitem = value;
323                 _bitmapcombo->Select(value);
324                 if(_bitmapsitems.size()>_currentitem)
325                 {
326                         currentconfig           = _bitmapsitems[_currentitem];
327                         _greyvect                       = currentconfig->getGreyVector();
328                         _redvect                        = currentconfig->getRedVector();
329                         _greenvect                      = currentconfig->getGreenVector();
330                         _bluevect                       = currentconfig->getBlueVector();
331                         _greyvecttransfer       = currentconfig->getGreyVectorTransfer();
332                         _value                          = currentconfig->getValueVector();
333                         wxCommandEvent newevent(wxEVT_COMMAND_COMBOBOX_SELECTED,this->GetId());
334             ProcessEvent(newevent);
335                 } // if size
336         } // _bitmapcombo
337 }
338
339
340 void HistogramDialogComboBox::OnBitmapComboItemSelected(wxCommandEvent& event)
341 {
342         BitmapComboItemSelect(  event.GetInt() );
343 }
344
345 void HistogramDialogComboBox::OnLoadComboBoxData(std::vector<HistogramDialogComboBoxItem*>& itembitmaps, std::string filename)
346 {
347         std::vector<double> redvect, greenvect, bluevect;
348         std::vector<double> greyvect, greyvecttransfunct, value;
349
350         redvect.clear();
351         greenvect.clear();
352         bluevect.clear();
353         greyvect.clear();
354         greyvecttransfunct.clear();
355         value.clear();
356
357         std::ifstream file;
358         file.open(  (const char*) (filename.c_str()) );
359         double gv=-1,gvtransfer=-1,intensity=-1,red=-1,gr=-1,bl=-1;
360         int in=-1;
361
362         //std::cout<<filename<<std::endl;
363         itembitmaps.clear();
364
365         if(file.is_open())
366         {
367                 bool add = false;
368                 HistogramDialogComboBoxItem* item=NULL;
369                 while(!file.eof())
370                 {
371                         std::string line;
372                         std::getline(file,line);
373                         //std::cout<<line<<std::endl;
374             if( (int)(line.find("<ComboBoxItem>")!=-1))
375                 {
376                         item = new HistogramDialogComboBoxItem();
377                 }else if((int)(line.find("<greyValue>"))!=-1) {
378                         int pos1=line.find(">");
379                         int pos2=line.find("<",pos1+1);
380                         std::string x=line.substr(pos1+1,pos2-pos1-1);
381                         gvtransfer=atof(x.c_str());
382                         greyvecttransfunct.push_back(gvtransfer);
383                 }else if((int)(line.find("<intensity>"))!=-1) {
384                         int pos1=line.find(">");
385                         int pos2=line.find("<",pos1+1);
386                         std::string x=line.substr(pos1+1,pos2-pos1-1);
387                         intensity=atof(x.c_str());
388                         value.push_back(intensity);
389                 }
390                 else if( (int)(line.find("<RGBgreyValue>"))!=-1)
391                 {
392                         int pos1=line.find(">");
393                         int pos2=line.find("<",pos1+1);
394                         std::string x=line.substr(pos1+1,pos2-pos1-1);
395                         gv=atof(x.c_str());
396                         greyvect.push_back(gv);
397                 }
398                 else if( (int)(line.find("<red>"))!=-1)
399                 {
400                         int pos1=line.find(">");
401                         int pos2=line.find("<",pos1+1);
402                         std::string x=line.substr(pos1+1,pos2-pos1-1);
403                         red=atof(x.c_str());
404                         redvect.push_back(red);
405                 }
406                 else if( (int)(line.find("<green>"))!=-1)
407                 {
408                         int pos1=line.find(">");
409                         int pos2=line.find("<",pos1+1);
410                         std::string x=line.substr(pos1+1,pos2-pos1-1);
411                         gr=atof(x.c_str());
412                         greenvect.push_back(gr);
413                 }
414                 else if( (int)(line.find("<blue>"))!=-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                         bl=atof(x.c_str());
420                         bluevect.push_back(bl);
421
422                 }else if( (int)(line.find("</ComboBoxItem>"))!=-1 ) {
423
424                         item->SetColors(greyvect,redvect,greenvect,bluevect);
425                         item->SetTransferFunction(greyvecttransfunct,value);
426                         /*for(int i = 0; i < greyvecttransfunct.size();i++){
427                                         std::cout<<"HistogramDialogComboBox::OnLoadComboBoxData("<<greyvecttransfunct[i]<<std::endl;
428                                         std::cout<<value[i]<<std::endl;
429                         }
430                         for(int i = 0; i < greyvect.size();i++){
431                                         std::cout<<"HistogramDialogComboBox::OnLoadComboBoxData("<<greyvect[i]<<std::endl;
432                                         std::cout<<redvect[i]<<std::endl;
433                                         std::cout<<greenvect[i]<<std::endl;
434                                         std::cout<<bluevect[i]<<std::endl;
435                         }*/
436
437                         itembitmaps.push_back(item);
438
439                         greyvecttransfunct.clear();
440                         value.clear();
441                         greyvect.clear();
442                         redvect.clear();
443                         greenvect.clear();
444                         bluevect.clear();
445                 }
446                 line.clear();
447         }
448         file.close();
449         //std::cout<<itembitmaps.size()<<std::endl;
450      }
451 }
452
453 /**
454 **      Returns two vectors, the grey level of the point and its value, the value is between [0,1]
455 **/
456 void HistogramDialogComboBox::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value)
457 {
458         for(int i = 0; i < _greyvecttransfer.size();i++) {
459                 greylevel.push_back(_greyvecttransfer[i]*_maxgreyvalue);
460                 value.push_back(_value[i]);
461         }
462 }
463
464 /**
465 **      Returns two vectors, the grey level of the point and its value, the red, green
466 **      and blue value is between [0,1]
467 **/
468 void HistogramDialogComboBox::GetValuesColorPointsFunction(std::vector<double>& greylevel,
469                                                                 std::vector<double>& red,
470                                                                 std::vector<double>& green,
471                                                                 std::vector<double>& blue)
472 {
473         for(int i = 0; i < _greyvect.size();i++) {
474                 greylevel.push_back(_greyvect[i]*_maxgreyvalue);
475                 red.push_back(_redvect[i]);
476                 green.push_back(_greenvect[i]);
477                 blue.push_back(_bluevect[i]);
478         }
479 }