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