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