Salome HOME
Issue #6 Disable commands on operation start
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
1 /*
2  * ModuleBase_Operation.cpp
3  *
4  *  Created on: Apr 2, 2014
5  *      Author: sbh
6  */
7
8 #include "ModuleBase_Operation.h"
9
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_PluginManager.h>
15 #include <ModelAPI_Document.h>
16
17 #ifdef _DEBUG
18 #include <QDebug>
19 #endif
20
21 /*!
22  \brief Constructor
23  \param XGUI_Workshop - workshop for this operation
24
25  Constructs an empty operation. Constructor should work very fast because many
26  operators may be created after starting workshop but only several from them
27  may be used. As result this constructor stores given workshop in myApp field
28  and set Waiting status.
29  */
30 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* parent)
31     : QObject(parent),
32       myFlags(Transaction),
33       myState(Waiting),
34       myExecStatus(Rejected),
35       myOperationId(theId)
36 {
37 }
38
39 /*!
40  * \brief Destructor
41  */
42 ModuleBase_Operation::~ModuleBase_Operation()
43 {
44
45 }
46
47 /*!
48  * \brief Unique name of the operation
49  *
50  *  Returns string name of the operation.
51  */
52 QString ModuleBase_Operation::operationId() const
53 {
54   return myOperationId;
55 }
56
57 /*!
58  * \brief Gets state of operation
59  * \return Value from OperationState enumeration
60  *
61  * Gets state of operation (see OperationState enumeration)
62  */
63 ModuleBase_Operation::OperationState ModuleBase_Operation::state() const
64 {
65   return myState;
66 }
67
68 /*!
69  * \brief Verifies whether operation is an ran one (state()==Running)
70  * \return TRUE if operation is active, FALSE otherwise
71  *
72  * Verifies whether operation is an running. Returns TRUE if state of operator
73  * is Running
74  */
75 bool ModuleBase_Operation::isRunning() const
76 {
77   return state() == Running;
78 }
79
80 /*!
81  * \brief Verifies whether given operator is valid for this one
82  * \param theOtherOp - other operation
83  * \return Returns TRUE if the given operator is valid for this one
84  *
85  * Verifies whether given operator is valid for this one (i.e. can be started "above"
86  * this operator)
87  */
88 bool ModuleBase_Operation::isValid(ModuleBase_Operation*) const
89 {
90   return false;
91 }
92
93 /*!
94  * \brief Verifies whether this operator can be always started above any already running one
95  * \return Returns TRUE if current operation must not be checked for ActiveOperation->IsValid( this )
96  *
97  * This method must be redefined in derived operation if operation of derived class
98  * must be always can start above any launched one. Default implementation returns FALSE,
99  * so it is being checked for IsValid, but some operations may overload IsGranted()
100  * In this case they will always start, no matter what operation is running.
101  */
102 bool ModuleBase_Operation::isGranted() const
103 {
104   return false;
105 }
106
107 /*
108  * Returns pointer to the root document.
109  */
110 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
111 {
112   return ModelAPI_PluginManager::get()->rootDocument();
113 }
114
115 /*!
116  * \brief Sets slot which is called when operation is started
117  * \param theReceiver - object containing slot
118  * \param theSlot - slot of theReceiver object
119  * \return TR if slot was connected successfully, FALSE otherwise
120  *
121  * Sets slot which is called when operation is started. There is no point in
122  * using this method. It would be better to inherit own operator from base
123  * one and redefine startOperation method
124  */
125 bool ModuleBase_Operation::setSlot(const QObject* theReceiver, const char* theSlot)
126 {
127   return connect(this, SIGNAL(callSlot()), theReceiver, theSlot);
128 }
129
130 /*!
131  * \brief Sets the flags of operation
132  * \param f - flags of operation to be set
133  *
134  *  Sets flags of operation (see Flags enumeration)
135  */
136 void ModuleBase_Operation::setFlags(const int f)
137 {
138   myFlags = myFlags | f;
139 }
140
141 /*!
142  * \brief Clears the flags of operation
143  * \param f - flags of operation to be cleared
144  *
145  *  Clears flags of operation (see Flags enumeration)
146  */
147 void ModuleBase_Operation::clearFlags(const int f)
148 {
149   myFlags = myFlags & ~f;
150 }
151
152 /*!
153  * \brief Test the flags of operation
154  * \param f - flags of operation to be tested
155  *
156  *  Returns TRUE if the specified flags set in the operation (see Flags enumeration)
157  */
158 bool ModuleBase_Operation::testFlags(const int f) const
159 {
160   return (myFlags & f) == f;
161 }
162
163 /*!
164  * \brief Gets execution status
165  * \return Execution status
166  *
167  * Gets execution status
168  */
169 int ModuleBase_Operation::execStatus() const
170 {
171   return myExecStatus;
172 }
173
174 /*!
175  *  \brief Returns XML representation of the operation's widget.
176  *  \return XML QString
177  *
178  *  Returns XML representation of the operation's widget.
179  */
180 const QString& ModuleBase_Operation::xmlRepresentation() const
181 {
182   return myXmlRepr;
183 }
184
185 /*!
186  *  \brief Sets XML representation of the operation's widget.
187  *  \param xmlRepr - XML QString
188  *
189  *  Sets XML representation of the operation's widget.
190  */
191 void ModuleBase_Operation::setXmlRepresentation(const QString& xmlRepr)
192 {
193   myXmlRepr = xmlRepr;
194 }
195
196 /*!
197  * \brief Starts operation
198  *
199  * Public slot. Verifies whether operation can be started and starts operation.
200  * This slot is not virtual and cannot be redefined. Redefine startOperation method
201  * to change behavior of operation. There is no point in using this method. It would
202  * be better to inherit own operator from base one and redefine startOperation method
203  * instead.
204  */
205 void ModuleBase_Operation::start()
206 {
207   //document()->start(this);
208   document()->startOperation();
209
210   startOperation();
211   emit started();
212 }
213
214 /*!
215  * \brief Aborts operation
216  *
217  * Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
218  * Redefine abortOperation method to change behavior of operation instead
219  */
220 void ModuleBase_Operation::abort()
221 {
222   abortOperation();
223   myState = Waiting;
224   emit aborted();
225
226   stopOperation();
227
228   document()->abortOperation();
229   emit stopped();
230 }
231
232 /*!
233  * \brief Commits operation
234  *
235  * Public slot. Commits operation. This slot is not virtual and cannot be redefined.
236  * Redefine commitOperation method to change behavior of operation instead
237  */
238 void ModuleBase_Operation::commit()
239 {
240   commitOperation();
241   myState = Waiting;
242   emit committed();
243
244   stopOperation();
245
246   document()->finishOperation();
247   emit stopped();
248 }
249
250 /*
251  * \brief Alias for start/abort slots
252  *
253  * Public slot. Aborts operation if false, else does nothing.
254  * Provided for S/S compatibility with QAction's toggle(bool)
255  */
256 void ModuleBase_Operation::setRunning(bool on)
257 {
258   if (!on) {
259     abort();
260   }
261 }
262
263 /*!
264  * \brief Stores a real value in model.
265  * \param theValue - to store
266  *
267  * Public slot. Passes theValue into the model.
268  */
269 void ModuleBase_Operation::storeReal(double theValue)
270 {
271   if(!myFeature){
272     #ifdef _DEBUG
273     qDebug() << "ModuleBase_Operation::storeReal: " <<
274         "trying to store value without opening a transaction.";
275     #endif
276     return;
277   }
278   QString anId = sender()->objectName();
279   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
280   std::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(anId.toStdString());
281   aReal->setValue(theValue);
282 }
283
284 /*!
285  * \brief Verifies whether operator is ready to start.
286  * \return TRUE if operation is ready to start
287  *
288  * Default implementation returns TRUE. Redefine this method to add own verifications
289  */
290 bool ModuleBase_Operation::isReadyToStart() const
291 {
292   return true;
293 }
294
295 /*!
296  * \brief Virtual method called when operation is started
297  *
298  * Virtual method called when operation started (see start() method for more description)
299  * Default implementation calls corresponding slot and commits immediately.
300  */
301 void ModuleBase_Operation::startOperation()
302 {
303   std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
304   myFeature = aDoc->addFeature(myOperationId.toStdString());
305   if (myFeature) // TODO: generate an error if feature was not created
306     myFeature->execute();
307   //emit callSlot();
308   //commit();
309 }
310
311 /*!
312  * \brief Virtual method called when operation is started
313  *
314  * Virtual method called when operation stopped - committed or aborted.
315  */
316 void ModuleBase_Operation::stopOperation()
317 {
318 }
319
320 /*!
321  * \brief Virtual method called when operation aborted
322  *
323  * Virtual method called when operation aborted (see abort() method for more description)
324  */
325 void ModuleBase_Operation::abortOperation()
326 {
327 }
328
329 /*!
330  * \brief Virtual method called when operation committed
331  *
332  * Virtual method called when operation committed (see commit() method for more description)
333  */
334 void ModuleBase_Operation::commitOperation()
335 {
336   if (myFeature) myFeature->execute();
337 }
338
339 /*!
340  * \brief Sets execution status
341  * \param theStatus - execution status
342  *
343  * Sets myExecStatus to the given value
344  */
345 void ModuleBase_Operation::setExecStatus(const int theVal)
346 {
347   myExecStatus = (ExecStatus) theVal;
348 }
349
350 /*!
351  * \brief Sets state of operation
352  * \param theState - state of operation to be set
353  *
354  *  Sets state of operation (see OperationState enumeration)
355  */
356 void ModuleBase_Operation::setState(const ModuleBase_Operation::OperationState theState)
357 {
358   myState = theState;
359 }