Back to index

salome-gui  6.5.0
CAF_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 "CAF_Study.h"
00024 
00025 #include "CAF_Tools.h"
00026 #include "CAF_Application.h"
00027 
00028 #include <Basics_OCCTVersion.hxx>
00029 
00030 #include <SUIT_Desktop.h>
00031 #include <SUIT_MessageBox.h>
00032 #include <SUIT_Application.h>
00033 
00034 #include <QDir>
00035 
00036 #include <TDF_Delta.hxx>
00037 #include <TDF_ListIteratorOfDeltaList.hxx>
00038 #include <TDocStd_Application.hxx>
00039 
00040 #include <Standard_Failure.hxx>
00041 #include <Standard_ErrorHandler.hxx>
00042 
00055 CAF_Study::CAF_Study(SUIT_Application* theApp)
00056 : SUIT_Study( theApp ),
00057   myModifiedCnt( 0 )
00058 {
00059 }
00060 
00066 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
00067 : SUIT_Study( theApp ),
00068   myStdDoc( aStdDoc ),
00069   myModifiedCnt( 0 )
00070 {
00071 }
00072 
00076 CAF_Study::~CAF_Study()
00077 {
00078 }
00079 
00084 Handle(TDocStd_Document) CAF_Study::stdDoc() const
00085 {
00086   return myStdDoc;
00087 }
00088 
00093 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
00094 {
00095   myStdDoc = aStdDoc;
00096 }
00097 
00103 bool CAF_Study::createDocument( const QString& doc )
00104 {
00105   bool res = SUIT_Study::createDocument( doc );
00106 
00107   CAF_Application* app = cafApplication();
00108   if ( res && app && !app->stdApp().IsNull() )
00109   {
00110     try {
00111 #if OCC_VERSION_LARGE > 0x06010000
00112       OCC_CATCH_SIGNALS;
00113 #endif
00114       TColStd_SequenceOfExtendedString formats;
00115       app->stdApp()->Formats( formats );
00116       if ( !formats.IsEmpty() )
00117         app->stdApp()->NewDocument( formats.First(), myStdDoc );
00118     }
00119     catch ( Standard_Failure ) {
00120       res = false;
00121     }
00122   }
00123   return res;
00124 }
00125 
00130 void CAF_Study::closeDocument( bool permanently )
00131 {
00132   Handle(TDocStd_Application) app = stdApp();
00133   if ( !app.IsNull() && !stdDoc().IsNull() )
00134     app->Close( stdDoc() );
00135 
00136   SUIT_Study::closeDocument( permanently );
00137 }
00138 
00144 bool CAF_Study::openDocument( const QString& fname )
00145 {
00146   Handle(TDocStd_Application) app = stdApp();
00147   if ( app.IsNull() )
00148     return false;
00149 
00150   bool status = false;
00151   try {
00152 #if OCC_VERSION_LARGE > 0x06010000
00153     OCC_CATCH_SIGNALS;
00154 #endif
00155 
00156 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
00157     status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == PCDM_RS_OK;
00158 #else
00159     status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == CDF_RS_OK;
00160 #endif
00161   }
00162   catch ( Standard_Failure ) {
00163     status = false;
00164   }
00165 
00166   return status && SUIT_Study::openDocument( fname );
00167 }
00168 
00173 bool CAF_Study::saveDocumentAs( const QString& fname )
00174 {
00175   Handle(TDocStd_Application) app = stdApp();
00176   if ( app.IsNull() )
00177     return false;
00178 
00179   bool save = false;
00180   if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
00181   {
00182     QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
00183     save = path == QDir::convertSeparators( fname );
00184   }
00185 
00186   bool status = false;
00187   try {
00188 #if OCC_VERSION_LARGE > 0x06010000
00189     OCC_CATCH_SIGNALS;
00190 #endif
00191     if ( save )
00192 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
00193       status = app->Save( stdDoc() ) == PCDM_SS_OK;
00194 #else
00195       status = app->Save( stdDoc() ) == CDF_SS_OK;
00196 #endif
00197     else
00198     {
00199       TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
00200       app->Format( path, format );
00201 
00202       if ( format.Length() )
00203         stdDoc()->ChangeStorageFormat( format );
00204 
00205 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
00206       status = app->SaveAs( stdDoc(), path ) == PCDM_SS_OK;
00207 #else
00208       status = app->SaveAs( stdDoc(), path ) == CDF_SS_OK;
00209 #endif
00210     }
00211   }
00212   catch ( Standard_Failure ) {
00213     status = false;
00214   }
00215 
00216   if ( status )
00217     status = SUIT_Study::saveDocumentAs( fname );
00218 
00219   if ( status )
00220     myModifiedCnt = 0;
00221 
00222   return status;
00223 }
00224 
00229 bool CAF_Study::openTransaction()
00230 {
00231   if ( myStdDoc.IsNull() )
00232     return false;
00233 
00234   bool res = true;
00235   try {
00236 #if OCC_VERSION_LARGE > 0x06010000
00237     OCC_CATCH_SIGNALS;
00238 #endif
00239     if ( myStdDoc->HasOpenCommand() )
00240       myStdDoc->AbortCommand();
00241 
00242     myStdDoc->OpenCommand();
00243   }
00244   catch ( Standard_Failure ) {
00245     res = false;
00246   }
00247 
00248   return res;
00249 }
00250 
00255 bool CAF_Study::abortTransaction()
00256 {
00257   if ( myStdDoc.IsNull() )
00258     return false;
00259 
00260   bool res = true;
00261   try {
00262 #if OCC_VERSION_LARGE > 0x06010000
00263     OCC_CATCH_SIGNALS;
00264 #endif
00265     myStdDoc->AbortCommand();
00266     update();
00267   }
00268   catch ( Standard_Failure ) {
00269     res = false;
00270   }
00271   return res;
00272 }
00273 
00278 bool CAF_Study::commitTransaction( const QString& name )
00279 {
00280   if ( myStdDoc.IsNull() )
00281     return false;
00282 
00283   bool res = true;
00284   try {
00285 #if OCC_VERSION_LARGE > 0x06010000
00286     OCC_CATCH_SIGNALS;
00287 #endif
00288     myStdDoc->CommitCommand();
00289 
00290     if ( canUndo() )
00291     {
00292       Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
00293       if ( !d.IsNull() )
00294         d->SetName( CAF_Tools::toExtString( name ) );
00295     }
00296   }
00297   catch ( Standard_Failure ) {
00298     res = false;
00299   }
00300   return res;
00301 }
00302 
00307 bool CAF_Study::hasTransaction() const
00308 {
00309   if ( myStdDoc.IsNull() )
00310     return false;
00311 
00312   return myStdDoc->HasOpenCommand();
00313 }
00314 
00319 bool CAF_Study::isSaved() const
00320 {
00321   if ( myStdDoc.IsNull() )
00322     return false;
00323 
00324   return myStdDoc->IsSaved();
00325 }
00326 
00331 bool CAF_Study::isModified() const
00332 {
00333   if ( myStdDoc.IsNull() )
00334     return false;
00335 
00336 //  return myStdDoc->IsModified();
00337   return myModifiedCnt;
00338 }
00339 
00350 void CAF_Study::doModified( bool undoable )
00351 {
00352   if ( myStdDoc.IsNull() )
00353     return;
00354 
00355   myModifiedCnt++;
00356 
00357   /*  Assumed that number of available undos / redos is NOT changed dynamically */
00358   if ( !undoable )
00359     myModifiedCnt += myStdDoc->GetAvailableUndos();
00360 }
00361 
00366 void CAF_Study::undoModified()
00367 {
00368   myModifiedCnt--;
00369 }
00370 
00375 void CAF_Study::clearModified()
00376 {
00377   myModifiedCnt = 0;
00378 }
00379 
00384 bool CAF_Study::undo()
00385 {
00386   if ( myStdDoc.IsNull() )
00387     return false;
00388 
00389   try {
00390 #if OCC_VERSION_LARGE > 0x06010000
00391     OCC_CATCH_SIGNALS;
00392 #endif
00393     myStdDoc->Undo();
00394     undoModified();     /* decrement modification counter */
00395   }
00396   catch ( Standard_Failure ) {
00397     SUIT_MessageBox::critical(application()->desktop(), tr( "ERR_ERROR" ),
00398                               tr( "ERR_DOC_UNDO" ));
00399     return false;
00400   }
00401   return true;
00402 }
00403 
00408 bool CAF_Study::redo()
00409 {
00410   if ( myStdDoc.IsNull() )
00411     return false;
00412 
00413   try {
00414 #if OCC_VERSION_LARGE > 0x06010000
00415     OCC_CATCH_SIGNALS;
00416 #endif
00417     myStdDoc->Redo();
00418     doModified();      /* increment modification counter */
00419   }
00420   catch ( Standard_Failure ) {
00421     SUIT_MessageBox::critical( application()->desktop(), tr( "ERR_ERROR" ),
00422                                tr( "ERR_DOC_REDO" ) );
00423     return false;
00424   }
00425   return true;
00426 }
00427 
00432 bool CAF_Study::canUndo() const
00433 {
00434   if ( myStdDoc.IsNull() )
00435     return false;
00436 
00437   return myStdDoc->GetAvailableUndos() > 0;
00438 }
00439 
00444 bool CAF_Study::canRedo() const
00445 {
00446   if ( myStdDoc.IsNull() )
00447     return false;
00448 
00449   return myStdDoc->GetAvailableRedos() > 0;
00450 }
00451 
00456 QStringList CAF_Study::undoNames() const
00457 {
00458   QStringList names;
00459   if ( !myStdDoc.IsNull() )
00460   {
00461     for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
00462       names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
00463   }
00464   return names;
00465 }
00466 
00471 QStringList CAF_Study::redoNames() const
00472 {
00473   QStringList names;
00474   if ( !myStdDoc.IsNull() )
00475   {
00476     for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
00477       names.append( CAF_Tools::toQString( it.Value()->Name() ) );
00478   }
00479   return names;
00480 }
00481 
00486 Handle(TDocStd_Application) CAF_Study::stdApp() const
00487 {
00488   Handle(TDocStd_Application) stdApp;
00489   CAF_Application* app = cafApplication();
00490   if ( app )
00491     stdApp = app->stdApp();
00492   return stdApp;
00493 }
00494 
00499 CAF_Application* CAF_Study::cafApplication() const
00500 {
00501   return qobject_cast<CAF_Application*>( application() );
00502 }