2 * ModuleBase_Operation.cpp
4 * Created on: Apr 2, 2014
8 #include "ModuleBase_Operation.h"
10 #include "ModuleBase_WidgetCustom.h"
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_Document.h>
14 #include <ModelAPI_Feature.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_PluginManager.h>
17 #include <ModelAPI_Document.h>
25 \param XGUI_Workshop - workshop for this operation
27 Constructs an empty operation. Constructor should work very fast because many
28 operators may be created after starting workshop but only several from them
29 may be used. As result this constructor stores given workshop in myApp field
30 and set Waiting status.
32 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* parent)
36 myExecStatus(Rejected),
44 ModuleBase_Operation::~ModuleBase_Operation()
50 * \brief Unique name of the operation
52 * Returns string name of the operation.
54 QString ModuleBase_Operation::operationId() const
59 boost::shared_ptr<ModelAPI_Feature> ModuleBase_Operation::feature() const
65 * \brief Gets state of operation
66 * \return Value from OperationState enumeration
68 * Gets state of operation (see OperationState enumeration)
70 ModuleBase_Operation::OperationState ModuleBase_Operation::state() const
76 * \brief Verifies whether operation is an ran one (state()==Running)
77 * \return TRUE if operation is active, FALSE otherwise
79 * Verifies whether operation is an running. Returns TRUE if state of operator
82 bool ModuleBase_Operation::isRunning() const
84 return state() == Running;
88 * \brief Verifies whether given operator is valid for this one
89 * \param theOtherOp - other operation
90 * \return Returns TRUE if the given operator is valid for this one
92 * Verifies whether given operator is valid for this one (i.e. can be started "above"
95 bool ModuleBase_Operation::isValid(ModuleBase_Operation*) const
101 * \brief Verifies whether this operator can be always started above any already running one
102 * \return Returns TRUE if current operation must not be checked for ActiveOperation->IsValid( this )
104 * This method must be redefined in derived operation if operation of derived class
105 * must be always can start above any launched one. Default impl returns FALSE,
106 * so it is being checked for IsValid, but some operations may overload IsGranted()
107 * In this case they will always start, no matter what operation is running.
109 bool ModuleBase_Operation::isGranted() const
115 * Returns pointer to the root document.
117 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
119 return ModelAPI_PluginManager::get()->rootDocument();
123 * \brief Sets slot which is called when operation is started
124 * \param theReceiver - object containing slot
125 * \param theSlot - slot of theReceiver object
126 * \return TR if slot was connected successfully, FALSE otherwise
128 * Sets slot which is called when operation is started. There is no point in
129 * using this method. It would be better to inherit own operator from base
130 * one and redefine startOperation method
132 bool ModuleBase_Operation::setSlot(const QObject* theReceiver, const char* theSlot)
134 return connect(this, SIGNAL(callSlot()), theReceiver, theSlot);
138 * \brief Sets the flags of operation
139 * \param f - flags of operation to be set
141 * Sets flags of operation (see Flags enumeration)
143 void ModuleBase_Operation::setFlags(const int f)
145 myFlags = myFlags | f;
149 * \brief Clears the flags of operation
150 * \param f - flags of operation to be cleared
152 * Clears flags of operation (see Flags enumeration)
154 void ModuleBase_Operation::clearFlags(const int f)
156 myFlags = myFlags & ~f;
160 * \brief Test the flags of operation
161 * \param f - flags of operation to be tested
163 * Returns TRUE if the specified flags set in the operation (see Flags enumeration)
165 bool ModuleBase_Operation::testFlags(const int f) const
167 return (myFlags & f) == f;
171 * \brief Gets execution status
172 * \return Execution status
174 * Gets execution status
176 int ModuleBase_Operation::execStatus() const
182 * \brief Starts operation
184 * Public slot. Verifies whether operation can be started and starts operation.
185 * This slot is not virtual and cannot be redefined. Redefine startOperation method
186 * to change behavior of operation. There is no point in using this method. It would
187 * be better to inherit own operator from base one and redefine startOperation method
190 void ModuleBase_Operation::start()
192 //document()->start(this);
193 document()->startOperation();
200 * \brief Resumes operation
202 * Public slot. Verifies whether operation can be started and starts operation.
203 * This slot is not virtual and cannot be redefined. Redefine startOperation method
204 * to change behavior of operation. There is no point in using this method. It would
205 * be better to inherit own operator from base one and redefine startOperation method
208 void ModuleBase_Operation::resume()
213 * \brief Aborts operation
215 * Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
216 * Redefine abortOperation method to change behavior of operation instead
218 void ModuleBase_Operation::abort()
226 document()->abortOperation();
231 * \brief Commits operation
233 * Public slot. Commits operation. This slot is not virtual and cannot be redefined.
234 * Redefine commitOperation method to change behavior of operation instead
236 void ModuleBase_Operation::commit()
244 document()->finishOperation();
249 * \brief Alias for start/abort slots
251 * Public slot. Aborts operation if false, else does nothing.
252 * Provided for S/S compatibility with QAction's toggle(bool)
254 void ModuleBase_Operation::setRunning(bool on)
262 * \brief Stores a real value in model.
263 * \param theValue - to store
265 * Public slot. Passes theValue into the model.
267 void ModuleBase_Operation::storeReal(double theValue)
271 qDebug() << "ModuleBase_Operation::storeReal: " <<
272 "trying to store value without opening a transaction.";
276 QString anId = sender()->objectName();
277 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
278 boost::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(anId.toStdString());
279 aReal->setValue(theValue);
283 * \brief Stores a real value in model.
284 * \param theValue - to store
286 * Public slot. Passes theValue into the model.
288 void ModuleBase_Operation::storeCustomValue()
292 qDebug() << "ModuleBase_Operation::storeCustom: " <<
293 "trying to store value without opening a transaction.";
298 ModuleBase_WidgetCustom* aCustom = dynamic_cast<ModuleBase_WidgetCustom*>(sender());
300 aCustom->store(myFeature);
304 * \brief Verifies whether operator is ready to start.
305 * \return TRUE if operation is ready to start
307 * Default impl returns TRUE. Redefine this method to add own verifications
309 bool ModuleBase_Operation::isReadyToStart() const
315 * \brief Virtual method called when operation is started
317 * Virtual method called when operation started (see start() method for more description)
318 * Default impl calls corresponding slot and commits immediately.
320 void ModuleBase_Operation::startOperation()
322 boost::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
323 myFeature = aDoc->addFeature(myOperationId.toStdString());
324 if (myFeature) // TODO: generate an error if feature was not created
325 myFeature->execute();
331 * \brief Virtual method called when operation is started
333 * Virtual method called when operation stopped - committed or aborted.
335 void ModuleBase_Operation::stopOperation()
340 * \brief Virtual method called when operation aborted
342 * Virtual method called when operation aborted (see abort() method for more description)
344 void ModuleBase_Operation::abortOperation()
349 * \brief Virtual method called when operation committed
351 * Virtual method called when operation committed (see commit() method for more description)
353 void ModuleBase_Operation::commitOperation()
355 if (myFeature) myFeature->execute();
359 * \brief Sets execution status
360 * \param theStatus - execution status
362 * Sets myExecStatus to the given value
364 void ModuleBase_Operation::setExecStatus(const int theVal)
366 myExecStatus = (ExecStatus) theVal;
370 * \brief Sets state of operation
371 * \param theState - state of operation to be set
373 * Sets state of operation (see OperationState enumeration)
375 void ModuleBase_Operation::setState(const ModuleBase_Operation::OperationState theState)