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/
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_ErrorHandler.hxx>
36 //////////////////////////////////////////////////////////////////////
37 // Construction/Destruction
38 //////////////////////////////////////////////////////////////////////
40 CAF_Study::CAF_Study(SUIT_Application* theApp)
41 : SUIT_Study( theApp ),
46 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
47 : SUIT_Study( theApp ),
53 CAF_Study::~CAF_Study()
57 Handle(TDocStd_Document) CAF_Study::stdDoc() const
62 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
67 void CAF_Study::createDocument()
69 SUIT_Study::createDocument();
71 CAF_Application* app = cafApplication();
72 if ( app && !app->stdApp().IsNull() )
75 TColStd_SequenceOfExtendedString formats;
76 app->stdApp()->Formats( formats );
77 if ( !formats.IsEmpty() )
78 app->stdApp()->NewDocument( formats.First(), myStdDoc );
80 catch ( Standard_Failure ) {
85 void CAF_Study::closeDocument( bool permanent )
87 Handle(TDocStd_Application) app = stdApp();
88 if ( !app.IsNull() && !stdDoc().IsNull() )
89 app->Close( stdDoc() );
91 SUIT_Study::closeDocument( permanent );
94 bool CAF_Study::openDocument( const QString& fname )
96 Handle(TDocStd_Application) app = stdApp();
102 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == CDF_RS_OK;
104 catch ( Standard_Failure ) {
108 return status && SUIT_Study::openDocument( fname );
111 bool CAF_Study::saveDocumentAs( const QString& fname )
113 Handle(TDocStd_Application) app = stdApp();
118 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
120 QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
121 save = path == QDir::convertSeparators( fname );
127 status = app->Save( stdDoc() ) == CDF_SS_OK;
130 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
131 app->Format( path, format );
133 if ( format.Length() )
134 stdDoc()->ChangeStorageFormat( format );
136 status = app->SaveAs( stdDoc(), path ) == CDF_SS_OK;
139 catch ( Standard_Failure ) {
143 return status && SUIT_Study::saveDocumentAs( fname );
146 bool CAF_Study::openTransaction()
148 if ( myStdDoc.IsNull() )
153 if ( myStdDoc->HasOpenCommand() )
154 myStdDoc->AbortCommand();
156 myStdDoc->OpenCommand();
158 catch ( Standard_Failure ) {
165 bool CAF_Study::abortTransaction()
167 if ( myStdDoc.IsNull() )
172 myStdDoc->AbortCommand();
175 catch ( Standard_Failure ) {
181 bool CAF_Study::commitTransaction( const QString& name )
183 if ( myStdDoc.IsNull() )
188 myStdDoc->CommitCommand();
192 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
194 d->SetName( CAF_Tools::toExtString( name ) );
197 catch ( Standard_Failure ) {
203 bool CAF_Study::hasTransaction() const
205 if ( myStdDoc.IsNull() )
208 return myStdDoc->HasOpenCommand();
212 Returns whether the document was saved in file. [ public ]
214 bool CAF_Study::isSaved() const
216 if ( myStdDoc.IsNull() )
219 return myStdDoc->IsSaved();
223 Returns whether the document is modified. [ public ]
225 bool CAF_Study::isModified() const
227 return ( myModifiedCnt != 0 );
231 Increments modification count. If 'undoable' is 'true', this modification
232 can be rolled back by 'undoModified' otherwise the document will be marked
233 as 'modiifed' until saved. [ protected ]
235 void CAF_Study::doModified( bool undoable )
237 if ( myStdDoc.IsNull() )
242 /* Assumed that number of available undos / redos is NOT changed dynamically */
244 myModifiedCnt += myStdDoc->GetAvailableUndos();
248 Decrements modification count. [ protected ]
250 void CAF_Study::undoModified()
256 Clears modification count. [ public ]
258 void CAF_Study::clearModified()
264 Undoes the last command. [ public ]
266 bool CAF_Study::undo()
268 if ( myStdDoc.IsNull() )
273 undoModified(); /* decrement modification counter */
275 catch ( Standard_Failure ) {
276 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
277 tr( "ERR_DOC_UNDO" ), tr ( "BUT_OK" ) );
284 Redoes the last undo. [ public ]
286 bool CAF_Study::redo()
288 if ( myStdDoc.IsNull() )
293 doModified(); /* increment modification counter */
295 catch ( Standard_Failure ) {
296 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
297 tr( "ERR_DOC_REDO" ), tr ( "BUT_OK" ) );
304 Check if possible to perform 'undo' command. [ public ]
306 bool CAF_Study::canUndo() const
308 if ( myStdDoc.IsNull() )
311 return myStdDoc->GetAvailableUndos() > 0;
315 Check if possible to perform 'redo' command. [ public ]
317 bool CAF_Study::canRedo() const
319 if ( myStdDoc.IsNull() )
322 return myStdDoc->GetAvailableRedos() > 0;
326 Returns the list of names of 'undo' actions available. [ public ]
328 QStringList CAF_Study::undoNames() const
331 if ( !myStdDoc.IsNull() )
333 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
334 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
340 Returns the list of names of 'redo' actions available. [ public ]
342 QStringList CAF_Study::redoNames() const
345 if ( !myStdDoc.IsNull() )
347 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
348 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
354 Returns the standard OCAF application from owner application. [ protected ]
356 Handle(TDocStd_Application) CAF_Study::stdApp() const
358 Handle(TDocStd_Application) stdApp;
359 CAF_Application* app = cafApplication();
361 stdApp = app->stdApp();
366 Returns the application casted to type CAF_Application. [ protected ]
368 CAF_Application* CAF_Study::cafApplication() const
370 return ::qt_cast<CAF_Application*>( application() );