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