1 // Copyright (C) 2007-2008 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
22 #include "CAF_Study.h"
24 #include "CAF_Tools.h"
25 #include "CAF_Application.h"
27 #include <SUIT_Desktop.h>
28 #include <SUIT_MessageBox.h>
29 #include <SUIT_Application.h>
33 #include <TDF_Delta.hxx>
34 #include <TDF_ListIteratorOfDeltaList.hxx>
35 #include <TDocStd_Application.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() )
108 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
111 TColStd_SequenceOfExtendedString formats;
112 app->stdApp()->Formats( 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();
149 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
152 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == CDF_RS_OK;
154 catch ( Standard_Failure ) {
158 return status && SUIT_Study::openDocument( fname );
162 \brief Save document with other name.
163 \param fname study file name
165 bool CAF_Study::saveDocumentAs( const QString& fname )
167 Handle(TDocStd_Application) app = stdApp();
172 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
174 QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
175 save = path == QDir::convertSeparators( fname );
180 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
184 status = app->Save( stdDoc() ) == CDF_SS_OK;
187 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
188 app->Format( path, format );
190 if ( format.Length() )
191 stdDoc()->ChangeStorageFormat( format );
193 status = app->SaveAs( stdDoc(), path ) == CDF_SS_OK;
196 catch ( Standard_Failure ) {
201 status = SUIT_Study::saveDocumentAs( fname );
210 \brief Open OCAF transaction.
211 \return \c true if transaction is opened successfully
213 bool CAF_Study::openTransaction()
215 if ( myStdDoc.IsNull() )
220 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
223 if ( myStdDoc->HasOpenCommand() )
224 myStdDoc->AbortCommand();
226 myStdDoc->OpenCommand();
228 catch ( Standard_Failure ) {
236 \brief Abort OCAF transaction.
237 \return \c true if transaction is aborted successfully
239 bool CAF_Study::abortTransaction()
241 if ( myStdDoc.IsNull() )
246 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
249 myStdDoc->AbortCommand();
252 catch ( Standard_Failure ) {
259 \brief Commit OCAF transaction
260 \return \c true if transaction is committed successfully
262 bool CAF_Study::commitTransaction( const QString& name )
264 if ( myStdDoc.IsNull() )
269 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
272 myStdDoc->CommitCommand();
276 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
278 d->SetName( CAF_Tools::toExtString( name ) );
281 catch ( Standard_Failure ) {
288 \brief Check if there is any transaction opened.
289 \return \c true if there is opened OCAF transaction
291 bool CAF_Study::hasTransaction() const
293 if ( myStdDoc.IsNull() )
296 return myStdDoc->HasOpenCommand();
300 \brief Check if the study is saved.
301 \return \c true if the document has been saved to file
303 bool CAF_Study::isSaved() const
305 if ( myStdDoc.IsNull() )
308 return myStdDoc->IsSaved();
312 \brief Check if the study is modified.
313 \return \c true if the document has been modified
315 bool CAF_Study::isModified() const
317 if ( myStdDoc.IsNull() )
320 // return myStdDoc->IsModified();
321 return myModifiedCnt;
325 \brief Increment modifications count.
327 If \a undoable is \c true, this modification can be rolled back by
328 undoModified(), otherwise the document will be marked as \c modified
331 \param undoable if \c true the operation is undoable
332 \sa undoModified(), clearModified()
334 void CAF_Study::doModified( bool undoable )
336 if ( myStdDoc.IsNull() )
341 /* Assumed that number of available undos / redos is NOT changed dynamically */
343 myModifiedCnt += myStdDoc->GetAvailableUndos();
347 \brief Decrement modifications count.
348 \sa doModified(), clearModified()
350 void CAF_Study::undoModified()
356 \brief Clear modifications count.
357 \sa doModified(), undoModified()
359 void CAF_Study::clearModified()
365 \brief Undo the last command.
366 \return \c true on success and \c false on error
368 bool CAF_Study::undo()
370 if ( myStdDoc.IsNull() )
374 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
378 undoModified(); /* decrement modification counter */
380 catch ( Standard_Failure ) {
381 SUIT_MessageBox::critical(application()->desktop(), tr( "ERR_ERROR" ),
382 tr( "ERR_DOC_UNDO" ));
389 \brief Redo the last undo.
390 \return \c true on success and \c false on error
392 bool CAF_Study::redo()
394 if ( myStdDoc.IsNull() )
398 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
402 doModified(); /* increment modification counter */
404 catch ( Standard_Failure ) {
405 SUIT_MessageBox::critical( application()->desktop(), tr( "ERR_ERROR" ),
406 tr( "ERR_DOC_REDO" ) );
413 \brief Check if it is possible to undo last command.
414 \return \c true if undo is avaiable
416 bool CAF_Study::canUndo() const
418 if ( myStdDoc.IsNull() )
421 return myStdDoc->GetAvailableUndos() > 0;
425 \brief Check if it is possible to redo last undo.
426 \return \c true if redo is avaiable
428 bool CAF_Study::canRedo() const
430 if ( myStdDoc.IsNull() )
433 return myStdDoc->GetAvailableRedos() > 0;
437 \brief Get names of available undo commands.
438 \return list of commands names
440 QStringList CAF_Study::undoNames() const
443 if ( !myStdDoc.IsNull() )
445 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
446 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
452 \brief Get names of available redo commands.
453 \return list of commands names
455 QStringList CAF_Study::redoNames() const
458 if ( !myStdDoc.IsNull() )
460 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
461 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
467 \brief Get OCAF application.
468 \return handle to the OCAF application object
470 Handle(TDocStd_Application) CAF_Study::stdApp() const
472 Handle(TDocStd_Application) stdApp;
473 CAF_Application* app = cafApplication();
475 stdApp = app->stdApp();
480 \brief Get application.
481 \return application object (CAF_Application)
483 CAF_Application* CAF_Study::cafApplication() const
485 return qobject_cast<CAF_Application*>( application() );