Salome HOME
Merge branch 'V7_dev'
[modules/gui.git] / src / SVTK / SVTK_InteractorStyle.h
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 #include <QtxRubberBand.h>
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 class vtkTDxInteractorStyle;
130
131 class SALOME_Actor;
132
133 class SVTK_Selector;
134 class SVTK_GenericRenderWindowInteractor;
135 class SVTK_Actor;
136
137 class QRubberBand;
138
139 #define VTK_INTERACTOR_STYLE_CAMERA_NONE    0
140 #define VTK_INTERACTOR_STYLE_CAMERA_ROTATE  1
141 #define VTK_INTERACTOR_STYLE_CAMERA_PAN     2
142 #define VTK_INTERACTOR_STYLE_CAMERA_ZOOM    3
143 #define VTK_INTERACTOR_STYLE_CAMERA_SPIN    4
144 #define VTK_INTERACTOR_STYLE_CAMERA_FIT        5
145 #define VTK_INTERACTOR_STYLE_CAMERA_SELECT     6
146 #define VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN 7
147 #define VTK_INTERACTOR_STYLE_CAMERA_SELECT_ROTATION_POINT 8
148
149 enum PolygonState { Disable, Start, InProcess, Finished, Closed, NotValid };
150
151 //! Introduce SALOME way of user interaction
152 /*!
153   This class defines SALOME way of user interaction for VTK viewer, as well, 
154   as introduce a new selection mechanism
155 */
156 class SVTK_EXPORT SVTK_InteractorStyle: public vtkInteractorStyle
157 {
158  public:
159   static SVTK_InteractorStyle *New();
160   vtkTypeMacro(SVTK_InteractorStyle, vtkInteractorStyle);
161
162   typedef boost::shared_ptr<SVTK_SelectionEvent> PSelectionEvent;
163
164   void SetTDxStyle(vtkTDxInteractorStyle*){} 
165
166   //! Generate special #SVTK_SelectionEvent
167   virtual SVTK_SelectionEvent* GetSelectionEvent();
168
169   //! Generate special #SVTK_SelectionEvent with flipped Y coordinate
170   virtual SVTK_SelectionEvent* GetSelectionEventFlipY();
171
172   //! Redefined in order to add an observer (callback) for custom event (space mouse event)
173   virtual void SetInteractor( vtkRenderWindowInteractor* );
174
175   //! To invoke #vtkRenderWindowInteractor::CreateTimer
176   virtual void Render();
177
178   //! To implement cached rendering
179   virtual void OnTimer();
180
181   //! To reset reset view
182   virtual void OnConfigure();
183
184   //! To handle mouse move event
185   virtual void OnMouseMove();
186
187   //! To handle left mouse button down event (reimplemented from #vtkInteractorStyle)
188   virtual void OnLeftButtonDown();
189
190   //! To handle left mouse button up event (reimplemented from #vtkInteractorStyle)
191   virtual void OnLeftButtonUp();
192
193   //! To handle middle mouse button down event (reimplemented from #vtkInteractorStyle)
194   virtual void OnMiddleButtonDown();
195
196   //! To handle middle mouse button up event (reimplemented from #vtkInteractorStyle)
197   virtual void OnMiddleButtonUp();
198
199   //! To handle right mouse button down event (reimplemented from #vtkInteractorStyle)
200   virtual void OnRightButtonDown();
201
202   //! To handle right mouse button up event (reimplemented from #vtkInteractorStyle)
203   virtual void OnRightButtonUp();
204
205   //! To handle mouse wheel forward event (reimplemented from #vtkInteractorStyle)
206   virtual void OnMouseWheelForward();
207
208   //! To handle mouse wheel backward event (reimplemented from #vtkInteractorStyle)
209   virtual void OnMouseWheelBackward();
210
211   //! To handle mouse button double click event
212   virtual void OnMouseButtonDoubleClick();
213
214   //! To handle keyboard event (reimplemented from #vtkInteractorStyle)
215   virtual void OnChar();
216
217   //! To set current increment controller 
218   void SetControllerIncrement(SVTK_ControllerIncrement*);
219
220   //! To modify current increment controller 
221   void SetIncrementSpeed(const int, const int = 0);
222
223   //! To get current increment controller 
224   SVTK_ControllerIncrement* ControllerIncrement();
225  
226   //! Redefine vtkInteractorStyle::OnKeyDown
227   virtual void OnKeyDown();
228
229   //! Provide instructions for Picking
230   void ActionPicking();
231
232   //! To set current OnKeyDown controller 
233   void SetControllerOnKeyDown(SVTK_ControllerOnKeyDown*);
234
235   //! To get current OnKeyDown controller 
236   SVTK_ControllerOnKeyDown* ControllerOnKeyDown();
237   
238   SVTK_Selector* GetSelector();
239
240   int          CurrentState() const { return State; }
241   PolygonState GetPolygonState() const { return myPoligonState; }
242   void         SetPolygonState( const PolygonState& thePolygonState )
243                               { myPoligonState = thePolygonState; }
244
245   void SetAdvancedZoomingEnabled( const bool theState ) { myIsAdvancedZoomingEnabled = theState; }
246   bool IsAdvancedZoomingEnabled() const { return myIsAdvancedZoomingEnabled; }
247
248   //! releaze myLastHighlitedActor and myLastPreHighlitedActor
249   void FreeActors();
250
251   protected:
252   SVTK_InteractorStyle();
253   ~SVTK_InteractorStyle();
254
255   QWidget*
256   GetRenderWidget();
257   
258   // Generic event bindings must be overridden in subclasses
259   virtual void OnMouseMove  (int ctrl, int shift, int x, int y);
260   virtual void OnLeftButtonDown(int ctrl, int shift, int x, int y);
261   virtual void OnLeftButtonUp  (int ctrl, int shift, int x, int y);
262   virtual void OnMiddleButtonDown(int ctrl, int shift, int x, int y);
263   virtual void OnMiddleButtonUp  (int ctrl, int shift, int x, int y);
264   virtual void OnRightButtonDown(int ctrl, int shift, int x, int y);
265   virtual void OnRightButtonUp  (int ctrl, int shift, int x, int y);
266
267   void RotateXY(int dx, int dy);
268   void PanXY(int x, int y, int oldX, int oldY);
269   void DollyXY(int dx, int dy);
270   void SpinXY(int dx, int dy, int oldX, int oldY);
271   void fitRect(const int left, const int top, const int right, const int bottom);
272   void Place(const int theX, const int theY);
273   void TranslateView(int toX, int toY, int fromX, int fromY);
274
275   void
276   IncrementalPan( const int incrX, const int incrY );
277
278   void
279   IncrementalZoom( const int incr );
280
281   void
282   IncrementalRotate( const int incrX, const int incrY );
283
284   // Main process event method (reimplemented from #vtkInteractorStyle)
285   static 
286   void
287   ProcessEvents(vtkObject* object, 
288                 unsigned long event,
289                 void* clientData, 
290                 void* callData );
291
292   float MotionFactor;
293   float RadianToDegree;                 // constant: for conv from deg to rad
294   double myScale;
295
296  protected:
297   void startZoom();
298   void startPan();
299   void startGlobalPan();
300   void startRotate();
301   void startFitArea();
302   void startSpin();
303
304   void startPointSelection();
305   void startFocalPointSelection();
306
307  protected:
308   void loadCursors();
309   void startOperation(int operation);
310   void onStartOperation();
311   virtual void onFinishOperation();
312   void onOperation(QPoint mousePos);
313   void onCursorMove(QPoint mousePos);
314   void setCursor(const int operation);
315
316   void onSpaceMouseMove( double* data );
317   virtual void onSpaceMouseButton( int button );
318
319   void DominantCombinedSwitch();
320   
321   void drawRect();
322   void endDrawRect();
323   void drawPolygon();
324   void endDrawPolygon();
325
326  protected:
327   QCursor                   myDefCursor;
328   QCursor                   myPanCursor;
329   QCursor                   myZoomCursor;
330   QCursor                   myRotateCursor;
331   QCursor                   mySpinCursor;
332   QCursor                   myHandCursor;
333   QCursor                   myGlobalPanCursor;
334   QPoint                    myPoint;
335   QPoint                    myOtherPoint;
336   bool                      myCursorState;
337   bool                      myShiftState;
338   int                       ForcedState;
339
340   vtkSmartPointer<SALOME_Actor> myLastHighlitedActor;
341   vtkSmartPointer<SALOME_Actor> myLastPreHighlitedActor;
342
343   //! "Increment" controller for pan/rotate/zoom operations
344   vtkSmartPointer<SVTK_ControllerIncrement> myControllerIncrement;
345
346   //!controls the behaviour of KeyDown event in SALOME way
347   vtkSmartPointer<SVTK_ControllerOnKeyDown> myControllerOnKeyDown;
348
349   // SpaceMouse short cuts
350   int                       mySMDecreaseSpeedBtn;
351   int                       mySMIncreaseSpeedBtn;
352   int                       mySMDominantCombinedSwitchBtn;
353   
354   vtkSmartPointer<SVTK_GenericRenderWindowInteractor> myInteractor;
355
356   PSelectionEvent mySelectionEvent;
357
358   unsigned long                   myCurrRotationPointType;
359   unsigned long                   myPrevRotationPointType;
360
361   unsigned long                   myCurrFocalPointType;
362   unsigned long                   myPrevFocalPointType;
363
364   double                          myRotationPointX;
365   double                          myRotationPointY;
366   double                          myRotationPointZ;
367
368   vtkSmartPointer<SVTK_Actor>     myHighlightSelectionPointActor;
369   vtkSmartPointer<vtkPointPicker> myPointPicker;
370   
371   double                          myBBCenter[3];
372   bool                            myBBFirstCheck;
373
374   QRubberBand*                    myRectBand; //!< selection rectangle rubber band
375   QtxPolyRubberBand*              myPolygonBand; //!< polygonal selection
376   QVector<QPoint>                 myPolygonPoints;
377   PolygonState                    myPoligonState;
378
379   bool                            myIsAdvancedZoomingEnabled;
380 };
381
382 #ifdef WIN32
383 #pragma warning ( default:4251 )
384 #endif
385
386 #endif