Salome HOME
2c0e7d9d7e79514d353401e80d8e9e75f15a30dd
[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   emit stopped();
228
229   document()->abortOperation();
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   emit stopped();
246
247   document()->finishOperation();
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   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
267   std::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(anId.toStdString());
268   aReal->setValue(theValue);
269 }
270
271 /*!
272  * \brief Verifies whether operator is ready to start.
273  * \return TRUE if operation is ready to start
274  *
275  * Default implementation returns TRUE. Redefine this method to add own verifications
276  */
277 bool ModuleBase_Operation::isReadyToStart() const
278 {
279   return true;
280 }
281
282 /*!
283  * \brief Virtual method called when operation is started
284  *
285  * Virtual method called when operation started (see start() method for more description)
286  * Default implementation calls corresponding slot and commits immediately.
287  */
288 void ModuleBase_Operation::startOperation()
289 {
290   std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
291   myFeature = aDoc->addFeature(myOperationId.toStdString());
292   if (myFeature) // TODO: generate an error if feature was not created
293     myFeature->execute();
294   //emit callSlot();
295   //commit();
296 }
297
298 /*!
299  * \brief Virtual method called when operation is started
300  *
301  * Virtual method called when operation stopped - committed or aborted.
302  */
303 void ModuleBase_Operation::stopOperation()
304 {
305 }
306
307 /*!
308  * \brief Virtual method called when operation aborted
309  *
310  * Virtual method called when operation aborted (see abort() method for more description)
311  */
312 void ModuleBase_Operation::abortOperation()
313 {
314 }
315
316 /*!
317  * \brief Virtual method called when operation committed
318  *
319  * Virtual method called when operation committed (see commit() method for more description)
320  */
321 void ModuleBase_Operation::commitOperation()
322 {
323   if (myFeature) myFeature->execute();
324 }
325
326 /*!
327  * \brief Sets execution status
328  * \param theStatus - execution status
329  *
330  * Sets myExecStatus to the given value
331  */
332 void ModuleBase_Operation::setExecStatus(const int theVal)
333 {
334   myExecStatus = (ExecStatus) theVal;
335 }
336
337 /*!
338  * \brief Sets state of operation
339  * \param theState - state of operation to be set
340  *
341  *  Sets state of operation (see OperationState enumeration)
342  */
343 void ModuleBase_Operation::setState(const ModuleBase_Operation::OperationState theState)
344 {
345   myState = theState;
346 }