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