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 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
62 bool ModuleBase_Operation::isPerformedImmediately() const
64 return xmlRepresentation().isEmpty();
67 std::shared_ptr<ModelAPI_Feature> ModuleBase_Operation::feature() const
73 * \brief Gets state of operation
74 * \return Value from OperationState enumeration
76 * Gets state of operation (see OperationState enumeration)
78 ModuleBase_Operation::OperationState ModuleBase_Operation::state() const
84 * \brief Verifies whether operation is an ran one (state()==Running)
85 * \return TRUE if operation is active, FALSE otherwise
87 * Verifies whether operation is an running. Returns TRUE if state of operator
90 bool ModuleBase_Operation::isRunning() const
92 return state() == Running;
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
100 * Verifies whether given operator is valid for this one (i.e. can be started "above"
103 bool ModuleBase_Operation::isValid(ModuleBase_Operation*) const
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 )
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.
117 bool ModuleBase_Operation::isGranted() const
123 * Returns pointer to the root document.
125 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
127 return ModelAPI_PluginManager::get()->rootDocument();
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
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
140 bool ModuleBase_Operation::setSlot(const QObject* theReceiver, const char* theSlot)
142 return connect(this, SIGNAL(callSlot()), theReceiver, theSlot);
146 * \brief Sets the flags of operation
147 * \param f - flags of operation to be set
149 * Sets flags of operation (see Flags enumeration)
151 void ModuleBase_Operation::setFlags(const int f)
153 myFlags = myFlags | f;
157 * \brief Clears the flags of operation
158 * \param f - flags of operation to be cleared
160 * Clears flags of operation (see Flags enumeration)
162 void ModuleBase_Operation::clearFlags(const int f)
164 myFlags = myFlags & ~f;
168 * \brief Test the flags of operation
169 * \param f - flags of operation to be tested
171 * Returns TRUE if the specified flags set in the operation (see Flags enumeration)
173 bool ModuleBase_Operation::testFlags(const int f) const
175 return (myFlags & f) == f;
179 * \brief Gets execution status
180 * \return Execution status
182 * Gets execution status
184 int ModuleBase_Operation::execStatus() const
190 * \brief Returns XML representation of the operation's widget.
191 * \return XML QString
193 * Returns XML representation of the operation's widget.
195 const QString& ModuleBase_Operation::xmlRepresentation() const
201 * \brief Sets XML representation of the operation's widget.
202 * \param xmlRepr - XML QString
204 * Sets XML representation of the operation's widget.
206 void ModuleBase_Operation::setXmlRepresentation(const QString& xmlRepr)
212 * \brief Starts operation
214 * Public slot. Verifies whether operation can be started and starts operation.
215 * This slot is not virtual and cannot be redefined. Redefine startOperation method
216 * to change behavior of operation. There is no point in using this method. It would
217 * be better to inherit own operator from base one and redefine startOperation method
220 void ModuleBase_Operation::start()
222 //document()->start(this);
223 document()->startOperation();
230 * \brief Aborts operation
232 * Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
233 * Redefine abortOperation method to change behavior of operation instead
235 void ModuleBase_Operation::abort()
243 document()->abortOperation();
248 * \brief Commits operation
250 * Public slot. Commits operation. This slot is not virtual and cannot be redefined.
251 * Redefine commitOperation method to change behavior of operation instead
253 void ModuleBase_Operation::commit()
261 document()->finishOperation();
266 * \brief Alias for start/abort slots
268 * Public slot. Aborts operation if false, else does nothing.
269 * Provided for S/S compatibility with QAction's toggle(bool)
271 void ModuleBase_Operation::setRunning(bool on)
279 * \brief Stores a real value in model.
280 * \param theValue - to store
282 * Public slot. Passes theValue into the model.
284 void ModuleBase_Operation::storeReal(double theValue)
288 qDebug() << "ModuleBase_Operation::storeReal: " <<
289 "trying to store value without opening a transaction.";
293 QString anId = sender()->objectName();
294 std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
295 std::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(anId.toStdString());
296 aReal->setValue(theValue);
300 * \brief Verifies whether operator is ready to start.
301 * \return TRUE if operation is ready to start
303 * Default implementation returns TRUE. Redefine this method to add own verifications
305 bool ModuleBase_Operation::isReadyToStart() const
311 * \brief Virtual method called when operation is started
313 * Virtual method called when operation started (see start() method for more description)
314 * Default implementation calls corresponding slot and commits immediately.
316 void ModuleBase_Operation::startOperation()
318 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
319 myFeature = aDoc->addFeature(myOperationId.toStdString());
320 if (myFeature) // TODO: generate an error if feature was not created
321 myFeature->execute();
327 * \brief Virtual method called when operation is started
329 * Virtual method called when operation stopped - committed or aborted.
331 void ModuleBase_Operation::stopOperation()
336 * \brief Virtual method called when operation aborted
338 * Virtual method called when operation aborted (see abort() method for more description)
340 void ModuleBase_Operation::abortOperation()
345 * \brief Virtual method called when operation committed
347 * Virtual method called when operation committed (see commit() method for more description)
349 void ModuleBase_Operation::commitOperation()
351 if (myFeature) myFeature->execute();
355 * \brief Sets execution status
356 * \param theStatus - execution status
358 * Sets myExecStatus to the given value
360 void ModuleBase_Operation::setExecStatus(const int theVal)
362 myExecStatus = (ExecStatus) theVal;
366 * \brief Sets state of operation
367 * \param theState - state of operation to be set
369 * Sets state of operation (see OperationState enumeration)
371 void ModuleBase_Operation::setState(const ModuleBase_Operation::OperationState theState)