Salome HOME
9ada28830a794c13ccca3a1bc3a2804d4f8148ac
[modules/visu.git] / src / VVTK / VVTK_InteractorStyle.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME VTKViewer : build VTK viewer into Salome desktop
23 //  File   : VVTK_InteractorStyle.cxx
24 //  Author : Christophe ATTANASIO
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "VVTK_InteractorStyle.h"
29 #include "VISU_GaussPtsSettings.h"
30 #include "SVTK_Selector.h"
31 #include "VISU_Event.h" 
32
33 #include <vtkObjectFactory.h>
34 #include <vtkRenderWindowInteractor.h>
35 #include <vtkCallbackCommand.h>
36 #include <vtkCommand.h>
37 #include <vtkRenderer.h>
38 #include <vtkCamera.h>
39
40
41 //----------------------------------------------------------------------------
42 vtkStandardNewMacro(VVTK_InteractorStyle);
43 //----------------------------------------------------------------------------
44
45 VVTK_InteractorStyle
46 ::VVTK_InteractorStyle():
47   myIsMidButtonDown( false ),
48   myIsLeftButtonDown( false ),
49   mySMDecreaseMagnificationBtn(10),
50   mySMIncreaseMagnificationBtn(11)
51 {
52 }
53
54 //----------------------------------------------------------------------------
55 VVTK_InteractorStyle
56 ::~VVTK_InteractorStyle() 
57 {
58 }
59
60 //----------------------------------------------------------------------------
61 void
62 VVTK_InteractorStyle
63 ::OnLeftButtonDown(int ctrl, int shift, 
64                    int x, int y) 
65 {
66   myIsLeftButtonDown = true;
67
68   if (this->HasObserver(vtkCommand::LeftButtonPressEvent)) {
69     this->InvokeEvent(vtkCommand::LeftButtonPressEvent,NULL);
70     return;
71   }
72   this->FindPokedRenderer(x, y);
73   if (this->CurrentRenderer == NULL) {
74     return;
75   }
76   myShiftState = shift;
77   // finishing current viewer operation
78   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
79     onFinishOperation();
80     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
81   }
82   myOtherPoint = myPoint = QPoint(x, y);
83   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
84     startOperation(ForcedState);
85   } 
86   else {
87     if (!(ctrl||shift)){
88       if (myIsMidButtonDown){
89         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
90       }
91       else{
92         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);
93       }
94     }
95   }
96   return;
97 }
98
99 //----------------------------------------------------------------------------
100 void
101 VVTK_InteractorStyle
102 ::OnMiddleButtonDown(int ctrl,
103                      int shift, 
104                      int x, int y) 
105 {
106   myIsMidButtonDown = true;
107
108   if (this->HasObserver(vtkCommand::MiddleButtonPressEvent))  {
109     this->InvokeEvent(vtkCommand::MiddleButtonPressEvent,NULL);
110     return;
111   }
112   this->FindPokedRenderer(x, y);
113   if (this->CurrentRenderer == NULL)    {
114     return;
115   }
116   myShiftState = shift;
117   // finishing current viewer operation
118   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
119     onFinishOperation();
120     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
121   }
122   myOtherPoint = myPoint = QPoint(x, y);
123   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
124     startOperation(ForcedState);
125   }
126   else {
127     if (!(ctrl||shift)){
128       if ( myIsLeftButtonDown ){
129         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
130       }
131       else{
132         startOperation(VTK_INTERACTOR_STYLE_CAMERA_PAN);
133       }
134     }
135   }
136 }
137
138 //----------------------------------------------------------------------------
139 void 
140 VVTK_InteractorStyle
141 ::OnLeftButtonUp(int ctrl, int shift, int x, int y)
142 {
143   myIsLeftButtonDown = false;
144   SVTK_InteractorStyle::OnLeftButtonUp( ctrl, shift, x, y );
145
146   if ( myIsMidButtonDown )
147     OnMiddleButtonDown( ctrl, shift, x, y );
148 }
149
150 //----------------------------------------------------------------------------
151 void  
152 VVTK_InteractorStyle
153 ::OnMiddleButtonUp(int ctrl, int shift, int x, int y)
154 {
155   myIsMidButtonDown = false;
156   SVTK_InteractorStyle::OnMiddleButtonUp( ctrl, shift, x, y );
157
158   if ( myIsLeftButtonDown )
159     OnLeftButtonDown( ctrl, shift, x, y );
160 }
161 //----------------------------------------------------------------------------
162 void  VVTK_InteractorStyle::onFinishOperation()
163 {
164   Superclass::onFinishOperation();
165 }
166
167
168 //----------------------------------------------------------------------------
169 void
170 VVTK_InteractorStyle
171 ::SetInteractor( vtkRenderWindowInteractor* theInteractor )
172 {
173   // register EventCallbackCommand as observer of standard events (keypress, mousemove, etc)
174   Superclass::SetInteractor( theInteractor );
175  
176   if(theInteractor){ 
177     theInteractor->AddObserver( VISU::SetSMDecreaseMagnificationEvent, EventCallbackCommand, Priority );
178     theInteractor->AddObserver( VISU::SetSMIncreaseMagnificationEvent, EventCallbackCommand, Priority );
179   }
180 }
181
182
183 //----------------------------------------------------------------------------
184 void
185 VVTK_InteractorStyle
186 ::ProcessEvents( vtkObject* object,
187                  unsigned long event,
188                  void* clientData, 
189                  void* callData )
190 {
191   if ( clientData ) {
192     vtkObject* anObject = reinterpret_cast<vtkObject*>( clientData );
193     VVTK_InteractorStyle* self = dynamic_cast<VVTK_InteractorStyle*>( anObject );
194     if( self ){
195       switch ( event ) {
196       case VISU::SetSMDecreaseMagnificationEvent:
197         self->mySMDecreaseMagnificationBtn = *((int*)callData);
198         return;
199       case VISU::SetSMIncreaseMagnificationEvent:
200         self->mySMIncreaseMagnificationBtn = *((int*)callData);
201         return;
202       }
203     }
204   }
205
206   Superclass::ProcessEvents( object, event, clientData, callData );
207 }
208
209
210 //----------------------------------------------------------------------------
211 void
212 VVTK_InteractorStyle
213 ::onSpaceMouseButton( int button )
214 {
215   if( mySMDecreaseMagnificationBtn == button )    
216     DecreaseGaussPointMagnification();
217   if( mySMIncreaseMagnificationBtn == button )    
218     IncreaseGaussPointMagnification();
219
220   Superclass::onSpaceMouseButton( button );
221 }
222
223 //----------------------------------------------------------------------------
224 void
225 VVTK_InteractorStyle
226 ::DecreaseGaussPointMagnification()
227 {
228   Interactor->InvokeEvent(VISU::SetSMDecreaseMagnificationEvent,NULL);
229 }
230
231 //----------------------------------------------------------------------------
232 void
233 VVTK_InteractorStyle
234 ::IncreaseGaussPointMagnification()
235 {
236   Interactor->InvokeEvent(VISU::SetSMIncreaseMagnificationEvent,NULL);
237 }
238 //============================================================================
239 //
240 //  Controllers
241 //
242 vtkStandardNewMacro(VVTK_ControllerIncrement);
243 //----------------------------------------------------------------------------
244 VVTK_ControllerIncrement::VVTK_ControllerIncrement()
245 {
246   myIncrement=10;
247 }
248 //----------------------------------------------------------------------------
249 VVTK_ControllerIncrement::~VVTK_ControllerIncrement()
250 {
251 }
252 //----------------------------------------------------------------------------
253 int VVTK_ControllerIncrement::Increase()
254 {
255   myIncrement*=2;
256   return myIncrement;
257 }
258 //----------------------------------------------------------------------------
259 int VVTK_ControllerIncrement::Decrease()
260 {
261   myIncrement/=2;
262   if (!myIncrement){
263     myIncrement=1;
264   }
265   return myIncrement;
266 }
267 //
268 vtkStandardNewMacro(VVTK_ControllerOnKeyDown);
269 //----------------------------------------------------------------------------
270 VVTK_ControllerOnKeyDown::VVTK_ControllerOnKeyDown()
271 {
272 }
273 //----------------------------------------------------------------------------
274 VVTK_ControllerOnKeyDown::~VVTK_ControllerOnKeyDown()
275 {
276 }
277 //----------------------------------------------------------------------------
278 bool VVTK_ControllerOnKeyDown::OnKeyDown(vtkInteractorStyle* theIS)
279 {
280   SVTK_InteractorStyle *pIS=dynamic_cast<SVTK_InteractorStyle *>(theIS);
281   if (pIS){
282     if(pIS->GetSelector()->SelectionMode()==GaussPointSelection){
283       char key = pIS->GetInteractor()->GetKeyCode();
284       //
285       if (key == 'S') {
286         pIS->ActionPicking();
287         return false;
288       }
289     }
290   }
291   return Superclass::OnKeyDown(theIS);
292 }