1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "CAF_Study.h"
21 #include "CAF_Tools.h"
22 #include "CAF_Operation.h"
23 #include "CAF_Application.h"
25 #include <SUIT_Desktop.h>
26 #include <SUIT_MessageBox.h>
27 #include <SUIT_Application.h>
31 #include <TDF_Delta.hxx>
32 #include <TDF_ListIteratorOfDeltaList.hxx>
34 #include <Standard_Failure.hxx>
35 #include <Standard_ErrorHandler.hxx>
40 CAF_Study::CAF_Study(SUIT_Application* theApp)
41 : SUIT_Study( theApp ),
49 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
50 : SUIT_Study( theApp ),
59 CAF_Study::~CAF_Study()
66 Handle(TDocStd_Document) CAF_Study::stdDoc() const
72 Sets new OCAF document
73 \param aStdDoc - new OCAF document
75 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
81 Custom document initialization
83 void CAF_Study::createDocument()
85 SUIT_Study::createDocument();
87 CAF_Application* app = cafApplication();
88 if ( app && !app->stdApp().IsNull() )
91 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
94 TColStd_SequenceOfExtendedString formats;
95 app->stdApp()->Formats( formats );
96 if ( !formats.IsEmpty() )
97 app->stdApp()->NewDocument( formats.First(), myStdDoc );
99 catch ( Standard_Failure ) {
107 void CAF_Study::closeDocument( bool permanent )
109 Handle(TDocStd_Application) app = stdApp();
110 if ( !app.IsNull() && !stdDoc().IsNull() )
111 app->Close( stdDoc() );
113 SUIT_Study::closeDocument( permanent );
118 \param fname - name of file
120 bool CAF_Study::openDocument( const QString& fname )
122 Handle(TDocStd_Application) app = stdApp();
128 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
131 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == CDF_RS_OK;
133 catch ( Standard_Failure ) {
137 return status && SUIT_Study::openDocument( fname );
141 Save document with other name
142 \param fname - name of file
144 bool CAF_Study::saveDocumentAs( const QString& fname )
146 Handle(TDocStd_Application) app = stdApp();
151 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
153 QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
154 save = path == QDir::convertSeparators( fname );
159 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
163 status = app->Save( stdDoc() ) == CDF_SS_OK;
166 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
167 app->Format( path, format );
169 if ( format.Length() )
170 stdDoc()->ChangeStorageFormat( format );
172 status = app->SaveAs( stdDoc(), path ) == CDF_SS_OK;
175 catch ( Standard_Failure ) {
180 status = SUIT_Study::saveDocumentAs( fname );
189 Open OCAF transaction
191 bool CAF_Study::openTransaction()
193 if ( myStdDoc.IsNull() )
198 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
201 if ( myStdDoc->HasOpenCommand() )
202 myStdDoc->AbortCommand();
204 myStdDoc->OpenCommand();
206 catch ( Standard_Failure ) {
214 Abort OCAF transaction
216 bool CAF_Study::abortTransaction()
218 if ( myStdDoc.IsNull() )
223 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
226 myStdDoc->AbortCommand();
229 catch ( Standard_Failure ) {
236 Commit OCAF transaction
238 bool CAF_Study::commitTransaction( const QString& name )
240 if ( myStdDoc.IsNull() )
245 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
248 myStdDoc->CommitCommand();
252 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
254 d->SetName( CAF_Tools::toExtString( name ) );
257 catch ( Standard_Failure ) {
264 \return true, if there is opened OCAF transaction
266 bool CAF_Study::hasTransaction() const
268 if ( myStdDoc.IsNull() )
271 return myStdDoc->HasOpenCommand();
275 \return whether the document was saved in file. [ public ]
277 bool CAF_Study::isSaved() const
279 if ( myStdDoc.IsNull() )
282 return myStdDoc->IsSaved();
286 \return whether the document is modified. [ public ]
288 bool CAF_Study::isModified() const
290 if ( myStdDoc.IsNull() )
293 // return myStdDoc->IsModified();
294 return myModifiedCnt;
298 Increments modification count. If 'undoable' is 'true', this modification
299 can be rolled back by 'undoModified' otherwise the document will be marked
300 as 'modiifed' until saved. [ protected ]
302 void CAF_Study::doModified( bool undoable )
304 if ( myStdDoc.IsNull() )
309 /* Assumed that number of available undos / redos is NOT changed dynamically */
311 myModifiedCnt += myStdDoc->GetAvailableUndos();
315 Decrements modification count. [ protected ]
317 void CAF_Study::undoModified()
323 Clears modification count. [ public ]
325 void CAF_Study::clearModified()
331 Undoes the last command. [ public ]
333 bool CAF_Study::undo()
335 if ( myStdDoc.IsNull() )
339 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
343 undoModified(); /* decrement modification counter */
345 catch ( Standard_Failure ) {
346 SUIT_MessageBox::error1(application()->desktop(), tr( "ERR_ERROR" ),
347 tr( "ERR_DOC_UNDO" ), tr ( "BUT_OK" ));
354 Redoes the last undo. [ public ]
356 bool CAF_Study::redo()
358 if ( myStdDoc.IsNull() )
362 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
366 doModified(); /* increment modification counter */
368 catch ( Standard_Failure ) {
369 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
370 tr( "ERR_DOC_REDO" ), tr ( "BUT_OK" ) );
377 \return true if possible to perform 'undo' command. [ public ]
379 bool CAF_Study::canUndo() const
381 if ( myStdDoc.IsNull() )
384 return myStdDoc->GetAvailableUndos() > 0;
388 \return true if possible to perform 'redo' command. [ public ]
390 bool CAF_Study::canRedo() const
392 if ( myStdDoc.IsNull() )
395 return myStdDoc->GetAvailableRedos() > 0;
399 \return the list of names of 'undo' actions available. [ public ]
401 QStringList CAF_Study::undoNames() const
404 if ( !myStdDoc.IsNull() )
406 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
407 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
413 \return the list of names of 'redo' actions available. [ public ]
415 QStringList CAF_Study::redoNames() const
418 if ( !myStdDoc.IsNull() )
420 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
421 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
427 \return the standard OCAF application from owner application. [ protected ]
429 Handle(TDocStd_Application) CAF_Study::stdApp() const
431 Handle(TDocStd_Application) stdApp;
432 CAF_Application* app = cafApplication();
434 stdApp = app->stdApp();
439 \return the application casted to type CAF_Application. [ protected ]
441 CAF_Application* CAF_Study::cafApplication() const
443 return ::qt_cast<CAF_Application*>( application() );