Salome HOME
Fix on:
[modules/kernel.git] / src / VTKViewer / VTKViewer_RenderWindowInteractor.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VTKViewer_RenderWindowInteractor.h
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #ifndef VTKViewer_RenderWindowInteractor_h
30 #define VTKViewer_RenderWindowInteractor_h
31
32 //===========================================================
33 // now we define the C++ class
34
35 #include "SALOME_InteractiveObject.hxx"
36 #include "SALOME_Selection.h"
37
38 // QT Includes
39 #include <qobject.h>
40 #include <qtimer.h>
41
42 // VTK Includes
43 #include <vtkVersion.h>
44 #include <vtkRenderWindowInteractor.h>
45
46 // Open CASCADE Includes
47 #include <TColStd_MapOfInteger.hxx>
48 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
49
50 class vtkPicker;
51 class vtkCellPicker;
52 class vtkPointPicker;
53 class vtkActorCollection;
54
55 class SALOME_Actor;
56 class VTKViewer_Actor;
57 class VTKViewer_ViewFrame;
58 class VTKViewer_RenderWindow;
59 class VTKViewer_InteractorStyleSALOME;
60
61 // ------------------------------------------------------------
62 // :TRICKY: Fri Apr 21 22:19:27 2000 Pagey
63 // The Signal/Slot mechanism used by Qt requires that QObject 
64 // appear as the first class when using multiple inheritance. 
65 // Hence the order of the two classes QObject and vtkRenderWindowInteractor
66 // matters here. Be careful not to change it by accident. 
67 // ------------------------------------------------------------
68 class VTK_EXPORT VTKViewer_RenderWindowInteractor : 
69   public QObject, public vtkRenderWindowInteractor
70 {
71   Q_OBJECT ;   
72   friend class VTKViewer_ViewFrame;
73 public:
74
75   static VTKViewer_RenderWindowInteractor *New() ; 
76
77   vtkTypeMacro(VTKViewer_RenderWindowInteractor,vtkRenderWindowInteractor);
78
79   void PrintSelf(ostream& os, vtkIndent indent);
80
81   // Description:
82   // Initializes the event handlers without an XtAppContext.  This is
83   // good for when you don`t have a user interface, but you still
84   // want to have mouse interaction.
85   virtual void Initialize();
86
87   virtual void SetInteractorStyle(vtkInteractorObserver *);
88   VTKViewer_InteractorStyleSALOME* GetInteractorStyleSALOME(){
89     return myInteractorStyle;
90   }
91
92   // Description:
93   // This will start up the X event loop and never return. If you
94   // call this method it will loop processing X events until the
95   // application is exited.
96   virtual void Start();
97   
98   // Description:
99   // Enable/Disable interactions.  By default interactors are enabled when
100   // initialized.  Initialize() must be called prior to enabling/disabling
101   // interaction. These methods are used when a window/widget is being
102   // shared by multiple renderers and interactors.  This allows a "modal"
103   // display where one interactor is active when its data is to be displayed
104   // and all other interactors associated with the widget are disabled
105   // when their data is not displayed.
106   virtual void Enable();
107   virtual void Disable();
108
109   // Description:
110   // Event loop notification member for Window size change
111   virtual void UpdateSize(int x,int y);
112
113   // Description:
114   // Timer methods must be overridden by platform dependent subclasses.
115   // flag is passed to indicate if this is first timer set or an update
116   // as Win32 uses repeating timers, whereas X uses One shot more timer
117   // if flag==VTKXI_TIMER_FIRST Win32 and X should createtimer
118   // otherwise Win32 should exit and X should perform AddTimeOut()
119   virtual int CreateTimer(int ) ; 
120   virtual int DestroyTimer() ; 
121   
122   // Description:
123   // This function is called on 'q','e' keypress if exitmethod is not
124   // specified and should be overidden by platform dependent subclasses
125   // to provide a termination procedure if one is required.
126   virtual void TerminateApp(void) { /* empty */ }
127   
128   // Description:
129   // These methods correspond to the the Exit, User and Pick
130   // callbacks. They allow for the Style to invoke them.
131   //virtual void ExitCallback();
132   //virtual void UserCallback();
133   //virtual void StartPickCallback();
134   //virtual void EndPickCallback();
135   
136   /* Selection Management */
137   bool highlightCell(const TColStd_IndexedMapOfInteger& MapIndex, 
138                      SALOME_Actor* theMapActor, 
139                      bool hilight, 
140                      bool update = true );
141   bool highlightEdge(const TColStd_IndexedMapOfInteger& MapIndex, 
142                      SALOME_Actor* theMapActor, 
143                      bool hilight, 
144                      bool update = true );
145   bool highlightPoint(const TColStd_IndexedMapOfInteger& MapIndex, 
146                       SALOME_Actor* theMapActor, 
147                       bool hilight, 
148                       bool update = true );
149
150   bool highlight(const Handle(SALOME_InteractiveObject)& IObject, bool hiligth, bool immediatly = true );
151   void unHighlightSubSelection();
152   bool unHighlightAll();
153
154   bool isInViewer( const Handle(SALOME_InteractiveObject)& IObject);
155   bool isVisible( const Handle(SALOME_InteractiveObject)& IObject);
156   void rename(const Handle(SALOME_InteractiveObject)& IObject, QString newName);
157
158   void SetSelectionMode(Selection_Mode mode);
159   void SetSelectionProp(const double& theRed = 1, const double& theGreen = 1,
160                         const double& theBlue = 0, const int& theWidth = 5);
161   void SetSelectionTolerance(const double& theTolNodes = 0.025, const double& theTolCell = 0.001);
162
163   // Displaymode management
164   int GetDisplayMode();
165   void SetDisplayMode(int);
166
167   // Switch representation wireframe/shading
168   void SetDisplayMode(const Handle(SALOME_InteractiveObject)& IObject, int theMode);
169
170   // Change all actors to wireframe or surface
171   void ChangeRepresentationToWireframe();
172   void ChangeRepresentationToSurface();
173
174   // Change to wireframe or surface a list of vtkactor
175   void ChangeRepresentationToWireframe(vtkActorCollection* ListofActors);
176   void ChangeRepresentationToSurface(vtkActorCollection* ListofActors);
177
178   // Erase Display functions
179   void EraseAll();
180   void DisplayAll();
181   void RemoveAll( const bool immediatly );
182   void Erase(const Handle(SALOME_InteractiveObject)& IObject, bool immediatly=true);
183   void Remove(const Handle(SALOME_InteractiveObject)& IObject, bool immediatly=true);
184   void Display(const Handle(SALOME_InteractiveObject)& IObject, bool immediatly=true);
185
186   void Display( SALOME_Actor* SActor, bool immediatly = true );
187   void Erase( SALOME_Actor* SActor, bool immediatly = true );
188   void Remove( SALOME_Actor* SActor, bool updateViewer = true );
189
190   // Transparency
191   void SetTransparency(const Handle(SALOME_InteractiveObject)& IObject,float trans);
192   float GetTransparency(const Handle(SALOME_InteractiveObject)& IObject);
193
194   // Color
195   void SetColor(const Handle(SALOME_InteractiveObject)& IObject,QColor thecolor);
196   QColor GetColor(const Handle(SALOME_InteractiveObject)& IObject);
197
198   void Update();
199
200   vtkRenderer* GetRenderer();
201
202   void setGUIWindow(QWidget* theWindow);
203
204   void setViewFrame(VTKViewer_ViewFrame* ViewFrame);
205   
206   void setCellData(const int& theIndex, 
207                    SALOME_Actor* theMapActor,
208                    VTKViewer_Actor* theActor);
209   void setEdgeData(const int& theCellIndex, 
210                    SALOME_Actor* theMapActor,
211                    const int& theEdgeIndex, 
212                    VTKViewer_Actor* theActor ); //NB
213   void setPointData(const int& theIndex, 
214                     SALOME_Actor* theMapActor,
215                     VTKViewer_Actor* theActor);
216
217   typedef void (*TUpdateActor)(const TColStd_IndexedMapOfInteger& theMapIndex,
218                                SALOME_Actor* theMapActor, 
219                                VTKViewer_Actor* theActor);
220  protected:
221
222   VTKViewer_RenderWindowInteractor();
223   ~VTKViewer_RenderWindowInteractor();
224
225   VTKViewer_InteractorStyleSALOME* myInteractorStyle;
226
227   bool highlight(const TColStd_IndexedMapOfInteger& theMapIndex, 
228                  SALOME_Actor* theMapActor, VTKViewer_Actor* theActor,
229                  TUpdateActor theFun, bool hilight, bool update);
230   void setActorData(const TColStd_IndexedMapOfInteger& theMapIndex,
231                     SALOME_Actor* theMapActor,
232                     VTKViewer_Actor *theActor,
233                     TUpdateActor theFun);
234   
235   // Timer used during various mouse events to figure 
236   // out mouse movements. 
237   QTimer *mTimer ;
238
239   int myDisplayMode;
240
241   //NRI: Selection mode
242   VTKViewer_Actor* myPointActor;
243   VTKViewer_Actor* myEdgeActor;
244   VTKViewer_Actor* myCellActor;
245   void MoveInternalActors();
246
247   vtkPicker* myBasicPicker;
248   vtkCellPicker* myCellPicker;
249   vtkPointPicker* myPointPicker;
250   
251   // User for switching to stereo mode.
252   int PositionBeforeStereo[2];
253
254   // Connect/disconnect all slots which allow events on the
255   // render window to be passed to the interactor. 
256   void ConnectSlots() ;
257   void DisconnectSlots() ;
258   void ProcessSlotConnections(bool conn) ;
259
260  public slots:
261   void MouseMove(const QMouseEvent *event) ;
262   void LeftButtonPressed(const QMouseEvent *event) ;
263   void LeftButtonReleased(const QMouseEvent *event) ;
264   void MiddleButtonPressed(const QMouseEvent *event) ;
265   void MiddleButtonReleased(const QMouseEvent *event) ;
266   void RightButtonPressed(const QMouseEvent *event) ;
267   void RightButtonReleased(const QMouseEvent *event) ;
268   void ButtonPressed(const QMouseEvent *event) ;
269   void ButtonReleased(const QMouseEvent *event) ;
270   void KeyPressed(QKeyEvent *event) ;
271
272   private slots:
273     // Not all of these slots are needed in VTK_MAJOR_VERSION=3,
274     // but moc does not understand "#if VTK_MAJOR_VERSION". Hence, 
275     // we have to include all of these for the time being. Once,
276     // this bug in MOC is fixed, we can separate these. 
277     void TimerFunc() ;
278
279  signals:
280   void RenderWindowModified() ;
281
282  private:
283   VTKViewer_ViewFrame* myViewFrame;  
284   QWidget* myGUIWindow;  
285   double myTolNodes;
286   double myTolItems;
287 };
288
289 #endif
290
291