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>
24 #include <ModelAPI_Session.h>
26 #include <GeomAPI_Pnt2d.h>
28 #include <Events_Loop.h>
34 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
40 myDescription = new ModuleBase_OperationDescription(theId);
43 ModuleBase_Operation::~ModuleBase_Operation()
48 QString ModuleBase_Operation::id() const
50 return getDescription()->operationId();
53 FeaturePtr ModuleBase_Operation::feature() const
58 bool ModuleBase_Operation::isValid() const
61 return true; // rename operation
62 //Get validators for the Id
63 SessionPtr aMgr = ModelAPI_Session::get();
64 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
65 return aFactory->validate(myFeature);
68 bool ModuleBase_Operation::isNestedOperationsEnabled() const
73 void ModuleBase_Operation::storeCustomValue()
77 qDebug() << "ModuleBase_Operation::storeCustom: " <<
78 "trying to store value without opening a transaction.";
83 ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
85 aCustom->storeValue();
88 void ModuleBase_Operation::startOperation()
94 void ModuleBase_Operation::stopOperation()
98 void ModuleBase_Operation::abortOperation()
102 void ModuleBase_Operation::commitOperation()
106 void ModuleBase_Operation::afterCommitOperation()
110 bool ModuleBase_Operation::canBeCommitted() const
115 void ModuleBase_Operation::flushUpdated()
117 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
120 void ModuleBase_Operation::flushCreated()
122 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
125 FeaturePtr ModuleBase_Operation::createFeature(
126 const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature)
128 if (theCompositeFeature) {
129 myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString());
131 boost::shared_ptr<ModelAPI_Document> aDoc = document();
132 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
134 if (myFeature) { // TODO: generate an error if feature was not created
136 // Model update should call "execute" of a feature.
137 //myFeature->execute();
138 // Init default values
139 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
140 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
141 for (; anIt != aLast; anIt++) {
142 (*anIt)->storeValue(aFeature);
151 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
153 myFeature = theFeature;
157 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
159 FeaturePtr aFeature = feature();
161 if (aFeature == theObj)
163 std::list<ResultPtr> aResults = aFeature->results();
164 std::list<ResultPtr>::const_iterator aIt;
165 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
166 if ((*aIt) == theObj)
174 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
176 return ModelAPI_Session::get()->moduleDocument();
180 void ModuleBase_Operation::start()
182 ModelAPI_Session::get()->startOperation();
188 void ModuleBase_Operation::resume()
191 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
192 this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
196 void ModuleBase_Operation::abort()
201 disconnect(myPropertyPanel, 0, this, 0);
205 ModelAPI_Session::get()->abortOperation();
209 bool ModuleBase_Operation::commit()
211 if (canBeCommitted()) {
216 disconnect(myPropertyPanel, 0, this, 0);
219 ModelAPI_Session::get()->finishOperation();
223 afterCommitOperation();
229 void ModuleBase_Operation::setRunning(bool theState)
236 void ModuleBase_Operation::activateByPreselection()
238 if (!myPropertyPanel)
240 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
241 if ((myPreSelection.size() > 0) && aActiveWgt) {
242 const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
243 ModuleBase_WidgetValueFeature aValue;
244 aValue.setObject(aPrs.object());
245 if (aActiveWgt->setValue(&aValue)) {
246 myPreSelection.remove(aPrs);
247 myPropertyPanel->activateNextWidget();
249 // If preselection is enough to make a valid feature - apply it immediately
253 void ModuleBase_Operation::initSelection(
254 const std::list<ModuleBase_ViewerPrs>& theSelected,
255 const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
257 myPreSelection = theSelected;
260 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
262 activateByPreselection();
263 //if (theWidget && myPropertyPanel) {
264 // myPropertyPanel->activateNextWidget();
265 //// //emit activateNextWidget(myActiveWidget);
269 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
271 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
274 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
275 aValue->setObject(theFeature);
276 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
277 bool isApplyed = aActiveWgt->setValue(aValue);
280 myIsModified = (myIsModified || isApplyed);
285 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
287 myPropertyPanel = theProp;
288 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
289 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));