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