]> Creatis software - creaContours.git/blob - lib/Interface_ManagerContour_NDimensions/wxInstantChooserPanel.cxx
e3bc789e5e8dcffa6471d430c907bbd3b9c4407a
[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:: onChechBoxClicked );               
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:: onChechBoxClicked );               
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<_concepts->size(); i++)
157                         {
158                                 changeResolutionAtConceptControl( (*_concepts)[i] );
159                         }
160                 }               
161
162         }
163         //------------------------------------------------------------------------------------------------------------
164         void wxInstantChooserPanel :: onChechBoxClicked( 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
192         //------------------------------------------------------------------------------------------------------------
193         void wxInstantChooserPanel :: onConceptRelease( wxCommandEvent& event )
194         {
195                 _lastConceptUpdated = (wxConceptControl*) event.GetEventObject();
196
197                 std::vector<int> theInstant;
198         getInstant( theInstant );
199                 //-------------------------------------------------
200                 //  Sending the event of wxINSTANT_CHOOSER_CHANGE
201                 //-------------------------------------------------
202                 wxCommandEvent newevent(wxINSTANT_CHOOSER_CHANGE,GetId());  
203                 //this->onChangeInstant(newevent);
204                 ((wxContourEventHandler*)_eventHandler)->onChangeInstant(newevent);
205                 //_eventHandler->ProcessEvent(newevent);
206         }
207
208         void wxInstantChooserPanel :: onChangeInstant( wxCommandEvent& event ){
209                 
210         }
211
212         //------------------------------------------------------------------------------------------------------------
213         //  Methods for sending events
214         //------------------------------------------------------------------------------------------------------------
215
216         //------------------------------------------------------------------------------------------------------------
217         //  Attributes getters and setters
218         //------------------------------------------------------------------------------------------------------------
219                 
220         ConceptDataWrap * wxInstantChooserPanel :: getLastConceptData()
221         {
222                 ConceptDataWrap * conceptData = new ConceptDataWrap();
223                 if( _lastConceptUpdated != NULL)
224                 {
225                         conceptData -> fillData( _lastConceptUpdated->getName(), _lastConceptUpdated->getMinValue(), _lastConceptUpdated->getMaxValue(), _lastConceptUpdated->getMinShowedValue(), _lastConceptUpdated->getMaxShowedValue(), _lastConceptUpdated->getActualValue());
226                 }
227                 return conceptData;
228         }
229
230         //------------------------------------------------------------------------------------------------------------
231         //  Other functional methods
232         //------------------------------------------------------------------------------------------------------------
233
234         void wxInstantChooserPanel :: setInstant(std::vector<int> choiceInstant)
235         {               
236                 int max = _concepts->size();
237                 if( choiceInstant.size() == max )
238                 {
239                         int i = 0;
240                         while(i< max)
241                         {
242                                 (*_concepts)[i]->setActualValue( choiceInstant[i] );
243                                 i++;
244                         }
245                 }
246         }
247         
248         //------------------------------------------------------------------------------------------------------------
249         void wxInstantChooserPanel :: getInstant(std::vector<int> & choiceInstant)
250         {
251                 if(_concepts!=NULL)
252                 {
253                         int i = 0;
254
255                         int size=_concepts->size();
256                         while( i< size )
257                         {
258                                 choiceInstant.push_back( ((*_concepts))[i]->getActualValue());
259                                 i++;
260                         }
261                 }               
262         }
263         //------------------------------------------------------------------------------------------------------------
264         void wxInstantChooserPanel :: setInstant(std::map<std::string,int>* _actualInstant )
265         {
266                 int size,i;
267                 size=_concepts->size();
268                 std::map<std::string,int>::iterator iteratorActualInstant;
269                 wxConceptControl* concepti;
270                 std::string nameConcepti;
271                 for(i=0;i<size;i++)
272                 {
273                         concepti=(*_concepts)[i];
274                         nameConcepti=concepti->getName();
275                         iteratorActualInstant=_actualInstant->find(nameConcepti);
276                         if(iteratorActualInstant!=_actualInstant->end())
277                         {
278                                 int actualValue=iteratorActualInstant->second;
279                                 concepti->setActualValue(actualValue);
280                         }
281                         nameConcepti.clear();
282                 }
283         }
284         //------------------------------------------------------------------------------------------------------------
285         std::map<std::string,int>* wxInstantChooserPanel :: getInstantData()
286         {
287                 int size,i=0;   
288                 wxConceptControl* concepti;
289                 if(_concepts!=NULL)
290                 {
291                         bool isEmpty=_actualInstant->empty();
292                         if(!isEmpty) _actualInstant->clear();
293                         size=_concepts->size();
294                         while( i< size )
295                         {
296                                 concepti=(*_concepts)[i];
297                                 _actualInstant->insert(std::pair<std::string,int>(concepti->getName(),concepti->getActualValue()));
298                                 i++;
299                         }
300                 }
301                 return _actualInstant;          
302         }
303         //------------------------------------------------------------------------------------------------------------
304         void wxInstantChooserPanel :: getInstantData(std::map<std::string,int> & choiceInstantData)
305         {
306                 int i = 0,size;
307                 size=_concepts->size();
308                 while(i< size)
309                 {
310                         choiceInstantData.insert(std::pair < std::string, int> ((*_concepts)[i]->getName(), (*_concepts)[i]->getActualValue()));
311                         i++;
312                 }
313         }
314         //------------------------------------------------------------------------------------------------------------
315         void wxInstantChooserPanel :: setResolutionRangeWithLasUpdate()
316         {
317                 int minShow = _lastConceptUpdated->getMinShowedValue();
318                 int maxShow = _lastConceptUpdated->getMaxShowedValue();
319                 _resolutionSlider->SetRange( minShow, maxShow );
320         }
321
322         
323         //------------------------------------------------------------------------------------------------------------
324         void wxInstantChooserPanel :: refreshConceptLabels()
325         {
326                 
327         }
328         //------------------------------------------------------------------------------------------------------------
329         void wxInstantChooserPanel :: setConfiguration(std::string aConfiguration)
330         {
331                 _configuration = aConfiguration;
332         }
333         //------------------------------------------------------------------------------------------------------------
334         std::string wxInstantChooserPanel :: getConfiguration()
335         {
336                 return _configuration;
337         }
338         //------------------------------------------------------------------------------------------------------------
339         void wxInstantChooserPanel :: setResolutionOverAll( bool condition )
340         {
341                 _resolutionOverALL = condition;
342         }
343         //------------------------------------------------------------------------------------------------------------
344         bool wxInstantChooserPanel :: getIfResolutionOverAll()
345         {
346                 return _resolutionOverALL;
347         }
348
349    //------------------------------------------------------------------------------------------------------------
350         void wxInstantChooserPanel :: initializeResolutionSlider()
351         {       
352                 _resolutionSlider = new wxSlider(this , -1,5,1,8,wxDefaultPosition, wxSize(25,45), wxSL_LEFT | wxSL_AUTOTICKS  );               
353                 Connect( _resolutionSlider->GetId(), wxEVT_COMMAND_SLIDER_UPDATED, wxScrollEventHandler(wxInstantChooserPanel::onResolutionSlider));//(wxObjectEventFunction) &wxInstantChooserPanel::onResolutionSlider ); 
354         }
355         //------------------------------------------------------------------------------------------------------------
356         void wxInstantChooserPanel :: changeResolutionAtConceptControl( wxConceptControl * theConceptToUpdate )
357         {
358                 int value = theConceptToUpdate->getActualValue();
359                 int min = theConceptToUpdate->getMinValue();
360                 int max = theConceptToUpdate->getMaxValue();
361                 int delta = (int) (pow( 2, _resolutionSlider->GetValue() ));
362                 int minTmp   = value - delta/2;
363                 int maxTmp   = value + delta/2;
364                 if (minTmp<min)
365                 {
366                         minTmp = min;   
367                 }
368                 if (maxTmp>max)
369                 {
370                         maxTmp = max;
371                 }                       
372                 theConceptToUpdate->setRange( minTmp,maxTmp );
373         }
374
375         //------------------------------------------------------------------------------------------------------------
376         void wxInstantChooserPanel :: setVerticalMaxSelectedNums( std::string groupMaxVertNums )
377         {
378                 int endSub;
379                 std::string::size_type numberIndex = groupMaxVertNums.find(" ", 0);
380                 std::string::size_type start = 0;
381                 while( numberIndex != std::string::npos ) 
382                 {       
383             endSub = numberIndex;
384                         std::string number = groupMaxVertNums.substr( 0, endSub );
385                         groupMaxVertNums.erase( 0, endSub+1 ); 
386                         int intReturn = atoi(number.c_str());                   
387                         if( intReturn>0 )
388                                 _verticalMaxSelected.push_back(intReturn);      
389                         else
390                                 _verticalMaxSelected.push_back( 1 );    
391                         start += numberIndex;
392                         numberIndex = groupMaxVertNums.find(" ", endSub);                       
393                 }                               
394         }
395         //------------------------------------------------------------------------------------------------------------
396         void wxInstantChooserPanel :: setConceptValue( std::string theConceptName, int theValue )
397         {
398                 int i, size = _concepts->size();
399                 bool setted = false;
400                 for ( i=0; i<size && !setted; i++ )
401                 {       
402                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
403                         {
404                                 (*_concepts)[i]->setActualValue( theValue );
405                                 setted = true;
406                         }
407                 }
408         }
409         
410         //------------------------------------------------------------------------------------------------------------
411         int wxInstantChooserPanel :: getConceptValue( std::string theConceptName )
412         {
413                 int i, size = _concepts->size();
414                 int value = -1;
415                 for ( i=0; i<size && value==-1; i++ )
416                 {       
417                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
418                         {
419                                 value = (*_concepts)[i]->getActualValue(  );
420                         }
421                 }
422                 return value;
423         }
424         //------------------------------------------------------------------------------------------------------------
425         ConceptDataWrap * wxInstantChooserPanel :: getConceptDataOf( std::string theConceptName )
426         {
427                 ConceptDataWrap * conceptData = new ConceptDataWrap();
428
429                 int i, size = _concepts->size();
430                 int value = -1;
431                 for ( i=0; i<size && value==-1; i++ )
432                 {       
433                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
434                         {               
435                                 conceptData -> fillData( (*_concepts)[i]->getName(), (*_concepts)[i]->getMinValue(), (*_concepts)[i]->getMaxValue(), (*_concepts)[i]->getMinShowedValue(), (*_concepts)[i]->getMaxShowedValue(), (*_concepts)[i]->getActualValue());                    
436                         }
437                 }
438                 return conceptData;
439         }
440         //------------------------------------------------------------------------------------------------------------
441         bool wxInstantChooserPanel :: getIfConceptCheckedAt( std::string theConceptName, int atChecksPosition )
442         {
443                 bool isChecked = false;
444
445                 int i,size = _concepts->size();
446                 wxConceptControl * aConcept = NULL;                     
447                 for ( i=0; i<size && !isChecked; i++ )
448                 {       
449                         if( (*_concepts)[i]->getName().compare( theConceptName ) == 0 )
450                         {               
451                                 isChecked = (*_concepts)[i]->getIfChecked( atChecksPosition );
452                         }
453                 }
454
455                 return isChecked;
456         }
457          
458