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