Salome HOME
2ffedddfeaed14f4cdff08218305af29bba9d254
[modules/gui.git] / src / SVTK / SVTK_KeyFreeInteractorStyle.cxx
1 //  Copyright (C) 2007-2008  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 //  SALOME VTKViewer : build VTK viewer into Salome desktop
23 //  File   : SVTK_KeyFreeInteractorStyle.cxx
24 //  Author : Christophe ATTANASIO
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "SVTK_KeyFreeInteractorStyle.h"
29 #include "SVTK_Selector.h"
30
31 #include <vtkObjectFactory.h>
32 #include <vtkRenderWindowInteractor.h>
33 #include <vtkCallbackCommand.h>
34 #include <vtkCommand.h>
35 #include <vtkRenderer.h>
36 #include <vtkCamera.h>
37
38
39 //----------------------------------------------------------------------------
40 vtkStandardNewMacro(SVTK_KeyFreeInteractorStyle);
41 //----------------------------------------------------------------------------
42
43 SVTK_KeyFreeInteractorStyle::SVTK_KeyFreeInteractorStyle():
44   myIsMidButtonDown( false ),
45   myIsLeftButtonDown( false )
46 {
47 }
48
49 //----------------------------------------------------------------------------
50 SVTK_KeyFreeInteractorStyle::~SVTK_KeyFreeInteractorStyle() 
51 {
52 }
53
54 //----------------------------------------------------------------------------
55 void SVTK_KeyFreeInteractorStyle::OnLeftButtonDown(int ctrl, int shift, 
56                                                    int x, int y) 
57 {
58   myIsLeftButtonDown = true;
59
60   if (this->HasObserver(vtkCommand::LeftButtonPressEvent)) {
61     this->InvokeEvent(vtkCommand::LeftButtonPressEvent,NULL);
62     return;
63   }
64   this->FindPokedRenderer(x, y);
65   if (this->CurrentRenderer == NULL) {
66     return;
67   }
68   myShiftState = shift;
69   // finishing current viewer operation
70   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
71     onFinishOperation();
72     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
73   }
74   myOtherPoint = myPoint = QPoint(x, y);
75   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
76     startOperation(ForcedState);
77   } 
78   else {
79     if (!(ctrl||shift)){
80       if (myIsMidButtonDown){
81         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
82       }
83       else{
84         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);
85       }
86     }
87   }
88   return;
89 }
90
91 //----------------------------------------------------------------------------
92 void SVTK_KeyFreeInteractorStyle::OnMiddleButtonDown(int ctrl,
93                                                      int shift, 
94                                                      int x, int y) 
95 {
96   myIsMidButtonDown = true;
97
98   if (this->HasObserver(vtkCommand::MiddleButtonPressEvent))  {
99     this->InvokeEvent(vtkCommand::MiddleButtonPressEvent,NULL);
100     return;
101   }
102   this->FindPokedRenderer(x, y);
103   if (this->CurrentRenderer == NULL)    {
104     return;
105   }
106   myShiftState = shift;
107   // finishing current viewer operation
108   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
109     onFinishOperation();
110     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
111   }
112   myOtherPoint = myPoint = QPoint(x, y);
113   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
114     startOperation(ForcedState);
115   }
116   else {
117     if (!(ctrl||shift)){
118       if ( myIsLeftButtonDown ){
119         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
120       }
121       else{
122         startOperation(VTK_INTERACTOR_STYLE_CAMERA_PAN);
123       }
124     }
125   }
126 }
127
128 //----------------------------------------------------------------------------
129 void SVTK_KeyFreeInteractorStyle::OnLeftButtonUp(int ctrl, int shift, int x, int y)
130 {
131   myIsLeftButtonDown = false;
132   SVTK_InteractorStyle::OnLeftButtonUp( ctrl, shift, x, y );
133
134   if ( myIsMidButtonDown )
135     OnMiddleButtonDown( ctrl, shift, x, y );
136 }
137
138 //----------------------------------------------------------------------------
139 void SVTK_KeyFreeInteractorStyle::OnMiddleButtonUp(int ctrl, int shift, int x, int y)
140 {
141   myIsMidButtonDown = false;
142   SVTK_InteractorStyle::OnMiddleButtonUp( ctrl, shift, x, y );
143
144   if ( myIsLeftButtonDown )
145     OnLeftButtonDown( ctrl, shift, x, y );
146 }
147
148 //----------------------------------------------------------------------------
149 void SVTK_KeyFreeInteractorStyle::OnChar()
150 {
151   char key = GetInteractor()->GetKeyCode();
152   switch (key) {
153   case 's':
154   case 'S':
155     ActionPicking();
156     EventCallbackCommand->AbortFlagOn();
157     return;
158   }
159   SVTK_InteractorStyle::OnChar();
160 }