Salome HOME
bos #26454 [EDF] (2021) SMESH: interactive mesh modification
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_InteractiveOp.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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 // File   : SMESHGUI_InteractiveOp.cxx
24 // Author : Roman NIKOLAEV, Open CASCADE S.A.S.
25
26 // SMESH includes
27 //
28 #include "SMESHGUI.h"
29 #include "SMESHGUI_InteractiveOp.h"
30 #include "SMESHGUI_VTKUtils.h"
31
32 // GUI includes
33 #include <SVTK_ViewWindow.h>
34 #include <SVTK_RenderWindowInteractor.h>
35 #include <SVTK_Event.h>
36
37 // VTK includes
38 #include <vtkInteractorStyle.h>
39 #include <vtkCallbackCommand.h>
40 #include <vtkGenericRenderWindowInteractor.h>
41 #include <vtkInteractorObserver.h>
42
43
44 //================================================================================
45 /*!
46  * \brief Constructor
47 */
48 //================================================================================
49
50 SMESHGUI_InteractiveOp::SMESHGUI_InteractiveOp() :
51   SMESHGUI_SelectionOp(),
52   myInteractorStyle(0),
53   myRWInteractor(0),
54   myStyleEventCallbackCommand(vtkCallbackCommand::New()),
55   myInteractorStypePriority(0.0),
56   myInteractorEventCallbackCommand(vtkCallbackCommand::New()),
57   myInteractorPriority(1.0)
58 {
59   myStyleEventCallbackCommand->Delete();
60   myStyleEventCallbackCommand->SetClientData(this);
61   myStyleEventCallbackCommand->SetCallback(SMESHGUI_InteractiveOp::ProcessStyleEvents);
62
63   myInteractorEventCallbackCommand->Delete();
64   myInteractorEventCallbackCommand->SetClientData(this);
65   myInteractorEventCallbackCommand->SetCallback(SMESHGUI_InteractiveOp::ProcessInteractorEvents);
66 }
67
68 //================================================================================
69 /*!
70  * \brief Destructor
71 */
72 //================================================================================
73 SMESHGUI_InteractiveOp::~SMESHGUI_InteractiveOp() 
74 {
75 }
76
77 //=======================================================================
78 // function : addObserver()
79 // purpose  : Add VTK observers to process SVTK_InteractorStyle and vtkGenericRenderWindowInteractor events
80 //======================================================================
81 void SMESHGUI_InteractiveOp::addObserver()
82 {
83   if (myInteractorStyle && !myInteractorStyle->HasObserver(SVTK::InteractiveSelectionChanged, myStyleEventCallbackCommand.GetPointer())) {
84     myInteractorStyle->AddObserver(SVTK::InteractiveSelectionChanged, myStyleEventCallbackCommand.GetPointer(),
85       myInteractorStypePriority);
86   }
87   if (myRWInteractor && myRWInteractor->GetDevice() &&
88     !myRWInteractor->GetDevice()->HasObserver(vtkCommand::LeftButtonPressEvent, myInteractorEventCallbackCommand.GetPointer())) {
89     myRWInteractor->GetDevice()->AddObserver(vtkCommand::LeftButtonPressEvent, myInteractorEventCallbackCommand.GetPointer(),
90       myInteractorPriority);
91   }
92 }
93
94 //=======================================================================
95 // function : removeObserver()
96 // purpose  : Remove VTK observers
97 //======================================================================
98 void SMESHGUI_InteractiveOp::removeObserver() {
99   if (myInteractorStyle && myInteractorStyle->HasObserver(SVTK::InteractiveSelectionChanged, myStyleEventCallbackCommand.GetPointer())) {
100     myInteractorStyle->RemoveObserver(myStyleEventCallbackCommand.GetPointer());
101   }
102   if (myRWInteractor && myRWInteractor->GetDevice() &&
103     myRWInteractor->GetDevice()->HasObserver(vtkCommand::LeftButtonPressEvent, myInteractorEventCallbackCommand.GetPointer())) {
104     myRWInteractor->GetDevice()->RemoveObserver(myInteractorEventCallbackCommand.GetPointer());
105   }
106 }
107
108 //=======================================================================
109 // function : startOperation()
110 // purpose  : Init dialog fields, connect signals and slots, show dialog
111 //=======================================================================
112
113 void SMESHGUI_InteractiveOp::startOperation()
114 {
115   SVTK_ViewWindow* svtkViewWindow = SMESH::GetViewWindow(getSMESHGUI());
116   if (svtkViewWindow) {
117     myInteractorStyle = svtkViewWindow->GetInteractorStyle();
118     myRWInteractor = svtkViewWindow->GetInteractor();
119   }
120 }
121
122 //================================================================================
123 /*!
124  * \brief Process interactor style events
125           Static method. Used by vtkCallbackCommand->SetCallback method.
126  */
127  //===============================================================================
128 void SMESHGUI_InteractiveOp::ProcessStyleEvents(vtkObject* vtkNotUsed(theObject),
129   unsigned long theEvent,
130   void* theClientData,
131   void* theCallData) {
132   SMESHGUI_InteractiveOp* self = reinterpret_cast<SMESHGUI_InteractiveOp*>(theClientData);
133   if (self)
134     self->processStyleEvents(theEvent, theCallData);
135 }
136
137 //================================================================================
138 /*!
139  * \brief Process Generic Render Window Interactor events.
140           Static method. Used by vtkCallbackCommand->SetCallback method.
141  */
142  //===============================================================================
143 void SMESHGUI_InteractiveOp::ProcessInteractorEvents(vtkObject* vtkNotUsed(theObject),
144   unsigned long theEvent,
145   void* theClientData,
146   void* theCallData) {
147   SMESHGUI_InteractiveOp* self = reinterpret_cast<SMESHGUI_InteractiveOp*>(theClientData);
148   if (self)
149     self->processInteractorEvents(theEvent, theCallData);
150 }
151
152 //================================================================================
153 /*!
154  * \brief Process interactor style events ()
155           Virtual method. Should be overridden in inherited classes.
156  */
157  //===============================================================================
158
159 void SMESHGUI_InteractiveOp::processStyleEvents(unsigned long theEvent, void* theCallData) {
160 }
161
162 //================================================================================
163 /*!
164  * \brief Process Generic Render Window Interactor events.
165           Virtual method. Should be overridden in inherited classes.
166  */
167  //===============================================================================
168 void SMESHGUI_InteractiveOp::processInteractorEvents(unsigned long theEvent, void* theCallData)
169 {
170 }
171
172 //================================================================================
173 /*!
174  * \brief Deactivate current operation in active VTK viewer
175  */
176  //===============================================================================
177 void SMESHGUI_InteractiveOp::deactivateCurrentViewOperation() {
178   if (SVTK_ViewWindow* svtkViewWindow = SMESH::GetViewWindow(getSMESHGUI())) {
179     svtkViewWindow->deactivateCurrectOperation();
180   }
181 }