2 * ModuleBase_Operation.cpp
4 * Created on: Apr 2, 2014
8 #include "ModuleBase_Operation.h"
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>
23 \param XGUI_Workshop - workshop for this operation
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.
30 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* parent)
34 myExecStatus(Rejected),
42 ModuleBase_Operation::~ModuleBase_Operation()
48 * \brief Unique name of the operation
50 * Returns string name of the operation.
52 QString ModuleBase_Operation::operationId() const
58 * \brief Gets state of operation
59 * \return Value from OperationState enumeration
61 * Gets state of operation (see OperationState enumeration)
63 ModuleBase_Operation::OperationState ModuleBase_Operation::state() const
69 * \brief Verifies whether operation is an ran one (state()==Running)
70 * \return TRUE if operation is active, FALSE otherwise
72 * Verifies whether operation is an running. Returns TRUE if state of operator
75 bool ModuleBase_Operation::isRunning() const
77 return state() == Running;
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
85 * Verifies whether given operator is valid for this one (i.e. can be started "above"
88 bool ModuleBase_Operation::isValid(ModuleBase_Operation*) const
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 )
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.
102 bool ModuleBase_Operation::isGranted() const
108 * Returns pointer to the root document.
110 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
112 return ModelAPI_PluginManager::get()->rootDocument();
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
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
125 bool ModuleBase_Operation::setSlot(const QObject* theReceiver, const char* theSlot)
127 return connect(this, SIGNAL(callSlot()), theReceiver, theSlot);
131 * \brief Sets the flags of operation
132 * \param f - flags of operation to be set
134 * Sets flags of operation (see Flags enumeration)
136 void ModuleBase_Operation::setFlags(const int f)
138 myFlags = myFlags | f;
142 * \brief Clears the flags of operation
143 * \param f - flags of operation to be cleared
145 * Clears flags of operation (see Flags enumeration)
147 void ModuleBase_Operation::clearFlags(const int f)
149 myFlags = myFlags & ~f;
153 * \brief Test the flags of operation
154 * \param f - flags of operation to be tested
156 * Returns TRUE if the specified flags set in the operation (see Flags enumeration)
158 bool ModuleBase_Operation::testFlags(const int f) const
160 return (myFlags & f) == f;
164 * \brief Gets execution status
165 * \return Execution status
167 * Gets execution status
169 int ModuleBase_Operation::execStatus() const
175 * \brief Returns XML representation of the operation's widget.
176 * \return XML QString
178 * Returns XML representation of the operation's widget.
180 const QString& ModuleBase_Operation::xmlRepresentation() const
186 * \brief Sets XML representation of the operation's widget.
187 * \param xmlRepr - XML QString
189 * Sets XML representation of the operation's widget.
191 void ModuleBase_Operation::setXmlRepresentation(const QString& xmlRepr)
197 * \brief Starts operation
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
205 void ModuleBase_Operation::start()
207 //document()->start(this);
208 document()->startOperation();
215 * \brief Aborts operation
217 * Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
218 * Redefine abortOperation method to change behavior of operation instead
220 void ModuleBase_Operation::abort()
228 document()->abortOperation();
233 * \brief Commits operation
235 * Public slot. Commits operation. This slot is not virtual and cannot be redefined.
236 * Redefine commitOperation method to change behavior of operation instead
238 void ModuleBase_Operation::commit()
246 document()->finishOperation();
251 * \brief Stores a real value in model.
252 * \param theValue - to store
254 * Public slot. Passes theValue into the model.
256 void ModuleBase_Operation::storeReal(double theValue)
260 qDebug() << "ModuleBase_Operation::storeReal: " <<
261 "trying to store value without opening a transaction.";
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);
272 * \brief Verifies whether operator is ready to start.
273 * \return TRUE if operation is ready to start
275 * Default implementation returns TRUE. Redefine this method to add own verifications
277 bool ModuleBase_Operation::isReadyToStart() const
283 * \brief Virtual method called when operation is started
285 * Virtual method called when operation started (see start() method for more description)
286 * Default implementation calls corresponding slot and commits immediately.
288 void ModuleBase_Operation::startOperation()
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();
299 * \brief Virtual method called when operation is started
301 * Virtual method called when operation stopped - committed or aborted.
303 void ModuleBase_Operation::stopOperation()
308 * \brief Virtual method called when operation aborted
310 * Virtual method called when operation aborted (see abort() method for more description)
312 void ModuleBase_Operation::abortOperation()
317 * \brief Virtual method called when operation committed
319 * Virtual method called when operation committed (see commit() method for more description)
321 void ModuleBase_Operation::commitOperation()
323 if (myFeature) myFeature->execute();
327 * \brief Sets execution status
328 * \param theStatus - execution status
330 * Sets myExecStatus to the given value
332 void ModuleBase_Operation::setExecStatus(const int theVal)
334 myExecStatus = (ExecStatus) theVal;
338 * \brief Sets state of operation
339 * \param theState - state of operation to be set
341 * Sets state of operation (see OperationState enumeration)
343 void ModuleBase_Operation::setState(const ModuleBase_Operation::OperationState theState)