Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / VVTK / VVTK_InteractorStyle.cxx
1 //  SALOME VTKViewer : build VTK viewer into Salome desktop
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VVTK_InteractorStyle.cxx
25 //  Author : Christophe ATTANASIO
26 //  Module : SALOME
27 //  $Header$
28
29
30 #include "VVTK_InteractorStyle.h"
31 #include "VISU_GaussPtsSettings.h"
32 #include "SVTK_Selector.h"
33 #include "VISU_Event.h" 
34
35 #include <vtkObjectFactory.h>
36 #include <vtkRenderWindowInteractor.h>
37 #include <vtkCallbackCommand.h>
38 #include <vtkCommand.h>
39 #include <vtkRenderer.h>
40 #include <vtkCamera.h>
41
42
43 //----------------------------------------------------------------------------
44 vtkStandardNewMacro(VVTK_InteractorStyle);
45 //----------------------------------------------------------------------------
46
47 VVTK_InteractorStyle
48 ::VVTK_InteractorStyle():
49   myIsMidButtonDown( false ),
50   myIsLeftButtonDown( false ),
51   mySMDecreaseMagnificationBtn(10),
52   mySMIncreaseMagnificationBtn(11)
53 {
54 }
55
56 //----------------------------------------------------------------------------
57 VVTK_InteractorStyle
58 ::~VVTK_InteractorStyle() 
59 {
60 }
61
62 //----------------------------------------------------------------------------
63 void
64 VVTK_InteractorStyle
65 ::OnLeftButtonDown(int ctrl, int shift, 
66                    int x, int y) 
67 {
68   myIsLeftButtonDown = true;
69
70   if (this->HasObserver(vtkCommand::LeftButtonPressEvent)) {
71     this->InvokeEvent(vtkCommand::LeftButtonPressEvent,NULL);
72     return;
73   }
74   this->FindPokedRenderer(x, y);
75   if (this->CurrentRenderer == NULL) {
76     return;
77   }
78   myShiftState = shift;
79   // finishing current viewer operation
80   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
81     onFinishOperation();
82     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
83   }
84   myOtherPoint = myPoint = QPoint(x, y);
85   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
86     startOperation(ForcedState);
87   } 
88   else {
89     if (!(ctrl||shift)){
90       if (myIsMidButtonDown){
91         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
92       }
93       else{
94         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);
95       }
96     }
97   }
98   return;
99 }
100
101 //----------------------------------------------------------------------------
102 void
103 VVTK_InteractorStyle
104 ::OnMiddleButtonDown(int ctrl,
105                      int shift, 
106                      int x, int y) 
107 {
108   myIsMidButtonDown = true;
109
110   if (this->HasObserver(vtkCommand::MiddleButtonPressEvent))  {
111     this->InvokeEvent(vtkCommand::MiddleButtonPressEvent,NULL);
112     return;
113   }
114   this->FindPokedRenderer(x, y);
115   if (this->CurrentRenderer == NULL)    {
116     return;
117   }
118   myShiftState = shift;
119   // finishing current viewer operation
120   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
121     onFinishOperation();
122     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
123   }
124   myOtherPoint = myPoint = QPoint(x, y);
125   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
126     startOperation(ForcedState);
127   }
128   else {
129     if (!(ctrl||shift)){
130       if ( myIsLeftButtonDown ){
131         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
132       }
133       else{
134         startOperation(VTK_INTERACTOR_STYLE_CAMERA_PAN);
135       }
136     }
137   }
138 }
139
140 //----------------------------------------------------------------------------
141 void 
142 VVTK_InteractorStyle
143 ::OnLeftButtonUp(int ctrl, int shift, int x, int y)
144 {
145   myIsLeftButtonDown = false;
146   SVTK_InteractorStyle::OnLeftButtonUp( ctrl, shift, x, y );
147
148   if ( myIsMidButtonDown )
149     OnMiddleButtonDown( ctrl, shift, x, y );
150 }
151
152 //----------------------------------------------------------------------------
153 void  
154 VVTK_InteractorStyle
155 ::OnMiddleButtonUp(int ctrl, int shift, int x, int y)
156 {
157   myIsMidButtonDown = false;
158   SVTK_InteractorStyle::OnMiddleButtonUp( ctrl, shift, x, y );
159
160   if ( myIsLeftButtonDown )
161     OnLeftButtonDown( ctrl, shift, x, y );
162 }
163 //----------------------------------------------------------------------------
164 void  VVTK_InteractorStyle::onFinishOperation()
165 {
166   Superclass::onFinishOperation();
167 }
168
169
170 //----------------------------------------------------------------------------
171 void
172 VVTK_InteractorStyle
173 ::SetInteractor( vtkRenderWindowInteractor* theInteractor )
174 {
175   // register EventCallbackCommand as observer of standard events (keypress, mousemove, etc)
176   Superclass::SetInteractor( theInteractor );
177  
178   if(theInteractor){ 
179     theInteractor->AddObserver( VISU::SetSMDecreaseMagnificationEvent, EventCallbackCommand, Priority );
180     theInteractor->AddObserver( VISU::SetSMIncreaseMagnificationEvent, EventCallbackCommand, Priority );
181   }
182 }
183
184
185 //----------------------------------------------------------------------------
186 void
187 VVTK_InteractorStyle
188 ::ProcessEvents( vtkObject* object,
189                  unsigned long event,
190                  void* clientData, 
191                  void* callData )
192 {
193   if ( clientData ) {
194     vtkObject* anObject = reinterpret_cast<vtkObject*>( clientData );
195     VVTK_InteractorStyle* self = dynamic_cast<VVTK_InteractorStyle*>( anObject );
196     if( self ){
197       switch ( event ) {
198       case VISU::SetSMDecreaseMagnificationEvent:
199         self->mySMDecreaseMagnificationBtn = *((int*)callData);
200         return;
201       case VISU::SetSMIncreaseMagnificationEvent:
202         self->mySMIncreaseMagnificationBtn = *((int*)callData);
203         return;
204       }
205     }
206   }
207
208   Superclass::ProcessEvents( object, event, clientData, callData );
209 }
210
211
212 //----------------------------------------------------------------------------
213 void
214 VVTK_InteractorStyle
215 ::onSpaceMouseButton( int button )
216 {
217   if( mySMDecreaseMagnificationBtn == button )    
218     DecreaseGaussPointMagnification();
219   if( mySMIncreaseMagnificationBtn == button )    
220     IncreaseGaussPointMagnification();
221
222   Superclass::onSpaceMouseButton( button );
223 }
224
225 //----------------------------------------------------------------------------
226 void
227 VVTK_InteractorStyle
228 ::DecreaseGaussPointMagnification()
229 {
230   Interactor->InvokeEvent(VISU::SetSMDecreaseMagnificationEvent,NULL);
231 }
232
233 //----------------------------------------------------------------------------
234 void
235 VVTK_InteractorStyle
236 ::IncreaseGaussPointMagnification()
237 {
238   Interactor->InvokeEvent(VISU::SetSMIncreaseMagnificationEvent,NULL);
239 }
240 //============================================================================
241 //
242 //  Controllers
243 //
244 vtkStandardNewMacro(VVTK_ControllerIncrement);
245 //----------------------------------------------------------------------------
246 VVTK_ControllerIncrement::VVTK_ControllerIncrement()
247 {
248   myIncrement=10;
249 }
250 //----------------------------------------------------------------------------
251 VVTK_ControllerIncrement::~VVTK_ControllerIncrement()
252 {
253 }
254 //----------------------------------------------------------------------------
255 int VVTK_ControllerIncrement::Increase()
256 {
257   myIncrement*=2;
258   return myIncrement;
259 }
260 //----------------------------------------------------------------------------
261 int VVTK_ControllerIncrement::Decrease()
262 {
263   myIncrement/=2;
264   if (!myIncrement){
265     myIncrement=1;
266   }
267   return myIncrement;
268 }
269 //
270 vtkStandardNewMacro(VVTK_ControllerOnKeyDown);
271 //----------------------------------------------------------------------------
272 VVTK_ControllerOnKeyDown::VVTK_ControllerOnKeyDown()
273 {
274 }
275 //----------------------------------------------------------------------------
276 VVTK_ControllerOnKeyDown::~VVTK_ControllerOnKeyDown()
277 {
278 }
279 //----------------------------------------------------------------------------
280 bool VVTK_ControllerOnKeyDown::OnKeyDown(vtkInteractorStyle* theIS)
281 {
282   SVTK_InteractorStyle *pIS=dynamic_cast<SVTK_InteractorStyle *>(theIS);
283   if (pIS){
284     if(pIS->GetSelector()->SelectionMode()==GaussPointSelection){
285       char key = pIS->GetInteractor()->GetKeyCode();
286       //
287       if (key == 'S') {
288         pIS->ActionPicking();
289         return false;
290       }
291     }
292   }
293   return Superclass::OnKeyDown(theIS);
294 }