Salome HOME
d0aa46797ed7806d16b405edc37ed6664a80062d
[modules/gui.git] / src / SVTK / SVTK_KeyFreeInteractorStyle.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  SALOME VTKViewer : build VTK viewer into Salome desktop
21 //  File   : SVTK_KeyFreeInteractorStyle.cxx
22 //  Author : Christophe ATTANASIO
23
24 #include "SVTK_KeyFreeInteractorStyle.h"
25 #include "SVTK_Selector.h"
26
27 #include <vtkObjectFactory.h>
28 #include <vtkRenderWindowInteractor.h>
29 #include <vtkCallbackCommand.h>
30 #include <vtkCommand.h>
31 #include <vtkRenderer.h>
32 #include <vtkCamera.h>
33
34
35 //----------------------------------------------------------------------------
36 vtkStandardNewMacro(SVTK_KeyFreeInteractorStyle)
37 //----------------------------------------------------------------------------
38
39 SVTK_KeyFreeInteractorStyle::SVTK_KeyFreeInteractorStyle():
40   myIsLeftButtonDown( false ),
41   myIsRightButtonDown( false )
42 {
43 }
44
45 //----------------------------------------------------------------------------
46 SVTK_KeyFreeInteractorStyle::~SVTK_KeyFreeInteractorStyle() 
47 {
48 }
49
50 //----------------------------------------------------------------------------
51 void SVTK_KeyFreeInteractorStyle::OnMouseMove(int ctrl,
52                                               int shift,
53                                               int x, int y)
54 {
55   // OnLeftButtonDown + OnMouseMove = Rotate
56   if ( myIsLeftButtonDown ) {
57     OnLeftButtonDown( ctrl, shift, x, y );
58     myIsLeftButtonDown = false;
59   }
60   // OnRightButtonDown + OnMouseMove = Zoom
61   if ( myIsRightButtonDown ) {
62     OnRightButtonDown( ctrl, shift, x, y );
63     myIsRightButtonDown = false;
64   }
65   SVTK_InteractorStyle::OnMouseMove( ctrl, shift, x, y );
66 }
67
68 //----------------------------------------------------------------------------
69 void SVTK_KeyFreeInteractorStyle::OnLeftButtonDown(int ctrl, int shift, 
70                                                    int x, int y) 
71 {
72   if ( ctrl ) {
73     SVTK_InteractorStyle::OnLeftButtonDown( !ctrl, shift, x, y );
74     return;
75   }
76
77   if( !myIsLeftButtonDown ) {
78     myIsLeftButtonDown = true;
79     return;
80   }
81
82   if (this->HasObserver(vtkCommand::LeftButtonPressEvent)) {
83     this->InvokeEvent(vtkCommand::LeftButtonPressEvent,NULL);
84     return;
85   }
86   this->FindPokedRenderer(x, y);
87   if (this->CurrentRenderer == NULL) {
88     return;
89   }
90   myShiftState = shift;
91   // finishing current viewer operation
92   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
93     onFinishOperation();
94     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
95   }
96   myOtherPoint = myPoint = QPoint(x, y);
97   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
98     startOperation(ForcedState);
99   } 
100   else if ( !shift )
101     startOperation(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);
102
103   return;
104 }
105
106 //----------------------------------------------------------------------------
107 void SVTK_KeyFreeInteractorStyle::OnMiddleButtonDown(int ctrl,
108                                                      int shift, 
109                                                      int x, int y) 
110 {
111   if ( ctrl ) {
112     SVTK_InteractorStyle::OnMiddleButtonDown( !ctrl, shift, x, y );
113     return;
114   }
115
116   if (this->HasObserver(vtkCommand::MiddleButtonPressEvent))  {
117     this->InvokeEvent(vtkCommand::MiddleButtonPressEvent,NULL);
118     return;
119   }
120   this->FindPokedRenderer(x, y);
121   if (this->CurrentRenderer == NULL)    {
122     return;
123   }
124   myShiftState = shift;
125   // finishing current viewer operation
126   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
127     onFinishOperation();
128     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
129   }
130   myOtherPoint = myPoint = QPoint(x, y);
131   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
132     startOperation(ForcedState);
133   }
134   else if ( !shift )
135     startOperation(VTK_INTERACTOR_STYLE_CAMERA_PAN);
136 }
137
138 //----------------------------------------------------------------------------
139 void SVTK_KeyFreeInteractorStyle::OnRightButtonDown( int ctrl,
140                                                      int shift,
141                                                      int x, int y )
142 {
143   if ( ctrl ) {
144     SVTK_InteractorStyle::OnRightButtonDown( !ctrl, shift, x, y );
145     return;
146   }
147
148   if( !myIsRightButtonDown ) {
149     myIsRightButtonDown = true;
150     return;
151   }
152
153   if( this->HasObserver( vtkCommand::RightButtonPressEvent ) ) {
154     this->InvokeEvent( vtkCommand::RightButtonPressEvent, NULL );
155     return;
156   }
157   this->FindPokedRenderer( x, y );
158   if( this->CurrentRenderer == NULL ) {
159     return;
160   }
161   myShiftState = shift;
162   // finishing current viewer operation
163   if ( State != VTK_INTERACTOR_STYLE_CAMERA_NONE ) {
164     onFinishOperation();
165     startOperation( VTK_INTERACTOR_STYLE_CAMERA_NONE );
166   }
167   myOtherPoint = myPoint = QPoint(x, y);
168   if ( ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE ) {
169     startOperation(ForcedState);
170   }
171   else if ( !shift )
172     startOperation( VTK_INTERACTOR_STYLE_CAMERA_ZOOM );
173 }
174
175 //----------------------------------------------------------------------------
176 void SVTK_KeyFreeInteractorStyle::OnLeftButtonUp(int ctrl, int shift, int x, int y)
177 {
178   // OnLeftButtonDown + OnLeftButtonUp = Select
179   if ( myIsLeftButtonDown ) {
180     SVTK_InteractorStyle::OnLeftButtonDown( ctrl, shift, x, y );
181     myIsLeftButtonDown = false;
182   }
183   SVTK_InteractorStyle::OnLeftButtonUp( ctrl, shift, x, y );
184 }
185
186 //----------------------------------------------------------------------------
187 void SVTK_KeyFreeInteractorStyle::OnMiddleButtonUp(int ctrl, int shift, int x, int y)
188 {
189   SVTK_InteractorStyle::OnMiddleButtonUp( ctrl, shift, x, y );
190 }
191
192 //----------------------------------------------------------------------------
193 void SVTK_KeyFreeInteractorStyle::OnRightButtonUp(int ctrl, int shift, int x, int y)
194 {
195   // OnRightButtonDown + OnRightButtonUp = Open context menu
196   if( myIsRightButtonDown ) {
197     myIsRightButtonDown = false;
198     SVTK_InteractorStyle::OnRightButtonDown( ctrl, shift, x, y );
199   }
200   SVTK_InteractorStyle::OnRightButtonUp( ctrl, shift, x, y );
201 }
202
203 //----------------------------------------------------------------------------
204 void SVTK_KeyFreeInteractorStyle::OnChar()
205 {
206   char key = GetInteractor()->GetKeyCode();
207   switch (key) {
208   case 's':
209   case 'S':
210     ActionPicking();
211     EventCallbackCommand->AbortFlagOn();
212     return;
213   }
214   SVTK_InteractorStyle::OnChar();
215 }