Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / SUIT / SUIT_Operation.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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/
18 //
19
20 /*!
21   SALOME SalomeApp
22
23   Copyright (C) 2005  CEA/DEN, EDF R&D
24
25   File   : SUIT_Operation.h
26   Author : Unknown
27   Module : SALOME
28 */
29
30 #include "SUIT_Operation.h"
31
32 #include "SUIT_Study.h"
33 #include "SUIT_Desktop.h"
34 #include "SUIT_MessageBox.h"
35 #include "SUIT_Application.h"
36
37 /*!
38  \brief Constructor
39  \param SUIT_Application - application for this operation
40
41   Constructs an empty operation. Constructor should work very fast because many
42   operators may be created after starting application but only several from them
43   may be used. As result this constructor stores given application in myApp field
44   and set Waiting status.
45 */
46 SUIT_Operation::SUIT_Operation( SUIT_Application* app )
47 : QObject(),
48 myApp( app ),
49 myStudy( 0 ),
50 myState( Waiting ),
51 myFlags( Transaction )
52 {
53 }
54
55 /*!
56    * \brief Destructor
57 */
58 SUIT_Operation::~SUIT_Operation()
59 {
60 }
61
62 /*!
63  * \brief Returns operation study
64   * \return Pointer to study
65 *
66 * Get study corresponding to this operation i.e. study which starts this operation.
67 */
68 SUIT_Study* SUIT_Operation::study() const
69 {
70   return myStudy;
71 }
72
73 /*!
74  * \brief Sets operation study
75   * \param theStudy - study corresponding to this operation
76 *
77 * Sets study corresponding to this operation i.e. study which starts this operation.
78 */
79 void SUIT_Operation::setStudy( SUIT_Study* theStudy )
80 {
81   myStudy = theStudy;
82 }
83
84 /*!
85  * \brief Gets application
86   * \return Pointer to application
87 *
88 * Gets application for this operation
89 */
90 SUIT_Application* SUIT_Operation::application() const
91 {
92   return myApp;
93 }
94
95 /*!
96  * \brief Sets application
97   * \param theApp - application for this operation
98 *
99 * Gets application for this operation
100 */
101 void SUIT_Operation::setApplication( SUIT_Application* theApp )
102 {
103   myApp = theApp;
104 }
105
106 /*!
107  * \brief Gets state of operation
108   * \return Value from OperationState enumeration
109 *
110 * Gets state of operation (see OperationState enumeration)
111 */
112 SUIT_Operation::OperationState SUIT_Operation::state() const
113 {
114   return myState;
115 }
116
117 /*!
118  * \brief Sets state of operation
119   * \param theState - state of operation to be set
120 *
121 *  Sets state of operation (see OperationState enumeration)
122 */
123 void SUIT_Operation::setState( const SUIT_Operation::OperationState theState )
124 {
125   myState = theState;
126 }
127
128 /*!
129  * \brief Sets the flags of operation
130   * \param f - flags of operation to be set
131 *
132 *  Sets flags of operation (see Flags enumeration)
133 */
134 void SUIT_Operation::setFlags( const int f )
135 {
136   myFlags = myFlags | f;
137 }
138
139 /*!
140  * \brief Clears the flags of operation
141   * \param f - flags of operation to be cleared
142 *
143 *  Clears flags of operation (see Flags enumeration)
144 */
145 void SUIT_Operation::clearFlags( const int f )
146 {
147   myFlags = myFlags & ~f;
148 }
149
150 /*!
151  * \brief Test the flags of operation
152   * \param f - flags of operation to be tested
153 *
154 *  Returns TRUE if the specified flags setted in the operation (see Flags enumeration)
155 */
156 bool SUIT_Operation::testFlags( const int f ) const
157 {
158   return ( myFlags & f ) == f;
159 }
160
161 /*!
162  * \brief Name of the operation
163 *
164 *  Returns string name of the operation. This name will be used for
165 *  automatically commited transaction.
166 */
167 QString SUIT_Operation::operationName() const
168 {
169   return QString::null;
170 }
171
172 /*!
173  * \brief Starts operation
174 *
175 * Public slot. Verifies whether operation can be started and starts operation.
176 * This slot is not virtual and cannot be redefined. Redefine startOperation method
177 * to change behavior of operation. There is no point in using this method. It would
178 * be better to inherit own operator from base one and redefine startOperation method
179 * instead.
180 */
181 void SUIT_Operation::start()
182 {
183   if ( study() )
184     study()->start( this );
185   else
186   {
187     startOperation();
188     emit started( this );
189   }
190 }
191
192 /*!
193  * \brief Aborts operation
194 *
195 * Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
196 * Redefine abortOperation method to change behavior of operation instead
197 */
198 void SUIT_Operation::abort()
199 {
200   if ( study() )
201     study()->abort( this );
202   else
203   {
204     abortOperation();
205     myState = Waiting;
206     emit aborted( this );
207
208     stopOperation();
209     emit stopped( this );
210   }
211 }
212
213 /*!
214  * \brief Commits operation
215 *
216 * Public slot. Commits operation. This slot is not virtual and cannot be redefined.
217 * Redefine commitOperation method to change behavior of operation instead
218 */
219 void SUIT_Operation::commit()
220 {
221   if ( study() )
222     study()->commit( this );
223   else
224   {
225     commitOperation();
226     myState = Waiting;
227     emit committed( this );
228
229     stopOperation();
230     emit stopped( this );
231   }
232 }
233
234 /*!
235  * \brief Resumes operation
236 *
237 * Public slot. Resumes operation. This slot is called when operation is resumed after
238 * previous suspending. This slot is not virtual and cannot be redefined. Redefine
239 * resumeOperation method to change behavior of operation instead
240 */
241 void SUIT_Operation::resume()
242 {
243   if ( study() )
244     study()->resume( this );
245   else
246   {
247     resumeOperation();
248     myState = Running;
249     emit resumed( this );
250   }
251 }
252
253 /*!
254  * \brief Suspend operation.
255 *
256 * Public slot. Suspend operation. This slot is called when operation is suspended
257 * (for starting other one, for example) This slot is not virtual and cannot be
258 * redefined. Redefine suspendOperation method to change behavior of operation instead
259 */
260 void SUIT_Operation::suspend()
261 {
262   if ( study() )
263     study()->suspend( this );
264   else
265   {
266     suspendOperation();
267     myState = Suspended;
268     emit suspended( this );
269   }
270 }
271
272 /*!
273  * \brief Verifies whether operator is ready to start.
274  * \return TRUE if operation is ready to start
275 *
276 * Default implementation returns TRUE. Redefine this method to add own verifications
277 */
278 bool SUIT_Operation::isReadyToStart() const
279 {
280   return true;
281 }
282
283 /*!
284  * \brief Virtual method called when operation is started
285 *
286 * Virtual method called when operation started (see start() method for more description)
287 */
288 void SUIT_Operation::startOperation()
289 {
290   emit callSlot();
291   commit();
292 }
293
294 /*!
295  * \brief Virtual method called when operation is started
296 *
297 * Virtual method called when operation stopped - comitted or aborted.
298 */
299 void SUIT_Operation::stopOperation()
300 {
301 }
302
303 /*!
304  * \brief Virtual method called when operation aborted
305 *
306 * Virtual method called when operation aborted (see abort() method for more description)
307 */
308 void SUIT_Operation::abortOperation()
309 {
310 }
311
312 /*!
313  * \brief Virtual method called when operation resumed
314 *
315 * Virtual method called when operation resumed (see resume() method for more description)
316 */
317 void SUIT_Operation::resumeOperation()
318 {
319 }
320
321 /*!
322  * \brief Virtual method called when operation suspended
323 *
324 * Virtual method called when operation suspended (see suspend() method for more description)
325 */
326 void SUIT_Operation::suspendOperation()
327 {
328 }
329
330 /*!
331  * \brief Virtual method called when operation committed
332 *
333 * Virtual method called when operation committed (see commit() method for more description)
334 */
335 void SUIT_Operation::commitOperation()
336 {
337 }
338
339 /*!
340  * \brief Sets slot which is called when operation is started
341   * \param theReceiver - object containing slot
342   * \param theSlot - slot of theReceiver object
343   * \return TR if slot was connected successfully, FALSE otherwise
344 *
345 * Sets slot which is called when operation is started. There is no point in
346 * using this method. It would be better to inherit own operator from base
347 * one and redefine startOperation method
348 */
349 bool SUIT_Operation::setSlot( const QObject* theReceiver, const char* theSlot )
350 {
351   return connect( this, SIGNAL( callSlot() ), theReceiver, theSlot );
352 }
353
354 /*!
355  * \brief Verifies whether given operator is valid for this one
356   * \param theOtherOp - other operation
357   * \return Returns TRUE if the given operator is valid for this one
358 *
359 * Verifies whether given operator is valid for this one (i.e. can be started "above"
360 * this operator)
361 */
362 bool SUIT_Operation::isValid( SUIT_Operation* ) const
363 {
364   return false;
365 }
366
367 /*!
368  * \brief Verifies whether this operator can be always started above any already runnig one
369   * \return Returns TRUE if current operation must not be checked for ActiveOperation->IsValid( this )
370 *
371 * This method must be redefined in derived operation if operation of derived class
372 * must be always can start above any launched one. Default implementation returns FALSE,
373 * so it is being checked for IsValid, but some operations may overload IsGranted()
374 * In this case they will always start, no matter what operation is running.
375 */
376 bool SUIT_Operation::isGranted() const
377 {
378   return false;
379 }
380
381 /*!
382  * \brief Verifies whether operation is an runned one (state()==Running)
383   * \return TRUE if operation is active, FALSE otherwise
384 *
385 * Verifies whether operation is an running. Returns TRUE if state of operator
386 * is Running
387 */
388 bool SUIT_Operation::isRunning() const
389 {
390   return state() == Running;
391 }
392
393 /*!
394  * \brief Verifies whether operation is an active for study.
395   * \return TRUE if operation is active, FALSE otherwise
396 *
397 * Verifies whether operation is an active on. Returns TRUE if this operator
398 * is active for study
399 */
400 bool SUIT_Operation::isActive() const
401 {
402   return study() ? study()->activeOperation() == this : false;
403 }
404
405 /*!
406  * \brief Starts operator above this one
407   * \param theOp - operation to be started
408 *
409 * Start operator above this one. Use this method if you want to call other operator
410 * from this one
411 */
412 void SUIT_Operation::start( SUIT_Operation* op, const bool check )
413 {
414   if ( !op )
415     return;
416     
417   if ( study() )
418     study()->start( op, check );
419   else
420   {
421     connect( this, SIGNAL( stopped( SUIT_Operation* ) ), op, SLOT( abort() ) );
422     op->start();
423   }
424 }
425
426 /*!
427  * \brief Sets execution status
428   * \param theStatus - execution status
429 *
430 * Sets myExecStatus to the given value
431 */
432 void SUIT_Operation::setExecStatus( const int theVal )
433 {
434   myExecStatus = (ExecStatus)theVal;
435 }
436
437 /*!
438  * \brief Gets execution status
439  * \return Execution status
440 *
441 * Gets execution status
442 */
443 int SUIT_Operation::execStatus() const
444 {
445   return myExecStatus;
446 }
447
448 /*!
449  * \brief Opens transaction for data modifications.
450 */
451 bool SUIT_Operation::openTransaction()
452 {
453   if ( !study() )
454     return false;
455
456   return study()->openTransaction();
457 }
458
459 /*!
460  * \brief Aborts transaction and all performed data modifications.
461 */
462 bool SUIT_Operation::abortTransaction()
463 {
464   if ( !study() )
465     return false;
466
467   return study()->abortTransaction();
468 }
469
470 /*!
471  * \brief Commits transaction and all performed data modifications.
472 */
473 bool SUIT_Operation::commitTransaction( const QString& name )
474 {
475   if ( !study() )
476     return false;
477
478   return study()->commitTransaction( name );
479 }
480
481 /*!
482  * \brief Returns TRUE if transaction is opened.
483 */
484 bool SUIT_Operation::hasTransaction() const
485 {
486   if ( !study() )
487     return false;
488
489   return study()->hasTransaction();
490 }