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>
19 #include <GeomDataAPI_Point2D.h>
27 \param XGUI_Workshop - workshop for this operation
29 Constructs an empty operation. Constructor should work very fast because many
30 operators may be created after starting workshop but only several from them
31 may be used. As result this constructor stores given workshop in myApp field
32 and set Waiting status.
34 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* parent)
38 myExecStatus(Rejected),
46 ModuleBase_Operation::~ModuleBase_Operation()
52 * \brief Unique name of the operation
54 * Returns string name of the operation.
56 QString ModuleBase_Operation::operationId() const
61 boost::shared_ptr<ModelAPI_Feature> ModuleBase_Operation::feature() const
67 * \brief Gets state of operation
68 * \return Value from OperationState enumeration
70 * Gets state of operation (see OperationState enumeration)
72 ModuleBase_Operation::OperationState ModuleBase_Operation::state() const
78 * \brief Verifies whether operation is an ran one (state()==Running)
79 * \return TRUE if operation is active, FALSE otherwise
81 * Verifies whether operation is an running. Returns TRUE if state of operator
84 bool ModuleBase_Operation::isRunning() const
86 return state() == Running;
90 * \brief Verifies whether given operator is valid for this one
91 * \param theOtherOp - other operation
92 * \return Returns TRUE if the given operator is valid for this one
94 * Verifies whether given operator is valid for this one (i.e. can be started "above"
97 bool ModuleBase_Operation::isValid(ModuleBase_Operation*) const
103 * \brief Verifies whether this operator can be always started above any already running one
104 * \return Returns TRUE if current operation must not be checked for ActiveOperation->IsValid( this )
106 * This method must be redefined in derived operation if operation of derived class
107 * must be always can start above any launched one. Default impl returns FALSE,
108 * so it is being checked for IsValid, but some operations may overload IsGranted()
109 * In this case they will always start, no matter what operation is running.
111 bool ModuleBase_Operation::isGranted() const
117 * Returns pointer to the root document.
119 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
121 return ModelAPI_PluginManager::get()->rootDocument();
125 * \brief Sets slot which is called when operation is started
126 * \param theReceiver - object containing slot
127 * \param theSlot - slot of theReceiver object
128 * \return TR if slot was connected successfully, FALSE otherwise
130 * Sets slot which is called when operation is started. There is no point in
131 * using this method. It would be better to inherit own operator from base
132 * one and redefine startOperation method
134 bool ModuleBase_Operation::setSlot(const QObject* theReceiver, const char* theSlot)
136 return connect(this, SIGNAL(callSlot()), theReceiver, theSlot);
140 * \brief Sets the flags of operation
141 * \param f - flags of operation to be set
143 * Sets flags of operation (see Flags enumeration)
145 void ModuleBase_Operation::setFlags(const int f)
147 myFlags = myFlags | f;
151 * \brief Clears the flags of operation
152 * \param f - flags of operation to be cleared
154 * Clears flags of operation (see Flags enumeration)
156 void ModuleBase_Operation::clearFlags(const int f)
158 myFlags = myFlags & ~f;
162 * \brief Test the flags of operation
163 * \param f - flags of operation to be tested
165 * Returns TRUE if the specified flags set in the operation (see Flags enumeration)
167 bool ModuleBase_Operation::testFlags(const int f) const
169 return (myFlags & f) == f;
173 * \brief Gets execution status
174 * \return Execution status
176 * Gets execution status
178 int ModuleBase_Operation::execStatus() const
184 * \brief Starts operation
186 * Public slot. Verifies whether operation can be started and starts operation.
187 * This slot is not virtual and cannot be redefined. Redefine startOperation method
188 * to change behavior of operation. There is no point in using this method. It would
189 * be better to inherit own operator from base one and redefine startOperation method
192 void ModuleBase_Operation::start()
194 //document()->start(this);
195 document()->startOperation();
202 * \brief Aborts operation
204 * Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
205 * Redefine abortOperation method to change behavior of operation instead
207 void ModuleBase_Operation::abort()
215 document()->abortOperation();
220 * \brief Commits operation
222 * Public slot. Commits operation. This slot is not virtual and cannot be redefined.
223 * Redefine commitOperation method to change behavior of operation instead
225 void ModuleBase_Operation::commit()
233 document()->finishOperation();
238 * \brief Alias for start/abort slots
240 * Public slot. Aborts operation if false, else does nothing.
241 * Provided for S/S compatibility with QAction's toggle(bool)
243 void ModuleBase_Operation::setRunning(bool on)
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 boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
267 boost::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(anId.toStdString());
268 aReal->setValue(theValue);
272 * \brief Stores a real value in model.
273 * \param theValue - to store
275 * Public slot. Passes theValue into the model.
277 void ModuleBase_Operation::storeCustomValue()
281 qDebug() << "ModuleBase_Operation::storeCustom: " <<
282 "trying to store value without opening a transaction.";
287 ModuleBase_WidgetCustom* aCustom = dynamic_cast<ModuleBase_WidgetCustom*>(sender());
289 aCustom->store(myFeature);
293 * \brief Verifies whether operator is ready to start.
294 * \return TRUE if operation is ready to start
296 * Default impl returns TRUE. Redefine this method to add own verifications
298 bool ModuleBase_Operation::isReadyToStart() const
304 * \brief Virtual method called when operation is started
306 * Virtual method called when operation started (see start() method for more description)
307 * Default impl calls corresponding slot and commits immediately.
309 void ModuleBase_Operation::startOperation()
311 boost::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
312 myFeature = aDoc->addFeature(myOperationId.toStdString());
313 if (myFeature) // TODO: generate an error if feature was not created
314 myFeature->execute();
320 * \brief Virtual method called when operation is started
322 * Virtual method called when operation stopped - committed or aborted.
324 void ModuleBase_Operation::stopOperation()
329 * \brief Virtual method called when operation aborted
331 * Virtual method called when operation aborted (see abort() method for more description)
333 void ModuleBase_Operation::abortOperation()
338 * \brief Virtual method called when operation committed
340 * Virtual method called when operation committed (see commit() method for more description)
342 void ModuleBase_Operation::commitOperation()
344 if (myFeature) myFeature->execute();
348 * \brief Sets execution status
349 * \param theStatus - execution status
351 * Sets myExecStatus to the given value
353 void ModuleBase_Operation::setExecStatus(const int theVal)
355 myExecStatus = (ExecStatus) theVal;
359 * \brief Sets state of operation
360 * \param theState - state of operation to be set
362 * Sets state of operation (see OperationState enumeration)
364 void ModuleBase_Operation::setState(const ModuleBase_Operation::OperationState theState)