1 // Copyright (C) 2007-2016 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 <SUIT_Desktop.h>
29 #include <SUIT_MessageBox.h>
30 #include <SUIT_Application.h>
34 #include <TDF_Delta.hxx>
35 #include <TDF_ListIteratorOfDeltaList.hxx>
36 #include <TColStd_SequenceOfAsciiString.hxx>
37 #include <Standard_Failure.hxx>
38 #include <Standard_ErrorHandler.hxx>
42 \brief Represents study for using in CAF module.
44 A study contains reference to OCAF std document and allows using OCAF services.
45 Provides necessary functionality for OCC transactions management.
50 \param theApp application
52 CAF_Study::CAF_Study(SUIT_Application* theApp)
53 : SUIT_Study( theApp ),
60 \param theApp application
61 \param aStdDoc OCAF document
63 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
64 : SUIT_Study( theApp ),
73 CAF_Study::~CAF_Study()
78 \brief Get OCAF document.
79 \return handle to the OCAF document object
81 Handle(TDocStd_Document) CAF_Study::stdDoc() const
87 \brief Set OCAF document.
88 \param aStdDoc new OCAF document
90 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
96 \brief Customize document initialization.
98 \return \c true on success and \c false on error
100 bool CAF_Study::createDocument( const QString& doc )
102 bool res = SUIT_Study::createDocument( doc );
104 CAF_Application* app = cafApplication();
105 if ( res && app && !app->stdApp().IsNull() )
109 TColStd_SequenceOfAsciiString formats;
110 app->stdApp()->WritingFormats( formats );
111 if ( formats.IsEmpty() )
112 app->stdApp()->ReadingFormats( formats );
113 if ( !formats.IsEmpty() )
114 app->stdApp()->NewDocument( formats.First(), myStdDoc );
116 catch ( Standard_Failure ) {
124 \brief Close document.
125 \param permanently if \c true, a document is closed permanently
127 void CAF_Study::closeDocument( bool permanently )
129 Handle(TDocStd_Application) app = stdApp();
130 if ( !app.IsNull() && !stdDoc().IsNull() )
131 app->Close( stdDoc() );
133 SUIT_Study::closeDocument( permanently );
137 \brief Open document.
138 \param fname study file name
139 \return \c true on success and \c false if document cannot be opened
141 bool CAF_Study::openDocument( const QString& fname )
143 Handle(TDocStd_Application) app = stdApp();
150 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == PCDM_RS_OK;
152 catch ( Standard_Failure ) {
156 return status && SUIT_Study::openDocument( fname );
160 \brief Save document with other name.
161 \param fname study file name
163 bool CAF_Study::saveDocumentAs( const QString& fname )
165 Handle(TDocStd_Application) app = stdApp();
170 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
172 QString path = QDir::toNativeSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
173 save = path == QDir::toNativeSeparators( fname );
180 status = app->Save( stdDoc() ) == PCDM_SS_OK;
183 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
184 app->Format( path, format );
186 if ( format.Length() )
187 stdDoc()->ChangeStorageFormat( format );
189 status = app->SaveAs( stdDoc(), path ) == PCDM_SS_OK;
192 catch ( Standard_Failure ) {
197 status = SUIT_Study::saveDocumentAs( fname );
206 \brief Open OCAF transaction.
207 \return \c true if transaction is opened successfully
209 bool CAF_Study::openTransaction()
211 if ( myStdDoc.IsNull() )
217 if ( myStdDoc->HasOpenCommand() )
218 myStdDoc->AbortCommand();
220 myStdDoc->OpenCommand();
222 catch ( Standard_Failure ) {
230 \brief Abort OCAF transaction.
231 \return \c true if transaction is aborted successfully
233 bool CAF_Study::abortTransaction()
235 if ( myStdDoc.IsNull() )
241 myStdDoc->AbortCommand();
244 catch ( Standard_Failure ) {
251 \brief Commit OCAF transaction
252 \return \c true if transaction is committed successfully
254 bool CAF_Study::commitTransaction( const QString& name )
256 if ( myStdDoc.IsNull() )
262 myStdDoc->CommitCommand();
266 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
268 d->SetName( CAF_Tools::toExtString( name ) );
271 catch ( Standard_Failure ) {
278 \brief Check if there is any transaction opened.
279 \return \c true if there is opened OCAF transaction
281 bool CAF_Study::hasTransaction() const
283 if ( myStdDoc.IsNull() )
286 return myStdDoc->HasOpenCommand();
290 \brief Check if the study is saved.
291 \return \c true if the document has been saved to file
293 bool CAF_Study::isSaved() const
295 if ( myStdDoc.IsNull() )
298 return myStdDoc->IsSaved();
302 \brief Check if the study is modified.
303 \return \c true if the document has been modified
305 bool CAF_Study::isModified() const
307 if ( myStdDoc.IsNull() )
310 // return myStdDoc->IsModified();
311 return myModifiedCnt;
315 \brief Increment modifications count.
317 If \a undoable is \c true, this modification can be rolled back by
318 undoModified(), otherwise the document will be marked as \c modified
321 \param undoable if \c true the operation is undoable
322 \sa undoModified(), clearModified()
324 void CAF_Study::doModified( bool undoable )
326 if ( myStdDoc.IsNull() )
331 /* Assumed that number of available undos / redos is NOT changed dynamically */
333 myModifiedCnt += myStdDoc->GetAvailableUndos();
337 \brief Decrement modifications count.
338 \sa doModified(), clearModified()
340 void CAF_Study::undoModified()
346 \brief Clear modifications count.
347 \sa doModified(), undoModified()
349 void CAF_Study::clearModified()
355 \brief Undo the last command.
356 \return \c true on success and \c false on error
358 bool CAF_Study::undo()
360 if ( myStdDoc.IsNull() )
366 undoModified(); /* decrement modification counter */
368 catch ( Standard_Failure ) {
369 SUIT_MessageBox::critical(application()->desktop(), tr( "ERR_ERROR" ),
370 tr( "ERR_DOC_UNDO" ));
377 \brief Redo the last undo.
378 \return \c true on success and \c false on error
380 bool CAF_Study::redo()
382 if ( myStdDoc.IsNull() )
388 doModified(); /* increment modification counter */
390 catch ( Standard_Failure ) {
391 SUIT_MessageBox::critical( application()->desktop(), tr( "ERR_ERROR" ),
392 tr( "ERR_DOC_REDO" ) );
399 \brief Check if it is possible to undo last command.
400 \return \c true if undo is avaiable
402 bool CAF_Study::canUndo() const
404 if ( myStdDoc.IsNull() )
407 return myStdDoc->GetAvailableUndos() > 0;
411 \brief Check if it is possible to redo last undo.
412 \return \c true if redo is avaiable
414 bool CAF_Study::canRedo() const
416 if ( myStdDoc.IsNull() )
419 return myStdDoc->GetAvailableRedos() > 0;
423 \brief Get names of available undo commands.
424 \return list of commands names
426 QStringList CAF_Study::undoNames() const
429 if ( !myStdDoc.IsNull() )
431 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
432 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
438 \brief Get names of available redo commands.
439 \return list of commands names
441 QStringList CAF_Study::redoNames() const
444 if ( !myStdDoc.IsNull() )
446 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
447 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
453 \brief Get OCAF application.
454 \return handle to the OCAF application object
456 Handle(TDocStd_Application) CAF_Study::stdApp() const
458 Handle(TDocStd_Application) stdApp;
459 CAF_Application* app = cafApplication();
461 stdApp = app->stdApp();
466 \brief Get application.
467 \return application object (CAF_Application)
469 CAF_Application* CAF_Study::cafApplication() const
471 return qobject_cast<CAF_Application*>( application() );