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