1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SUIT_Study.h"
25 #include "SUIT_Desktop.h"
26 #include "SUIT_Operation.h"
27 #include "SUIT_DataObject.h"
28 #include "SUIT_MessageBox.h"
29 #include "SUIT_Application.h"
32 * Support study management. Object management. Operation management.
36 SUIT_Study::SUIT_Study( SUIT_Application* app )
41 myIsModified( false ),
42 myBlockChangeState( false )
48 myRoot = new SUIT_DataObject();
52 SUIT_Study::~SUIT_Study()
61 int SUIT_Study::id() const
67 *\retval root data object.
69 SUIT_DataObject* SUIT_Study::root() const
77 SUIT_Application* SUIT_Study::application() const
85 QString SUIT_Study::studyName() const
91 *\retval active operation.
93 SUIT_Operation* SUIT_Study::activeOperation() const
95 return myOperations.count() > 0 ? myOperations.last() : 0;
99 *\retval TRUE - if study saved, else FALSE.
101 bool SUIT_Study::isSaved() const
107 *\retval TRUE - if study modified, else FALSE.
109 bool SUIT_Study::isModified() const
115 *Close document. NOT IMPLEMENTED.
117 void SUIT_Study::closeDocument(bool /*permanently*/)
122 Custom document initialization to be performed \n
123 within onNewDoc() handler can be put here
125 bool SUIT_Study::createDocument( const QString& )
131 * Open document. Sets file name. return true.
133 bool SUIT_Study::openDocument( const QString& fileName )
137 myIsModified = false;
143 * Save document as \a fileName. Set file name.
145 bool SUIT_Study::saveDocumentAs( const QString& fileName )
149 myIsModified = false;
155 *\retval TRUE - if document saved successful, else FALSE.
157 bool SUIT_Study::saveDocument()
159 return saveDocumentAs( myName );
163 *Abort all operations.
165 void SUIT_Study::abortAllOperations()
167 myBlockChangeState = true;
168 for ( Operations::iterator it = myOperations.begin(); it != myOperations.end(); ++it )
170 myBlockChangeState = false;
171 myOperations.clear();
175 Update study. NOT IMPLEMENTED HERE.
177 void SUIT_Study::update()
184 void SUIT_Study::sendChangesNotification()
186 emit studyModified( this );
190 Set study saved to \a on.
192 void SUIT_Study::setIsSaved( const bool on )
198 Set study modified to \a on.
200 void SUIT_Study::setIsModified( const bool on )
206 Set study modified to \a on.
208 void SUIT_Study::Modified()
210 setIsModified( true );
211 sendChangesNotification();
217 void SUIT_Study::setRoot( SUIT_DataObject* obj )
222 // This is necessary in order not to destroy the complete tree of objects
224 obj->reparentChildren( myRoot );
233 void SUIT_Study::setStudyName( const QString& name )
239 * \brief Verifies whether operation can be activated above already started ones
240 * \param theOp - operation to be checked
241 * \return NULL if operation can be activated, pointer to operation which denies
242 * starting tested operation
244 * Verifies whether operation can be activated above already started ones. This method
245 * is called from SUIT_Study::start() and SUIT_Study::resume() methods.
247 SUIT_Operation* SUIT_Study::blockingOperation( SUIT_Operation* theOp ) const
249 if( theOp->isGranted() )
252 Operations tmpOps( myOperations );
253 for ( Operations::const_iterator it = tmpOps.end(); it != tmpOps.begin(); )
256 SUIT_Operation* anOp = *it;
257 if ( anOp != 0 && anOp!= theOp && !anOp->isValid( theOp ) )
265 * \brief Starts operation
266 * \param theOp - operation to be started
267 * \param toCheck - if parameters is equal TRUE then checking performed whether
268 * all already started operations allow to start this operation above them (default
270 * \return TRUE if operation is started, FALSE otherwise
272 * Verifies whether theOp operation can be started above already started ones (if toCheck
273 * parameter is equal TRUE) and starts it
275 bool SUIT_Study::start( SUIT_Operation* theOp, const bool toCheck )
277 if ( !theOp || myOperations.contains( theOp ) )
280 theOp->setExecStatus( SUIT_Operation::Rejected );
281 theOp->setStudy( this );
283 if ( !theOp->isReadyToStart() )
288 while ( SUIT_Operation* anOp = blockingOperation( theOp ) )
290 int anAnsw = SUIT_MessageBox::question( application()->desktop(),
291 tr( "OPERATION_LAUNCH" ), tr( "PREVIOUS_NOT_FINISHED" ),
292 tr( "CONTINUE" ), tr( "CANCEL" ) );
301 SUIT_Operation* anOp = activeOperation();
304 activeOperation()->suspendOperation();
305 anOp->setState( SUIT_Operation::Suspended );
308 theOp->setState( SUIT_Operation::Running );
309 myOperations.append( theOp );
311 emit theOp->started( theOp );
312 operationStarted( theOp );
313 theOp->startOperation();
319 * \brief Aborts operation
320 * \param theOp - operation to be aborted
321 * \return TRUE if operation is aborted successfully
323 * Verifies whether operation already started and aborts it in this case (sets execution
324 * status to Rejected and stops operation)
326 bool SUIT_Study::abort( SUIT_Operation* theOp )
328 if ( !theOp || !myOperations.contains( theOp ) )
331 theOp->setExecStatus( SUIT_Operation::Rejected );
333 theOp->abortOperation();
334 operationAborted( theOp );
335 emit theOp->aborted( theOp );
343 * \brief Commits operation
344 * \param theOp - operation to be committed
345 * \return TRUE if operation is committed successfully
347 * Verifies whether operation already started and commits it in this case (sets execution
348 * status to Accepted and stops operation)
350 bool SUIT_Study::commit( SUIT_Operation* theOp )
352 if ( !theOp || !myOperations.contains( theOp ) )
355 theOp->setExecStatus( SUIT_Operation::Accepted );
357 theOp->commitOperation();
358 operationCommited( theOp );
359 emit theOp->committed( theOp );
363 emit studyModified( this );
369 * \brief Commits operation
370 * \param theOp - operation to be committed
371 * \return TRUE if operation is suspended successfully
373 * Verifies whether operation already started and suspends it in this case. Operations
374 * ususlly are suspended to start other one above them.
376 bool SUIT_Study::suspend( SUIT_Operation* theOp )
378 if ( !theOp || !myOperations.contains( theOp ) || theOp->state() == SUIT_Operation::Suspended )
381 theOp->setState( SUIT_Operation::Suspended );
382 theOp->suspendOperation();
383 emit theOp->suspended( theOp );
389 * \brief Resumes operation
390 * \param theOp - operation to be resumed
391 * \return TRUE if operation is aborted successfully
393 * Verifies whether operation already started but suspended and resumesit in this case.
395 bool SUIT_Study::resume( SUIT_Operation* theOp )
397 if ( !theOp || !myOperations.contains( theOp ) ||
398 theOp->state() == SUIT_Operation::Running ||
399 blockingOperation( theOp ) != 0 )
402 if ( myOperations.count() > 0 )
403 suspend( myOperations.last() );
405 theOp->setState( SUIT_Operation::Running );
406 theOp->resumeOperation();
408 // Move operation at the end of list in order to sort it in the order of activation.
409 // As result active operation is a last operation of list, operation which was active
410 // before currently active operation is located before it and so on
411 myOperations.removeAll( theOp );
412 myOperations.append( theOp );
414 emit theOp->resumed( theOp );
419 * \brief Stops operation
420 * \param theOp - operation to be stopped
422 * Stops operation. This private method is called from abort() and commit() ones to perform
423 * common actions when operation is stopped
425 void SUIT_Study::stop( SUIT_Operation* theOp )
427 theOp->setState( SUIT_Operation::Waiting );
428 myOperations.removeAll( theOp );
430 // get last operation which can be resumed
431 SUIT_Operation* aResultOp = 0;
433 QListIterator<SUIT_Operation*> it (myOperations);
435 while( it.hasPrevious() )
437 SUIT_Operation* anOp = it.previous();
438 if ( anOp && anOp != theOp && blockingOperation( anOp ) == 0 )
445 theOp->stopOperation();
446 operationStopped( theOp );
447 emit theOp->stopped( theOp );
454 * \brief Get all started operations
455 * \return List of all started operations
457 const QList<SUIT_Operation*>& SUIT_Study::operations() const
463 * \brief Perform some actions when operation starting
465 void SUIT_Study::operationStarted( SUIT_Operation* op )
470 if ( op->testFlags( SUIT_Operation::Transaction ) )
471 op->openTransaction();
475 * \brief Perform some actions when operation aborted
477 void SUIT_Study::operationAborted( SUIT_Operation* op )
479 if ( op->testFlags( SUIT_Operation::Transaction ) )
480 op->abortTransaction();
484 * \brief Perform some actions when operation commited
486 void SUIT_Study::operationCommited( SUIT_Operation* op )
488 if ( op->testFlags( SUIT_Operation::Transaction ) )
489 op->commitTransaction( op->operationName() );
493 * \brief Perform some actions when operation stopped
495 void SUIT_Study::operationStopped( SUIT_Operation* )
500 * \brief Opens transaction for data modifications.
502 bool SUIT_Study::openTransaction()
508 * \brief Aborts transaction and all performed data modifications.
510 bool SUIT_Study::abortTransaction()
516 * \brief Commits transaction and all performed data modifications.
518 bool SUIT_Study::commitTransaction( const QString& )
524 * \brief Returns TRUE if transaction is opened.
526 bool SUIT_Study::hasTransaction() const
532 * \brief Restores the study state.
534 void SUIT_Study::restoreState(int /*savePoint*/)