]> SALOME platform Git repositories - modules/gui.git/blob - src/CAF/CAF_Study.cxx
Salome HOME
c69305e3a2c94d62fc2de43479f1bd4acc66d0b9
[modules/gui.git] / src / CAF / CAF_Study.cxx
1 #include "CAF_Study.h"
2
3 #include "CAF_Tools.h"
4 #include "CAF_Operation.h"
5 #include "CAF_Application.h"
6
7 #include <SUIT_Desktop.h>
8 #include <SUIT_MessageBox.h>
9 #include <SUIT_Application.h>
10
11 #include <qdir.h>
12
13 #include <TDF_Delta.hxx>
14 #include <TDF_ListIteratorOfDeltaList.hxx>
15
16 #include <Standard_ErrorHandler.hxx>
17
18 //////////////////////////////////////////////////////////////////////
19 // Construction/Destruction
20 //////////////////////////////////////////////////////////////////////
21
22 CAF_Study::CAF_Study(SUIT_Application* theApp)
23 : SUIT_Study( theApp ),
24 myModifiedCnt( 0 )
25 {
26 }
27
28 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
29 : SUIT_Study( theApp ),
30 myStdDoc( aStdDoc ),
31 myModifiedCnt( 0 )
32 {
33 }
34
35 CAF_Study::~CAF_Study()
36 {
37 }
38
39 Handle(TDocStd_Document) CAF_Study::stdDoc() const
40 {
41   return myStdDoc;
42 }
43
44 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
45 {
46   myStdDoc = aStdDoc;
47 }
48
49 void CAF_Study::createDocument()
50 {
51   SUIT_Study::createDocument();
52
53   CAF_Application* app = cafApplication();
54   if ( app && !app->stdApp().IsNull() )
55   {
56     try {
57       TColStd_SequenceOfExtendedString formats;
58             app->stdApp()->Formats( formats );
59       if ( !formats.IsEmpty() )
60         app->stdApp()->NewDocument( formats.First(), myStdDoc );
61     }
62     catch ( Standard_Failure ) {
63     }
64   }
65 }
66
67 void CAF_Study::closeDocument( bool permanent )
68 {
69   Handle(TDocStd_Application) app = stdApp();
70   if ( !app.IsNull() && !stdDoc().IsNull() )
71     app->Close( stdDoc() );
72
73   SUIT_Study::closeDocument( permanent );
74 }
75
76 bool CAF_Study::openDocument( const QString& fname )
77 {
78   Handle(TDocStd_Application) app = stdApp();
79   if ( app.IsNull() )
80     return false;
81
82   bool status = false;
83   try {
84     status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == CDF_RS_OK;
85   }
86   catch ( Standard_Failure ) {
87     status = false;
88   }
89
90   return status && SUIT_Study::openDocument( fname );
91 }
92
93 bool CAF_Study::saveDocumentAs( const QString& fname )
94 {
95   Handle(TDocStd_Application) app = stdApp();
96   if ( app.IsNull() )
97     return false;
98
99   bool save = false;
100   if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
101   {
102     QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
103     save = path == QDir::convertSeparators( fname );
104   }
105
106   bool status = false;
107   try {
108     if ( save )
109       status = app->Save( stdDoc() ) == CDF_SS_OK;
110     else
111     {
112       TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
113       app->Format( path, format );
114
115       if ( format.Length() )
116         stdDoc()->ChangeStorageFormat( format );
117
118       status = app->SaveAs( stdDoc(), path ) == CDF_SS_OK;
119     }
120   }
121   catch ( Standard_Failure ) {
122     status = false;
123   }
124
125   return status && SUIT_Study::saveDocumentAs( fname );
126 }
127
128 bool CAF_Study::openTransaction()
129 {
130         if ( myStdDoc.IsNull() )
131     return false;
132
133   bool res = true;
134   try {
135     if ( myStdDoc->HasOpenCommand() )
136       myStdDoc->AbortCommand();
137
138     myStdDoc->OpenCommand();
139   }
140   catch ( Standard_Failure ) {
141     res = false;
142   }
143
144   return res;
145 }
146
147 bool CAF_Study::abortTransaction()
148 {
149         if ( myStdDoc.IsNull() )
150     return false;
151
152   bool res = true;
153         try {
154     myStdDoc->AbortCommand();
155                 update();
156   }
157   catch ( Standard_Failure ) {
158     res = false;
159   }
160   return res;
161 }
162
163 bool CAF_Study::commitTransaction( const QString& name )
164 {
165         if ( myStdDoc.IsNull() )
166     return false;
167
168   bool res = true;
169         try {
170     myStdDoc->CommitCommand();
171
172     if ( canUndo() )
173     {
174       Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
175                         if ( !d.IsNull() )
176         d->SetName( CAF_Tools::toExtString( name ) );
177     }
178   }
179   catch ( Standard_Failure ) {
180     res = false;
181   }
182   return res;
183 }
184
185 bool CAF_Study::hasTransaction() const
186 {
187         if ( myStdDoc.IsNull() )
188     return false;
189
190   return myStdDoc->HasOpenCommand();
191 }
192
193 /*!
194     Returns whether the document was saved in file. [ public ]
195 */
196 bool CAF_Study::isSaved() const
197 {
198         if ( myStdDoc.IsNull() )
199     return false;
200
201   return myStdDoc->IsSaved();
202 }
203
204 /*!
205     Returns whether the document is modified. [ public ]
206 */
207 bool CAF_Study::isModified() const
208 {
209   return ( myModifiedCnt != 0 );
210 }
211
212 /*!
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 ]
216 */
217 void CAF_Study::doModified( bool undoable )
218 {
219         if ( myStdDoc.IsNull() )
220     return;
221
222         myModifiedCnt++;
223
224     /*  Assumed that number of available undos / redos is NOT changed dynamically */
225         if ( !undoable )
226     myModifiedCnt += myStdDoc->GetAvailableUndos();
227 }
228
229 /*!
230     Decrements modification count. [ protected ]
231 */
232 void CAF_Study::undoModified()
233 {
234   myModifiedCnt--;
235 }
236
237 /*!
238     Clears modification count. [ public ]
239 */
240 void CAF_Study::clearModified()
241 {
242   myModifiedCnt = 0;
243 }
244
245 /*!
246     Undoes the last command. [ public ]
247 */
248 bool CAF_Study::undo()
249 {
250         if ( myStdDoc.IsNull() )
251     return false;
252
253   try {
254     myStdDoc->Undo();
255     undoModified();     /* decrement modification counter */
256   }
257   catch ( Standard_Failure ) {
258                 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
259                              tr( "ERR_DOC_UNDO" ), tr ( "BUT_OK" ) );
260                 return false;
261         }
262   return true;
263 }
264
265 /*!
266     Redoes the last undo. [ public ]
267 */
268 bool CAF_Study::redo()
269 {
270         if ( myStdDoc.IsNull() )
271     return false;
272
273   try {
274     myStdDoc->Redo();
275     doModified();      /* increment modification counter */
276   }
277   catch ( Standard_Failure ) {
278     SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
279                              tr( "ERR_DOC_REDO" ), tr ( "BUT_OK" ) );
280     return false;
281   }
282   return true;
283 }
284
285 /*!
286     Check if possible to perform 'undo' command. [ public ]
287 */
288 bool CAF_Study::canUndo() const
289 {
290   if ( myStdDoc.IsNull() )
291     return false;
292
293   return myStdDoc->GetAvailableUndos() > 0;
294 }
295
296 /*!
297     Check if possible to perform 'redo' command. [ public ]
298 */
299 bool CAF_Study::canRedo() const
300 {
301   if ( myStdDoc.IsNull() )
302     return false;
303
304   return myStdDoc->GetAvailableRedos() > 0;
305 }
306
307 /*!
308     Returns the list of names of 'undo' actions available. [ public ]
309 */
310 QStringList CAF_Study::undoNames() const
311 {
312   QStringList names;
313   if ( !myStdDoc.IsNull() )
314   {
315     for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
316       names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
317   }
318   return names;
319 }
320
321 /*!
322     Returns the list of names of 'redo' actions available. [ public ]
323 */
324 QStringList CAF_Study::redoNames() const
325 {
326   QStringList names;
327   if ( !myStdDoc.IsNull() )
328   {
329     for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
330       names.append( CAF_Tools::toQString( it.Value()->Name() ) );
331   }
332   return names;
333 }
334
335 /*!
336     Returns the standard OCAF application from owner application. [ protected ]
337 */
338 Handle(TDocStd_Application) CAF_Study::stdApp() const
339 {
340   Handle(TDocStd_Application) stdApp;
341   CAF_Application* app = cafApplication();
342   if ( app )
343     stdApp = app->stdApp();
344   return stdApp;
345 }
346
347 /*!
348     Returns the application casted to type CAF_Application. [ protected ]
349 */
350 CAF_Application* CAF_Study::cafApplication() const
351 {
352   return ::qt_cast<CAF_Application*>( application() );
353 }