Salome HOME
Added about dilaog message for CAF_Application class scope
[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
6 #include <SUIT_Desktop.h>
7 #include <SUIT_MessageBox.h>
8 #include <SUIT_Application.h>
9
10 #include <TDF_Delta.hxx>
11 #include <TDF_ListIteratorOfDeltaList.hxx>
12
13 //////////////////////////////////////////////////////////////////////
14 // Construction/Destruction
15 //////////////////////////////////////////////////////////////////////
16
17 CAF_Study::CAF_Study(SUIT_Application* theApp)
18 : SUIT_Study( theApp ),
19 myModifiedCnt( 0 )
20 {
21 }
22
23 CAF_Study::CAF_Study(SUIT_Application* theApp, Handle (TDocStd_Document)& aStdDoc)
24 : SUIT_Study( theApp ),
25 myStdDoc( aStdDoc ),
26 myModifiedCnt( 0 )
27 {
28 }
29
30 CAF_Study::~CAF_Study()
31 {
32 }
33
34 bool CAF_Study::startOperation()
35 {
36         if ( myStdDoc.IsNull() )
37     return false;
38
39   bool res = true;
40   try {
41     if ( myStdDoc->HasOpenCommand() )
42       myStdDoc->AbortCommand();
43
44     myStdDoc->OpenCommand();
45   }
46   catch( Standard_Failure ) {
47     res = false;
48   }
49
50   return res;
51 }
52
53 void CAF_Study::abortOperation()
54 {
55         if ( myStdDoc.IsNull() )
56     return;
57
58         try {
59     myStdDoc->AbortCommand();
60                 update();
61   }
62   catch ( Standard_Failure ) {
63   }
64 }
65
66 void CAF_Study::commitOperation()
67 {
68         if ( myStdDoc.IsNull() )
69     return;
70
71         try {
72     myStdDoc->CommitCommand();
73
74     if ( canUndo() )
75     {
76       CAF_Operation* cafOp = 0;
77       if ( activeOperation() && activeOperation()->inherits( "CAF_Operation" ) )
78         cafOp = (CAF_Operation*)activeOperation();
79
80       Handle(TDF_Delta) d = myStdDoc->GetUndos().Last();
81                         if ( cafOp && !d.IsNull() )
82         d->SetName( CAF_Tools::toExtString( cafOp->getName() ) );
83     }
84   }
85   catch ( Standard_Failure ) {
86   }
87 }
88
89 /*!
90     Returns whether the document was saved in file. [ public ]
91 */
92 bool CAF_Study::isSaved() const
93 {
94         if ( myStdDoc.IsNull() )
95     return false;
96
97   return myStdDoc->IsSaved();
98 }
99
100 /*!
101     Returns whether the document is modified. [ public ]
102 */
103 bool CAF_Study::isModified() const
104 {
105   return ( myModifiedCnt != 0 );
106 }
107
108 /*!
109     Increments modification count. If 'undoable' is 'true', this modification
110     can be rolled back by 'undoModified' otherwise the document will be marked
111     as 'modiifed' until saved. [ protected ]
112 */
113 void CAF_Study::doModified( bool undoable )
114 {
115         if ( myStdDoc.IsNull() )
116     return;
117
118         myModifiedCnt++;
119
120     /*  Assumed that number of available undos / redos is NOT changed dynamically */
121         if ( !undoable )
122     myModifiedCnt += myStdDoc->GetAvailableUndos();
123 }
124
125 /*!
126     Decrements modification count. [ protected ]
127 */
128 void CAF_Study::undoModified()
129 {
130   myModifiedCnt--;
131 }
132
133 /*!
134     Clears modification count. [ public ]
135 */
136 void CAF_Study::clearModified()
137 {
138   myModifiedCnt = 0;
139 }
140
141 /*!
142     Undoes the last command. [ public ]
143 */
144 bool CAF_Study::undo()
145 {
146         if ( myStdDoc.IsNull() )
147     return false;
148
149   try {
150     myStdDoc->Undo();
151     undoModified();     /* decrement modification counter */
152   }
153   catch ( Standard_Failure ) {
154                 SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
155                              tr( "ERR_DOC_UNDO" ), tr ( "BUT_OK" ) );
156                 return false;
157         }
158   return true;
159 }
160
161 /*!
162     Redoes the last undo. [ public ]
163 */
164 bool CAF_Study::redo()
165 {
166         if ( myStdDoc.IsNull() )
167     return false;
168
169   try {
170     myStdDoc->Redo();
171     doModified();      /* increment modification counter */
172   }
173   catch ( Standard_Failure ) {
174     SUIT_MessageBox::error1( application()->desktop(), tr( "ERR_ERROR" ),
175                              tr( "ERR_DOC_REDO" ), tr ( "BUT_OK" ) );
176     return false;
177   }
178   return true;
179 }
180
181 /*!
182     Check if possible to perform 'undo' command. [ public ]
183 */
184 bool CAF_Study::canUndo() const
185 {
186   if ( myStdDoc.IsNull() )
187     return false;
188
189   return myStdDoc->GetAvailableUndos() > 0;
190 }
191
192 /*!
193     Check if possible to perform 'redo' command. [ public ]
194 */
195 bool CAF_Study::canRedo() const
196 {
197   if ( myStdDoc.IsNull() )
198     return false;
199
200   return myStdDoc->GetAvailableRedos() > 0;
201 }
202
203 /*!
204     Returns the list of names of 'undo' actions available. [ public ]
205 */
206 QStringList CAF_Study::undoNames() const
207 {
208   QStringList names;
209   if ( !myStdDoc.IsNull() )
210   {
211     for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetUndos() ); it.More(); it.Next() )
212       names.prepend( CAF_Tools::toQString( it.Value()->Name() ) );
213   }
214   return names;
215 }
216
217 /*!
218     Returns the list of names of 'redo' actions available. [ public ]
219 */
220 QStringList CAF_Study::redoNames() const
221 {
222   QStringList names;
223   if ( !myStdDoc.IsNull() )
224   {
225     for ( TDF_ListIteratorOfDeltaList it( myStdDoc->GetRedos() ); it.More(); it.Next() )
226       names.append( CAF_Tools::toQString( it.Value()->Name() ) );
227   }
228   return names;
229 }