Salome HOME
Copyright update 2022
[modules/gui.git] / src / LightApp / LightApp_Operation.cxx
1 // Copyright (C) 2007-2022  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   : LightApp_Operation.h
24 //  Author : Sergey LITONIN
25 //  Module : LightApp
26 //
27 #include "LightApp_Operation.h"
28 #include "LightApp_Module.h"
29 #include "LightApp_Application.h"
30 #include "LightApp_SelectionMgr.h"
31 #include "LightApp_Dialog.h"
32
33 #include <SUIT_Desktop.h>
34 #include <SUIT_Study.h>
35
36 /*!
37  * \brief Constructor
38 *
39 * Constructor sets myModule in NULL and myIsAutoResumed in \c true
40 */
41 LightApp_Operation::LightApp_Operation()
42 : SUIT_Operation( 0 ),
43   myModule( 0 ),
44   myIsAutoResumed( true )
45 {
46 }
47
48 /*!
49  * \brief Destructor
50 *
51 * Destructor does nothing
52 */
53 LightApp_Operation::~LightApp_Operation()
54 {
55   
56 }
57
58 /*!
59  * \brief Gets module of operation
60   * \return Pointer to the module 
61 *
62 * Gets pointer to the module or NULL if module was not set. It is strongly recomended to
63 * set valid pointer on the module before start of operation
64 */
65 LightApp_Module* LightApp_Operation::module() const
66 {
67   return myModule;
68 }
69
70
71 /*!
72  * \brief Sets module of operation
73  * \param theModule - module to be set
74 *
75 * Sets pointer to the module. It is strongly recomended to set valid pointer on the
76 * module before start of operation
77 */
78 void LightApp_Operation::setModule( LightApp_Module* theModule )
79 {
80   myModule = theModule;
81   setApplication( myModule ? myModule->application() : 0 );
82   setStudy( application() ? application()->activeStudy() : 0 );
83 }
84
85 /*!
86  * \brief Gets desktop of operation
87   * \return Pointer to the desktop
88 *
89 * Gets pointer to the desktop or NULL if application was not set. It is strongly recomended
90 * to set valid pointer on the application before start of operation
91 */
92 SUIT_Desktop* LightApp_Operation::desktop() const
93 {
94   return application() != 0 ? application()->desktop() : 0;
95 }
96
97 /*!
98  * \brief Enable dialog of operation
99 *
100 * Virtual method redefined from the base class. Enable dialog if it was desabled (in
101 * suspend method) and activate selection
102 */
103 void LightApp_Operation::resumeOperation()
104 {
105   SUIT_Operation::resumeOperation();
106   setDialogActive( true );
107 }
108
109 /*!
110  * \brief Performs actions needed for starting operation
111 *
112 * Virtual method redefined from the base class. Connect signal of selection manager to
113 * onSelectionDone() slot
114 */
115 void LightApp_Operation::startOperation()
116 {
117   if( selectionMgr() )
118     connect( selectionMgr(), SIGNAL( selectionChanged() ), SLOT( onSelectionDone() ) );
119     
120   //If suspended operation was stopped during starting other operation,
121   //the dialog is inactive now, We must activate it
122   setDialogActive( true );
123 }
124
125 /*!
126  * \brief Performs actions needed for suspending operation
127 *
128 * Virtual method redefined from the base class. This implementation calls corresponding
129 * method of base class and cals setDialogActive( false )
130 */
131 void LightApp_Operation::suspendOperation()
132 {
133   SUIT_Operation::suspendOperation();
134   setDialogActive( false );
135 }
136
137 /*!
138  * \brief Performs actions needed for aborting operation
139 *
140 * Virtual method redefined from the base class calls corresponding method of base class
141 * and hides dialog box (if it is exists), disconnect slots from selection manager
142 */
143 void LightApp_Operation::abortOperation()
144 {
145   SUIT_Operation::abortOperation();
146   setDialogActive( true );
147   if ( dlg() )
148     dlg()->hide();
149
150   if( selectionMgr() )
151     disconnect( selectionMgr(), SIGNAL( selectionChanged() ), this, SLOT( onSelectionDone() ) );
152 }
153
154 /*!
155  * \brief Performs actions needed for committing operation
156 *
157 * Virtual method redefined from the base class calls corresponding method of base class
158 * and hides dialog box (if it is exists), disconnect slots from selection manager
159 */
160 void LightApp_Operation::commitOperation()
161 {
162   SUIT_Operation::commitOperation();
163   setDialogActive( true );
164   if ( dlg() )
165     dlg()->hide();
166
167   if( selectionMgr() )
168     disconnect( selectionMgr(), SIGNAL( selectionChanged() ), this, SLOT( onSelectionDone() ) );
169 }
170
171 /*!
172  * \brief Gets dialog
173   * \return Pointer to the dialog of this operation or NULL if it does not exist
174 *
175 * This method should be redefined in derived classes if they use dialogs. If this
176 * function returns pointer to dialog then dialog will be correctly
177 * -# deactivated in suspendOperation method
178 * -# activated in resumeOperation method
179 * -# hidden in abortOperation and commitOperation methods
180 */
181 LightApp_Dialog* LightApp_Operation::dlg() const
182 {
183   return 0;
184 }
185
186 /*!
187  * \brief Activates selection
188 *
189 * Virtual method should be redefined in derived classes if they use own selection modes
190 * (different from default)
191 */
192 void LightApp_Operation::activateSelection()
193 {
194 }
195
196 /*!
197  * \brief Virtual method called when selection is changed
198 *
199 * Virtual method should be redefined in derived classes if they works with selection
200 * to provide reaction on the change of selection
201 */
202 void LightApp_Operation::selectionDone()
203 {
204 }
205
206 /*!
207  * \brief Gets active operation
208 *
209 * This method provided for convinience calls SUIT_Study::activeOperation() one
210 */
211 SUIT_Operation* LightApp_Operation::activeOperation() const
212 {
213   return study() != 0 ? study()->activeOperation() : 0;
214 }
215
216 /*!
217  * \brief Gets selection manager
218 *
219 * This method provided for convinience calls LightApp_Application::selectionMgr() one
220 */
221 LightApp_SelectionMgr* LightApp_Operation::selectionMgr() const
222 {
223   SUIT_Application* app = application();
224   if ( app != 0 && app->inherits( "LightApp_Application" ) )
225     return ( (LightApp_Application*)app )->selectionMgr();
226   else
227     return 0;
228 }
229
230 /*!
231  * \brief Call selectionDone() method 
232 *
233 * Call selectionDone() method if operator is an active one (see selectionDone() for more
234 * description )
235 */
236 void LightApp_Operation::onSelectionDone()
237 {
238   if ( isActive() )
239     selectionDone();
240 }
241
242 /*!
243  * \brief Update object browser or/and viewer etc.
244  * \param flags - update flags
245 *
246 * This method provided for convinience calls LightApp_Module::update() one (see
247 * LightApp_Module::update() for more description)
248 */
249 void LightApp_Operation::update( const int flags )
250 {
251   if ( myModule != 0 )
252     myModule->update( flags );
253 }
254
255 /*!
256  * \brief Activate/Deactivate dialog of operation
257  * \param active - State of the dialog to be set
258 *
259 * Activate/Deactivate dialog of operation. This method called from startOperation(),
260 * suspendOperation() ones and so on
261 */
262 void LightApp_Operation::setDialogActive( const bool active )
263 {
264   if( dlg() )
265   {
266     if( active )
267     {
268       activateSelection();
269       dlg()->activateWindow();
270     }
271   }
272 }
273
274 /*!
275  * \brief Gets autoresume property
276  * \return Autoresume property.
277 *
278 * Autoresume property is used during automatic resuming operation. If operation is
279 * suspended and cursor is moved above dialog of the operation then operation is resumed
280 * automatically (if possible). It can be resumed only program call otherwise (see
281 * LightApp_SwitchOp for more description). This property is \c true by default and may be
282 * changed with setAutoResumed() method call.
283 */
284 bool LightApp_Operation::isAutoResumed() const
285 {
286   return myIsAutoResumed;
287 }
288
289 /*!
290  * \brief Sets autoresume property
291  * \param on - Value to be set
292  * \return Autoresume property.
293 *
294 * Sets autoresume property (see isAutoResumed() for more description)
295 */
296 void LightApp_Operation::setAutoResumed( const bool on )
297 {
298   myIsAutoResumed = on;
299 }