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 //////////////////////////////////////////////////////////////////////
17 // Construction/Destruction
18 //////////////////////////////////////////////////////////////////////
20 CAF_Study::CAF_Study(SUIT_Application* theApp)
21 : SUIT_Study( theApp ),
26 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
27 : SUIT_Study( theApp ),
33 CAF_Study::~CAF_Study()
37 Handle(TDocStd_Document) CAF_Study::stdDoc() const
42 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
47 void CAF_Study::createDocument()
49 SUIT_Study::createDocument();
51 CAF_Application* app = cafApplication();
52 if ( app && !app->stdApp().IsNull() )
55 TColStd_SequenceOfExtendedString formats;
56 app->stdApp()->Formats( formats );
57 if ( !formats.IsEmpty() )
58 app->stdApp()->NewDocument( formats.First(), myStdDoc );
60 catch ( Standard_Failure ) {
65 void CAF_Study::closeDocument( bool permanent )
67 Handle(TDocStd_Application) app = stdApp();
68 if ( !app.IsNull() && !stdDoc().IsNull() )
69 app->Close( stdDoc() );
71 SUIT_Study::closeDocument( permanent );
74 bool CAF_Study::openDocument( const QString& fname )
76 Handle(TDocStd_Application) app = stdApp();
81 app->Open( CAF_Tools::toExtString( fname ), myStdDoc );
83 catch ( Standard_Failure ) {
87 return SUIT_Study::openDocument( fname );
90 bool CAF_Study::saveDocumentAs( const QString& fname )
92 Handle(TDocStd_Application) app = stdApp();
97 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
99 QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
100 save = path == QDir::convertSeparators( fname );
105 app->Save( stdDoc() );
108 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
109 app->Format( path, format );
111 if ( format.Length() )
112 stdDoc()->ChangeStorageFormat( format );
114 app->SaveAs( stdDoc(), path );
117 catch ( Standard_Failure ) {
121 return SUIT_Study::saveDocumentAs( fname );
124 bool CAF_Study::startOperation()
126 if ( myStdDoc.IsNull() )
131 if ( myStdDoc->HasOpenCommand() )
132 myStdDoc->AbortCommand();
134 myStdDoc->OpenCommand();
136 catch ( Standard_Failure ) {
143 void CAF_Study::abortOperation()
145 if ( myStdDoc.IsNull() )
149 myStdDoc->AbortCommand();
152 catch ( Standard_Failure ) {
156 void CAF_Study::commitOperation()
158 if ( myStdDoc.IsNull() )
162 myStdDoc->CommitCommand();
166 CAF_Operation* cafOp = 0;
167 if ( activeOperation() && activeOperation()->inherits( "CAF_Operation" ) )
168 cafOp = (CAF_Operation*)activeOperation();
170 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
171 if ( cafOp && !d.IsNull() )
172 d->SetName( CAF_Tools::toExtString( cafOp->getName() ) );
175 catch ( Standard_Failure ) {
180 Returns whether the document was saved in file. [ public ]
182 bool CAF_Study::isSaved() const
184 if ( myStdDoc.IsNull() )
187 return myStdDoc->IsSaved();
191 Returns whether the document is modified. [ public ]
193 bool CAF_Study::isModified() const
195 return ( myModifiedCnt != 0 );
199 Increments modification count. If 'undoable' is 'true', this modification
200 can be rolled back by 'undoModified' otherwise the document will be marked
201 as 'modiifed' until saved. [ protected ]
203 void CAF_Study::doModified( bool undoable )
205 if ( myStdDoc.IsNull() )
210 /* Assumed that number of available undos / redos is NOT changed dynamically */
212 myModifiedCnt += myStdDoc->GetAvailableUndos();
216 Decrements modification count. [ protected ]
218 void CAF_Study::undoModified()
224 Clears modification count. [ public ]
226 void CAF_Study::clearModified()
232 Undoes the last command. [ public ]
234 bool CAF_Study::undo()
236 if ( myStdDoc.IsNull() )
241 undoModified(); /* decrement modification counter */
243 catch ( Standard_Failure ) {
244 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
245 tr( "ERR_DOC_UNDO" ), tr ( "BUT_OK" ) );
252 Redoes the last undo. [ public ]
254 bool CAF_Study::redo()
256 if ( myStdDoc.IsNull() )
261 doModified(); /* increment modification counter */
263 catch ( Standard_Failure ) {
264 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
265 tr( "ERR_DOC_REDO" ), tr ( "BUT_OK" ) );
272 Check if possible to perform 'undo' command. [ public ]
274 bool CAF_Study::canUndo() const
276 if ( myStdDoc.IsNull() )
279 return myStdDoc->GetAvailableUndos() > 0;
283 Check if possible to perform 'redo' command. [ public ]
285 bool CAF_Study::canRedo() const
287 if ( myStdDoc.IsNull() )
290 return myStdDoc->GetAvailableRedos() > 0;
294 Returns the list of names of 'undo' actions available. [ public ]
296 QStringList CAF_Study::undoNames() const
299 if ( !myStdDoc.IsNull() )
301 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
302 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
308 Returns the list of names of 'redo' actions available. [ public ]
310 QStringList CAF_Study::redoNames() const
313 if ( !myStdDoc.IsNull() )
315 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
316 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
322 Returns the standard OCAF application from owner application. [ protected ]
324 Handle(TDocStd_Application) CAF_Study::stdApp() const
326 Handle(TDocStd_Application) stdApp;
327 CAF_Application* app = cafApplication();
329 stdApp = app->stdApp();
334 Returns the application casted to type CAF_Application. [ protected ]
336 CAF_Application* CAF_Study::cafApplication() const
338 return ::qt_cast<CAF_Application*>( application() );