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