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