Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / SVTK / SVTK_InteractorStyle.h
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : SVTK_InteractorStyle.h
25 //  Author : Christophe ATTANASIO
26 //  Module : SALOME
27 //  $Header$
28
29 #ifndef __SVTK_InteractorStyle_h
30 #define __SVTK_InteractorStyle_h
31
32 #include "SVTK.h"
33
34 #include "SVTK_SelectionEvent.h"
35
36 #include <boost/shared_ptr.hpp>
37
38 #include <vtkInteractorStyle.h>
39 #include <vtkSmartPointer.h>
40
41 #include <qcursor.h>
42 #include <qevent.h>
43
44 #include <map>
45
46 #include <vtkObject.h>
47 //
48 //! Control the value of increment  in SALOME way.
49 /*!
50   This class controls of value of increment,
51   for pan/rotate/zoom operations in SALOME way
52 */
53 class SVTK_ControllerIncrement : public vtkObject{
54  public:
55   vtkTypeMacro(SVTK_ControllerIncrement, vtkObject);
56   static SVTK_ControllerIncrement* New();
57
58   //! Set start value of increment
59   void SetStartValue(const int );
60
61   //! Get current value of increment
62   int Current()const;
63
64   //! Increace the increment value by add 1
65   virtual int Increase();
66
67   //! Decreace the increment value by subtract 1
68   virtual int Decrease();
69  protected:
70   SVTK_ControllerIncrement();
71   virtual ~SVTK_ControllerIncrement();
72  protected:
73   int  myIncrement;
74  private:
75   SVTK_ControllerIncrement(const SVTK_ControllerIncrement&);//Not implemented
76   void operator=(const SVTK_ControllerIncrement&);          //Not implemented
77 };
78 //
79 //! Control the behaviour of KeyDown event in SALOME way.
80 /*!
81   This class controls the behaviour of KeyDown event
82   in SALOME way
83 */
84 class SVTK_ControllerOnKeyDown : public vtkObject{
85  public:
86   vtkTypeMacro(SVTK_ControllerOnKeyDown, vtkObject);
87   static SVTK_ControllerOnKeyDown* New();
88
89   //! Provides the action on event 
90   virtual bool OnKeyDown(vtkInteractorStyle* );
91
92  protected:
93   SVTK_ControllerOnKeyDown();
94   virtual ~SVTK_ControllerOnKeyDown();
95   
96  private:
97   SVTK_ControllerOnKeyDown(const SVTK_ControllerOnKeyDown&);//Not implemented
98   void operator=(const SVTK_ControllerOnKeyDown&);          //Not implemented
99 };
100
101 class vtkCell;
102 class vtkPicker;
103
104 class SALOME_Actor;
105
106 class SVTK_Selector;
107 class SVTK_GenericRenderWindowInteractor;
108
109 #define VTK_INTERACTOR_STYLE_CAMERA_NONE    0
110 #define VTK_INTERACTOR_STYLE_CAMERA_ROTATE  1
111 #define VTK_INTERACTOR_STYLE_CAMERA_PAN     2
112 #define VTK_INTERACTOR_STYLE_CAMERA_ZOOM    3
113 #define VTK_INTERACTOR_STYLE_CAMERA_SPIN    4
114 #define VTK_INTERACTOR_STYLE_CAMERA_FIT        5
115 #define VTK_INTERACTOR_STYLE_CAMERA_SELECT     6
116 #define VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN 7
117
118 //! Introduce SALOME way of user interaction
119 /*!
120   This class defines SALOME way of user interaction for VTK viewer, as well, 
121   as introduce a new selection mechanism
122 */
123 class SVTK_EXPORT SVTK_InteractorStyle: public vtkInteractorStyle
124 {
125  public:
126   static SVTK_InteractorStyle *New();
127   vtkTypeMacro(SVTK_InteractorStyle, vtkInteractorStyle);
128
129   typedef boost::shared_ptr<SVTK_SelectionEvent> PSelectionEvent;
130
131   
132
133   //! Generate special #SVTK_SelectionEvent
134   virtual
135   SVTK_SelectionEvent*
136   GetSelectionEvent();
137
138   //! Generate special #SVTK_SelectionEvent with flipped Y coordinate
139   virtual
140   SVTK_SelectionEvent*
141   GetSelectionEventFlipY();
142
143   //! Redefined in order to add an observer (callback) for custom event (space mouse event)
144   virtual
145   void
146   SetInteractor( vtkRenderWindowInteractor* );
147
148   //! To invoke #vtkRenderWindowInteractor::CreateTimer
149   virtual 
150   void
151   Render();
152
153   //! To implement cached rendering
154   virtual
155   void
156   OnTimer();
157
158   //! To reset reset view
159   virtual
160   void
161   OnConfigure();
162
163   //! To handle mouse move event
164   virtual 
165   void
166   OnMouseMove();
167
168   //! To handle left mouse button down event (reimplemented from #vtkInteractorStyle)
169   virtual
170   void
171   OnLeftButtonDown();
172
173   //! To handle left mouse button up event (reimplemented from #vtkInteractorStyle)
174   virtual
175   void
176   OnLeftButtonUp();
177
178   //! To handle middle mouse button down event (reimplemented from #vtkInteractorStyle)
179   virtual
180   void
181   OnMiddleButtonDown();
182
183   //! To handle middle mouse button up event (reimplemented from #vtkInteractorStyle)
184   virtual
185   void
186   OnMiddleButtonUp();
187
188   //! To handle right mouse button down event (reimplemented from #vtkInteractorStyle)
189   virtual
190   void
191   OnRightButtonDown();
192
193   //! To handle right mouse button up event (reimplemented from #vtkInteractorStyle)
194   virtual
195   void
196   OnRightButtonUp();
197
198   //! To handle keyboard event (reimplemented from #vtkInteractorStyle)
199   virtual
200   void
201   OnChar();
202
203   //! To set current increment controller 
204   void SetControllerIncrement(SVTK_ControllerIncrement*);
205
206   //! To get current increment controller 
207   SVTK_ControllerIncrement* ControllerIncrement();
208  
209   //! Redefine vtkInteractorStyle::OnKeyDown
210   virtual void OnKeyDown();
211
212   //! Provide instructions for Picking
213   void ActionPicking();
214
215   //! To set current OnKeyDown controller 
216   void SetControllerOnKeyDown(SVTK_ControllerOnKeyDown*);
217
218   //! To get current OnKeyDown controller 
219   SVTK_ControllerOnKeyDown* ControllerOnKeyDown();
220   
221   SVTK_Selector* GetSelector();
222
223  protected:
224   SVTK_InteractorStyle();
225   ~SVTK_InteractorStyle();
226
227   QWidget*
228   GetRenderWidget();
229   
230   // Generic event bindings must be overridden in subclasses
231   virtual void OnMouseMove  (int ctrl, int shift, int x, int y);
232   virtual void OnLeftButtonDown(int ctrl, int shift, int x, int y);
233   virtual void OnLeftButtonUp  (int ctrl, int shift, int x, int y);
234   virtual void OnMiddleButtonDown(int ctrl, int shift, int x, int y);
235   virtual void OnMiddleButtonUp  (int ctrl, int shift, int x, int y);
236   virtual void OnRightButtonDown(int ctrl, int shift, int x, int y);
237   virtual void OnRightButtonUp  (int ctrl, int shift, int x, int y);
238
239   void RotateXY(int dx, int dy);
240   void PanXY(int x, int y, int oldX, int oldY);
241   void DollyXY(int dx, int dy);
242   void SpinXY(int dx, int dy, int oldX, int oldY);
243   void fitRect(const int left, const int top, const int right, const int bottom);
244   void Place(const int theX, const int theY);
245   void TranslateView(int toX, int toY, int fromX, int fromY);
246
247   void
248   IncrementalPan( const int incrX, const int incrY );
249
250   void
251   IncrementalZoom( const int incr );
252
253   void
254   IncrementalRotate( const int incrX, const int incrY );
255
256   // Main process event method (reimplemented from #vtkInteractorStyle)
257   static 
258   void
259   ProcessEvents(vtkObject* object, 
260                 unsigned long event,
261                 void* clientData, 
262                 void* callData );
263
264   float MotionFactor;
265   float RadianToDegree;                 // constant: for conv from deg to rad
266   double myScale;
267
268  protected:
269   void startZoom();
270   void startPan();
271   void startGlobalPan();
272   void startRotate();
273   void startFitArea();
274   void startSpin();
275
276  protected:
277   void loadCursors();
278   void startOperation(int operation);
279   void onStartOperation();
280   virtual void onFinishOperation();
281   void onOperation(QPoint mousePos);
282   void onCursorMove(QPoint mousePos);
283   void setCursor(const int operation);
284
285   void onSpaceMouseMove( double* data );
286   virtual void onSpaceMouseButton( int button );
287
288   void DominantCombinedSwitch();
289   
290  protected:
291   QCursor                   myDefCursor;
292   QCursor                   myPanCursor;
293   QCursor                   myZoomCursor;
294   QCursor                   myRotateCursor;
295   QCursor                   mySpinCursor;
296   QCursor                   myHandCursor;
297   QCursor                   myGlobalPanCursor;
298   QPoint                    myPoint;
299   QPoint                    myOtherPoint;
300   bool                      myCursorState;
301   bool                      myShiftState;
302   int                       ForcedState;
303
304   vtkSmartPointer<SALOME_Actor> myLastHighlitedActor;
305   vtkSmartPointer<SALOME_Actor> myLastPreHighlitedActor;
306
307   //! "Increment" controller for pan/rotate/zoom operations
308   vtkSmartPointer<SVTK_ControllerIncrement> myControllerIncrement;
309
310   //!controls the behaviour of KeyDown event in SALOME way
311   vtkSmartPointer<SVTK_ControllerOnKeyDown> myControllerOnKeyDown;
312
313   // SpaceMouse short cuts
314   int                       mySMDecreaseSpeedBtn;
315   int                       mySMIncreaseSpeedBtn;
316   int                       mySMDominantCombinedSwitchBtn;
317   
318   vtkSmartPointer<SVTK_GenericRenderWindowInteractor> myInteractor;
319
320   PSelectionEvent mySelectionEvent;
321
322   vtkSmartPointer<vtkPicker> myPicker;
323 };
324
325 #endif