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