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