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"
12 #include "ModuleBase_WidgetValueFeature.h"
13 #include "ModuleBase_ViewerPrs.h"
14 #include "ModuleBase_IPropertyPanel.h"
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_Feature.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Events.h>
22 #include <ModelAPI_Result.h>
23 #include <ModelAPI_Validator.h>
25 #include <GeomAPI_Pnt2d.h>
27 #include <Events_Loop.h>
33 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
39 myDescription = new ModuleBase_OperationDescription(theId);
42 ModuleBase_Operation::~ModuleBase_Operation()
47 QString ModuleBase_Operation::id() const
49 return getDescription()->operationId();
52 FeaturePtr ModuleBase_Operation::feature() const
57 bool ModuleBase_Operation::isValid() const
60 return true; // rename operation
61 //Get validators for the Id
62 SessionPtr aMgr = ModelAPI_Session::get();
63 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
64 return aFactory->validate(myFeature);
67 bool ModuleBase_Operation::isNestedOperationsEnabled() const
72 void ModuleBase_Operation::storeCustomValue()
76 qDebug() << "ModuleBase_Operation::storeCustom: " <<
77 "trying to store value without opening a transaction.";
82 ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
84 aCustom->storeValue();
87 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;
151 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
153 FeaturePtr aFeature = feature();
155 if (aFeature == theObj)
157 std::list<ResultPtr> aResults = aFeature->results();
158 std::list<ResultPtr>::const_iterator aIt;
159 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
160 if ((*aIt) == theObj)
168 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
170 return ModelAPI_Session::get()->moduleDocument();
174 void ModuleBase_Operation::start()
176 ModelAPI_Session::get()->startOperation();
182 void ModuleBase_Operation::resume()
185 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
186 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
190 void ModuleBase_Operation::abort()
195 disconnect(myPropertyPanel, 0, this, 0);
199 ModelAPI_Session::get()->abortOperation();
203 bool ModuleBase_Operation::commit()
205 if (canBeCommitted()) {
210 disconnect(myPropertyPanel, 0, this, 0);
214 ModelAPI_Session::get()->finishOperation();
217 afterCommitOperation();
223 void ModuleBase_Operation::setRunning(bool theState)
230 void ModuleBase_Operation::activateByPreselection()
232 if (!myPropertyPanel)
234 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
235 if ((myPreSelection.size() > 0) && aActiveWgt) {
236 const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
237 ModuleBase_WidgetValueFeature aValue;
238 aValue.setObject(aPrs.object());
239 if (aActiveWgt->setValue(&aValue)) {
240 myPreSelection.remove(aPrs);
241 myPropertyPanel->activateNextWidget();
243 // If preselection is enough to make a valid feature - apply it immediately
247 void ModuleBase_Operation::initSelection(
248 const std::list<ModuleBase_ViewerPrs>& theSelected,
249 const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
251 myPreSelection = theSelected;
254 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
256 activateByPreselection();
257 //if (theWidget && myPropertyPanel) {
258 // myPropertyPanel->activateNextWidget();
259 //// //emit activateNextWidget(myActiveWidget);
263 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
265 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
268 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
269 aValue->setObject(theFeature);
270 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
271 bool isApplyed = aActiveWgt->setValue(aValue);
274 myIsModified = (myIsModified || isApplyed);
279 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
281 myPropertyPanel = theProp;
282 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
283 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));