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)
120 void SUIT_Study::createDocument()
122 /*! Custom document initialization to be performed \n
123 * within onNewDoc() handler can be put here
128 * Open document. Sets file name. return true.
130 bool SUIT_Study::openDocument( const QString& fileName )
134 myIsModified = false;
140 * Save document as \a fileName. Set file name.
142 bool SUIT_Study::saveDocumentAs( const QString& fileName )
146 myIsModified = false;
152 *\retval TRUE - if document saved successful, else FALSE.
154 bool SUIT_Study::saveDocument()
156 return saveDocumentAs( myName );
160 *Abort all operations.
162 void SUIT_Study::abortAllOperations()
164 myBlockChangeState = true;
165 for( SUIT_Operation* op = myOperations.first(); op; op = myOperations.next() )
167 myBlockChangeState = false;
168 myOperations.clear();
172 Update study. NOT IMPLEMENTED HERE.
174 void SUIT_Study::update()
181 void SUIT_Study::sendChangesNotification()
183 emit studyModified( this );
187 Set study saved to \a on.
189 void SUIT_Study::setIsSaved( const bool on )
195 Set study modified to \a on.
197 void SUIT_Study::setIsModified( const bool on )
205 void SUIT_Study::setRoot( SUIT_DataObject* obj )
210 // This is necessary in order not to destroy the complete tree of objects
212 obj->reparentChildren( myRoot );
221 void SUIT_Study::setStudyName( const QString& name )
227 * \brief Verifies whether operation can be activated above already started ones
228 * \param theOp - operation to be checked
229 * \return NULL if operation can be activated, pointer to operation which denies
230 * starting tested operation
232 * Verifies whether operation can be activated above already started ones. This method
233 * is called from SUIT_Study::start() and SUIT_Study::resume() methods.
235 SUIT_Operation* SUIT_Study::blockingOperation( SUIT_Operation* theOp ) const
237 if( theOp->isGranted() )
240 Operations tmpOps( myOperations );
241 SUIT_Operation* anOp = 0;
242 for ( anOp = tmpOps.last(); anOp; anOp = tmpOps.prev() )
244 if ( anOp != 0 && anOp!= theOp && !anOp->isValid( theOp ) )
252 * \brief Starts operation
253 * \param theOp - operation to be started
254 * \param toCheck - if parameters is equal TRUE then checking performed whether
255 * all already started operations allow to start this operation above them (default
257 * \return TRUE if operation is started, FALSE otherwise
259 * Verifies whether theOp operation can be started above already started ones (if toCheck
260 * parameter is equal TRUE) and starts it
262 bool SUIT_Study::start( SUIT_Operation* theOp, const bool toCheck )
264 if ( !theOp || myOperations.find( theOp ) >= 0 )
267 theOp->setExecStatus( SUIT_Operation::Rejected );
268 theOp->setStudy( this );
270 if ( !theOp->isReadyToStart() )
275 while( SUIT_Operation* anOp = blockingOperation( theOp ) )
277 int anAnsw = SUIT_MessageBox::warn2( application()->desktop(),
278 tr( "OPERATION_LAUNCH" ), tr( "PREVIOUS_NOT_FINISHED" ),
279 tr( "CONTINUE" ), tr( "CANCEL" ), 0, 1, 1 );
288 SUIT_Operation* anOp = activeOperation();
291 activeOperation()->suspendOperation();
292 anOp->setState( SUIT_Operation::Suspended );
295 theOp->setState( SUIT_Operation::Running );
296 myOperations.append( theOp );
298 emit theOp->started( theOp );
299 operationStarted( theOp );
300 theOp->startOperation();
306 * \brief Aborts operation
307 * \param theOp - operation to be aborted
308 * \return TRUE if operation is aborted successfully
310 * Verifies whether operation already started and aborts it in this case (sets execution
311 * status to Rejected and stops operation)
313 bool SUIT_Study::abort( SUIT_Operation* theOp )
315 if ( !theOp || myOperations.find( theOp ) == -1 )
318 theOp->setExecStatus( SUIT_Operation::Rejected );
320 theOp->abortOperation();
321 operationAborted( theOp );
322 emit theOp->aborted( theOp );
330 * \brief Commits operation
331 * \param theOp - operation to be committed
332 * \return TRUE if operation is committed successfully
334 * Verifies whether operation already started and commits it in this case (sets execution
335 * status to Accepted and stops operation)
337 bool SUIT_Study::commit( SUIT_Operation* theOp )
339 if ( !theOp || myOperations.find( theOp ) == -1 )
342 theOp->setExecStatus( SUIT_Operation::Accepted );
344 theOp->commitOperation();
345 operationCommited( theOp );
346 emit theOp->committed( theOp );
350 emit studyModified( this );
356 * \brief Commits operation
357 * \param theOp - operation to be committed
358 * \return TRUE if operation is suspended successfully
360 * Verifies whether operation already started and suspends it in this case. Operations
361 * ususlly are suspended to start other one above them.
363 bool SUIT_Study::suspend( SUIT_Operation* theOp )
365 if ( !theOp || myOperations.find( theOp ) == -1 || theOp->state() == SUIT_Operation::Suspended )
368 theOp->setState( SUIT_Operation::Suspended );
369 theOp->suspendOperation();
370 emit theOp->suspended( theOp );
376 * \brief Resumes operation
377 * \param theOp - operation to be resumed
378 * \return TRUE if operation is aborted successfully
380 * Verifies whether operation already started but suspended and resumesit in this case.
382 bool SUIT_Study::resume( SUIT_Operation* theOp )
384 if ( !theOp || myOperations.find( theOp ) == -1 ||
385 theOp->state() == SUIT_Operation::Running ||
386 blockingOperation( theOp ) != 0 )
389 if ( myOperations.count() > 0 )
390 suspend( myOperations.last() );
392 theOp->setState( SUIT_Operation::Running );
393 theOp->resumeOperation();
395 // Move operation at the end of list in order to sort it in the order of activation.
396 // As result active operation is a last operation of list, operation which was active
397 // before currently active operation is located before it and so on
398 myOperations.remove( theOp );
399 myOperations.append( theOp );
401 emit theOp->resumed( theOp );
406 * \brief Stops operation
407 * \param theOp - operation to be stopped
409 * Stops operation. This private method is called from abort() and commit() ones to perform
410 * common actions when operation is stopped
412 void SUIT_Study::stop( SUIT_Operation* theOp )
414 theOp->setState( SUIT_Operation::Waiting );
415 myOperations.remove( theOp );
417 // get last operation which can be resumed
418 SUIT_Operation* anOp, *aResultOp = 0;
419 for ( anOp = myOperations.last(); anOp; anOp = myOperations.prev() )
421 if ( anOp && anOp != theOp && blockingOperation( anOp ) == 0 )
428 theOp->stopOperation();
429 operationStopped( theOp );
430 emit theOp->stopped( theOp );
437 * \brief Get all started operations
438 * \return List of all started operations
440 const QPtrList<SUIT_Operation>& SUIT_Study::operations() const
446 * \brief Perform some actions when operation starting
448 void SUIT_Study::operationStarted( SUIT_Operation* op )
453 if ( op->testFlags( SUIT_Operation::Transaction ) )
454 op->openTransaction();
458 * \brief Perform some actions when operation aborted
460 void SUIT_Study::operationAborted( SUIT_Operation* op )
462 if ( op->testFlags( SUIT_Operation::Transaction ) )
463 op->abortTransaction();
467 * \brief Perform some actions when operation commited
469 void SUIT_Study::operationCommited( SUIT_Operation* op )
471 if ( op->testFlags( SUIT_Operation::Transaction ) )
472 op->commitTransaction( op->operationName() );
476 * \brief Perform some actions when operation stopped
478 void SUIT_Study::operationStopped( SUIT_Operation* )
483 * \brief Opens transaction for data modifications.
485 bool SUIT_Study::openTransaction()
491 * \brief Aborts transaction and all performed data modifications.
493 bool SUIT_Study::abortTransaction()
499 * \brief Commits transaction and all performed data modifications.
501 bool SUIT_Study::commitTransaction( const QString& )
507 * \brief Returns TRUE if transaction is opened.
509 bool SUIT_Study::hasTransaction() const
515 * \brief Stores the study state
517 int SUIT_Study::storeState()
523 * \brief Restores the study state
525 void SUIT_Study::restoreState(int savePoint)