]> Creatis software - creaContours.git/blob - lib/Interface_ManagerContour_NDimensions/wxInstantChooserPanel.cxx
4e14484d72f53506ff2cc54958fa80fd9b588946
[creaContours.git] / lib / Interface_ManagerContour_NDimensions / wxInstantChooserPanel.cxx
1 //----------------------------------------------------------------------------------------------------------------
2 // Class definition include
3 //----------------------------------------------------------------------------------------------------------------
4 #include "wxInstantChooserPanel.h"
5 #include "wxContourMainFrame.h"
6
7 //Test JCP 13-10-08
8 //#include "wxContourEventHandler.h"
9 //Test JCP 13-10-08
10
11 //----------------------------------------------------------------------------------------------------------------
12 // Class implementation
13 //----------------------------------------------------------------------------------------------------------------
14
15 //----------------------------------------------------------------------------------------------------------------
16 // Throwed Events definition
17 //----------------------------------------------------------------------------------------------------------------
18
19
20 DEFINE_EVENT_TYPE( wxINSTANT_CHOOSER_CHANGE )
21         
22
23 /** @file wxInstantChooserPanel.cxx */
24
25         //------------------------------------------------------------------------------------------------------------
26         // Constructors & Destructors
27         //------------------------------------------------------------------------------------------------------------
28         
29
30
31         wxInstantChooserPanel :: wxInstantChooserPanel (wxWindow * parent, std::string title, bool theOrientation, bool resolutionALL,std::string configurationElem, std::string groupConfig )
32         :wxPanel(parent, -1, wxPoint(20,20), wxDefaultSize, wxTAB_TRAVERSAL)
33         {
34                 _lastConceptUpdated = NULL;             
35                 _concepts = new std::vector<wxConceptControl*>();
36                 _resolutionOverALL = resolutionALL;
37                 _configuration = configurationElem;
38                 //--- groupConfig -> format: ("## ## # ## "), #:any positive integer number, : space needed between the numbers and at the end of the string.
39                 setVerticalMaxSelectedNums( groupConfig );
40                 
41                 //_configuration = "CCCc";
42                 //setVerticalMaxSelectedNums( "1 2 3 3 ");//groupConfig );
43
44                 SetSize(300,300);
45                 _outSizer = new wxFlexGridSizer ( 1, 10, 10);
46                 _orientation = theOrientation;
47                 initializeResolutionSlider();
48                 _actualInstant=new std::map<std::string,int>();
49                 
50                 wxFlexGridSizer * resolutionSizer = new wxFlexGridSizer( 3,1,10,10 );
51                 resolutionSizer->AddSpacer(10);
52                 resolutionSizer->Add( _resolutionSlider, 1, wxEXPAND | wxGROW );
53                 resolutionSizer->AddSpacer(10);
54                 
55                 wxFlexGridSizer * sizer = new wxFlexGridSizer( 1,2,10,10 );
56                 sizer->Add( _outSizer, 1, wxEXPAND| wxGROW );
57                 sizer->Add( resolutionSizer, 1, wxEXPAND | wxGROW );
58                 SetSizer( sizer );
59                 Connect( wxID_ANY,  wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) &wxInstantChooserPanel:: onCheckBoxClicked );               
60
61         }
62
63         wxInstantChooserPanel ::wxInstantChooserPanel (wxWindow * parent,wxWindowID id ,std::string title, const wxPoint& pos, const wxSize& size , bool theOrientation, std::string configurationElem , std::string groupConfig  )
64                 :wxPanel(parent, id ,pos, size) 
65         {
66                 _lastConceptUpdated = NULL;             
67                 _concepts = new std::vector<wxConceptControl*>();
68                 _resolutionOverALL = false;
69                 _configuration = configurationElem;
70                 //--- groupConfig -> format: ("## ## # ## "), #:any positive integer number, : space needed between the numbers and at the end of the string.
71                 setVerticalMaxSelectedNums( groupConfig );
72                 
73                 SetSize(size);
74                 _outSizer = new wxFlexGridSizer ( 1, 10, 10);
75                 _orientation = theOrientation;
76                 initializeResolutionSlider();
77                 _actualInstant=new std::map<std::string,int>();
78                 wxFlexGridSizer * sizer = new wxFlexGridSizer( 1,2,10,10 );
79                 sizer->Add( _outSizer, 1, wxEXPAND| wxGROW );
80                 sizer->Add( _resolutionSlider, 1, wxEXPAND | wxGROW );
81                 SetSizer( sizer );
82                 Connect( wxID_ANY,  wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction) &wxInstantChooserPanel:: onCheckBoxClicked );               
83         }
84
85         wxInstantChooserPanel :: ~wxInstantChooserPanel()
86         {
87                 int size,i;
88                 //deleting the _concepts
89                 if(_concepts!=NULL)
90                 {
91                         size=_concepts->size();
92                         for(i=0; i< size ; i++)
93                                 {
94                                         delete (*_concepts)[i];
95                                 }
96                         _concepts->clear();
97                 }
98                 _verticalMaxSelected.clear();
99                 //deleting the actual instant
100                 _actualInstant->clear();
101         }
102
103         //------------------------------------------------------------------------------------------------------------
104         // Creational and initialization methods
105         //------------------------------------------------------------------------------------------------------------
106         
107         /**
108         * Adds a concept to the instant managment. PRE: The concept doesn't exists in the included _concepts->
109         * @param aConceptName 
110         * @param minV
111         * @param maxV
112         * @param actualV
113         */
114         void wxInstantChooserPanel :: addConcept(std::string aConceptName, int minV, int maxV, int actualV)
115         {
116                 wxConceptControl * nwConcept = new wxConceptControl(this, _orientation, minV, maxV, actualV, aConceptName, _configuration);
117                 _concepts->push_back( nwConcept );
118                 _lastConceptUpdated = nwConcept;
119                 _outSizer->Add(nwConcept, wxEXPAND);
120                 Connect(nwConcept->GetId(), wxEVT_CONCEPT_RELEASE, (wxObjectEventFunction) (wxCommandEventFunction)  &wxInstantChooserPanel::onConceptRelease );
121         }
122
123         void wxInstantChooserPanel :: setWxEventHandler( wxEvtHandler * theEventHandler )
124         {
125                 _eventHandler = theEventHandler;
126         }
127
128         //------------------------------------------------------------------------------------------------------------
129         // Methods for capturing events
130         //------------------------------------------------------------------------------------------------------------
131         void wxInstantChooserPanel :: onResolutionSlider(wxScrollEvent& event)
132         {
133                 std::string lasConcept = _lastConceptUpdated->getName();
134                 int value = _lastConceptUpdated->getActualValue();
135                 int min = _lastConceptUpdated->getMinValue();
136                 int max = _lastConceptUpdated->getMaxValue();
137                 int delta = (int) (pow((double) 2,(double) _resolutionSlider->GetValue() ));
138
139                 int minTmp   = value - delta/2;
140                 int maxTmp   = value + delta/2;
141                 if (minTmp<min)
142                         {
143                                 minTmp = min;   
144                         }
145                 if (maxTmp>max)
146                         {
147                                 maxTmp = max;
148                         }                       
149                 _lastConceptUpdated->setRange( minTmp,maxTmp );
150
151                 if (! _resolutionOverALL)
152                 {
153                         changeResolutionAtConceptControl( _lastConceptUpdated );
154                 }
155                 else
156                 {
157                         for(int i=0; i< (int)(_concepts->size()); i++)
158                         {
159                                 changeResolutionAtConceptControl( (*_concepts)[i] );
160                         }
161                 }               
162
163         }
164         //------------------------------------------------------------------------------------------------------------
165         void wxInstantChooserPanel :: onCheckBoxClicked( wxCommandEvent& event )
166         {
167                 /*int groupID = (int)event.GetClientData();
168                 wxConceptControl * eventConcept = (wxConceptControl *)event.GetEventObject();
169                 if ( eventConcept->getIfChecked( groupID ) )
170                 {
171                         int i,size = _concepts->size();
172                         wxConceptControl * aConcept = NULL;
173                         int accum = 0;          
174                         for( i=0; i<size; i++)
175                         {
176                                 accum+= (*_concepts)[i]->getIfChecked( groupID ) ? 1 :0;
177                         }               
178                         for( i=0; i<size && _verticalMaxSelected[groupID] < accum; i++)
179                         {
180                                 aConcept = (*_concepts)[i]; 
181                                 if( aConcept!= eventConcept )
182                                 {
183                                         if( aConcept->getIfChecked( groupID ) )
184                                         {       
185                                                 aConcept->clearCheckAt( groupID );
186                                                 accum--;
187                                         }                                       
188                                 }
189                         }                                       
190                 }*/
191
192                 int groupID = (int)event.GetClientData();
193                 wxConceptControl * eventConcept = (wxConceptControl *)event.GetEventObject();
194                 if ( eventConcept->getIfChecked( groupID ) )
195                 {
196                         int size = _concepts->size();
197                         wxConceptControl * aConcept = NULL;
198 //                      int accum = 0;
199
200                         for(int i = 0; i < eventConcept->getNumCheckBox();i++){
201                                 if(i!=groupID){
202                                         eventConcept->clearCheckAt(i);
203                                 }
204                         }
205                         for(int i=0; i<size; i++)
206                         {
207                                 aConcept = (*_concepts)[i]; 
208                                 if( aConcept!= eventConcept )
209                                 {
210                                         if( aConcept->getIfChecked( groupID ) )
211                                         {       
212                                                 aConcept->clearCheckAt( groupID );
213                                         }                                       
214                                 }
215                         }                       
216                 }
217         }
218
219         //------------------------------------------------------------------------------------------------------------
220         void wxInstantChooserPanel :: onConceptRelease( wxCommandEvent& event )
221         {
222                 _lastConceptUpdated = (wxConceptControl*) event.GetEventObject();
223
224                 std::vector<int> theInstant;
225         getInstant( theInstant );
226                 //-------------------------------------------------
227                 //  Sending the event of wxINSTANT_CHOOSER_CHANGE
228                 //-------------------------------------------------
229                 wxCommandEvent newevent(wxINSTANT_CHOOSER_CHANGE,GetId());  
230                 //this->onChangeInstant(newevent);
231                 ConceptDataWrap * dataConcept = getLastConceptData(); 
232                 if(getIfConceptCheckedAt( dataConcept->getName(), 0 )){
233                         wxContourMainFrame::getInstance()->onChangeInstantInstantPanel(dataConcept->getName(), dataConcept->getActualValue(), dataConcept->getMinShowedValue(), dataConcept->getMaxShowedValue());
234                 }
235                 //((wxContourEventHandler*)_eventHandler)->onChangeInstant(newevent);
236                 //_eventHandler->ProcessEvent(newevent);
237         }
238
239         void wxInstantChooserPanel :: onChangeInstant( wxCommandEvent& event ){
240                 
241         }
242
243         //------------------------------------------------------------------------------------------------------------
244         //  Methods for sending events
245         //------------------------------------------------------------------------------------------------------------
246
247         //------------------------------------------------------------------------------------------------------------
248         //  Attributes getters and setters
249         //------------------------------------------------------------------------------------------------------------
250                 
251         ConceptDataWrap * wxInstantChooserPanel :: getLastConceptData()
252         {
253                 ConceptDataWrap * conceptData = new ConceptDataWrap();
254                 if( _lastConceptUpdated != NULL)
255                 {
256                         conceptData -> fillData( _lastConceptUpdated->getName(), _lastConceptUpdated->getMinValue(), _lastConceptUpdated->getMaxValue(), _lastConceptUpdated->getMinShowedValue(), _lastConceptUpdated->getMaxShowedValue(), _lastConceptUpdated->getActualValue());
257                 }
258                 return conceptData;
259         }
260
261         //------------------------------------------------------------------------------------------------------------
262         //  Other functional methods
263         //------------------------------------------------------------------------------------------------------------
264
265         void wxInstantChooserPanel :: setInstant(std::vector<int> choiceInstant)
266         {               
267                 int max = _concepts->size();
268                 if( (int)(choiceInstant.size()) == max )
269                 {
270                         int i = 0;
271                         while(i< max)
272                         {
273                                 (*_concepts)[i]->setActualValue( choiceInstant[i] );
274                                 i++;
275                         }
276                 }
277         }
278         
279         //------------------------------------------------------------------------------------------------------------
280         void wxInstantChooserPanel :: getInstant(std::vector<int> & choiceInstant)
281         {
282                 if(_concepts!=NULL)
283                 {
284                         int i = 0;
285
286                         int size=_concepts->size();
287                         while( i< size )
288                         {
289                                 choiceInstant.push_back( ((*_concepts))[i]->getActualValue());
290                                 i++;
291                         }
292                 }               
293         }
294         //------------------------------------------------------------------------------------------------------------
295         void wxInstantChooserPanel :: setInstant(std::map<std::string,int>* _actualInstant )
296         {
297                 int size,i;
298                 size=_concepts->size();
299                 std::map<std::string,int>::iterator iteratorActualInstant;
300                 wxConceptControl* concepti;
301                 std::string nameConcepti;
302                 for(i=0;i<size;i++)
303                 {
304                         concepti=(*_concepts)[i];
305                         nameConcepti=concepti->getName();
306                         iteratorActualInstant=_actualInstant->find(nameConcepti);
307                         if(iteratorActualInstant!=_actualInstant->end())
308                         {
309                                 int actualValue=iteratorActualInstant->second;
310                                 concepti->setActualValue(actualValue);
311                         }
312                         nameConcepti.clear();
313                 }
314         }
315         //------------------------------------------------------------------------------------------------------------
316         std::map<std::string,int>* wxInstantChooserPanel :: getInstantData()
317         {
318                 int size,i=0;   
319                 wxConceptControl* concepti;
320                 if(_concepts!=NULL)
321                 {
322                         bool isEmpty=_actualInstant->empty();
323                         if(!isEmpty) _actualInstant->clear();
324                         size=_concepts->size();
325                         while( i< size )
326                         {
327                                 concepti=(*_concepts)[i];
328                                 _actualInstant->insert(std::pair<std::string,int>(concepti->getName(),concepti->getActualValue()));
329                                 i++;
330                         }
331                 }
332                 return _actualInstant;          
333         }
334         //------------------------------------------------------------------------------------------------------------
335         void wxInstantChooserPanel :: getInstantData(std::map<std::string,int> & choiceInstantData)
336         {
337                 int i = 0,size;
338                 size=_concepts->size();
339                 while(i< size)
340                 {
341                         choiceInstantData.insert(std::pair < std::string, int> ((*_concepts)[i]->getName(), (*_concepts)[i]->getActualValue()));
342                         i++;
343                 }
344         }
345         //------------------------------------------------------------------------------------------------------------
346         void wxInstantChooserPanel :: setResolutionRangeWithLasUpdate()
347         {
348                 int minShow = _lastConceptUpdated->getMinShowedValue();
349                 int maxShow = _lastConceptUpdated->getMaxShowedValue();
350                 _resolutionSlider->SetRange( minShow, maxShow );
351         }
352
353         
354         //------------------------------------------------------------------------------------------------------------
355         void wxInstantChooserPanel :: refreshConceptLabels()
356         {
357                 
358         }
359         //------------------------------------------------------------------------------------------------------------
360         void wxInstantChooserPanel :: setConfiguration(std::string aConfiguration)
361         {
362                 _configuration = aConfiguration;
363         }
364         //------------------------------------------------------------------------------------------------------------
365         std::string wxInstantChooserPanel :: getConfiguration()
366         {
367                 return _configuration;
368         }
369         //------------------------------------------------------------------------------------------------------------
370         void wxInstantChooserPanel :: setResolutionOverAll( bool condition )
371         {
372                 _resolutionOverALL = condition;
373         }
374         //------------------------------------------------------------------------------------------------------------
375         bool wxInstantChooserPanel :: getIfResolutionOverAll()
376         {
377                 return _resolutionOverALL;
378         }
379
380    //------------------------------------------------------------------------------------------------------------
381         void wxInstantChooserPanel :: initializeResolutionSlider()
382         {       
383                 _resolutionSlider = new wxSlider(this , -1,5,1,8,wxDefaultPosition, wxSize(25,45), wxSL_LEFT | wxSL_AUTOTICKS  );               
384                 Connect( _resolutionSlider->GetId(), wxEVT_COMMAND_SLIDER_UPDATED, wxScrollEventHandler(wxInstantChooserPanel::onResolutionSlider));//(wxObjectEventFunction) &wxInstantChooserPanel::onResolutionSlider ); 
385         }
386         //------------------------------------------------------------------------------------------------------------
387         void wxInstantChooserPanel :: changeResolutionAtConceptControl( wxConceptControl * theConceptToUpdate )
388         {
389                 int value = theConceptToUpdate->getActualValue();
390                 int min = theConceptToUpdate->getMinValue();
391                 int max = theConceptToUpdate->getMaxValue();
392                 int delta = (int) (pow( (double)2, (double)_resolutionSlider->GetValue() ));
393                 int minTmp   = value - delta/2;
394                 int maxTmp   = value + delta/2;
395                 if (minTmp<min)
396                 {
397                         minTmp = min;   
398                 }
399                 if (maxTmp>max)
400                 {
401                         maxTmp = max;
402                 }                       
403                 theConceptToUpdate->setRange( minTmp,maxTmp );
404         }
405
406         //------------------------------------------------------------------------------------------------------------
407         void wxInstantChooserPanel :: setVerticalMaxSelectedNums( std::string groupMaxVertNums )
408         {
409                 int endSub;
410                 std::string::size_type numberIndex = groupMaxVertNums.find(" ", 0);
411                 std::string::size_type start = 0;
412                 while( numberIndex != std::string::npos ) 
413                 {       
414             endSub = numberIndex;
415                         std::string number = groupMaxVertNums.substr( 0, endSub );
416                         groupMaxVertNums.erase( 0, endSub+1 ); 
417                         int intReturn = atoi(number.c_str());                   
418                         if( intReturn>0 )
419                                 _verticalMaxSelected.push_back(intReturn);      
420                         else
421                                 _verticalMaxSelected.push_back( 1 );    
422                         start += numberIndex;
423                         numberIndex = groupMaxVertNums.find(" ", endSub);                       
424                 }                               
425         }
426         //------------------------------------------------------------------------------------------------------------
427         void wxInstantChooserPanel :: setConceptValue( std::string theConceptName, int theValue )
428         {
429                 int i, size = _concepts->size();
430                 bool setted = false;
431                 for ( i=0; i<size && !setted; i++ )
432                 {       
433                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
434                         {
435                                 (*_concepts)[i]->setActualValue( theValue );
436                                 setted = true;
437                         }
438                 }
439         }
440         
441         //------------------------------------------------------------------------------------------------------------
442         int wxInstantChooserPanel :: getConceptValue( std::string theConceptName )
443         {
444                 int i, size = _concepts->size();
445                 int value = -1;
446                 for ( i=0; i<size && value==-1; i++ )
447                 {       
448                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
449                         {
450                                 value = (*_concepts)[i]->getActualValue(  );
451                         }
452                 }
453                 return value;
454         }
455         //------------------------------------------------------------------------------------------------------------
456         ConceptDataWrap * wxInstantChooserPanel :: getConceptDataOf( std::string theConceptName )
457         {
458                 ConceptDataWrap * conceptData = new ConceptDataWrap();
459
460                 int i, size = _concepts->size();
461                 int value = -1;
462                 for ( i=0; i<size && value==-1; i++ )
463                 {       
464                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
465                         {               
466                                 conceptData -> fillData( (*_concepts)[i]->getName(), (*_concepts)[i]->getMinValue(), (*_concepts)[i]->getMaxValue(), (*_concepts)[i]->getMinShowedValue(), (*_concepts)[i]->getMaxShowedValue(), (*_concepts)[i]->getActualValue());                    
467                         }
468                 }
469                 return conceptData;
470         }
471         //------------------------------------------------------------------------------------------------------------
472         bool wxInstantChooserPanel :: getIfConceptCheckedAt( std::string theConceptName, int atChecksPosition )
473         {
474                 bool isChecked = false;
475
476                 int i,size = _concepts->size();
477 //              wxConceptControl * aConcept = NULL;                     
478                 for ( i=0; i<size && !isChecked; i++ )
479                 {       
480                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
481                         {               
482                                 isChecked = (*_concepts)[i]->getIfChecked( atChecksPosition );
483                         }
484                 }
485
486                 return isChecked;
487         }
488          
489          
490         void wxInstantChooserPanel::addConcepts(std::vector<std::string> conceptNameVect,std::vector<int> conceptSizeVect){
491                 int i=0;
492                 int max = conceptNameVect.size();                       
493                 for( ; i<max; i++)
494                 {
495                         if( conceptNameVect[i].compare("Axe Depth") == 0 )
496                         {
497                                 addConcept( conceptNameVect[i], 0, conceptSizeVect[i], 1);                                      
498                         } else {
499                                 addConcept( conceptNameVect[i], 1, conceptSizeVect[i], 1);
500                         } // if
501                 }
502         }
503