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
57 boost::shared_ptr<ModelAPI_Feature> ModuleBase_Operation::feature() const
63 * \brief Gets state of operation
64 * \return Value from OperationState enumeration
66 * Gets state of operation (see OperationState enumeration)
68 ModuleBase_Operation::OperationState ModuleBase_Operation::state() const
74 * \brief Verifies whether operation is an ran one (state()==Running)
75 * \return TRUE if operation is active, FALSE otherwise
77 * Verifies whether operation is an running. Returns TRUE if state of operator
80 bool ModuleBase_Operation::isRunning() const
82 return state() == Running;
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
90 * Verifies whether given operator is valid for this one (i.e. can be started "above"
93 bool ModuleBase_Operation::isValid(ModuleBase_Operation*) const
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 )
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.
107 bool ModuleBase_Operation::isGranted() const
113 * Returns pointer to the root document.
115 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
117 return ModelAPI_PluginManager::get()->rootDocument();
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
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
130 bool ModuleBase_Operation::setSlot(const QObject* theReceiver, const char* theSlot)
132 return connect(this, SIGNAL(callSlot()), theReceiver, theSlot);
136 * \brief Sets the flags of operation
137 * \param f - flags of operation to be set
139 * Sets flags of operation (see Flags enumeration)
141 void ModuleBase_Operation::setFlags(const int f)
143 myFlags = myFlags | f;
147 * \brief Clears the flags of operation
148 * \param f - flags of operation to be cleared
150 * Clears flags of operation (see Flags enumeration)
152 void ModuleBase_Operation::clearFlags(const int f)
154 myFlags = myFlags & ~f;
158 * \brief Test the flags of operation
159 * \param f - flags of operation to be tested
161 * Returns TRUE if the specified flags set in the operation (see Flags enumeration)
163 bool ModuleBase_Operation::testFlags(const int f) const
165 return (myFlags & f) == f;
169 * \brief Gets execution status
170 * \return Execution status
172 * Gets execution status
174 int ModuleBase_Operation::execStatus() const
180 * \brief Starts operation
182 * Public slot. Verifies whether operation can be started and starts operation.
183 * This slot is not virtual and cannot be redefined. Redefine startOperation method
184 * to change behavior of operation. There is no point in using this method. It would
185 * be better to inherit own operator from base one and redefine startOperation method
188 void ModuleBase_Operation::start()
190 //document()->start(this);
191 document()->startOperation();
198 * \brief Aborts operation
200 * Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
201 * Redefine abortOperation method to change behavior of operation instead
203 void ModuleBase_Operation::abort()
211 document()->abortOperation();
216 * \brief Commits operation
218 * Public slot. Commits operation. This slot is not virtual and cannot be redefined.
219 * Redefine commitOperation method to change behavior of operation instead
221 void ModuleBase_Operation::commit()
229 document()->finishOperation();
234 * \brief Alias for start/abort slots
236 * Public slot. Aborts operation if false, else does nothing.
237 * Provided for S/S compatibility with QAction's toggle(bool)
239 void ModuleBase_Operation::setRunning(bool on)
247 * \brief Stores a real value in model.
248 * \param theValue - to store
250 * Public slot. Passes theValue into the model.
252 void ModuleBase_Operation::storeReal(double theValue)
256 qDebug() << "ModuleBase_Operation::storeReal: " <<
257 "trying to store value without opening a transaction.";
261 QString anId = sender()->objectName();
262 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
263 boost::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(anId.toStdString());
264 aReal->setValue(theValue);
268 * \brief Verifies whether operator is ready to start.
269 * \return TRUE if operation is ready to start
271 * Default implementation returns TRUE. Redefine this method to add own verifications
273 bool ModuleBase_Operation::isReadyToStart() const
279 * \brief Virtual method called when operation is started
281 * Virtual method called when operation started (see start() method for more description)
282 * Default implementation calls corresponding slot and commits immediately.
284 void ModuleBase_Operation::startOperation()
286 boost::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
287 myFeature = aDoc->addFeature(myOperationId.toStdString());
288 if (myFeature) // TODO: generate an error if feature was not created
289 myFeature->execute();
295 * \brief Virtual method called when operation is started
297 * Virtual method called when operation stopped - committed or aborted.
299 void ModuleBase_Operation::stopOperation()
304 * \brief Virtual method called when operation aborted
306 * Virtual method called when operation aborted (see abort() method for more description)
308 void ModuleBase_Operation::abortOperation()
313 * \brief Virtual method called when operation committed
315 * Virtual method called when operation committed (see commit() method for more description)
317 void ModuleBase_Operation::commitOperation()
319 if (myFeature) myFeature->execute();
323 * \brief Sets execution status
324 * \param theStatus - execution status
326 * Sets myExecStatus to the given value
328 void ModuleBase_Operation::setExecStatus(const int theVal)
330 myExecStatus = (ExecStatus) theVal;
334 * \brief Sets state of operation
335 * \param theState - state of operation to be set
337 * Sets state of operation (see OperationState enumeration)
339 void ModuleBase_Operation::setState(const ModuleBase_Operation::OperationState theState)