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