]> SALOME platform Git repositories - modules/gui.git/blob - src/CAF/CAF_Study.cxx
Salome HOME
79d0da098ad6a9a674bf5a92f20ca7039e4de232
[modules/gui.git] / src / CAF / CAF_Study.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "CAF_Study.h"
20
21 #include "CAF_Tools.h"
22 #include "CAF_Operation.h"
23 #include "CAF_Application.h"
24
25 #include <SUIT_Desktop.h>
26 #include <SUIT_MessageBox.h>
27 #include <SUIT_Application.h>
28
29 #include <qdir.h>
30
31 #include <TDF_Delta.hxx>
32 #include <TDF_ListIteratorOfDeltaList.hxx>
33
34 #include <Standard_Failure.hxx>
35 #include <Standard_ErrorHandler.hxx>
36
37 /*!
38   Constructor
39 */
40 CAF_Study::CAF_Study(SUIT_Application* theApp)
41 : SUIT_Study( theApp ),
42 myModifiedCnt( 0 )
43 {
44 }
45
46 /*!
47   Constructor
48 */
49 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
50 : SUIT_Study( theApp ),
51 myStdDoc( aStdDoc ),
52 myModifiedCnt( 0 )
53 {
54 }
55
56 /*!
57   Destructor
58 */
59 CAF_Study::~CAF_Study()
60 {
61 }
62
63 /*!
64   \return OCAF document
65 */
66 Handle(TDocStd_Document) CAF_Study::stdDoc() const
67 {
68   return myStdDoc;
69 }
70
71 /*!
72   Sets new OCAF document
73   \param aStdDoc - new OCAF document
74 */
75 void CAF_Study::setStdDoc( Handle(TDocStd_Document)& aStdDoc )
76 {
77   myStdDoc = aStdDoc;
78 }
79
80 /*!
81   Custom document initialization
82 */
83 void CAF_Study::createDocument()
84 {
85   SUIT_Study::createDocument();
86
87   CAF_Application* app = cafApplication();
88   if ( app && !app->stdApp().IsNull() )
89   {
90     try {
91 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
92       OCC_CATCH_SIGNALS;
93 #endif
94       TColStd_SequenceOfExtendedString formats;
95       app->stdApp()->Formats( formats );
96       if ( !formats.IsEmpty() )
97         app->stdApp()->NewDocument( formats.First(), myStdDoc );
98     }
99     catch ( Standard_Failure ) {
100     }
101   }
102 }
103
104 /*!
105   Close document
106 */
107 void CAF_Study::closeDocument( bool permanent )
108 {
109   Handle(TDocStd_Application) app = stdApp();
110   if ( !app.IsNull() && !stdDoc().IsNull() )
111     app->Close( stdDoc() );
112
113   SUIT_Study::closeDocument( permanent );
114 }
115
116 /*!
117   Open document
118   \param fname - name of file
119 */
120 bool CAF_Study::openDocument( const QString& fname )
121 {
122   Handle(TDocStd_Application) app = stdApp();
123   if ( app.IsNull() )
124     return false;
125
126   bool status = false;
127   try {
128 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
129     OCC_CATCH_SIGNALS;
130 #endif
131     status = app->Open( CAF_Tools::toExtString( fname ), myStdDoc ) == CDF_RS_OK;
132   }
133   catch ( Standard_Failure ) {
134     status = false;
135   }
136
137   return status && SUIT_Study::openDocument( fname );
138 }
139
140 /*!
141   Save document with other name
142   \param fname - name of file
143 */
144 bool CAF_Study::saveDocumentAs( const QString& fname )
145 {
146   Handle(TDocStd_Application) app = stdApp();
147   if ( app.IsNull() )
148     return false;
149
150   bool save = false;
151   if ( !stdDoc().IsNull() && stdDoc()->IsSaved() )
152   {
153     QString path = QDir::convertSeparators( CAF_Tools::toQString( stdDoc()->GetPath() ) );
154     save = path == QDir::convertSeparators( fname );
155   }
156
157   bool status = false;
158   try {
159 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
160     OCC_CATCH_SIGNALS;
161 #endif
162     if ( save )
163       status = app->Save( stdDoc() ) == CDF_SS_OK;
164     else
165     {
166       TCollection_ExtendedString format, path( CAF_Tools::toExtString( fname ) );
167       app->Format( path, format );
168
169       if ( format.Length() )
170         stdDoc()->ChangeStorageFormat( format );
171
172       status = app->SaveAs( stdDoc(), path ) == CDF_SS_OK;
173     }
174   }
175   catch ( Standard_Failure ) {
176     status = false;
177   }
178
179   if ( status )
180     status = SUIT_Study::saveDocumentAs( fname );
181
182   if ( status )
183     myModifiedCnt = 0;
184
185   return status;
186 }
187
188 /*!
189   Open OCAF transaction
190 */
191 bool CAF_Study::openTransaction()
192 {
193   if ( myStdDoc.IsNull() )
194     return false;
195
196   bool res = true;
197   try {
198 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
199     OCC_CATCH_SIGNALS;
200 #endif
201     if ( myStdDoc->HasOpenCommand() )
202       myStdDoc->AbortCommand();
203
204     myStdDoc->OpenCommand();
205   }
206   catch ( Standard_Failure ) {
207     res = false;
208   }
209
210   return res;
211 }
212
213 /*!
214   Abort OCAF transaction
215 */
216 bool CAF_Study::abortTransaction()
217 {
218   if ( myStdDoc.IsNull() )
219     return false;
220
221   bool res = true;
222   try {
223 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
224     OCC_CATCH_SIGNALS;
225 #endif
226     myStdDoc->AbortCommand();
227     update();
228   }
229   catch ( Standard_Failure ) {
230     res = false;
231   }
232   return res;
233 }
234
235 /*!
236   Commit OCAF transaction
237 */
238 bool CAF_Study::commitTransaction( const QString& name )
239 {
240   if ( myStdDoc.IsNull() )
241     return false;
242
243   bool res = true;
244   try {
245 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
246     OCC_CATCH_SIGNALS;
247 #endif
248     myStdDoc->CommitCommand();
249
250     if ( canUndo() )
251     {
252       Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
253       if ( !d.IsNull() )
254         d->SetName( CAF_Tools::toExtString( name ) );
255     }
256   }
257   catch ( Standard_Failure ) {
258     res = false;
259   }
260   return res;
261 }
262
263 /*!
264   \return true, if there is opened OCAF transaction
265 */
266 bool CAF_Study::hasTransaction() const
267 {
268   if ( myStdDoc.IsNull() )
269     return false;
270
271   return myStdDoc->HasOpenCommand();
272 }
273
274 /*!
275   \return whether the document was saved in file. [ public ]
276 */
277 bool CAF_Study::isSaved() const
278 {
279   if ( myStdDoc.IsNull() )
280     return false;
281
282   return myStdDoc->IsSaved();
283 }
284
285 /*!
286   \return whether the document is modified. [ public ]
287 */
288 bool CAF_Study::isModified() const
289 {
290   if ( myStdDoc.IsNull() )
291     return false;
292
293 //  return myStdDoc->IsModified();
294   return myModifiedCnt;
295 }
296
297 /*!
298     Increments modification count. If 'undoable' is 'true', this modification
299     can be rolled back by 'undoModified' otherwise the document will be marked
300     as 'modiifed' until saved. [ protected ]
301 */
302 void CAF_Study::doModified( bool undoable )
303 {
304         if ( myStdDoc.IsNull() )
305     return;
306
307         myModifiedCnt++;
308
309     /*  Assumed that number of available undos / redos is NOT changed dynamically */
310         if ( !undoable )
311     myModifiedCnt += myStdDoc->GetAvailableUndos();
312 }
313
314 /*!
315     Decrements modification count. [ protected ]
316 */
317 void CAF_Study::undoModified()
318 {
319   myModifiedCnt--;
320 }
321
322 /*!
323     Clears modification count. [ public ]
324 */
325 void CAF_Study::clearModified()
326 {
327   myModifiedCnt = 0;
328 }
329
330 /*!
331     Undoes the last command. [ public ]
332 */
333 bool CAF_Study::undo()
334 {
335   if ( myStdDoc.IsNull() )
336     return false;
337
338   try {
339 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
340     OCC_CATCH_SIGNALS;
341 #endif
342     myStdDoc->Undo();
343     undoModified();     /* decrement modification counter */
344   }
345   catch ( Standard_Failure ) {
346     SUIT_MessageBox::error1(application()->desktop(), tr( "ERR_ERROR" ),
347                             tr( "ERR_DOC_UNDO" ), tr ( "BUT_OK" ));
348     return false;
349   }
350   return true;
351 }
352
353 /*!
354     Redoes the last undo. [ public ]
355 */
356 bool CAF_Study::redo()
357 {
358   if ( myStdDoc.IsNull() )
359     return false;
360
361   try {
362 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
363     OCC_CATCH_SIGNALS;
364 #endif
365     myStdDoc->Redo();
366     doModified();      /* increment modification counter */
367   }
368   catch ( Standard_Failure ) {
369     SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
370                              tr( "ERR_DOC_REDO" ), tr ( "BUT_OK" ) );
371     return false;
372   }
373   return true;
374 }
375
376 /*!
377   \return true if possible to perform 'undo' command. [ public ]
378 */
379 bool CAF_Study::canUndo() const
380 {
381   if ( myStdDoc.IsNull() )
382     return false;
383
384   return myStdDoc->GetAvailableUndos() > 0;
385 }
386
387 /*!
388   \return true if possible to perform 'redo' command. [ public ]
389 */
390 bool CAF_Study::canRedo() const
391 {
392   if ( myStdDoc.IsNull() )
393     return false;
394
395   return myStdDoc->GetAvailableRedos() > 0;
396 }
397
398 /*!
399   \return the list of names of 'undo' actions available. [ public ]
400 */
401 QStringList CAF_Study::undoNames() const
402 {
403   QStringList names;
404   if ( !myStdDoc.IsNull() )
405   {
406     for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
407       names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
408   }
409   return names;
410 }
411
412 /*!
413   \return the list of names of 'redo' actions available. [ public ]
414 */
415 QStringList CAF_Study::redoNames() const
416 {
417   QStringList names;
418   if ( !myStdDoc.IsNull() )
419   {
420     for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
421       names.append( CAF_Tools::toQString( it.Value()->Name() ) );
422   }
423   return names;
424 }
425
426 /*!
427   \return the standard OCAF application from owner application. [ protected ]
428 */
429 Handle(TDocStd_Application) CAF_Study::stdApp() const
430 {
431   Handle(TDocStd_Application) stdApp;
432   CAF_Application* app = cafApplication();
433   if ( app )
434     stdApp = app->stdApp();
435   return stdApp;
436 }
437
438 /*!
439   \return the application casted to type CAF_Application. [ protected ]
440 */
441 CAF_Application* CAF_Study::cafApplication() const
442 {
443   return ::qt_cast<CAF_Application*>( application() );
444 }