1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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.
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
17 // See http://www.salome-platform.org/
19 #include "SUIT_Study.h"
21 #include "SUIT_Desktop.h"
22 #include "SUIT_Operation.h"
23 #include "SUIT_DataObject.h"
24 #include "SUIT_MessageBox.h"
25 #include "SUIT_Application.h"
26 #include <qvaluevector.h>
29 * Support study management. Object management. Operation management.
33 SUIT_Study::SUIT_Study( SUIT_Application* app )
37 myIsModified( false ),
39 myBlockChangeState( false )
45 myRoot = new SUIT_DataObject();
46 myOperations.setAutoDelete( false );
47 myOperations.setAutoDelete( false );
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.getLast() : 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 void SUIT_Study::createDocument()
129 * Open document. Sets file name. return true.
131 bool SUIT_Study::openDocument( const QString& fileName )
135 myIsModified = false;
141 * Save document as \a fileName. Set file name.
143 bool SUIT_Study::saveDocumentAs( const QString& fileName )
147 myIsModified = false;
153 *\retval TRUE - if document saved successful, else FALSE.
155 bool SUIT_Study::saveDocument()
157 return saveDocumentAs( myName );
161 *Abort all operations.
163 void SUIT_Study::abortAllOperations()
165 myBlockChangeState = true;
166 for( SUIT_Operation* op = myOperations.first(); op; op = myOperations.next() )
168 myBlockChangeState = false;
169 myOperations.clear();
173 Update study. NOT IMPLEMENTED HERE.
175 void SUIT_Study::update()
182 void SUIT_Study::sendChangesNotification()
184 emit studyModified( this );
188 Set study saved to \a on.
190 void SUIT_Study::setIsSaved( const bool on )
196 Set study modified to \a on.
198 void SUIT_Study::setIsModified( const bool on )
206 void SUIT_Study::setRoot( SUIT_DataObject* obj )
211 // This is necessary in order not to destroy the complete tree of objects
213 obj->reparentChildren( myRoot );
222 void SUIT_Study::setStudyName( const QString& name )
228 * \brief Verifies whether operation can be activated above already started ones
229 * \param theOp - operation to be checked
230 * \return NULL if operation can be activated, pointer to operation which denies
231 * starting tested operation
233 * Verifies whether operation can be activated above already started ones. This method
234 * is called from SUIT_Study::start() and SUIT_Study::resume() methods.
236 SUIT_Operation* SUIT_Study::blockingOperation( SUIT_Operation* theOp ) const
238 if( theOp->isGranted() )
241 Operations tmpOps( myOperations );
242 SUIT_Operation* anOp = 0;
243 for ( anOp = tmpOps.last(); anOp; anOp = tmpOps.prev() )
245 if ( anOp != 0 && anOp!= theOp && !anOp->isValid( theOp ) )
253 * \brief Starts operation
254 * \param theOp - operation to be started
255 * \param toCheck - if parameters is equal TRUE then checking performed whether
256 * all already started operations allow to start this operation above them (default
258 * \return TRUE if operation is started, FALSE otherwise
260 * Verifies whether theOp operation can be started above already started ones (if toCheck
261 * parameter is equal TRUE) and starts it
263 bool SUIT_Study::start( SUIT_Operation* theOp, const bool toCheck )
265 if ( !theOp || myOperations.find( theOp ) >= 0 )
268 theOp->setExecStatus( SUIT_Operation::Rejected );
269 theOp->setStudy( this );
271 if ( !theOp->isReadyToStart() )
276 while( SUIT_Operation* anOp = blockingOperation( theOp ) )
278 int anAnsw = SUIT_MessageBox::warn2( application()->desktop(),
279 tr( "OPERATION_LAUNCH" ), tr( "PREVIOUS_NOT_FINISHED" ),
280 tr( "CONTINUE" ), tr( "CANCEL" ), 0, 1, 1 );
289 SUIT_Operation* anOp = activeOperation();
292 activeOperation()->suspendOperation();
293 anOp->setState( SUIT_Operation::Suspended );
296 theOp->setState( SUIT_Operation::Running );
297 myOperations.append( theOp );
299 emit theOp->started( theOp );
300 operationStarted( theOp );
301 theOp->startOperation();
307 * \brief Aborts operation
308 * \param theOp - operation to be aborted
309 * \return TRUE if operation is aborted successfully
311 * Verifies whether operation already started and aborts it in this case (sets execution
312 * status to Rejected and stops operation)
314 bool SUIT_Study::abort( SUIT_Operation* theOp )
316 if ( !theOp || myOperations.find( theOp ) == -1 )
319 theOp->setExecStatus( SUIT_Operation::Rejected );
321 theOp->abortOperation();
322 operationAborted( theOp );
323 emit theOp->aborted( theOp );
331 * \brief Commits operation
332 * \param theOp - operation to be committed
333 * \return TRUE if operation is committed successfully
335 * Verifies whether operation already started and commits it in this case (sets execution
336 * status to Accepted and stops operation)
338 bool SUIT_Study::commit( SUIT_Operation* theOp )
340 if ( !theOp || myOperations.find( theOp ) == -1 )
343 theOp->setExecStatus( SUIT_Operation::Accepted );
345 theOp->commitOperation();
346 operationCommited( theOp );
347 emit theOp->committed( theOp );
351 emit studyModified( this );
357 * \brief Commits operation
358 * \param theOp - operation to be committed
359 * \return TRUE if operation is suspended successfully
361 * Verifies whether operation already started and suspends it in this case. Operations
362 * ususlly are suspended to start other one above them.
364 bool SUIT_Study::suspend( SUIT_Operation* theOp )
366 if ( !theOp || myOperations.find( theOp ) == -1 || theOp->state() == SUIT_Operation::Suspended )
369 theOp->setState( SUIT_Operation::Suspended );
370 theOp->suspendOperation();
371 emit theOp->suspended( theOp );
377 * \brief Resumes operation
378 * \param theOp - operation to be resumed
379 * \return TRUE if operation is aborted successfully
381 * Verifies whether operation already started but suspended and resumesit in this case.
383 bool SUIT_Study::resume( SUIT_Operation* theOp )
385 if ( !theOp || myOperations.find( theOp ) == -1 ||
386 theOp->state() == SUIT_Operation::Running ||
387 blockingOperation( theOp ) != 0 )
390 if ( myOperations.count() > 0 )
391 suspend( myOperations.last() );
393 theOp->setState( SUIT_Operation::Running );
394 theOp->resumeOperation();
396 // Move operation at the end of list in order to sort it in the order of activation.
397 // As result active operation is a last operation of list, operation which was active
398 // before currently active operation is located before it and so on
399 myOperations.remove( theOp );
400 myOperations.append( theOp );
402 emit theOp->resumed( theOp );
407 * \brief Stops operation
408 * \param theOp - operation to be stopped
410 * Stops operation. This private method is called from abort() and commit() ones to perform
411 * common actions when operation is stopped
413 void SUIT_Study::stop( SUIT_Operation* theOp )
415 theOp->setState( SUIT_Operation::Waiting );
416 myOperations.remove( theOp );
418 // get last operation which can be resumed
419 SUIT_Operation* anOp, *aResultOp = 0;
420 for ( anOp = myOperations.last(); anOp; anOp = myOperations.prev() )
422 if ( anOp && anOp != theOp && blockingOperation( anOp ) == 0 )
429 theOp->stopOperation();
430 operationStopped( theOp );
431 emit theOp->stopped( theOp );
438 * \brief Get all started operations
439 * \return List of all started operations
441 const QPtrList<SUIT_Operation>& SUIT_Study::operations() const
447 * \brief Perform some actions when operation starting
449 void SUIT_Study::operationStarted( SUIT_Operation* op )
454 if ( op->testFlags( SUIT_Operation::Transaction ) )
455 op->openTransaction();
459 * \brief Perform some actions when operation aborted
461 void SUIT_Study::operationAborted( SUIT_Operation* op )
463 if ( op->testFlags( SUIT_Operation::Transaction ) )
464 op->abortTransaction();
468 * \brief Perform some actions when operation commited
470 void SUIT_Study::operationCommited( SUIT_Operation* op )
472 if ( op->testFlags( SUIT_Operation::Transaction ) )
473 op->commitTransaction( op->operationName() );
477 * \brief Perform some actions when operation stopped
479 void SUIT_Study::operationStopped( SUIT_Operation* )
484 * \brief Opens transaction for data modifications.
486 bool SUIT_Study::openTransaction()
492 * \brief Aborts transaction and all performed data modifications.
494 bool SUIT_Study::abortTransaction()
500 * \brief Commits transaction and all performed data modifications.
502 bool SUIT_Study::commitTransaction( const QString& )
508 * \brief Returns TRUE if transaction is opened.
510 bool SUIT_Study::hasTransaction() const
516 * \brief Stores the study state
518 int SUIT_Study::storeState()
524 * \brief Restores the study state
526 void SUIT_Study::restoreState(int savePoint)