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