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