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