Back to index

salome-gui  6.5.0
SUIT_Study.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "SUIT_Study.h"
00024 
00025 #include "SUIT_Desktop.h"
00026 #include "SUIT_Operation.h"
00027 #include "SUIT_DataObject.h"
00028 #include "SUIT_MessageBox.h"
00029 #include "SUIT_Application.h"
00030 
00036 SUIT_Study::SUIT_Study( SUIT_Application* app )
00037 : QObject(),
00038 myApp( app ),
00039 myName( "" ),
00040 myIsSaved( false ),
00041 myIsModified( false ),
00042 myBlockChangeState( false )
00043 {
00044   static int _id = 0;
00045 
00046   myId = ++_id;
00047 
00048   myRoot = new SUIT_DataObject();
00049 }
00050 
00052 SUIT_Study::~SUIT_Study()
00053 {
00054   delete myRoot;
00055   myRoot = 0;
00056 }
00057 
00061 int SUIT_Study::id() const
00062 {
00063   return myId;
00064 }
00065 
00069 SUIT_DataObject* SUIT_Study::root() const
00070 {
00071   return myRoot;
00072 }
00073 
00077 SUIT_Application* SUIT_Study::application() const
00078 {
00079   return myApp;
00080 }
00081 
00085 QString SUIT_Study::studyName() const
00086 {
00087   return myName;
00088 }
00089 
00093 SUIT_Operation* SUIT_Study::activeOperation() const
00094 {
00095   return myOperations.count() > 0 ? myOperations.last() : 0;
00096 }
00097 
00101 bool SUIT_Study::isSaved() const
00102 {
00103   return myIsSaved;
00104 }
00105 
00109 bool SUIT_Study::isModified() const
00110 {
00111   return myIsModified;
00112 }
00113 
00117 void SUIT_Study::closeDocument(bool /*permanently*/)
00118 {
00119 }
00120 
00125 bool SUIT_Study::createDocument( const QString& )
00126 {
00127   return true;
00128 }
00129 
00133 bool SUIT_Study::openDocument( const QString& fileName )
00134 {
00135   myName = fileName;
00136   myIsSaved = true;
00137   myIsModified = false;
00138 
00139   return true;
00140 }
00141 
00145 bool SUIT_Study::saveDocumentAs( const QString& fileName )
00146 {
00147   myName = fileName;
00148   myIsSaved = true;
00149   myIsModified = false;
00150 
00151   return true;
00152 }
00153 
00157 bool SUIT_Study::saveDocument()
00158 {
00159   return saveDocumentAs( myName );
00160 }
00161 
00165 void SUIT_Study::abortAllOperations()
00166 {
00167   myBlockChangeState = true;
00168   for ( Operations::iterator it = myOperations.begin(); it != myOperations.end(); ++it )
00169     (*it)->abort();
00170   myBlockChangeState = false;
00171   myOperations.clear();
00172 }
00173 
00177 void SUIT_Study::update()
00178 {
00179 }
00180 
00184 void SUIT_Study::sendChangesNotification()
00185 {
00186   emit studyModified( this );
00187 }
00188 
00192 void SUIT_Study::setIsSaved( const bool on )
00193 {
00194   myIsSaved = on;
00195 }
00196 
00200 void SUIT_Study::setIsModified( const bool on )
00201 {
00202   myIsModified = on;
00203 }
00204 
00208 void SUIT_Study::Modified()
00209 {
00210   setIsModified( true );
00211   sendChangesNotification();
00212 }
00213 
00217 void SUIT_Study::setRoot( SUIT_DataObject* obj )
00218 {
00219   if ( myRoot == obj )
00220     return;
00221 
00222   // This is necessary in order not to destroy the complete tree of objects
00223   if ( obj )
00224     obj->reparentChildren( myRoot );
00225 
00226   delete myRoot;
00227   myRoot = obj;
00228 }
00229 
00233 void SUIT_Study::setStudyName( const QString& name )
00234 {
00235   myName = name;
00236 }
00237 
00247 SUIT_Operation* SUIT_Study::blockingOperation( SUIT_Operation* theOp ) const
00248 {
00249   if( theOp->isGranted() )
00250     return 0;
00251 
00252   Operations tmpOps( myOperations );
00253   for ( Operations::const_iterator it = tmpOps.end(); it != tmpOps.begin(); )
00254   {
00255     it--;
00256     SUIT_Operation* anOp = *it;
00257     if ( anOp != 0 && anOp!= theOp && !anOp->isValid( theOp ) )
00258       return anOp;
00259   }
00260 
00261   return 0;
00262 }
00263 
00275 bool SUIT_Study::start( SUIT_Operation* theOp, const bool toCheck )
00276 {
00277   if ( !theOp || myOperations.contains( theOp ) )
00278     return false;
00279 
00280   theOp->setExecStatus( SUIT_Operation::Rejected );
00281   theOp->setStudy( this );
00282 
00283   if ( !theOp->isReadyToStart() )
00284     return false;
00285 
00286   if ( toCheck )
00287   {
00288     while ( SUIT_Operation* anOp = blockingOperation( theOp ) )
00289     {
00290       int anAnsw = SUIT_MessageBox::question( application()->desktop(),
00291                                               tr( "OPERATION_LAUNCH" ), tr( "PREVIOUS_NOT_FINISHED" ),
00292                                               tr( "CONTINUE" ), tr( "CANCEL" ) );
00293 
00294       if ( anAnsw == 1 )
00295         return false;
00296       else
00297         anOp->abort();
00298     }
00299   }
00300 
00301   SUIT_Operation* anOp = activeOperation();
00302   if ( anOp )
00303   {
00304     activeOperation()->suspendOperation();
00305     anOp->setState( SUIT_Operation::Suspended );
00306   }
00307 
00308   theOp->setState( SUIT_Operation::Running );
00309   myOperations.append( theOp );
00310  
00311   emit theOp->started( theOp );
00312   operationStarted( theOp );
00313   theOp->startOperation();
00314 
00315   return true;
00316 }
00317 
00326 bool SUIT_Study::abort( SUIT_Operation* theOp )
00327 {
00328   if ( !theOp || !myOperations.contains( theOp ) )
00329     return false;
00330 
00331   theOp->setExecStatus( SUIT_Operation::Rejected );
00332 
00333   theOp->abortOperation();
00334   operationAborted( theOp );
00335   emit theOp->aborted( theOp );
00336 
00337   stop( theOp );
00338 
00339   return true;
00340 }
00341 
00350 bool SUIT_Study::commit( SUIT_Operation* theOp )
00351 {
00352   if ( !theOp || !myOperations.contains( theOp ) )
00353     return false;
00354 
00355   theOp->setExecStatus( SUIT_Operation::Accepted );
00356 
00357   theOp->commitOperation();
00358   operationCommited( theOp );
00359   emit theOp->committed( theOp );
00360 
00361   stop( theOp );
00362 
00363   emit studyModified( this );
00364 
00365   return true;
00366 }
00367 
00376 bool SUIT_Study::suspend( SUIT_Operation* theOp )
00377 {
00378   if ( !theOp || !myOperations.contains( theOp ) || theOp->state() == SUIT_Operation::Suspended )
00379     return false;
00380 
00381   theOp->setState( SUIT_Operation::Suspended );
00382   theOp->suspendOperation();
00383   emit theOp->suspended( theOp );
00384   return true;
00385 }
00386 
00387 
00395 bool SUIT_Study::resume( SUIT_Operation* theOp )
00396 {
00397   if ( !theOp || !myOperations.contains( theOp ) ||
00398        theOp->state() == SUIT_Operation::Running ||
00399        blockingOperation( theOp ) != 0 )
00400     return false;
00401 
00402   if ( myOperations.count() > 0 )
00403     suspend( myOperations.last() );
00404 
00405   theOp->setState( SUIT_Operation::Running );
00406   theOp->resumeOperation();
00407 
00408   // Move operation at the end of list in order to sort it in the order of activation.
00409   // As result active operation is a last operation of list, operation which was active
00410   // before currently active operation is located before it and so on
00411   myOperations.removeAll( theOp );
00412   myOperations.append( theOp );
00413 
00414   emit theOp->resumed( theOp );
00415   return true;
00416 }
00417 
00425 void SUIT_Study::stop( SUIT_Operation* theOp )
00426 {
00427   theOp->setState( SUIT_Operation::Waiting );
00428   myOperations.removeAll( theOp );
00429 
00430   // get last operation which can be resumed
00431   SUIT_Operation* aResultOp = 0;
00432 
00433   QListIterator<SUIT_Operation*> it (myOperations);
00434   it.toBack();
00435   while( it.hasPrevious() )
00436   {
00437     SUIT_Operation* anOp = it.previous();
00438     if ( anOp && anOp != theOp && blockingOperation( anOp ) == 0 )
00439     {
00440       aResultOp = anOp;
00441       break;
00442     }
00443   }
00444 
00445   theOp->stopOperation();
00446   operationStopped( theOp );
00447   emit theOp->stopped( theOp );
00448 
00449   if ( aResultOp )
00450     resume( aResultOp );
00451 }
00452 
00457 const QList<SUIT_Operation*>& SUIT_Study::operations() const
00458 {
00459   return myOperations;
00460 }
00461 
00465 void SUIT_Study::operationStarted( SUIT_Operation* op )
00466 {
00467   if ( !op )
00468     return;
00469 
00470   if ( op->testFlags( SUIT_Operation::Transaction ) )
00471     op->openTransaction();
00472 }
00473 
00477 void SUIT_Study::operationAborted( SUIT_Operation* op )
00478 {
00479   if ( op->testFlags( SUIT_Operation::Transaction ) )
00480     op->abortTransaction();
00481 }
00482 
00486 void SUIT_Study::operationCommited( SUIT_Operation* op )
00487 {
00488   if ( op->testFlags( SUIT_Operation::Transaction ) )
00489     op->commitTransaction( op->operationName() );
00490 }
00491 
00495 void SUIT_Study::operationStopped( SUIT_Operation* )
00496 {
00497 }
00498 
00502 bool SUIT_Study::openTransaction()
00503 {
00504   return true;
00505 }
00506 
00510 bool SUIT_Study::abortTransaction()
00511 {
00512   return true;
00513 }
00514 
00518 bool SUIT_Study::commitTransaction( const QString& )
00519 {
00520   return true;
00521 }
00522 
00526 bool SUIT_Study::hasTransaction() const
00527 {
00528   return false;
00529 }
00530 
00534 void SUIT_Study::restoreState(int /*savePoint*/)
00535 {
00536 }