Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / SVTK / SVTK_KeyFreeInteractorStyle.cxx
1 // Copyright (C) 2007-2012  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.
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   myIsMidButtonDown( false ),
41   myIsLeftButtonDown( false )
42 {
43 }
44
45 //----------------------------------------------------------------------------
46 SVTK_KeyFreeInteractorStyle::~SVTK_KeyFreeInteractorStyle() 
47 {
48 }
49
50 //----------------------------------------------------------------------------
51 void SVTK_KeyFreeInteractorStyle::OnLeftButtonDown(int ctrl, int shift, 
52                                                    int x, int y) 
53 {
54   myIsLeftButtonDown = true;
55
56   if (this->HasObserver(vtkCommand::LeftButtonPressEvent)) {
57     this->InvokeEvent(vtkCommand::LeftButtonPressEvent,NULL);
58     return;
59   }
60   this->FindPokedRenderer(x, y);
61   if (this->CurrentRenderer == NULL) {
62     return;
63   }
64   myShiftState = shift;
65   // finishing current viewer operation
66   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
67     onFinishOperation();
68     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
69   }
70   myOtherPoint = myPoint = QPoint(x, y);
71   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
72     startOperation(ForcedState);
73   } 
74   else {
75     if (!(ctrl||shift)){
76       if (myIsMidButtonDown){
77         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
78       }
79       else{
80         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);
81       }
82     }
83   }
84   return;
85 }
86
87 //----------------------------------------------------------------------------
88 void SVTK_KeyFreeInteractorStyle::OnMiddleButtonDown(int ctrl,
89                                                      int shift, 
90                                                      int x, int y) 
91 {
92   myIsMidButtonDown = true;
93
94   if (this->HasObserver(vtkCommand::MiddleButtonPressEvent))  {
95     this->InvokeEvent(vtkCommand::MiddleButtonPressEvent,NULL);
96     return;
97   }
98   this->FindPokedRenderer(x, y);
99   if (this->CurrentRenderer == NULL)    {
100     return;
101   }
102   myShiftState = shift;
103   // finishing current viewer operation
104   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
105     onFinishOperation();
106     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
107   }
108   myOtherPoint = myPoint = QPoint(x, y);
109   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
110     startOperation(ForcedState);
111   }
112   else {
113     if (!(ctrl||shift)){
114       if ( myIsLeftButtonDown ){
115         startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
116       }
117       else{
118         startOperation(VTK_INTERACTOR_STYLE_CAMERA_PAN);
119       }
120     }
121   }
122 }
123
124 //----------------------------------------------------------------------------
125 void SVTK_KeyFreeInteractorStyle::OnLeftButtonUp(int ctrl, int shift, int x, int y)
126 {
127   myIsLeftButtonDown = false;
128   SVTK_InteractorStyle::OnLeftButtonUp( ctrl, shift, x, y );
129
130   if ( myIsMidButtonDown )
131     OnMiddleButtonDown( ctrl, shift, x, y );
132 }
133
134 //----------------------------------------------------------------------------
135 void SVTK_KeyFreeInteractorStyle::OnMiddleButtonUp(int ctrl, int shift, int x, int y)
136 {
137   myIsMidButtonDown = false;
138   SVTK_InteractorStyle::OnMiddleButtonUp( ctrl, shift, x, y );
139
140   if ( myIsLeftButtonDown )
141     OnLeftButtonDown( ctrl, shift, x, y );
142 }
143
144 //----------------------------------------------------------------------------
145 void SVTK_KeyFreeInteractorStyle::OnChar()
146 {
147   char key = GetInteractor()->GetKeyCode();
148   switch (key) {
149   case 's':
150   case 'S':
151     ActionPicking();
152     EventCallbackCommand->AbortFlagOn();
153     return;
154   }
155   SVTK_InteractorStyle::OnChar();
156 }