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>
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_SequenceOfExtendedString formats;
110 app->stdApp()->Formats( formats );
111 if ( !formats.IsEmpty() )
112 app->stdApp()->NewDocument( formats.First(), myStdDoc );
114 catch ( Standard_Failure ) {
122 \brief Close document.
123 \param permanently if \c true, a document is closed permanently
125 void CAF_Study::closeDocument( bool permanently )
127 Handle(TDocStd_Application) app = stdApp();
128 if ( !app.IsNull() && !stdDoc().IsNull() )
129 app->Close( stdDoc() );
131 SUIT_Study::closeDocument( permanently );
135 \brief Open document.
136 \param fname study file name
137 \return \c true on success and \c false if document cannot be opened
139 bool CAF_Study::openDocument( const QString& fname )
141 Handle(TDocStd_Application) app = stdApp();
148 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == PCDM_RS_OK;
150 catch ( Standard_Failure ) {
154 return status && SUIT_Study::openDocument( fname );
158 \brief Save document with other name.
159 \param fname study file name
161 bool CAF_Study::saveDocumentAs( const QString& fname )
163 Handle(TDocStd_Application) app = stdApp();
168 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
170 QString path = QDir::toNativeSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
171 save = path == QDir::toNativeSeparators( fname );
178 status = app->Save( stdDoc() ) == PCDM_SS_OK;
181 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
182 app->Format( path, format );
184 if ( format.Length() )
185 stdDoc()->ChangeStorageFormat( format );
187 status = app->SaveAs( stdDoc(), path ) == PCDM_SS_OK;
190 catch ( Standard_Failure ) {
195 status = SUIT_Study::saveDocumentAs( fname );
204 \brief Open OCAF transaction.
205 \return \c true if transaction is opened successfully
207 bool CAF_Study::openTransaction()
209 if ( myStdDoc.IsNull() )
215 if ( myStdDoc->HasOpenCommand() )
216 myStdDoc->AbortCommand();
218 myStdDoc->OpenCommand();
220 catch ( Standard_Failure ) {
228 \brief Abort OCAF transaction.
229 \return \c true if transaction is aborted successfully
231 bool CAF_Study::abortTransaction()
233 if ( myStdDoc.IsNull() )
239 myStdDoc->AbortCommand();
242 catch ( Standard_Failure ) {
249 \brief Commit OCAF transaction
250 \return \c true if transaction is committed successfully
252 bool CAF_Study::commitTransaction( const QString& name )
254 if ( myStdDoc.IsNull() )
260 myStdDoc->CommitCommand();
264 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
266 d->SetName( CAF_Tools::toExtString( name ) );
269 catch ( Standard_Failure ) {
276 \brief Check if there is any transaction opened.
277 \return \c true if there is opened OCAF transaction
279 bool CAF_Study::hasTransaction() const
281 if ( myStdDoc.IsNull() )
284 return myStdDoc->HasOpenCommand();
288 \brief Check if the study is saved.
289 \return \c true if the document has been saved to file
291 bool CAF_Study::isSaved() const
293 if ( myStdDoc.IsNull() )
296 return myStdDoc->IsSaved();
300 \brief Check if the study is modified.
301 \return \c true if the document has been modified
303 bool CAF_Study::isModified() const
305 if ( myStdDoc.IsNull() )
308 // return myStdDoc->IsModified();
309 return myModifiedCnt;
313 \brief Increment modifications count.
315 If \a undoable is \c true, this modification can be rolled back by
316 undoModified(), otherwise the document will be marked as \c modified
319 \param undoable if \c true the operation is undoable
320 \sa undoModified(), clearModified()
322 void CAF_Study::doModified( bool undoable )
324 if ( myStdDoc.IsNull() )
329 /* Assumed that number of available undos / redos is NOT changed dynamically */
331 myModifiedCnt += myStdDoc->GetAvailableUndos();
335 \brief Decrement modifications count.
336 \sa doModified(), clearModified()
338 void CAF_Study::undoModified()
344 \brief Clear modifications count.
345 \sa doModified(), undoModified()
347 void CAF_Study::clearModified()
353 \brief Undo the last command.
354 \return \c true on success and \c false on error
356 bool CAF_Study::undo()
358 if ( myStdDoc.IsNull() )
364 undoModified(); /* decrement modification counter */
366 catch ( Standard_Failure ) {
367 SUIT_MessageBox::critical(application()->desktop(), tr( "ERR_ERROR" ),
368 tr( "ERR_DOC_UNDO" ));
375 \brief Redo the last undo.
376 \return \c true on success and \c false on error
378 bool CAF_Study::redo()
380 if ( myStdDoc.IsNull() )
386 doModified(); /* increment modification counter */
388 catch ( Standard_Failure ) {
389 SUIT_MessageBox::critical( application()->desktop(), tr( "ERR_ERROR" ),
390 tr( "ERR_DOC_REDO" ) );
397 \brief Check if it is possible to undo last command.
398 \return \c true if undo is avaiable
400 bool CAF_Study::canUndo() const
402 if ( myStdDoc.IsNull() )
405 return myStdDoc->GetAvailableUndos() > 0;
409 \brief Check if it is possible to redo last undo.
410 \return \c true if redo is avaiable
412 bool CAF_Study::canRedo() const
414 if ( myStdDoc.IsNull() )
417 return myStdDoc->GetAvailableRedos() > 0;
421 \brief Get names of available undo commands.
422 \return list of commands names
424 QStringList CAF_Study::undoNames() const
427 if ( !myStdDoc.IsNull() )
429 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
430 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
436 \brief Get names of available redo commands.
437 \return list of commands names
439 QStringList CAF_Study::redoNames() const
442 if ( !myStdDoc.IsNull() )
444 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
445 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
451 \brief Get OCAF application.
452 \return handle to the OCAF application object
454 Handle(TDocStd_Application) CAF_Study::stdApp() const
456 Handle(TDocStd_Application) stdApp;
457 CAF_Application* app = cafApplication();
459 stdApp = app->stdApp();
464 \brief Get application.
465 \return application object (CAF_Application)
467 CAF_Application* CAF_Study::cafApplication() const
469 return qobject_cast<CAF_Application*>( application() );