Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / SVTK / SVTK_RenderWindowInteractor.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   : 
25 //  Author : 
26
27 #ifndef SVTK_RenderWindowInteractor_h
28 #define SVTK_RenderWindowInteractor_h
29
30 #include "SVTK.h"
31 #include "SVTK_Selection.h"
32
33 #include <vtkSmartPointer.h>
34 #include <QWidget>
35
36 // undefining min and max because CASCADE's defines them and
37 // it clashes with std::min(), std::max()
38 #undef min
39 #undef max
40
41 #include <stack>
42
43 class vtkGenericRenderWindowInteractor;
44 class vtkInteractorStyle;
45 class vtkCallbackCommand;
46 class vtkRenderWindow;
47 class vtkRenderer;
48 class vtkObject;
49
50 class SVTK_Selector;
51 class SVTK_Renderer;
52
53 #ifdef WIN32
54 #pragma warning ( disable:4251 )
55 #endif
56
57 /*!
58   \class QVTK_RenderWindowInteractor
59   Implements Qt based vtkRenderWindowInteractor.
60   The class inherits #QWidget class in order to be possible process Qt events.
61   It invokes corresponding VTK events through usage of its device - a #vtkGenericRenderWindowInteractor.
62   Also, it creates, initialize and holds vtkRenderWindow instance.
63 */
64 class SVTK_EXPORT QVTK_RenderWindowInteractor: public QWidget
65 {
66   Q_OBJECT;
67
68  public:
69   QVTK_RenderWindowInteractor(QWidget* theParent, 
70                               const char* theName);
71
72   ~QVTK_RenderWindowInteractor();
73
74   //! To initialize by #vtkGenericRenderWindowInteractor instance
75   virtual
76   void
77   Initialize(vtkGenericRenderWindowInteractor* theDevice);
78
79   vtkGenericRenderWindowInteractor* 
80   GetDevice();
81
82   vtkRenderWindow*
83   getRenderWindow();
84
85   //! Just to simplify usage of its device (#vtkGenericRenderWindowInteractor)
86   virtual
87   void
88   InvokeEvent(unsigned long theEvent, void* theCallData);
89
90   //! Get paint engine for the scene
91   virtual QPaintEngine* paintEngine() const;
92
93  public slots:
94    //! Need for initial contents display on Win32
95   virtual void show();
96
97   //! To implement final initialization, just before the widget is displayed
98   virtual void polish();
99
100   //! To adjust widget and vtkRenderWindow size
101   virtual void resize(int w, int h);
102
103  protected:
104   virtual void paintEvent( QPaintEvent* );
105   virtual void resizeEvent( QResizeEvent* );
106
107   virtual void mouseMoveEvent( QMouseEvent* );
108   virtual void mousePressEvent( QMouseEvent* );
109   virtual void mouseReleaseEvent( QMouseEvent* );
110   virtual void mouseDoubleClickEvent( QMouseEvent* );
111   virtual void wheelEvent( QWheelEvent* );
112   virtual void keyPressEvent( QKeyEvent* );
113   virtual void keyReleaseEvent( QKeyEvent* );
114   virtual void enterEvent( QEvent * );
115   virtual void leaveEvent( QEvent * );
116
117   virtual void contextMenuEvent( QContextMenuEvent * e );
118
119   // reimplemented from QWidget in order to set window - receiver
120   // of space mouse events. 
121   virtual void focusInEvent( QFocusEvent* );
122   virtual void focusOutEvent( QFocusEvent* );
123
124   //! To handle native events (from such devices as SpaceMouse)
125 #ifdef WIN32
126   virtual bool winEvent( MSG*, long* );
127 #else
128   virtual bool x11Event( XEvent *e );
129 #endif
130
131   vtkSmartPointer<vtkRenderWindow> myRenderWindow;
132   vtkSmartPointer<vtkGenericRenderWindowInteractor> myDevice;
133 };
134
135
136 //! Extends QVTK_RenderWindowInteractor functionality.
137 /*!
138   \class SVTK_RenderWindowInteractor
139   Implements such features as 
140   support of selection, 
141   run-time interactor style management,
142   one render window per one renderer collaboration and
143   SUIT_ViewWindow events invocation.
144 */
145 class SVTK_EXPORT SVTK_RenderWindowInteractor: public QVTK_RenderWindowInteractor
146 {
147   Q_OBJECT;
148
149  public:
150   SVTK_RenderWindowInteractor(QWidget* theParent, 
151                               const char* theName);
152
153   ~SVTK_RenderWindowInteractor();
154
155   //! To initialize properly the class
156   virtual
157   void
158   Initialize(vtkGenericRenderWindowInteractor* theDevice,
159              SVTK_Renderer* theRenderer,
160              SVTK_Selector* theSelector);
161
162   //----------------------------------------------------------------------------
163   //! To get corresponding SVTK_Renderer instance
164   SVTK_Renderer* 
165   GetRenderer();
166
167   //! To get corresponding SVTK_Renderer device (just to simplify collobaration with SVTK_Renderer)
168   vtkRenderer* 
169   getRenderer();
170
171   //----------------------------------------------------------------------------
172   //! To get current interactor style
173   vtkInteractorStyle* 
174   GetInteractorStyle();
175
176   //! To change current interactor style by pushing the new one into the container
177   void
178   PushInteractorStyle(vtkInteractorStyle* theStyle);
179
180   //! To restore previous interactor style
181   void
182   PopInteractorStyle();
183
184   //----------------------------------------------------------------------------
185   //! To get corresponding SVTK_Selector
186   SVTK_Selector* 
187   GetSelector();
188
189   //! To get current selection mode (just to simplify collobaration with SVTK_Selector)
190   Selection_Mode 
191   SelectionMode() const;
192
193   //! To change selection mode (just to simplify collobaration with SVTK_Selector)
194   void 
195   SetSelectionMode(Selection_Mode theMode);
196
197  public:
198   //! To transform vtkCommand::EndPickEvent to Qt selectionChanged signal
199   void
200   onEmitSelectionChanged();
201
202  public:
203  signals:
204   void MouseMove( QMouseEvent* );
205   void MouseButtonPressed( QMouseEvent* );
206   void MouseButtonReleased( QMouseEvent* );
207   void MouseDoubleClicked( QMouseEvent* );
208   void ButtonPressed(const QMouseEvent *event);
209   void ButtonReleased(const QMouseEvent *event);
210   void WheelMoved( QWheelEvent* );
211   void KeyPressed( QKeyEvent* );
212   void KeyReleased( QKeyEvent* );
213   void contextMenuRequested( QContextMenuEvent *e );
214
215   void selectionChanged();
216
217  protected:
218   virtual void mouseMoveEvent( QMouseEvent* );
219   virtual void mousePressEvent( QMouseEvent* );
220   virtual void mouseReleaseEvent( QMouseEvent* );
221   virtual void mouseDoubleClickEvent( QMouseEvent* );
222   virtual void wheelEvent( QWheelEvent* );
223   virtual void keyPressEvent( QKeyEvent* );
224   virtual void keyReleaseEvent( QKeyEvent* );
225
226   void
227   SetRenderer(SVTK_Renderer *theRenderer);
228
229   void
230   SetSelector(SVTK_Selector* theSelector);
231
232   void
233   InitInteractorStyle(vtkInteractorStyle* theStyle);
234
235   //----------------------------------------------------------------
236   // Main process VTK event method
237   static
238   void
239   ProcessEvents(vtkObject* theObject, 
240                 unsigned long theEvent,
241                 void* theClientData, 
242                 void* theCallData);
243
244   // Used to process VTK events
245   vtkSmartPointer<vtkCallbackCommand> myEventCallbackCommand;
246
247   // Priority at which events are processed
248   float myPriority;
249
250   //----------------------------------------------------------------
251   vtkSmartPointer<SVTK_Selector> mySelector;
252
253   vtkSmartPointer<SVTK_Renderer> myRenderer;
254
255   typedef vtkSmartPointer<vtkInteractorStyle> PInteractorStyle;
256   typedef std::stack<PInteractorStyle> TInteractorStyles;
257   TInteractorStyles myInteractorStyles;
258 };
259
260 #ifdef WIN32
261 #pragma warning ( default:4251 )
262 #endif
263
264 #endif