1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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() )
112 TColStd_SequenceOfExtendedString formats;
113 app->stdApp()->Formats( formats );
114 if ( !formats.IsEmpty() )
115 app->stdApp()->NewDocument( formats.First(), myStdDoc );
117 catch ( Standard_Failure ) {
125 \brief Close document.
126 \param permanently if \c true, a document is closed permanently
128 void CAF_Study::closeDocument( bool permanently )
130 Handle(TDocStd_Application) app = stdApp();
131 if ( !app.IsNull() && !stdDoc().IsNull() )
132 app->Close( stdDoc() );
134 SUIT_Study::closeDocument( permanently );
138 \brief Open document.
139 \param fname study file name
140 \return \c true on success and \c false if document cannot be opened
142 bool CAF_Study::openDocument( const QString& fname )
144 Handle(TDocStd_Application) app = stdApp();
151 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == PCDM_RS_OK;
153 catch ( Standard_Failure ) {
157 return status && SUIT_Study::openDocument( fname );
161 \brief Save document with other name.
162 \param fname study file name
164 bool CAF_Study::saveDocumentAs( const QString& fname )
166 Handle(TDocStd_Application) app = stdApp();
171 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
173 QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
174 save = path == QDir::convertSeparators( fname );
181 status = app->Save( stdDoc() ) == PCDM_SS_OK;
184 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
185 app->Format( path, format );
187 if ( format.Length() )
188 stdDoc()->ChangeStorageFormat( format );
190 status = app->SaveAs( stdDoc(), path ) == PCDM_SS_OK;
193 catch ( Standard_Failure ) {
198 status = SUIT_Study::saveDocumentAs( fname );
207 \brief Open OCAF transaction.
208 \return \c true if transaction is opened successfully
210 bool CAF_Study::openTransaction()
212 if ( myStdDoc.IsNull() )
218 if ( myStdDoc->HasOpenCommand() )
219 myStdDoc->AbortCommand();
221 myStdDoc->OpenCommand();
223 catch ( Standard_Failure ) {
231 \brief Abort OCAF transaction.
232 \return \c true if transaction is aborted successfully
234 bool CAF_Study::abortTransaction()
236 if ( myStdDoc.IsNull() )
242 myStdDoc->AbortCommand();
245 catch ( Standard_Failure ) {
252 \brief Commit OCAF transaction
253 \return \c true if transaction is committed successfully
255 bool CAF_Study::commitTransaction( const QString& name )
257 if ( myStdDoc.IsNull() )
263 myStdDoc->CommitCommand();
267 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
269 d->SetName( CAF_Tools::toExtString( name ) );
272 catch ( Standard_Failure ) {
279 \brief Check if there is any transaction opened.
280 \return \c true if there is opened OCAF transaction
282 bool CAF_Study::hasTransaction() const
284 if ( myStdDoc.IsNull() )
287 return myStdDoc->HasOpenCommand();
291 \brief Check if the study is saved.
292 \return \c true if the document has been saved to file
294 bool CAF_Study::isSaved() const
296 if ( myStdDoc.IsNull() )
299 return myStdDoc->IsSaved();
303 \brief Check if the study is modified.
304 \return \c true if the document has been modified
306 bool CAF_Study::isModified() const
308 if ( myStdDoc.IsNull() )
311 // return myStdDoc->IsModified();
312 return myModifiedCnt;
316 \brief Increment modifications count.
318 If \a undoable is \c true, this modification can be rolled back by
319 undoModified(), otherwise the document will be marked as \c modified
322 \param undoable if \c true the operation is undoable
323 \sa undoModified(), clearModified()
325 void CAF_Study::doModified( bool undoable )
327 if ( myStdDoc.IsNull() )
332 /* Assumed that number of available undos / redos is NOT changed dynamically */
334 myModifiedCnt += myStdDoc->GetAvailableUndos();
338 \brief Decrement modifications count.
339 \sa doModified(), clearModified()
341 void CAF_Study::undoModified()
347 \brief Clear modifications count.
348 \sa doModified(), undoModified()
350 void CAF_Study::clearModified()
356 \brief Undo the last command.
357 \return \c true on success and \c false on error
359 bool CAF_Study::undo()
361 if ( myStdDoc.IsNull() )
367 undoModified(); /* decrement modification counter */
369 catch ( Standard_Failure ) {
370 SUIT_MessageBox::critical(application()->desktop(), tr( "ERR_ERROR" ),
371 tr( "ERR_DOC_UNDO" ));
378 \brief Redo the last undo.
379 \return \c true on success and \c false on error
381 bool CAF_Study::redo()
383 if ( myStdDoc.IsNull() )
389 doModified(); /* increment modification counter */
391 catch ( Standard_Failure ) {
392 SUIT_MessageBox::critical( application()->desktop(), tr( "ERR_ERROR" ),
393 tr( "ERR_DOC_REDO" ) );
400 \brief Check if it is possible to undo last command.
401 \return \c true if undo is avaiable
403 bool CAF_Study::canUndo() const
405 if ( myStdDoc.IsNull() )
408 return myStdDoc->GetAvailableUndos() > 0;
412 \brief Check if it is possible to redo last undo.
413 \return \c true if redo is avaiable
415 bool CAF_Study::canRedo() const
417 if ( myStdDoc.IsNull() )
420 return myStdDoc->GetAvailableRedos() > 0;
424 \brief Get names of available undo commands.
425 \return list of commands names
427 QStringList CAF_Study::undoNames() const
430 if ( !myStdDoc.IsNull() )
432 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
433 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
439 \brief Get names of available redo commands.
440 \return list of commands names
442 QStringList CAF_Study::redoNames() const
445 if ( !myStdDoc.IsNull() )
447 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
448 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
454 \brief Get OCAF application.
455 \return handle to the OCAF application object
457 Handle(TDocStd_Application) CAF_Study::stdApp() const
459 Handle(TDocStd_Application) stdApp;
460 CAF_Application* app = cafApplication();
462 stdApp = app->stdApp();
467 \brief Get application.
468 \return application object (CAF_Application)
470 CAF_Application* CAF_Study::cafApplication() const
472 return qobject_cast<CAF_Application*>( application() );