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 if ( myStdDoc.IsNull() )
230 return myStdDoc->IsModified();
234 Increments modification count. If 'undoable' is 'true', this modification
235 can be rolled back by 'undoModified' otherwise the document will be marked
236 as 'modiifed' until saved. [ protected ]
238 void CAF_Study::doModified( bool undoable )
240 if ( myStdDoc.IsNull() )
245 /* Assumed that number of available undos / redos is NOT changed dynamically */
247 myModifiedCnt += myStdDoc->GetAvailableUndos();
251 Decrements modification count. [ protected ]
253 void CAF_Study::undoModified()
259 Clears modification count. [ public ]
261 void CAF_Study::clearModified()
267 Undoes the last command. [ public ]
269 bool CAF_Study::undo()
271 if ( myStdDoc.IsNull() )
276 undoModified(); /* decrement modification counter */
278 catch ( Standard_Failure ) {
279 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
280 tr( "ERR_DOC_UNDO" ), tr ( "BUT_OK" ) );
287 Redoes the last undo. [ public ]
289 bool CAF_Study::redo()
291 if ( myStdDoc.IsNull() )
296 doModified(); /* increment modification counter */
298 catch ( Standard_Failure ) {
299 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
300 tr( "ERR_DOC_REDO" ), tr ( "BUT_OK" ) );
307 Check if possible to perform 'undo' command. [ public ]
309 bool CAF_Study::canUndo() const
311 if ( myStdDoc.IsNull() )
314 return myStdDoc->GetAvailableUndos() > 0;
318 Check if possible to perform 'redo' command. [ public ]
320 bool CAF_Study::canRedo() const
322 if ( myStdDoc.IsNull() )
325 return myStdDoc->GetAvailableRedos() > 0;
329 Returns the list of names of 'undo' actions available. [ public ]
331 QStringList CAF_Study::undoNames() const
334 if ( !myStdDoc.IsNull() )
336 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
337 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
343 Returns the list of names of 'redo' actions available. [ public ]
345 QStringList CAF_Study::redoNames() const
348 if ( !myStdDoc.IsNull() )
350 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
351 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
357 Returns the standard OCAF application from owner application. [ protected ]
359 Handle(TDocStd_Application) CAF_Study::stdApp() const
361 Handle(TDocStd_Application) stdApp;
362 CAF_Application* app = cafApplication();
364 stdApp = app->stdApp();
369 Returns the application casted to type CAF_Application. [ protected ]
371 CAF_Application* CAF_Study::cafApplication() const
373 return ::qt_cast<CAF_Application*>( application() );