1 // Copyright (C) 2007-2008 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
22 #include "SUIT_Study.h"
24 #include "SUIT_Desktop.h"
25 #include "SUIT_Operation.h"
26 #include "SUIT_DataObject.h"
27 #include "SUIT_MessageBox.h"
28 #include "SUIT_Application.h"
31 * Support study management. Object management. Operation management.
35 SUIT_Study::SUIT_Study( SUIT_Application* app )
40 myIsModified( false ),
41 myBlockChangeState( false )
47 myRoot = new SUIT_DataObject();
51 SUIT_Study::~SUIT_Study()
60 int SUIT_Study::id() const
66 *\retval root data object.
68 SUIT_DataObject* SUIT_Study::root() const
76 SUIT_Application* SUIT_Study::application() const
84 QString SUIT_Study::studyName() const
90 *\retval active operation.
92 SUIT_Operation* SUIT_Study::activeOperation() const
94 return myOperations.count() > 0 ? myOperations.last() : 0;
98 *\retval TRUE - if study saved, else FALSE.
100 bool SUIT_Study::isSaved() const
106 *\retval TRUE - if study modified, else FALSE.
108 bool SUIT_Study::isModified() const
114 *Close document. NOT IMPLEMENTED.
116 void SUIT_Study::closeDocument(bool /*permanently*/)
121 Custom document initialization to be performed \n
122 within onNewDoc() handler can be put here
124 bool SUIT_Study::createDocument( const QString& )
130 * Open document. Sets file name. return true.
132 bool SUIT_Study::openDocument( const QString& fileName )
136 myIsModified = false;
142 * Save document as \a fileName. Set file name.
144 bool SUIT_Study::saveDocumentAs( const QString& fileName )
148 myIsModified = false;
154 *\retval TRUE - if document saved successful, else FALSE.
156 bool SUIT_Study::saveDocument()
158 return saveDocumentAs( myName );
162 *Abort all operations.
164 void SUIT_Study::abortAllOperations()
166 myBlockChangeState = true;
167 for ( Operations::iterator it = myOperations.begin(); it != myOperations.end(); ++it )
169 myBlockChangeState = false;
170 myOperations.clear();
174 Update study. NOT IMPLEMENTED HERE.
176 void SUIT_Study::update()
183 void SUIT_Study::sendChangesNotification()
185 emit studyModified( this );
189 Set study saved to \a on.
191 void SUIT_Study::setIsSaved( const bool on )
197 Set study modified to \a on.
199 void SUIT_Study::setIsModified( const bool on )
205 Set study modified to \a on.
207 void SUIT_Study::Modified()
209 setIsModified( true );
210 sendChangesNotification();
216 void SUIT_Study::setRoot( SUIT_DataObject* obj )
221 // This is necessary in order not to destroy the complete tree of objects
223 obj->reparentChildren( myRoot );
232 void SUIT_Study::setStudyName( const QString& name )
238 * \brief Verifies whether operation can be activated above already started ones
239 * \param theOp - operation to be checked
240 * \return NULL if operation can be activated, pointer to operation which denies
241 * starting tested operation
243 * Verifies whether operation can be activated above already started ones. This method
244 * is called from SUIT_Study::start() and SUIT_Study::resume() methods.
246 SUIT_Operation* SUIT_Study::blockingOperation( SUIT_Operation* theOp ) const
248 if( theOp->isGranted() )
251 Operations tmpOps( myOperations );
252 for ( Operations::const_iterator it = tmpOps.end(); it != tmpOps.begin(); )
255 SUIT_Operation* anOp = *it;
256 if ( anOp != 0 && anOp!= theOp && !anOp->isValid( theOp ) )
264 * \brief Starts operation
265 * \param theOp - operation to be started
266 * \param toCheck - if parameters is equal TRUE then checking performed whether
267 * all already started operations allow to start this operation above them (default
269 * \return TRUE if operation is started, FALSE otherwise
271 * Verifies whether theOp operation can be started above already started ones (if toCheck
272 * parameter is equal TRUE) and starts it
274 bool SUIT_Study::start( SUIT_Operation* theOp, const bool toCheck )
276 if ( !theOp || myOperations.contains( theOp ) )
279 theOp->setExecStatus( SUIT_Operation::Rejected );
280 theOp->setStudy( this );
282 if ( !theOp->isReadyToStart() )
287 while ( SUIT_Operation* anOp = blockingOperation( theOp ) )
289 int anAnsw = SUIT_MessageBox::question( application()->desktop(),
290 tr( "OPERATION_LAUNCH" ), tr( "PREVIOUS_NOT_FINISHED" ),
291 tr( "CONTINUE" ), tr( "CANCEL" ) );
300 SUIT_Operation* anOp = activeOperation();
303 activeOperation()->suspendOperation();
304 anOp->setState( SUIT_Operation::Suspended );
307 theOp->setState( SUIT_Operation::Running );
308 myOperations.append( theOp );
310 emit theOp->started( theOp );
311 operationStarted( theOp );
312 theOp->startOperation();
318 * \brief Aborts operation
319 * \param theOp - operation to be aborted
320 * \return TRUE if operation is aborted successfully
322 * Verifies whether operation already started and aborts it in this case (sets execution
323 * status to Rejected and stops operation)
325 bool SUIT_Study::abort( SUIT_Operation* theOp )
327 if ( !theOp || !myOperations.contains( theOp ) )
330 theOp->setExecStatus( SUIT_Operation::Rejected );
332 theOp->abortOperation();
333 operationAborted( theOp );
334 emit theOp->aborted( theOp );
342 * \brief Commits operation
343 * \param theOp - operation to be committed
344 * \return TRUE if operation is committed successfully
346 * Verifies whether operation already started and commits it in this case (sets execution
347 * status to Accepted and stops operation)
349 bool SUIT_Study::commit( SUIT_Operation* theOp )
351 if ( !theOp || !myOperations.contains( theOp ) )
354 theOp->setExecStatus( SUIT_Operation::Accepted );
356 theOp->commitOperation();
357 operationCommited( theOp );
358 emit theOp->committed( theOp );
362 emit studyModified( this );
368 * \brief Commits operation
369 * \param theOp - operation to be committed
370 * \return TRUE if operation is suspended successfully
372 * Verifies whether operation already started and suspends it in this case. Operations
373 * ususlly are suspended to start other one above them.
375 bool SUIT_Study::suspend( SUIT_Operation* theOp )
377 if ( !theOp || !myOperations.contains( theOp ) || theOp->state() == SUIT_Operation::Suspended )
380 theOp->setState( SUIT_Operation::Suspended );
381 theOp->suspendOperation();
382 emit theOp->suspended( theOp );
388 * \brief Resumes operation
389 * \param theOp - operation to be resumed
390 * \return TRUE if operation is aborted successfully
392 * Verifies whether operation already started but suspended and resumesit in this case.
394 bool SUIT_Study::resume( SUIT_Operation* theOp )
396 if ( !theOp || !myOperations.contains( theOp ) ||
397 theOp->state() == SUIT_Operation::Running ||
398 blockingOperation( theOp ) != 0 )
401 if ( myOperations.count() > 0 )
402 suspend( myOperations.last() );
404 theOp->setState( SUIT_Operation::Running );
405 theOp->resumeOperation();
407 // Move operation at the end of list in order to sort it in the order of activation.
408 // As result active operation is a last operation of list, operation which was active
409 // before currently active operation is located before it and so on
410 myOperations.removeAll( theOp );
411 myOperations.append( theOp );
413 emit theOp->resumed( theOp );
418 * \brief Stops operation
419 * \param theOp - operation to be stopped
421 * Stops operation. This private method is called from abort() and commit() ones to perform
422 * common actions when operation is stopped
424 void SUIT_Study::stop( SUIT_Operation* theOp )
426 theOp->setState( SUIT_Operation::Waiting );
427 myOperations.removeAll( theOp );
429 // get last operation which can be resumed
430 SUIT_Operation* aResultOp = 0;
432 QListIterator<SUIT_Operation*> it (myOperations);
434 while( it.hasPrevious() )
436 SUIT_Operation* anOp = it.previous();
437 if ( anOp && anOp != theOp && blockingOperation( anOp ) == 0 )
444 theOp->stopOperation();
445 operationStopped( theOp );
446 emit theOp->stopped( theOp );
453 * \brief Get all started operations
454 * \return List of all started operations
456 const QList<SUIT_Operation*>& SUIT_Study::operations() const
462 * \brief Perform some actions when operation starting
464 void SUIT_Study::operationStarted( SUIT_Operation* op )
469 if ( op->testFlags( SUIT_Operation::Transaction ) )
470 op->openTransaction();
474 * \brief Perform some actions when operation aborted
476 void SUIT_Study::operationAborted( SUIT_Operation* op )
478 if ( op->testFlags( SUIT_Operation::Transaction ) )
479 op->abortTransaction();
483 * \brief Perform some actions when operation commited
485 void SUIT_Study::operationCommited( SUIT_Operation* op )
487 if ( op->testFlags( SUIT_Operation::Transaction ) )
488 op->commitTransaction( op->operationName() );
492 * \brief Perform some actions when operation stopped
494 void SUIT_Study::operationStopped( SUIT_Operation* )
499 * \brief Opens transaction for data modifications.
501 bool SUIT_Study::openTransaction()
507 * \brief Aborts transaction and all performed data modifications.
509 bool SUIT_Study::abortTransaction()
515 * \brief Commits transaction and all performed data modifications.
517 bool SUIT_Study::commitTransaction( const QString& )
523 * \brief Returns TRUE if transaction is opened.
525 bool SUIT_Study::hasTransaction() const
531 * \brief Restores the study state.
533 void SUIT_Study::restoreState(int /*savePoint*/)