4 #include "CAF_Operation.h"
5 #include "CAF_Application.h"
7 #include <SUIT_Desktop.h>
8 #include <SUIT_MessageBox.h>
9 #include <SUIT_Application.h>
13 #include <TDF_Delta.hxx>
14 #include <TDF_ListIteratorOfDeltaList.hxx>
16 #include <Standard_ErrorHandler.hxx>
18 //////////////////////////////////////////////////////////////////////
19 // Construction/Destruction
20 //////////////////////////////////////////////////////////////////////
22 CAF_Study::CAF_Study(SUIT_Application* theApp)
23 : SUIT_Study( theApp ),
28 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
29 : SUIT_Study( theApp ),
35 CAF_Study::~CAF_Study()
39 Handle(TDocStd_Document) CAF_Study::stdDoc() const
44 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
49 void CAF_Study::createDocument()
51 SUIT_Study::createDocument();
53 CAF_Application* app = cafApplication();
54 if ( app && !app->stdApp().IsNull() )
57 TColStd_SequenceOfExtendedString formats;
58 app->stdApp()->Formats( formats );
59 if ( !formats.IsEmpty() )
60 app->stdApp()->NewDocument( formats.First(), myStdDoc );
62 catch ( Standard_Failure ) {
67 void CAF_Study::closeDocument( bool permanent )
69 Handle(TDocStd_Application) app = stdApp();
70 if ( !app.IsNull() && !stdDoc().IsNull() )
71 app->Close( stdDoc() );
73 SUIT_Study::closeDocument( permanent );
76 bool CAF_Study::openDocument( const QString& fname )
78 Handle(TDocStd_Application) app = stdApp();
84 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == CDF_RS_OK;
86 catch ( Standard_Failure ) {
90 return status && SUIT_Study::openDocument( fname );
93 bool CAF_Study::saveDocumentAs( const QString& fname )
95 Handle(TDocStd_Application) app = stdApp();
100 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
102 QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
103 save = path == QDir::convertSeparators( fname );
109 status = app->Save( stdDoc() ) == CDF_SS_OK;
112 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
113 app->Format( path, format );
115 if ( format.Length() )
116 stdDoc()->ChangeStorageFormat( format );
118 status = app->SaveAs( stdDoc(), path ) == CDF_SS_OK;
121 catch ( Standard_Failure ) {
125 return status && SUIT_Study::saveDocumentAs( fname );
128 bool CAF_Study::openTransaction()
130 if ( myStdDoc.IsNull() )
135 if ( myStdDoc->HasOpenCommand() )
136 myStdDoc->AbortCommand();
138 myStdDoc->OpenCommand();
140 catch ( Standard_Failure ) {
147 bool CAF_Study::abortTransaction()
149 if ( myStdDoc.IsNull() )
154 myStdDoc->AbortCommand();
157 catch ( Standard_Failure ) {
163 bool CAF_Study::commitTransaction( const QString& name )
165 if ( myStdDoc.IsNull() )
170 myStdDoc->CommitCommand();
174 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
176 d->SetName( CAF_Tools::toExtString( name ) );
179 catch ( Standard_Failure ) {
185 bool CAF_Study::hasTransaction() const
187 if ( myStdDoc.IsNull() )
190 return myStdDoc->HasOpenCommand();
194 Returns whether the document was saved in file. [ public ]
196 bool CAF_Study::isSaved() const
198 if ( myStdDoc.IsNull() )
201 return myStdDoc->IsSaved();
205 Returns whether the document is modified. [ public ]
207 bool CAF_Study::isModified() const
209 return ( myModifiedCnt != 0 );
213 Increments modification count. If 'undoable' is 'true', this modification
214 can be rolled back by 'undoModified' otherwise the document will be marked
215 as 'modiifed' until saved. [ protected ]
217 void CAF_Study::doModified( bool undoable )
219 if ( myStdDoc.IsNull() )
224 /* Assumed that number of available undos / redos is NOT changed dynamically */
226 myModifiedCnt += myStdDoc->GetAvailableUndos();
230 Decrements modification count. [ protected ]
232 void CAF_Study::undoModified()
238 Clears modification count. [ public ]
240 void CAF_Study::clearModified()
246 Undoes the last command. [ public ]
248 bool CAF_Study::undo()
250 if ( myStdDoc.IsNull() )
255 undoModified(); /* decrement modification counter */
257 catch ( Standard_Failure ) {
258 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
259 tr( "ERR_DOC_UNDO" ), tr ( "BUT_OK" ) );
266 Redoes the last undo. [ public ]
268 bool CAF_Study::redo()
270 if ( myStdDoc.IsNull() )
275 doModified(); /* increment modification counter */
277 catch ( Standard_Failure ) {
278 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
279 tr( "ERR_DOC_REDO" ), tr ( "BUT_OK" ) );
286 Check if possible to perform 'undo' command. [ public ]
288 bool CAF_Study::canUndo() const
290 if ( myStdDoc.IsNull() )
293 return myStdDoc->GetAvailableUndos() > 0;
297 Check if possible to perform 'redo' command. [ public ]
299 bool CAF_Study::canRedo() const
301 if ( myStdDoc.IsNull() )
304 return myStdDoc->GetAvailableRedos() > 0;
308 Returns the list of names of 'undo' actions available. [ public ]
310 QStringList CAF_Study::undoNames() const
313 if ( !myStdDoc.IsNull() )
315 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
316 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
322 Returns the list of names of 'redo' actions available. [ public ]
324 QStringList CAF_Study::redoNames() const
327 if ( !myStdDoc.IsNull() )
329 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
330 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
336 Returns the standard OCAF application from owner application. [ protected ]
338 Handle(TDocStd_Application) CAF_Study::stdApp() const
340 Handle(TDocStd_Application) stdApp;
341 CAF_Application* app = cafApplication();
343 stdApp = app->stdApp();
348 Returns the application casted to type CAF_Application. [ protected ]
350 CAF_Application* CAF_Study::cafApplication() const
352 return ::qt_cast<CAF_Application*>( application() );