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