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 while ( !myOperations.isEmpty() ) {
169 Operations::iterator it = myOperations.begin();
172 myBlockChangeState = false;
173 myOperations.clear();
177 Update study. NOT IMPLEMENTED HERE.
179 void SUIT_Study::update()
186 void SUIT_Study::sendChangesNotification()
188 emit studyModified( this );
192 Set study saved to \a on.
194 void SUIT_Study::setIsSaved( const bool on )
200 Set study modified to \a on.
202 void SUIT_Study::setIsModified( const bool on )
208 Set study modified to \a on.
210 void SUIT_Study::Modified()
212 setIsModified( true );
213 sendChangesNotification();
219 void SUIT_Study::setRoot( SUIT_DataObject* obj )
224 // This is necessary in order not to destroy the complete tree of objects
226 obj->reparentChildren( myRoot );
235 void SUIT_Study::setStudyName( const QString& name )
241 * \brief Verifies whether operation can be activated above already started ones
242 * \param theOp - operation to be checked
243 * \return NULL if operation can be activated, pointer to operation which denies
244 * starting tested operation
246 * Verifies whether operation can be activated above already started ones. This method
247 * is called from SUIT_Study::start() and SUIT_Study::resume() methods.
249 SUIT_Operation* SUIT_Study::blockingOperation( SUIT_Operation* theOp ) const
251 if( theOp->isGranted() )
254 Operations tmpOps( myOperations );
255 for ( Operations::const_iterator it = tmpOps.end(); it != tmpOps.begin(); )
258 SUIT_Operation* anOp = *it;
259 if ( anOp != 0 && anOp!= theOp && !anOp->isValid( theOp ) )
267 * \brief Starts operation
268 * \param theOp - operation to be started
269 * \param toCheck - if parameters is equal TRUE then checking performed whether
270 * all already started operations allow to start this operation above them (default
272 * \return TRUE if operation is started, FALSE otherwise
274 * Verifies whether theOp operation can be started above already started ones (if toCheck
275 * parameter is equal TRUE) and starts it
277 bool SUIT_Study::start( SUIT_Operation* theOp, const bool toCheck )
279 if ( !theOp || myOperations.contains( theOp ) )
282 theOp->setExecStatus( SUIT_Operation::Rejected );
283 theOp->setStudy( this );
285 if ( !theOp->isReadyToStart() )
290 while ( SUIT_Operation* anOp = blockingOperation( theOp ) )
292 int anAnsw = SUIT_MessageBox::question( application()->desktop(),
293 tr( "OPERATION_LAUNCH" ), tr( "PREVIOUS_NOT_FINISHED" ),
294 tr( "CONTINUE" ), tr( "CANCEL" ) );
303 SUIT_Operation* anOp = activeOperation();
306 activeOperation()->suspendOperation();
307 anOp->setState( SUIT_Operation::Suspended );
310 theOp->setState( SUIT_Operation::Running );
311 myOperations.append( theOp );
313 emit theOp->started( theOp );
314 operationStarted( theOp );
315 theOp->startOperation();
321 * \brief Aborts operation
322 * \param theOp - operation to be aborted
323 * \return TRUE if operation is aborted successfully
325 * Verifies whether operation already started and aborts it in this case (sets execution
326 * status to Rejected and stops operation)
328 bool SUIT_Study::abort( SUIT_Operation* theOp )
330 if ( !theOp || !myOperations.contains( theOp ) )
333 theOp->setExecStatus( SUIT_Operation::Rejected );
335 theOp->abortOperation();
336 operationAborted( theOp );
337 emit theOp->aborted( theOp );
345 * \brief Commits operation
346 * \param theOp - operation to be committed
347 * \return TRUE if operation is committed successfully
349 * Verifies whether operation already started and commits it in this case (sets execution
350 * status to Accepted and stops operation)
352 bool SUIT_Study::commit( SUIT_Operation* theOp )
354 if ( !theOp || !myOperations.contains( theOp ) )
357 theOp->setExecStatus( SUIT_Operation::Accepted );
359 theOp->commitOperation();
360 operationCommited( theOp );
361 emit theOp->committed( theOp );
365 emit studyModified( this );
371 * \brief Commits operation
372 * \param theOp - operation to be committed
373 * \return TRUE if operation is suspended successfully
375 * Verifies whether operation already started and suspends it in this case. Operations
376 * ususlly are suspended to start other one above them.
378 bool SUIT_Study::suspend( SUIT_Operation* theOp )
380 if ( !theOp || !myOperations.contains( theOp ) || theOp->state() == SUIT_Operation::Suspended )
383 theOp->setState( SUIT_Operation::Suspended );
384 theOp->suspendOperation();
385 emit theOp->suspended( theOp );
391 * \brief Resumes operation
392 * \param theOp - operation to be resumed
393 * \return TRUE if operation is aborted successfully
395 * Verifies whether operation already started but suspended and resumesit in this case.
397 bool SUIT_Study::resume( SUIT_Operation* theOp )
399 if ( !theOp || !myOperations.contains( theOp ) ||
400 theOp->state() == SUIT_Operation::Running ||
401 blockingOperation( theOp ) != 0 )
404 if ( myOperations.count() > 0 )
405 suspend( myOperations.last() );
407 theOp->setState( SUIT_Operation::Running );
408 theOp->resumeOperation();
410 // Move operation at the end of list in order to sort it in the order of activation.
411 // As result active operation is a last operation of list, operation which was active
412 // before currently active operation is located before it and so on
413 myOperations.removeAll( theOp );
414 myOperations.append( theOp );
416 emit theOp->resumed( theOp );
421 * \brief Stops operation
422 * \param theOp - operation to be stopped
424 * Stops operation. This private method is called from abort() and commit() ones to perform
425 * common actions when operation is stopped
427 void SUIT_Study::stop( SUIT_Operation* theOp )
429 theOp->setState( SUIT_Operation::Waiting );
430 myOperations.removeAll( theOp );
432 // get last operation which can be resumed
433 SUIT_Operation* aResultOp = 0;
435 QListIterator<SUIT_Operation*> it (myOperations);
437 while( it.hasPrevious() )
439 SUIT_Operation* anOp = it.previous();
440 if ( anOp && anOp != theOp && blockingOperation( anOp ) == 0 )
447 theOp->stopOperation();
448 operationStopped( theOp );
449 emit theOp->stopped( theOp );
456 * \brief Get all started operations
457 * \return List of all started operations
459 const QList<SUIT_Operation*>& SUIT_Study::operations() const
465 * \brief Perform some actions when operation starting
467 void SUIT_Study::operationStarted( SUIT_Operation* op )
472 if ( op->testFlags( SUIT_Operation::Transaction ) )
473 op->openTransaction();
477 * \brief Perform some actions when operation aborted
479 void SUIT_Study::operationAborted( SUIT_Operation* op )
481 if ( op->testFlags( SUIT_Operation::Transaction ) )
482 op->abortTransaction();
486 * \brief Perform some actions when operation commited
488 void SUIT_Study::operationCommited( SUIT_Operation* op )
490 if ( op->testFlags( SUIT_Operation::Transaction ) )
491 op->commitTransaction( op->operationName() );
495 * \brief Perform some actions when operation stopped
497 void SUIT_Study::operationStopped( SUIT_Operation* )
502 * \brief Opens transaction for data modifications.
504 bool SUIT_Study::openTransaction()
510 * \brief Aborts transaction and all performed data modifications.
512 bool SUIT_Study::abortTransaction()
518 * \brief Commits transaction and all performed data modifications.
520 bool SUIT_Study::commitTransaction( const QString& )
526 * \brief Returns TRUE if transaction is opened.
528 bool SUIT_Study::hasTransaction() const
534 * \brief Restores the study state.
536 void SUIT_Study::restoreState(int /*savePoint*/)