1 // Copyright (C) 2007-2015 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 <TDocStd_Application.hxx>
38 #include <Standard_Failure.hxx>
39 #include <Standard_ErrorHandler.hxx>
43 \brief Represents study for using in CAF module.
45 A study contains reference to OCAF std document and allows using OCAF services.
46 Provides necessary functionality for OCC transactions management.
51 \param theApp application
53 CAF_Study::CAF_Study(SUIT_Application* theApp)
54 : SUIT_Study( theApp ),
61 \param theApp application
62 \param aStdDoc OCAF document
64 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
65 : SUIT_Study( theApp ),
74 CAF_Study::~CAF_Study()
79 \brief Get OCAF document.
80 \return handle to the OCAF document object
82 Handle(TDocStd_Document) CAF_Study::stdDoc() const
88 \brief Set OCAF document.
89 \param aStdDoc new OCAF document
91 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
97 \brief Customize document initialization.
99 \return \c true on success and \c false on error
101 bool CAF_Study::createDocument( const QString& doc )
103 bool res = SUIT_Study::createDocument( doc );
105 CAF_Application* app = cafApplication();
106 if ( res && app && !app->stdApp().IsNull() )
110 TColStd_SequenceOfExtendedString formats;
111 app->stdApp()->Formats( formats );
112 if ( !formats.IsEmpty() )
113 app->stdApp()->NewDocument( formats.First(), myStdDoc );
115 catch ( Standard_Failure ) {
123 \brief Close document.
124 \param permanently if \c true, a document is closed permanently
126 void CAF_Study::closeDocument( bool permanently )
128 Handle(TDocStd_Application) app = stdApp();
129 if ( !app.IsNull() && !stdDoc().IsNull() )
130 app->Close( stdDoc() );
132 SUIT_Study::closeDocument( permanently );
136 \brief Open document.
137 \param fname study file name
138 \return \c true on success and \c false if document cannot be opened
140 bool CAF_Study::openDocument( const QString& fname )
142 Handle(TDocStd_Application) app = stdApp();
149 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == PCDM_RS_OK;
151 catch ( Standard_Failure ) {
155 return status && SUIT_Study::openDocument( fname );
159 \brief Save document with other name.
160 \param fname study file name
162 bool CAF_Study::saveDocumentAs( const QString& fname )
164 Handle(TDocStd_Application) app = stdApp();
169 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
171 QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
172 save = path == QDir::convertSeparators( fname );
179 status = app->Save( stdDoc() ) == PCDM_SS_OK;
182 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
183 app->Format( path, format );
185 if ( format.Length() )
186 stdDoc()->ChangeStorageFormat( format );
188 status = app->SaveAs( stdDoc(), path ) == PCDM_SS_OK;
191 catch ( Standard_Failure ) {
196 status = SUIT_Study::saveDocumentAs( fname );
205 \brief Open OCAF transaction.
206 \return \c true if transaction is opened successfully
208 bool CAF_Study::openTransaction()
210 if ( myStdDoc.IsNull() )
216 if ( myStdDoc->HasOpenCommand() )
217 myStdDoc->AbortCommand();
219 myStdDoc->OpenCommand();
221 catch ( Standard_Failure ) {
229 \brief Abort OCAF transaction.
230 \return \c true if transaction is aborted successfully
232 bool CAF_Study::abortTransaction()
234 if ( myStdDoc.IsNull() )
240 myStdDoc->AbortCommand();
243 catch ( Standard_Failure ) {
250 \brief Commit OCAF transaction
251 \return \c true if transaction is committed successfully
253 bool CAF_Study::commitTransaction( const QString& name )
255 if ( myStdDoc.IsNull() )
261 myStdDoc->CommitCommand();
265 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
267 d->SetName( CAF_Tools::toExtString( name ) );
270 catch ( Standard_Failure ) {
277 \brief Check if there is any transaction opened.
278 \return \c true if there is opened OCAF transaction
280 bool CAF_Study::hasTransaction() const
282 if ( myStdDoc.IsNull() )
285 return myStdDoc->HasOpenCommand();
289 \brief Check if the study is saved.
290 \return \c true if the document has been saved to file
292 bool CAF_Study::isSaved() const
294 if ( myStdDoc.IsNull() )
297 return myStdDoc->IsSaved();
301 \brief Check if the study is modified.
302 \return \c true if the document has been modified
304 bool CAF_Study::isModified() const
306 if ( myStdDoc.IsNull() )
309 // return myStdDoc->IsModified();
310 return myModifiedCnt;
314 \brief Increment modifications count.
316 If \a undoable is \c true, this modification can be rolled back by
317 undoModified(), otherwise the document will be marked as \c modified
320 \param undoable if \c true the operation is undoable
321 \sa undoModified(), clearModified()
323 void CAF_Study::doModified( bool undoable )
325 if ( myStdDoc.IsNull() )
330 /* Assumed that number of available undos / redos is NOT changed dynamically */
332 myModifiedCnt += myStdDoc->GetAvailableUndos();
336 \brief Decrement modifications count.
337 \sa doModified(), clearModified()
339 void CAF_Study::undoModified()
345 \brief Clear modifications count.
346 \sa doModified(), undoModified()
348 void CAF_Study::clearModified()
354 \brief Undo the last command.
355 \return \c true on success and \c false on error
357 bool CAF_Study::undo()
359 if ( myStdDoc.IsNull() )
365 undoModified(); /* decrement modification counter */
367 catch ( Standard_Failure ) {
368 SUIT_MessageBox::critical(application()->desktop(), tr( "ERR_ERROR" ),
369 tr( "ERR_DOC_UNDO" ));
376 \brief Redo the last undo.
377 \return \c true on success and \c false on error
379 bool CAF_Study::redo()
381 if ( myStdDoc.IsNull() )
387 doModified(); /* increment modification counter */
389 catch ( Standard_Failure ) {
390 SUIT_MessageBox::critical( application()->desktop(), tr( "ERR_ERROR" ),
391 tr( "ERR_DOC_REDO" ) );
398 \brief Check if it is possible to undo last command.
399 \return \c true if undo is avaiable
401 bool CAF_Study::canUndo() const
403 if ( myStdDoc.IsNull() )
406 return myStdDoc->GetAvailableUndos() > 0;
410 \brief Check if it is possible to redo last undo.
411 \return \c true if redo is avaiable
413 bool CAF_Study::canRedo() const
415 if ( myStdDoc.IsNull() )
418 return myStdDoc->GetAvailableRedos() > 0;
422 \brief Get names of available undo commands.
423 \return list of commands names
425 QStringList CAF_Study::undoNames() const
428 if ( !myStdDoc.IsNull() )
430 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
431 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
437 \brief Get names of available redo commands.
438 \return list of commands names
440 QStringList CAF_Study::redoNames() const
443 if ( !myStdDoc.IsNull() )
445 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
446 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
452 \brief Get OCAF application.
453 \return handle to the OCAF application object
455 Handle(TDocStd_Application) CAF_Study::stdApp() const
457 Handle(TDocStd_Application) stdApp;
458 CAF_Application* app = cafApplication();
460 stdApp = app->stdApp();
465 \brief Get application.
466 \return application object (CAF_Application)
468 CAF_Application* CAF_Study::cafApplication() const
470 return qobject_cast<CAF_Application*>( application() );