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