]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/manualContour/manualContourBaseControler.cpp
#3533 Update Curent contour and actual point ShowNPoints in Undo Redo
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / manualContour / manualContourBaseControler.cpp
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 #include "manualContourBaseControler.h"
27
28
29 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
32
33 manualContourBaseControler::manualContourBaseControler()
34 {
35         _manViewBaseCont        = NULL;
36         _manContModel           = NULL;
37         _state                          = 0;
38         
39 //EED 21 mars 2012  FLIP probleme  ..PLOP..
40 //      _z                                      = 900;
41         _z                                      = -900;
42         
43         _editable                       = true;
44         _posibleToMove          = true;
45         _moving                         = false;
46         _created                        = false;
47         _keyBoardMoving         = false;
48 }
49 // ----------------------------------------------------------------------------
50 manualContourBaseControler::~manualContourBaseControler()
51 {
52 }
53
54 // ----------------------------------------------------------------------------
55 manualContourBaseControler * manualContourBaseControler :: Clone()  // virtual
56 {
57         manualContourBaseControler * clone = new manualContourBaseControler();
58         CopyAttributesTo(clone);
59         return clone;
60 }
61
62 // ---------------------------------------------------------------------------
63
64 void manualContourBaseControler::CopyAttributesTo( manualContourBaseControler * cloneObject)
65 {
66         // Fathers object
67         InteractorStyleMaracas::CopyAttributesTo(cloneObject);
68         cloneObject->SetZ( this->GetZ() );
69         cloneObject->SetState( this->GetState() );
70         cloneObject->SetEditable( this->IsEditable() );
71         cloneObject->SetPosibleToMove( this->GetPosibleToMove() );
72         cloneObject->SetMoving( this->IsMoving() );
73         cloneObject->SetCompleteCreation( this->GetIfCompleteCreation() );
74         cloneObject->SetKeyBoardMoving( this->GetKeyBoardMoving() );
75 }
76
77 // ----------------------------------------------------------------------------
78 void manualContourBaseControler::Configure() //virtual
79 {
80 }
81
82 // ----------------------------------------------------------------------------
83 bool manualContourBaseControler::OnChar()
84 {
85         if ( _vtkInteractorStyleBaseView!=NULL )
86         {
87                 char keyCode = _vtkInteractorStyleBaseView->GetInteractor()-> GetKeyCode();
88
89                 int X,Y;
90                 crea::wxVTKRenderWindowInteractor *_wxVTKiren;
91                 _wxVTKiren= _vtkInteractorStyleBaseView->GetWxVtk2DBaseView()->GetWxVTKRenderWindowInteractor();
92                 _wxVTKiren->GetEventPosition(X, Y);
93                 //int Z = GetZ(); // JPRx
94                 // Delete Point
95                 if ((keyCode==8) || (keyCode==127))
96                 {
97
98                         if (!GetManualViewBaseContour()->GetPosibleSelected()==true)
99                         {
100                                 DeleteActualMousePoint(X,Y);
101                         }
102                         GetManualViewBaseContour()->Refresh();
103                         this->_vtkInteractorStyleBaseView->SetRefresh_waiting();
104                 }
105                 else
106                 {
107                         // Magnet
108                         if (keyCode==32)
109                         {
110                                 Magnet(X,Y);
111                                 GetManualViewBaseContour()->Refresh();
112                                 this->_vtkInteractorStyleBaseView->SetRefresh_waiting();
113                         }
114                         else if( !IsEditable() )
115                         {
116                                 if ( keyCode == 'L' )
117                                 {
118                                         GetManualViewBaseContour()->MoveContour( -1, 0 );
119                                         SetKeyBoardMoving( true );
120                                 }
121                                 else if ( keyCode == 'R' )
122                                 {
123                                         GetManualViewBaseContour()->MoveContour( 1, 0 );
124                                         SetKeyBoardMoving( true );
125                                 }
126                                 else if ( keyCode == 'U' )
127                                 {
128                                         GetManualViewBaseContour()->MoveContour( 0, -1 );
129                                         SetKeyBoardMoving( true );
130                                 }
131                                 else if ( keyCode == 'D' )
132                                 {
133                                         GetManualViewBaseContour()->MoveContour( 0, 1 );
134                                         SetKeyBoardMoving( true );
135                                 }
136                                 else if ( keyCode == 'W' )//Diagonal left down
137                                 {
138                                         GetManualViewBaseContour()->MoveContour( -1, 1 );
139                                         SetKeyBoardMoving( true );
140                                 }
141                                 else if ( keyCode == 'Q' )//Diagonal left up
142                                 {
143                                         GetManualViewBaseContour()->MoveContour( -1, -1 );
144                                         SetKeyBoardMoving( true );
145                                 }
146                                 else if( keyCode == 'P' )//Diagonal right up
147                                 {
148                                         GetManualViewBaseContour()->MoveContour( 1, -1 );
149                                         SetKeyBoardMoving( true );
150                                 }
151                                 else if( keyCode == 'M' )//Diagonal right down
152                                 {
153                                         GetManualViewBaseContour()->MoveContour( 1, 1 );
154                                         SetKeyBoardMoving( true );
155                                 }
156                                 if( GetKeyBoardMoving() )
157                                 {
158                                         GetManualViewBaseContour()->Refresh();
159                                         this->_vtkInteractorStyleBaseView->SetRefresh_waiting();
160                                 }
161                         }
162                 }
163         }
164         return true;
165 }
166 // ----------------------------------------------------------------------------
167 bool manualContourBaseControler::OnMouseMove()
168 {
169         if ( _vtkInteractorStyleBaseView!=NULL)
170         {
171                 int X,Y;
172                 crea::wxVTKRenderWindowInteractor *_wxVTKiren;
173                 _wxVTKiren= _vtkInteractorStyleBaseView->GetWxVtk2DBaseView()->GetWxVTKRenderWindowInteractor();
174                 _wxVTKiren->GetEventPosition( X , Y );
175
176                 if ( (_vtkInteractorStyleBaseView->GetInteractor()->GetControlKey()==0) &&
177                         (_vtkInteractorStyleBaseView->GetInteractor()->GetShiftKey()==0) ) {
178                         MouseMove(X,Y);
179                 }
180         }
181         return true;
182 }
183 // ----------------------------------------------------------------------------
184 bool manualContourBaseControler::OnLeftButtonDown()
185 {
186         SetKeyBoardMoving( false );
187         if ( _vtkInteractorStyleBaseView!=NULL )
188         {
189                 int X,Y;
190                 crea::wxVTKRenderWindowInteractor *wxVTKiren;
191                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk2DBaseView()->GetWxVTKRenderWindowInteractor();
192                 wxVTKiren->GetEventPosition(X,Y);
193
194                 MouseClickLeft(X,Y);
195         }
196         return true;
197 }
198 // ----------------------------------------------------------------------------
199 bool manualContourBaseControler::OnLeftButtonUp()
200 {
201         if ( _vtkInteractorStyleBaseView!=NULL )
202         {
203                 int X,Y;
204                 crea::wxVTKRenderWindowInteractor *wxVTKiren;
205                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk2DBaseView()->GetWxVTKRenderWindowInteractor();
206                 wxVTKiren->GetEventPosition(X,Y);
207                 MouseReleaseLeft(X,Y);
208         }
209         return true;
210 }
211 // ----------------------------------------------------------------------------
212 bool manualContourBaseControler::OnLeftDClick()
213 {
214         if ( _vtkInteractorStyleBaseView!=NULL )
215         {
216                 int X,Y;
217                 crea::wxVTKRenderWindowInteractor *wxVTKiren;
218                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk2DBaseView()->GetWxVTKRenderWindowInteractor();
219                 wxVTKiren->GetEventPosition(X,Y);
220
221                 this->MouseDLeft(X,Y);
222         }
223         return true;
224 }
225 // ----------------------------------------------------------------------------
226 bool manualContourBaseControler::OnMiddleButtonDown()
227 {
228 //      SetKeyBoardMoving( false );
229         if ( _vtkInteractorStyleBaseView!=NULL )
230         {
231                 int X,Y;
232                 crea::wxVTKRenderWindowInteractor *wxVTKiren;
233                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk2DBaseView()->GetWxVTKRenderWindowInteractor();
234                 wxVTKiren->GetEventPosition(X,Y);
235                 GetManualViewBaseContour()->InitMove( X, Y,GetZ());
236         }
237         return true;
238 }
239 // ----------------------------------------------------------------------------
240 bool manualContourBaseControler::OnMiddleButtonUp()
241 {
242         return true;
243 }
244 // ----------------------------------------------------------------------------
245 bool manualContourBaseControler::OnRightButtonDown()
246 {
247         if( _vtkInteractorStyleBaseView!= NULL )
248         {
249                 int X,Y;
250                 crea::wxVTKRenderWindowInteractor *wxVTKiren;
251                 wxVTKiren = _vtkInteractorStyleBaseView->GetWxVtk2DBaseView()->GetWxVTKRenderWindowInteractor();
252                 wxVTKiren->GetEventPosition(X, Y);
253
254                 MouseClickRight(X,Y);
255         }
256         return true;
257 }
258 // ----------------------------------------------------------------------------
259 bool manualContourBaseControler::OnRightButtonUp()
260 {
261         return true;
262 }
263 // ----------------------------------------------------------------------------
264 void manualContourBaseControler::SetModelView(manualBaseModel *manContModel, manualViewBaseContour *manViewBaseCont){
265         _manContModel           =       manContModel;
266         _manViewBaseCont        =       manViewBaseCont;
267         _manViewBaseCont->SetEditable( &_editable );
268 }
269 // ----------------------------------------------------------------------------
270 manualBaseModel* manualContourBaseControler::GetManualContourModel()
271 {
272         return _manContModel;
273 }
274 // ----------------------------------------------------------------------------
275 manualViewBaseContour* manualContourBaseControler::GetManualViewBaseContour()
276 {
277         return _manViewBaseCont;
278 }
279 // ----------------------------------------------------------------------------
280 void manualContourBaseControler::MouseClickLeft(int x, int y) // virtual
281 {
282
283 }
284 // ----------------------------------------------------------------------------
285 void manualContourBaseControler::MouseClickRight(int x, int y)
286 {
287         SetCompleteCreation( true );
288         SetKeyBoardMoving( false );
289         this->GetManualContourModel()->SetCloseContour(true);
290
291 //      if (_state==1)
292 //      {
293 //              _state=0;
294 //      }
295         SetEditable( false );
296         SetPosibleToMove( false );
297         _state = 0;
298
299 //EED 24Avril2009       _state=7;
300 }
301 // ----------------------------------------------------------------------------
302 void manualContourBaseControler::MouseReleaseLeft(int x, int y)
303 {
304         if (_state==5){ _state = 0; }
305         if (_state==6){ _state = 0; }
306         if (_state==7){ _state = 0; }
307         SetMoving( false );
308         GetManualViewBaseContour()->SelectPosibleContour(x,y,GetZ());
309         if( GetIfCompleteCreation() && IsEditable() && !GetManualViewBaseContour()->GetPosibleSelected() && (GetManualViewBaseContour()->GetIdPoint(x,y,GetZ())==-1)  )
310         {
311                 SetEditable( false );
312                 SetPosibleToMove( false );
313         }
314 }
315 // ----------------------------------------------------------------------------
316 void manualContourBaseControler::MouseDLeft(int x, int y )
317 {
318         if (_state==0)
319         {
320                 int z=GetZ();
321                 /*bool temp = */ _manViewBaseCont->SelectPosibleContour(x,y,z);
322                 _manViewBaseCont->SelectPosiblePoint(x,y,z);
323                 if ( _manViewBaseCont->GetPosibleSelected() )
324                 {
325                         _editable = true;
326                 }
327         } // if _state==0
328 }
329 // ----------------------------------------------------------------------------
330 void manualContourBaseControler::MouseMove(int x, int y) // virtual
331 {
332 }
333 // ----------------------------------------------------------------------------
334 void manualContourBaseControler::SetState(int state)
335 {
336         _state=state;
337 }
338 // ----------------------------------------------------------------------------
339 int manualContourBaseControler::GetState()
340 {
341         return _state;
342 }
343 // ----------------------------------------------------------------------------
344 bool manualContourBaseControler::IsEditable( )
345 {
346         return _editable;
347 }
348 // ----------------------------------------------------------------------------
349 void manualContourBaseControler::SetEditable(  bool condition  )
350 {
351         if (GetManualViewBaseContour()!=NULL) {
352                 if( !condition )
353                 {
354                         GetManualViewBaseContour()->RemoveControlPoints();
355                 }
356                 GetManualViewBaseContour()->SetSelected( condition );
357         }
358         _editable = condition;
359 }
360
361 // ----------------------------------------------------------------------------
362 bool manualContourBaseControler::GetPosibleToMove()
363 {
364         return _posibleToMove;
365 }
366 // ----------------------------------------------------------------------------
367 void manualContourBaseControler::SetPosibleToMove( bool condition )
368 {
369         _posibleToMove = condition;
370 }
371 // ----------------------------------------------------------------------------
372 bool manualContourBaseControler::IsMoving()
373 {
374         return _moving;
375 }
376 // ----------------------------------------------------------------------------
377 void manualContourBaseControler::SetMoving( bool condition )
378 {
379         _moving = condition;
380 }
381 // ----------------------------------------------------------------------------
382 void manualContourBaseControler::SetCompleteCreation( bool condition )
383 {
384         _created = condition;
385 }
386 // ----------------------------------------------------------------------------
387 bool manualContourBaseControler::GetIfCompleteCreation ( )
388 {
389         return _created;
390 }
391 // ----------------------------------------------------------------------------
392 void manualContourBaseControler::SetKeyBoardMoving( bool condition )
393 {
394         _keyBoardMoving = condition;
395 }
396 // ----------------------------------------------------------------------------
397 bool manualContourBaseControler::GetKeyBoardMoving(  )
398 {
399         return _keyBoardMoving;
400 }
401 // ----------------------------------------------------------------------------
402 void manualContourBaseControler::CreateNewManualContour(){
403         _manViewBaseCont->CreateNewContour();
404 }
405 // ----------------------------------------------------------------------------
406 int     manualContourBaseControler::GetNumberOfPointsManualContour(){
407         return _manViewBaseCont->GetNumberOfPoints();
408 }
409 // ----------------------------------------------------------------------------
410
411 //JSTG - 25-02-08 -------------------------------------------------------------
412 int     manualContourBaseControler::GetNumberOfPointsSplineManualContour(){
413         //return _manViewBaseCont->GetNumberOfPointsSpline();
414         return _manContModel->GetNumberOfPointsSpline();
415 }
416 // ----------------------------------------------------------------------------
417
418 double* manualContourBaseControler::GetVectorPointsXManualContour(){
419         return _manViewBaseCont->GetVectorPointsXManualContour();
420 }
421 // ----------------------------------------------------------------------------
422 double* manualContourBaseControler::GetVectorPointsYManualContour(){
423         return _manViewBaseCont->GetVectorPointsYManualContour();
424 }
425 // ----------------------------------------------------------------------------
426 void manualContourBaseControler::DeleteContour(){
427         _manViewBaseCont->DeleteContour();
428         _manContModel->DeleteAllPoints();
429 }
430 // ----------------------------------------------------------------------------
431 void manualContourBaseControler::DeleteActualMousePoint(int x, int y)// virtual
432 {
433         if ((_manContModel!=NULL) && (_manViewBaseCont!=NULL) )
434         {
435                 int id=_manViewBaseCont->GetIdPoint(x,y,GetZ());
436                 if ((id!=-1) && (_manContModel->GetSizeLstPoints()>2) ){
437                         _manContModel->DeletePoint(id);
438                         _manViewBaseCont->DeletePoint(id);
439                 }
440         }
441         _state = 0;
442 }
443
444 // ----------------------------------------------------------------------------
445 void manualContourBaseControler::Magnet(int x, int y)
446 {
447         if( IsEditable())
448         {
449                 /*int id= */ _manViewBaseCont->GetIdPoint(x,y,GetZ()); // JPRx
450                 if (GetManualContourModel()!=NULL){
451                         double  xx      = x;
452                         double  yy      = y;
453                         double  zz      = GetZ();
454                         GetManualViewBaseContour()->TransfromCoordViewWorld(xx,yy,zz);
455                         int                     id      = GetManualContourModel()->GetIdPoint(xx,yy,zz,32000,-1);
456                         if (id!=-1)
457                         {
458                                 manualPoint     *mp     = GetManualContourModel()->GetManualPoint(id);
459                                 mp->SetPoint(xx,yy,zz);
460                         }
461         //              GetManualViewBaseContour()->UpdateViewPoint(id);
462                 }
463                 _state = 0;
464         }
465 }
466
467 // ----------------------------------------------------------------------------
468 void manualContourBaseControler::SetZ(int z)
469 {
470         _z=z;
471 }
472 // ----------------------------------------------------------------------------
473 int manualContourBaseControler::GetZ()
474 {
475         return _z;
476 }
477 // ----------------------------------------------------------------------------
478 void manualContourBaseControler::AddPoint(int x, int y, int z) // virtual
479 {
480         if (GetManualContourModel()!=NULL){
481                 double  xx      = x;
482                 double  yy      = y;
483                 double  zz      = z;
484
485                 GetManualViewBaseContour()->TransfromCoordViewWorld(xx,yy,zz);
486                 GetManualContourModel()->AddPoint(xx,yy,zz);
487                 GetManualViewBaseContour()->AddPoint();
488 //              GetManualViewBaseContour()->UpdateViewPoint(id);
489         }
490 }
491 // ----------------------------------------------------------------------------
492 void manualContourBaseControler::InsertPoint(int x,int y,int z)  // virtual
493 {
494 //EEDzz
495         int id=-1;
496         if (GetManualContourModel()!=NULL){
497                 double                          xx              = x;
498                 double                          yy              = y;
499                 double                          zz              = z;
500                 GetManualViewBaseContour()->TransfromCoordViewWorld(xx,yy,zz);
501                 if (GetManualContourModel()->GetSizeLstPoints()>1){
502                         id = GetManualContourModel()->InsertPoint(xx,yy,zz);
503                         GetManualViewBaseContour()->InsertPoint(id);
504 //                      GetManualViewBaseContour()->UpdateViewPoint(id);
505                 } else {
506                         GetManualContourModel()->AddPoint(xx,yy,zz);
507                         GetManualViewBaseContour()->AddPoint();
508 //                      AddPoint(x,y,z);
509 //                      GetManualViewBaseContour()->UpdateViewPoint(id);
510                 }
511         }
512 }
513
514 // ----------------------------------------------------------------------------
515 void manualContourBaseControler::SetPoint( int id ,int x , int y , int z){ // virtual
516         if ((GetManualViewBaseContour()!=NULL) && (id>=0)){
517                 double xx = x;
518                 double yy = y;
519                 double zz = z;
520
521                 GetManualViewBaseContour()->TransfromCoordViewWorld(xx,yy,zz);
522                 manualPoint             *mp             = _manContModel->GetManualPoint(id);
523                 mp->SetPoint(xx,yy,zz);
524                 GetManualViewBaseContour()->UpdateViewPoint(id);
525         }
526 }
527 // ----------------------------------------------------------------------------
528 void manualContourBaseControler::SetPointX( int id ,int x  ){
529         if ((GetManualViewBaseContour()!=NULL) && (id>=0)){
530                 double xx = x;
531                 double yy = 0;
532                 double zz = 0;
533                 GetManualViewBaseContour()->TransfromCoordViewWorld(xx,yy,zz);
534                 manualPoint             *mp             = _manContModel->GetManualPoint(id);
535                 mp->SetPointX(xx);
536                 GetManualViewBaseContour()->UpdateViewPoint(id);
537         }
538 }
539 // ----------------------------------------------------------------------------
540 void manualContourBaseControler::SetPointY( int id ,int y  ){
541         if ((GetManualViewBaseContour()!=NULL) && (id>=0)){
542                 double xx = 0;
543                 double yy = y;
544                 double zz = 0;
545                 GetManualViewBaseContour()->TransfromCoordViewWorld(xx,yy,zz);
546                 manualPoint             *mp             = _manContModel->GetManualPoint(id);
547                 mp->SetPointY(yy);
548                 GetManualViewBaseContour()->UpdateViewPoint(id);
549         }
550 }
551 // ----------------------------------------------------------------------------
552 void manualContourBaseControler::SetPointZ( int id ,int z  ){
553         if ((GetManualViewBaseContour()!=NULL) && (id>=0)){
554                 double xx = 0;
555                 double yy = 0;
556                 double zz = z;
557                 GetManualViewBaseContour()->TransfromCoordViewWorld(xx,yy,zz);
558                 manualPoint             *mp             = _manContModel->GetManualPoint(id);
559                 mp->SetPointZ(zz);
560                 GetManualViewBaseContour()->UpdateViewPoint(id);
561         }
562 }
563 // ----------------------------------------------------------------------------
564 void manualContourBaseControler::ResetContour() // virtual
565 {
566         this->DeleteContour();
567         GetManualViewBaseContour()->CreateNewContour();
568         this->SetState(0);
569 }
570