Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / SVTK / SVTK_InteractorStyle.h
1 // Copyright (C) 2007-2012  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
23 //  SALOME VTKViewer : build VTK viewer into Salome desktop
24 //  File   : SVTK_InteractorStyle.h
25 //  Author : Christophe ATTANASIO
26
27 #ifndef __SVTK_InteractorStyle_h
28 #define __SVTK_InteractorStyle_h
29
30 #include "SVTK.h"
31
32 #include "SVTK_SelectionEvent.h"
33
34 #include <boost/shared_ptr.hpp>
35
36 #include <vtkInteractorStyle.h>
37 #include <vtkSmartPointer.h>
38
39 #include <QCursor>
40
41 #include <map>
42
43 #include <vtkObject.h>
44
45 #ifdef WIN32
46 #pragma warning ( disable:4251 )
47 #endif
48
49 //
50 //! Control the value of increment  in arithmetic progression mode.
51 /*!
52   This class controls of value of increment,
53   for pan/rotate/zoom operations in arithmetic progression mode
54 */
55 class SVTK_EXPORT SVTK_ControllerIncrement : public vtkObject{
56  public:
57   vtkTypeMacro(SVTK_ControllerIncrement, vtkObject);
58   static SVTK_ControllerIncrement* New();
59
60   //! Set start value of increment
61   void SetStartValue(const int );
62
63   //! Get current value of increment
64   int Current()const;
65
66   //! Increace the increment value by add 1
67   virtual int Increase();
68
69   //! Decreace the increment value by subtract 1
70   virtual int Decrease();
71  protected:
72   SVTK_ControllerIncrement();
73   virtual ~SVTK_ControllerIncrement();
74  protected:
75   int  myIncrement;
76  private:
77   SVTK_ControllerIncrement(const SVTK_ControllerIncrement&);//Not implemented
78   void operator=(const SVTK_ControllerIncrement&);          //Not implemented
79 };
80
81 //
82 //! Control the value of increment  in geometric progression mode.
83 /*!
84   This class controls of value of increment,
85   for pan/rotate/zoom operations in geometric progression mode.
86 */
87 class SVTK_EXPORT SVTK_GeomControllerIncrement : public SVTK_ControllerIncrement{
88  public:
89   vtkTypeMacro(SVTK_GeomControllerIncrement, SVTK_ControllerIncrement);
90   static SVTK_GeomControllerIncrement* New();
91
92   //! Increace the increment value by add 1
93   virtual int Increase();
94
95   //! Decreace the increment value by subtract 1
96   virtual int Decrease();
97  protected:
98   SVTK_GeomControllerIncrement();
99   virtual ~SVTK_GeomControllerIncrement();
100  private:
101   SVTK_GeomControllerIncrement(const SVTK_GeomControllerIncrement&);//Not implemented
102   void operator=(const SVTK_GeomControllerIncrement&);              //Not implemented
103 };
104 //
105 //! Control the behaviour of KeyDown event in SALOME way.
106 /*!
107   This class controls the behaviour of KeyDown event
108   in SALOME way
109 */
110 class SVTK_EXPORT SVTK_ControllerOnKeyDown : public vtkObject{
111  public:
112   vtkTypeMacro(SVTK_ControllerOnKeyDown, vtkObject);
113   static SVTK_ControllerOnKeyDown* New();
114
115   //! Provides the action on event 
116   virtual bool OnKeyDown(vtkInteractorStyle* );
117
118  protected:
119   SVTK_ControllerOnKeyDown();
120   virtual ~SVTK_ControllerOnKeyDown();
121   
122  private:
123   SVTK_ControllerOnKeyDown(const SVTK_ControllerOnKeyDown&);//Not implemented
124   void operator=(const SVTK_ControllerOnKeyDown&);          //Not implemented
125 };
126
127 class vtkPointPicker;
128 class vtkTDxInteractorStyle;
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   void SetTDxStyle(vtkTDxInteractorStyle*){} 
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 mouse wheel forward event (reimplemented from #vtkInteractorStyle)
203   virtual void OnMouseWheelForward();
204
205   //! To handle mouse wheel backward event (reimplemented from #vtkInteractorStyle)
206   virtual void OnMouseWheelBackward();
207
208   //! To handle keyboard event (reimplemented from #vtkInteractorStyle)
209   virtual void OnChar();
210
211   //! To set current increment controller 
212   void SetControllerIncrement(SVTK_ControllerIncrement*);
213
214   //! To modify current increment controller 
215   void SetIncrementSpeed(const int, const int = 0);
216
217   //! To get current increment controller 
218   SVTK_ControllerIncrement* ControllerIncrement();
219  
220   //! Redefine vtkInteractorStyle::OnKeyDown
221   virtual void OnKeyDown();
222
223   //! Provide instructions for Picking
224   void ActionPicking();
225
226   //! To set current OnKeyDown controller 
227   void SetControllerOnKeyDown(SVTK_ControllerOnKeyDown*);
228
229   //! To get current OnKeyDown controller 
230   SVTK_ControllerOnKeyDown* ControllerOnKeyDown();
231   
232   SVTK_Selector* GetSelector();
233
234   int   CurrentState() const { return State; }
235
236   void SetAdvancedZoomingEnabled( const bool theState ) { myIsAdvancedZoomingEnabled = theState; }
237   bool IsAdvancedZoomingEnabled() const { return myIsAdvancedZoomingEnabled; }
238
239   protected:
240   SVTK_InteractorStyle();
241   ~SVTK_InteractorStyle();
242
243   QWidget*
244   GetRenderWidget();
245   
246   // Generic event bindings must be overridden in subclasses
247   virtual void OnMouseMove  (int ctrl, int shift, int x, int y);
248   virtual void OnLeftButtonDown(int ctrl, int shift, int x, int y);
249   virtual void OnLeftButtonUp  (int ctrl, int shift, int x, int y);
250   virtual void OnMiddleButtonDown(int ctrl, int shift, int x, int y);
251   virtual void OnMiddleButtonUp  (int ctrl, int shift, int x, int y);
252   virtual void OnRightButtonDown(int ctrl, int shift, int x, int y);
253   virtual void OnRightButtonUp  (int ctrl, int shift, int x, int y);
254
255   void RotateXY(int dx, int dy);
256   void PanXY(int x, int y, int oldX, int oldY);
257   void DollyXY(int dx, int dy);
258   void SpinXY(int dx, int dy, int oldX, int oldY);
259   void fitRect(const int left, const int top, const int right, const int bottom);
260   void Place(const int theX, const int theY);
261   void TranslateView(int toX, int toY, int fromX, int fromY);
262
263   void
264   IncrementalPan( const int incrX, const int incrY );
265
266   void
267   IncrementalZoom( const int incr );
268
269   void
270   IncrementalRotate( const int incrX, const int incrY );
271
272   // Main process event method (reimplemented from #vtkInteractorStyle)
273   static 
274   void
275   ProcessEvents(vtkObject* object, 
276                 unsigned long event,
277                 void* clientData, 
278                 void* callData );
279
280   float MotionFactor;
281   float RadianToDegree;                 // constant: for conv from deg to rad
282   double myScale;
283
284  protected:
285   void startZoom();
286   void startPan();
287   void startGlobalPan();
288   void startRotate();
289   void startFitArea();
290   void startSpin();
291
292   void startPointSelection();
293   void startFocalPointSelection();
294
295  protected:
296   void loadCursors();
297   void startOperation(int operation);
298   void onStartOperation();
299   virtual void onFinishOperation();
300   void onOperation(QPoint mousePos);
301   void onCursorMove(QPoint mousePos);
302   void setCursor(const int operation);
303
304   void onSpaceMouseMove( double* data );
305   virtual void onSpaceMouseButton( int button );
306
307   void DominantCombinedSwitch();
308   
309   void drawRect();
310   void endDrawRect();
311
312  protected:
313   QCursor                   myDefCursor;
314   QCursor                   myPanCursor;
315   QCursor                   myZoomCursor;
316   QCursor                   myRotateCursor;
317   QCursor                   mySpinCursor;
318   QCursor                   myHandCursor;
319   QCursor                   myGlobalPanCursor;
320   QPoint                    myPoint;
321   QPoint                    myOtherPoint;
322   bool                      myCursorState;
323   bool                      myShiftState;
324   int                       ForcedState;
325
326   vtkSmartPointer<SALOME_Actor> myLastHighlitedActor;
327   vtkSmartPointer<SALOME_Actor> myLastPreHighlitedActor;
328
329   //! "Increment" controller for pan/rotate/zoom operations
330   vtkSmartPointer<SVTK_ControllerIncrement> myControllerIncrement;
331
332   //!controls the behaviour of KeyDown event in SALOME way
333   vtkSmartPointer<SVTK_ControllerOnKeyDown> myControllerOnKeyDown;
334
335   // SpaceMouse short cuts
336   int                       mySMDecreaseSpeedBtn;
337   int                       mySMIncreaseSpeedBtn;
338   int                       mySMDominantCombinedSwitchBtn;
339   
340   vtkSmartPointer<SVTK_GenericRenderWindowInteractor> myInteractor;
341
342   PSelectionEvent mySelectionEvent;
343
344   unsigned long                   myCurrRotationPointType;
345   unsigned long                   myPrevRotationPointType;
346
347   unsigned long                   myCurrFocalPointType;
348   unsigned long                   myPrevFocalPointType;
349
350   double                          myRotationPointX;
351   double                          myRotationPointY;
352   double                          myRotationPointZ;
353
354   vtkSmartPointer<SVTK_Actor>     myHighlightSelectionPointActor;
355   vtkSmartPointer<vtkPointPicker> myPointPicker;
356   
357   vtkFloatingPointType            myBBCenter[3];
358   bool                            myBBFirstCheck;
359
360   QRubberBand*                    myRectBand; //!< selection rectangle rubber band
361
362   bool                            myIsAdvancedZoomingEnabled;
363 };
364
365 #ifdef WIN32
366 #pragma warning ( default:4251 )
367 #endif
368
369 #endif