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>
39 CAF_Study::CAF_Study(SUIT_Application* theApp)
40 : SUIT_Study( theApp ),
48 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
49 : SUIT_Study( theApp ),
58 CAF_Study::~CAF_Study()
65 Handle(TDocStd_Document) CAF_Study::stdDoc() const
71 Sets new OCAF document
72 \param aStdDoc - new OCAF document
74 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
80 Custom document initialization
82 void CAF_Study::createDocument()
84 SUIT_Study::createDocument();
86 CAF_Application* app = cafApplication();
87 if ( app && !app->stdApp().IsNull() )
90 TColStd_SequenceOfExtendedString formats;
91 app->stdApp()->Formats( formats );
92 if ( !formats.IsEmpty() )
93 app->stdApp()->NewDocument( formats.First(), myStdDoc );
95 catch ( Standard_Failure ) {
103 void CAF_Study::closeDocument( bool permanent )
105 Handle(TDocStd_Application) app = stdApp();
106 if ( !app.IsNull() && !stdDoc().IsNull() )
107 app->Close( stdDoc() );
109 SUIT_Study::closeDocument( permanent );
114 \param fname - name of file
116 bool CAF_Study::openDocument( const QString& fname )
118 Handle(TDocStd_Application) app = stdApp();
124 status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == CDF_RS_OK;
126 catch ( Standard_Failure ) {
130 return status && SUIT_Study::openDocument( fname );
134 Save document with other name
135 \param fname - name of file
137 bool CAF_Study::saveDocumentAs( const QString& fname )
139 Handle(TDocStd_Application) app = stdApp();
144 if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
146 QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
147 save = path == QDir::convertSeparators( fname );
153 status = app->Save( stdDoc() ) == CDF_SS_OK;
156 TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
157 app->Format( path, format );
159 if ( format.Length() )
160 stdDoc()->ChangeStorageFormat( format );
162 status = app->SaveAs( stdDoc(), path ) == CDF_SS_OK;
165 catch ( Standard_Failure ) {
170 status = SUIT_Study::saveDocumentAs( fname );
179 Open OCAF transaction
181 bool CAF_Study::openTransaction()
183 if ( myStdDoc.IsNull() )
188 if ( myStdDoc->HasOpenCommand() )
189 myStdDoc->AbortCommand();
191 myStdDoc->OpenCommand();
193 catch ( Standard_Failure ) {
201 Abort OCAF transaction
203 bool CAF_Study::abortTransaction()
205 if ( myStdDoc.IsNull() )
210 myStdDoc->AbortCommand();
213 catch ( Standard_Failure ) {
220 Commit OCAF transaction
222 bool CAF_Study::commitTransaction( const QString& name )
224 if ( myStdDoc.IsNull() )
229 myStdDoc->CommitCommand();
233 Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
235 d->SetName( CAF_Tools::toExtString( name ) );
238 catch ( Standard_Failure ) {
245 \return true, if there is opened OCAF transaction
247 bool CAF_Study::hasTransaction() const
249 if ( myStdDoc.IsNull() )
252 return myStdDoc->HasOpenCommand();
256 \return whether the document was saved in file. [ public ]
258 bool CAF_Study::isSaved() const
260 if ( myStdDoc.IsNull() )
263 return myStdDoc->IsSaved();
267 \return whether the document is modified. [ public ]
269 bool CAF_Study::isModified() const
271 if ( myStdDoc.IsNull() )
274 // return myStdDoc->IsModified();
275 return myModifiedCnt;
279 Increments modification count. If 'undoable' is 'true', this modification
280 can be rolled back by 'undoModified' otherwise the document will be marked
281 as 'modiifed' until saved. [ protected ]
283 void CAF_Study::doModified( bool undoable )
285 if ( myStdDoc.IsNull() )
290 /* Assumed that number of available undos / redos is NOT changed dynamically */
292 myModifiedCnt += myStdDoc->GetAvailableUndos();
296 Decrements modification count. [ protected ]
298 void CAF_Study::undoModified()
304 Clears modification count. [ public ]
306 void CAF_Study::clearModified()
312 Undoes the last command. [ public ]
314 bool CAF_Study::undo()
316 if ( myStdDoc.IsNull() )
321 undoModified(); /* decrement modification counter */
323 catch ( Standard_Failure ) {
324 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
325 tr( "ERR_DOC_UNDO" ), tr ( "BUT_OK" ) );
332 Redoes the last undo. [ public ]
334 bool CAF_Study::redo()
336 if ( myStdDoc.IsNull() )
341 doModified(); /* increment modification counter */
343 catch ( Standard_Failure ) {
344 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
345 tr( "ERR_DOC_REDO" ), tr ( "BUT_OK" ) );
352 \return true if possible to perform 'undo' command. [ public ]
354 bool CAF_Study::canUndo() const
356 if ( myStdDoc.IsNull() )
359 return myStdDoc->GetAvailableUndos() > 0;
363 \return true if possible to perform 'redo' command. [ public ]
365 bool CAF_Study::canRedo() const
367 if ( myStdDoc.IsNull() )
370 return myStdDoc->GetAvailableRedos() > 0;
374 \return the list of names of 'undo' actions available. [ public ]
376 QStringList CAF_Study::undoNames() const
379 if ( !myStdDoc.IsNull() )
381 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
382 names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
388 \return the list of names of 'redo' actions available. [ public ]
390 QStringList CAF_Study::redoNames() const
393 if ( !myStdDoc.IsNull() )
395 for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
396 names.append( CAF_Tools::toQString( it.Value()->Name() ) );
402 \return the standard OCAF application from owner application. [ protected ]
404 Handle(TDocStd_Application) CAF_Study::stdApp() const
406 Handle(TDocStd_Application) stdApp;
407 CAF_Application* app = cafApplication();
409 stdApp = app->stdApp();
414 \return the application casted to type CAF_Application. [ protected ]
416 CAF_Application* CAF_Study::cafApplication() const
418 return ::qt_cast<CAF_Application*>( application() );