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