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