1 // Copyright (C) 2007-2013 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
23 #include "CAF_Study.h"
25 #include "CAF_Tools.h"
26 #include "CAF_Application.h"
28 #include <Basics_OCCTVersion.hxx>
30 #include <SUIT_Desktop.h>
31 #include <SUIT_MessageBox.h>
32 #include <SUIT_Application.h>
36 #include <TDF_Delta.hxx>
37 #include <TDF_ListIteratorOfDeltaList.hxx>
38 #include <TDocStd_Application.hxx>
40 #include <Standard_Failure.hxx>
41 #include <Standard_ErrorHandler.hxx>
45 \brief Represents study for using in CAF module.
47 A study contains reference to OCAF std document and allows using OCAF services.
48 Provides necessary functionality for OCC transactions management.
53 \param theApp application
55 CAF_Study::CAF_Study(SUIT_Application* theApp)
56 : SUIT_Study( theApp ),
63 \param theApp application
64 \param aStdDoc OCAF document
66 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
67 : SUIT_Study( theApp ),
76 CAF_Study::~CAF_Study()
81 \brief Get OCAF document.
82 \return handle to the OCAF document object
84 Handle(TDocStd_Document) CAF_Study::stdDoc() const
90 \brief Set OCAF document.
91 \param aStdDoc new OCAF document
93 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
99 \brief Customize document initialization.
100 \param doc study name
101 \return \c true on success and \c false on error
103 bool CAF_Study::createDocument( const QString& doc )
105 bool res = SUIT_Study::createDocument( doc );
107 CAF_Application* app = cafApplication();
108 if ( res && app && !app->stdApp().IsNull() )
111 #if OCC_VERSION_LARGE > 0x06010000
114 TColStd_SequenceOfExtendedString formats;
115 app->stdApp()->Formats( formats );
116 if ( !formats.IsEmpty() )
117 app->stdApp()->NewDocument( formats.First(), myStdDoc );
119 catch ( Standard_Failure ) {
127 \brief Close document.
128 \param permanently if \c true, a document is closed permanently
130 void CAF_Study::closeDocument( bool permanently )
132 Handle(TDocStd_Application) app = stdApp();
133 if ( !app.IsNull() && !stdDoc().IsNull() )
134 app->Close( stdDoc() );
136 SUIT_Study::closeDocument( permanently );
140 \brief Open document.
141 \param fname study file name
142 \return \c true on success and \c false if document cannot be opened
144 bool CAF_Study::openDocument( const QString& fname )
146 Handle(TDocStd_Application) app = stdApp();
152 #if OCC_VERSION_LARGE > 0x06010000
156 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
157 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == PCDM_RS_OK;
159 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == CDF_RS_OK;
162 catch ( Standard_Failure ) {
166 return status && SUIT_Study::openDocument( fname );
170 \brief Save document with other name.
171 \param fname study file name
173 bool CAF_Study::saveDocumentAs( const QString& fname )
175 Handle(TDocStd_Application) app = stdApp();
180 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
182 QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
183 save = path == QDir::convertSeparators( fname );
188 #if OCC_VERSION_LARGE > 0x06010000
192 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
193 status = app->Save( stdDoc() ) == PCDM_SS_OK;
195 status = app->Save( stdDoc() ) == CDF_SS_OK;
199 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
200 app->Format( path, format );
202 if ( format.Length() )
203 stdDoc()->ChangeStorageFormat( format );
205 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
206 status = app->SaveAs( stdDoc(), path ) == PCDM_SS_OK;
208 status = app->SaveAs( stdDoc(), path ) == CDF_SS_OK;
212 catch ( Standard_Failure ) {
217 status = SUIT_Study::saveDocumentAs( fname );
226 \brief Open OCAF transaction.
227 \return \c true if transaction is opened successfully
229 bool CAF_Study::openTransaction()
231 if ( myStdDoc.IsNull() )
236 #if OCC_VERSION_LARGE > 0x06010000
239 if ( myStdDoc->HasOpenCommand() )
240 myStdDoc->AbortCommand();
242 myStdDoc->OpenCommand();
244 catch ( Standard_Failure ) {
252 \brief Abort OCAF transaction.
253 \return \c true if transaction is aborted successfully
255 bool CAF_Study::abortTransaction()
257 if ( myStdDoc.IsNull() )
262 #if OCC_VERSION_LARGE > 0x06010000
265 myStdDoc->AbortCommand();
268 catch ( Standard_Failure ) {
275 \brief Commit OCAF transaction
276 \return \c true if transaction is committed successfully
278 bool CAF_Study::commitTransaction( const QString& name )
280 if ( myStdDoc.IsNull() )
285 #if OCC_VERSION_LARGE > 0x06010000
288 myStdDoc->CommitCommand();
292 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
294 d->SetName( CAF_Tools::toExtString( name ) );
297 catch ( Standard_Failure ) {
304 \brief Check if there is any transaction opened.
305 \return \c true if there is opened OCAF transaction
307 bool CAF_Study::hasTransaction() const
309 if ( myStdDoc.IsNull() )
312 return myStdDoc->HasOpenCommand();
316 \brief Check if the study is saved.
317 \return \c true if the document has been saved to file
319 bool CAF_Study::isSaved() const
321 if ( myStdDoc.IsNull() )
324 return myStdDoc->IsSaved();
328 \brief Check if the study is modified.
329 \return \c true if the document has been modified
331 bool CAF_Study::isModified() const
333 if ( myStdDoc.IsNull() )
336 // return myStdDoc->IsModified();
337 return myModifiedCnt;
341 \brief Increment modifications count.
343 If \a undoable is \c true, this modification can be rolled back by
344 undoModified(), otherwise the document will be marked as \c modified
347 \param undoable if \c true the operation is undoable
348 \sa undoModified(), clearModified()
350 void CAF_Study::doModified( bool undoable )
352 if ( myStdDoc.IsNull() )
357 /* Assumed that number of available undos / redos is NOT changed dynamically */
359 myModifiedCnt += myStdDoc->GetAvailableUndos();
363 \brief Decrement modifications count.
364 \sa doModified(), clearModified()
366 void CAF_Study::undoModified()
372 \brief Clear modifications count.
373 \sa doModified(), undoModified()
375 void CAF_Study::clearModified()
381 \brief Undo the last command.
382 \return \c true on success and \c false on error
384 bool CAF_Study::undo()
386 if ( myStdDoc.IsNull() )
390 #if OCC_VERSION_LARGE > 0x06010000
394 undoModified(); /* decrement modification counter */
396 catch ( Standard_Failure ) {
397 SUIT_MessageBox::critical(application()->desktop(), tr( "ERR_ERROR" ),
398 tr( "ERR_DOC_UNDO" ));
405 \brief Redo the last undo.
406 \return \c true on success and \c false on error
408 bool CAF_Study::redo()
410 if ( myStdDoc.IsNull() )
414 #if OCC_VERSION_LARGE > 0x06010000
418 doModified(); /* increment modification counter */
420 catch ( Standard_Failure ) {
421 SUIT_MessageBox::critical( application()->desktop(), tr( "ERR_ERROR" ),
422 tr( "ERR_DOC_REDO" ) );
429 \brief Check if it is possible to undo last command.
430 \return \c true if undo is avaiable
432 bool CAF_Study::canUndo() const
434 if ( myStdDoc.IsNull() )
437 return myStdDoc->GetAvailableUndos() > 0;
441 \brief Check if it is possible to redo last undo.
442 \return \c true if redo is avaiable
444 bool CAF_Study::canRedo() const
446 if ( myStdDoc.IsNull() )
449 return myStdDoc->GetAvailableRedos() > 0;
453 \brief Get names of available undo commands.
454 \return list of commands names
456 QStringList CAF_Study::undoNames() const
459 if ( !myStdDoc.IsNull() )
461 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
462 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
468 \brief Get names of available redo commands.
469 \return list of commands names
471 QStringList CAF_Study::redoNames() const
474 if ( !myStdDoc.IsNull() )
476 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
477 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
483 \brief Get OCAF application.
484 \return handle to the OCAF application object
486 Handle(TDocStd_Application) CAF_Study::stdApp() const
488 Handle(TDocStd_Application) stdApp;
489 CAF_Application* app = cafApplication();
491 stdApp = app->stdApp();
496 \brief Get application.
497 \return application object (CAF_Application)
499 CAF_Application* CAF_Study::cafApplication() const
501 return qobject_cast<CAF_Application*>( application() );