2 * ModuleBase_Operation.cpp
4 * Created on: Apr 2, 2014
8 #include "ModuleBase_Operation.h"
10 #include "ModuleBase_OperationDescription.h"
11 #include "ModuleBase_ModelWidget.h"
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_Document.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Result.h>
20 #include <ModelAPI_Validator.h>
22 #include <Events_Loop.h>
28 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
33 myDescription = new ModuleBase_OperationDescription(theId);
36 ModuleBase_Operation::~ModuleBase_Operation()
41 QString ModuleBase_Operation::id() const
43 return getDescription()->operationId();
46 FeaturePtr ModuleBase_Operation::feature() const
51 bool ModuleBase_Operation::isValid() const
54 return true; // rename operation
55 //Get validators for the Id
56 SessionPtr aMgr = ModelAPI_Session::get();
57 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
58 return aFactory->validate(myFeature);
61 bool ModuleBase_Operation::isNestedOperationsEnabled() const
66 void ModuleBase_Operation::storeCustomValue()
70 qDebug() << "ModuleBase_Operation::storeCustom: " <<
71 "trying to store value without opening a transaction.";
76 ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
78 aCustom->storeValue();
81 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
85 void ModuleBase_Operation::startOperation()
93 void ModuleBase_Operation::stopOperation()
97 void ModuleBase_Operation::abortOperation()
101 void ModuleBase_Operation::commitOperation()
105 void ModuleBase_Operation::afterCommitOperation()
109 bool ModuleBase_Operation::canBeCommitted() const
114 void ModuleBase_Operation::flushUpdated()
116 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
119 void ModuleBase_Operation::flushCreated()
121 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
124 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
126 boost::shared_ptr<ModelAPI_Document> aDoc = document();
127 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
128 if (myFeature) { // TODO: generate an error if feature was not created
130 // Model update should call "execute" of a feature.
131 //myFeature->execute();
132 // Init default values
133 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
134 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
135 for (; anIt != aLast; anIt++) {
136 (*anIt)->storeValue(aFeature);
145 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
147 myFeature = theFeature;
150 void ModuleBase_Operation::setEditingFeature(FeaturePtr theFeature)
152 setFeature(theFeature);
156 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
158 FeaturePtr aFeature = feature();
160 if (aFeature == theObj)
162 std::list<ResultPtr> aResults = aFeature->results();
163 std::list<ResultPtr>::const_iterator aIt;
164 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
165 if ((*aIt) == theObj)
173 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
175 return ModelAPI_Session::get()->moduleDocument();
179 void ModuleBase_Operation::start()
181 ModelAPI_Session::get()->startOperation();
187 void ModuleBase_Operation::resume()
192 void ModuleBase_Operation::abort()
199 ModelAPI_Session::get()->abortOperation();
203 bool ModuleBase_Operation::commit()
205 if (canBeCommitted()) {
211 ModelAPI_Session::get()->finishOperation();
214 afterCommitOperation();
220 void ModuleBase_Operation::setRunning(bool theState)