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