]> Creatis software - creaContours.git/blob - lib/Interface_ManagerContour_NDimensions/wxInstantChooserPanel.cxx
Changes with manualBaseModel. It improves the functionality with the new
[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( 2, _resolutionSlider->GetValue() ));
138                 int minTmp   = value - delta/2;
139                 int maxTmp   = value + delta/2;
140                 if (minTmp<min)
141                         {
142                                 minTmp = min;   
143                         }
144                 if (maxTmp>max)
145                         {
146                                 maxTmp = max;
147                         }                       
148                 _lastConceptUpdated->setRange( minTmp,maxTmp );
149
150                 if (! _resolutionOverALL)
151                 {
152                         changeResolutionAtConceptControl( _lastConceptUpdated );
153                 }
154                 else
155                 {
156                         for(int i=0; i< (int)(_concepts->size()); i++)
157                         {
158                                 changeResolutionAtConceptControl( (*_concepts)[i] );
159                         }
160                 }               
161
162         }
163         //------------------------------------------------------------------------------------------------------------
164         void wxInstantChooserPanel :: onCheckBoxClicked( wxCommandEvent& event )
165         {
166                 /*int groupID = (int)event.GetClientData();
167                 wxConceptControl * eventConcept = (wxConceptControl *)event.GetEventObject();
168                 if ( eventConcept->getIfChecked( groupID ) )
169                 {
170                         int i,size = _concepts->size();
171                         wxConceptControl * aConcept = NULL;
172                         int accum = 0;          
173                         for( i=0; i<size; i++)
174                         {
175                                 accum+= (*_concepts)[i]->getIfChecked( groupID ) ? 1 :0;
176                         }               
177                         for( i=0; i<size && _verticalMaxSelected[groupID] < accum; i++)
178                         {
179                                 aConcept = (*_concepts)[i]; 
180                                 if( aConcept!= eventConcept )
181                                 {
182                                         if( aConcept->getIfChecked( groupID ) )
183                                         {       
184                                                 aConcept->clearCheckAt( groupID );
185                                                 accum--;
186                                         }                                       
187                                 }
188                         }                                       
189                 }*/
190
191                 int groupID = (int)event.GetClientData();
192                 wxConceptControl * eventConcept = (wxConceptControl *)event.GetEventObject();
193                 if ( eventConcept->getIfChecked( groupID ) )
194                 {
195                         int size = _concepts->size();
196                         wxConceptControl * aConcept = NULL;
197 //                      int accum = 0;
198
199                         for(int i = 0; i < eventConcept->getNumCheckBox();i++){
200                                 if(i!=groupID){
201                                         eventConcept->clearCheckAt(i);
202                                 }
203                         }
204                         for(int i=0; i<size; i++)
205                         {
206                                 aConcept = (*_concepts)[i]; 
207                                 if( aConcept!= eventConcept )
208                                 {
209                                         if( aConcept->getIfChecked( groupID ) )
210                                         {       
211                                                 aConcept->clearCheckAt( groupID );
212                                         }                                       
213                                 }
214                         }                       
215                 }
216         }
217
218         //------------------------------------------------------------------------------------------------------------
219         void wxInstantChooserPanel :: onConceptRelease( wxCommandEvent& event )
220         {
221                 _lastConceptUpdated = (wxConceptControl*) event.GetEventObject();
222
223                 std::vector<int> theInstant;
224         getInstant( theInstant );
225                 //-------------------------------------------------
226                 //  Sending the event of wxINSTANT_CHOOSER_CHANGE
227                 //-------------------------------------------------
228                 wxCommandEvent newevent(wxINSTANT_CHOOSER_CHANGE,GetId());  
229                 //this->onChangeInstant(newevent);
230                 ConceptDataWrap * dataConcept = getLastConceptData(); 
231                 if(getIfConceptCheckedAt( dataConcept->getName(), 0 )){
232                         wxContourMainFrame::getInstance()->onChangeInstantInstantPanel(dataConcept->getName(), dataConcept->getActualValue(), dataConcept->getMinShowedValue(), dataConcept->getMaxShowedValue());
233                 }
234                 //((wxContourEventHandler*)_eventHandler)->onChangeInstant(newevent);
235                 //_eventHandler->ProcessEvent(newevent);
236         }
237
238         void wxInstantChooserPanel :: onChangeInstant( wxCommandEvent& event ){
239                 
240         }
241
242         //------------------------------------------------------------------------------------------------------------
243         //  Methods for sending events
244         //------------------------------------------------------------------------------------------------------------
245
246         //------------------------------------------------------------------------------------------------------------
247         //  Attributes getters and setters
248         //------------------------------------------------------------------------------------------------------------
249                 
250         ConceptDataWrap * wxInstantChooserPanel :: getLastConceptData()
251         {
252                 ConceptDataWrap * conceptData = new ConceptDataWrap();
253                 if( _lastConceptUpdated != NULL)
254                 {
255                         conceptData -> fillData( _lastConceptUpdated->getName(), _lastConceptUpdated->getMinValue(), _lastConceptUpdated->getMaxValue(), _lastConceptUpdated->getMinShowedValue(), _lastConceptUpdated->getMaxShowedValue(), _lastConceptUpdated->getActualValue());
256                 }
257                 return conceptData;
258         }
259
260         //------------------------------------------------------------------------------------------------------------
261         //  Other functional methods
262         //------------------------------------------------------------------------------------------------------------
263
264         void wxInstantChooserPanel :: setInstant(std::vector<int> choiceInstant)
265         {               
266                 int max = _concepts->size();
267                 if( (int)(choiceInstant.size()) == max )
268                 {
269                         int i = 0;
270                         while(i< max)
271                         {
272                                 (*_concepts)[i]->setActualValue( choiceInstant[i] );
273                                 i++;
274                         }
275                 }
276         }
277         
278         //------------------------------------------------------------------------------------------------------------
279         void wxInstantChooserPanel :: getInstant(std::vector<int> & choiceInstant)
280         {
281                 if(_concepts!=NULL)
282                 {
283                         int i = 0;
284
285                         int size=_concepts->size();
286                         while( i< size )
287                         {
288                                 choiceInstant.push_back( ((*_concepts))[i]->getActualValue());
289                                 i++;
290                         }
291                 }               
292         }
293         //------------------------------------------------------------------------------------------------------------
294         void wxInstantChooserPanel :: setInstant(std::map<std::string,int>* _actualInstant )
295         {
296                 int size,i;
297                 size=_concepts->size();
298                 std::map<std::string,int>::iterator iteratorActualInstant;
299                 wxConceptControl* concepti;
300                 std::string nameConcepti;
301                 for(i=0;i<size;i++)
302                 {
303                         concepti=(*_concepts)[i];
304                         nameConcepti=concepti->getName();
305                         iteratorActualInstant=_actualInstant->find(nameConcepti);
306                         if(iteratorActualInstant!=_actualInstant->end())
307                         {
308                                 int actualValue=iteratorActualInstant->second;
309                                 concepti->setActualValue(actualValue);
310                         }
311                         nameConcepti.clear();
312                 }
313         }
314         //------------------------------------------------------------------------------------------------------------
315         std::map<std::string,int>* wxInstantChooserPanel :: getInstantData()
316         {
317                 int size,i=0;   
318                 wxConceptControl* concepti;
319                 if(_concepts!=NULL)
320                 {
321                         bool isEmpty=_actualInstant->empty();
322                         if(!isEmpty) _actualInstant->clear();
323                         size=_concepts->size();
324                         while( i< size )
325                         {
326                                 concepti=(*_concepts)[i];
327                                 _actualInstant->insert(std::pair<std::string,int>(concepti->getName(),concepti->getActualValue()));
328                                 i++;
329                         }
330                 }
331                 return _actualInstant;          
332         }
333         //------------------------------------------------------------------------------------------------------------
334         void wxInstantChooserPanel :: getInstantData(std::map<std::string,int> & choiceInstantData)
335         {
336                 int i = 0,size;
337                 size=_concepts->size();
338                 while(i< size)
339                 {
340                         choiceInstantData.insert(std::pair < std::string, int> ((*_concepts)[i]->getName(), (*_concepts)[i]->getActualValue()));
341                         i++;
342                 }
343         }
344         //------------------------------------------------------------------------------------------------------------
345         void wxInstantChooserPanel :: setResolutionRangeWithLasUpdate()
346         {
347                 int minShow = _lastConceptUpdated->getMinShowedValue();
348                 int maxShow = _lastConceptUpdated->getMaxShowedValue();
349                 _resolutionSlider->SetRange( minShow, maxShow );
350         }
351
352         
353         //------------------------------------------------------------------------------------------------------------
354         void wxInstantChooserPanel :: refreshConceptLabels()
355         {
356                 
357         }
358         //------------------------------------------------------------------------------------------------------------
359         void wxInstantChooserPanel :: setConfiguration(std::string aConfiguration)
360         {
361                 _configuration = aConfiguration;
362         }
363         //------------------------------------------------------------------------------------------------------------
364         std::string wxInstantChooserPanel :: getConfiguration()
365         {
366                 return _configuration;
367         }
368         //------------------------------------------------------------------------------------------------------------
369         void wxInstantChooserPanel :: setResolutionOverAll( bool condition )
370         {
371                 _resolutionOverALL = condition;
372         }
373         //------------------------------------------------------------------------------------------------------------
374         bool wxInstantChooserPanel :: getIfResolutionOverAll()
375         {
376                 return _resolutionOverALL;
377         }
378
379    //------------------------------------------------------------------------------------------------------------
380         void wxInstantChooserPanel :: initializeResolutionSlider()
381         {       
382                 _resolutionSlider = new wxSlider(this , -1,5,1,8,wxDefaultPosition, wxSize(25,45), wxSL_LEFT | wxSL_AUTOTICKS  );               
383                 Connect( _resolutionSlider->GetId(), wxEVT_COMMAND_SLIDER_UPDATED, wxScrollEventHandler(wxInstantChooserPanel::onResolutionSlider));//(wxObjectEventFunction) &wxInstantChooserPanel::onResolutionSlider ); 
384         }
385         //------------------------------------------------------------------------------------------------------------
386         void wxInstantChooserPanel :: changeResolutionAtConceptControl( wxConceptControl * theConceptToUpdate )
387         {
388                 int value = theConceptToUpdate->getActualValue();
389                 int min = theConceptToUpdate->getMinValue();
390                 int max = theConceptToUpdate->getMaxValue();
391                 int delta = (int) (pow( 2, _resolutionSlider->GetValue() ));
392                 int minTmp   = value - delta/2;
393                 int maxTmp   = value + delta/2;
394                 if (minTmp<min)
395                 {
396                         minTmp = min;   
397                 }
398                 if (maxTmp>max)
399                 {
400                         maxTmp = max;
401                 }                       
402                 theConceptToUpdate->setRange( minTmp,maxTmp );
403         }
404
405         //------------------------------------------------------------------------------------------------------------
406         void wxInstantChooserPanel :: setVerticalMaxSelectedNums( std::string groupMaxVertNums )
407         {
408                 int endSub;
409                 std::string::size_type numberIndex = groupMaxVertNums.find(" ", 0);
410                 std::string::size_type start = 0;
411                 while( numberIndex != std::string::npos ) 
412                 {       
413             endSub = numberIndex;
414                         std::string number = groupMaxVertNums.substr( 0, endSub );
415                         groupMaxVertNums.erase( 0, endSub+1 ); 
416                         int intReturn = atoi(number.c_str());                   
417                         if( intReturn>0 )
418                                 _verticalMaxSelected.push_back(intReturn);      
419                         else
420                                 _verticalMaxSelected.push_back( 1 );    
421                         start += numberIndex;
422                         numberIndex = groupMaxVertNums.find(" ", endSub);                       
423                 }                               
424         }
425         //------------------------------------------------------------------------------------------------------------
426         void wxInstantChooserPanel :: setConceptValue( std::string theConceptName, int theValue )
427         {
428                 int i, size = _concepts->size();
429                 bool setted = false;
430                 for ( i=0; i<size && !setted; i++ )
431                 {       
432                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
433                         {
434                                 (*_concepts)[i]->setActualValue( theValue );
435                                 setted = true;
436                         }
437                 }
438         }
439         
440         //------------------------------------------------------------------------------------------------------------
441         int wxInstantChooserPanel :: getConceptValue( std::string theConceptName )
442         {
443                 int i, size = _concepts->size();
444                 int value = -1;
445                 for ( i=0; i<size && value==-1; i++ )
446                 {       
447                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
448                         {
449                                 value = (*_concepts)[i]->getActualValue(  );
450                         }
451                 }
452                 return value;
453         }
454         //------------------------------------------------------------------------------------------------------------
455         ConceptDataWrap * wxInstantChooserPanel :: getConceptDataOf( std::string theConceptName )
456         {
457                 ConceptDataWrap * conceptData = new ConceptDataWrap();
458
459                 int i, size = _concepts->size();
460                 int value = -1;
461                 for ( i=0; i<size && value==-1; i++ )
462                 {       
463                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
464                         {               
465                                 conceptData -> fillData( (*_concepts)[i]->getName(), (*_concepts)[i]->getMinValue(), (*_concepts)[i]->getMaxValue(), (*_concepts)[i]->getMinShowedValue(), (*_concepts)[i]->getMaxShowedValue(), (*_concepts)[i]->getActualValue());                    
466                         }
467                 }
468                 return conceptData;
469         }
470         //------------------------------------------------------------------------------------------------------------
471         bool wxInstantChooserPanel :: getIfConceptCheckedAt( std::string theConceptName, int atChecksPosition )
472         {
473                 bool isChecked = false;
474
475                 int i,size = _concepts->size();
476 //              wxConceptControl * aConcept = NULL;                     
477                 for ( i=0; i<size && !isChecked; i++ )
478                 {       
479                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
480                         {               
481                                 isChecked = (*_concepts)[i]->getIfChecked( atChecksPosition );
482                         }
483                 }
484
485                 return isChecked;
486         }
487          
488          
489         void wxInstantChooserPanel::addConcepts(std::vector<std::string> conceptNameVect,std::vector<int> conceptSizeVect){
490                 int i=0;
491                 int max = conceptNameVect.size();                       
492                 for( ; i<max; i++)
493                 {
494                         if( conceptNameVect[i].compare("Axe Depth") == 0 )
495                         {
496                                 addConcept( conceptNameVect[i], 0, conceptSizeVect[i], 1);                                      
497                         } else {
498                                 addConcept( conceptNameVect[i], 1, conceptSizeVect[i], 1);
499                         } // if
500                 }
501         }
502