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 Aborts operation
202 * Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
203 * Redefine abortOperation method to change behavior of operation instead
205 void ModuleBase_Operation::abort()
213 document()->abortOperation();
218 * \brief Commits operation
220 * Public slot. Commits operation. This slot is not virtual and cannot be redefined.
221 * Redefine commitOperation method to change behavior of operation instead
223 void ModuleBase_Operation::commit()
231 document()->finishOperation();
236 * \brief Alias for start/abort slots
238 * Public slot. Aborts operation if false, else does nothing.
239 * Provided for S/S compatibility with QAction's toggle(bool)
241 void ModuleBase_Operation::setRunning(bool on)
249 * \brief Stores a real value in model.
250 * \param theValue - to store
252 * Public slot. Passes theValue into the model.
254 void ModuleBase_Operation::storeReal(double theValue)
258 qDebug() << "ModuleBase_Operation::storeReal: " <<
259 "trying to store value without opening a transaction.";
263 QString anId = sender()->objectName();
264 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
265 boost::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(anId.toStdString());
266 aReal->setValue(theValue);
270 * \brief Stores a real value in model.
271 * \param theValue - to store
273 * Public slot. Passes theValue into the model.
275 void ModuleBase_Operation::storeCustomValue()
279 qDebug() << "ModuleBase_Operation::storeCustom: " <<
280 "trying to store value without opening a transaction.";
285 ModuleBase_WidgetCustom* aCustom = dynamic_cast<ModuleBase_WidgetCustom*>(sender());
287 aCustom->store(myFeature);
291 * \brief Verifies whether operator is ready to start.
292 * \return TRUE if operation is ready to start
294 * Default impl returns TRUE. Redefine this method to add own verifications
296 bool ModuleBase_Operation::isReadyToStart() const
302 * \brief Virtual method called when operation is started
304 * Virtual method called when operation started (see start() method for more description)
305 * Default impl calls corresponding slot and commits immediately.
307 void ModuleBase_Operation::startOperation()
309 boost::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
310 myFeature = aDoc->addFeature(myOperationId.toStdString());
311 if (myFeature) // TODO: generate an error if feature was not created
312 myFeature->execute();
318 * \brief Virtual method called when operation is started
320 * Virtual method called when operation stopped - committed or aborted.
322 void ModuleBase_Operation::stopOperation()
327 * \brief Virtual method called when operation aborted
329 * Virtual method called when operation aborted (see abort() method for more description)
331 void ModuleBase_Operation::abortOperation()
336 * \brief Virtual method called when operation committed
338 * Virtual method called when operation committed (see commit() method for more description)
340 void ModuleBase_Operation::commitOperation()
342 if (myFeature) myFeature->execute();
346 * \brief Sets execution status
347 * \param theStatus - execution status
349 * Sets myExecStatus to the given value
351 void ModuleBase_Operation::setExecStatus(const int theVal)
353 myExecStatus = (ExecStatus) theVal;
357 * \brief Sets state of operation
358 * \param theState - state of operation to be set
360 * Sets state of operation (see OperationState enumeration)
362 void ModuleBase_Operation::setState(const ModuleBase_Operation::OperationState theState)