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(
125 const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature)
127 if (theCompositeFeature) {
128 myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString());
130 boost::shared_ptr<ModelAPI_Document> aDoc = document();
131 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
133 if (myFeature) { // TODO: generate an error if feature was not created
135 // Model update should call "execute" of a feature.
136 //myFeature->execute();
137 // Init default values
138 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
139 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
140 for (; anIt != aLast; anIt++) {
141 (*anIt)->storeValue(aFeature);
150 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
152 myFeature = 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()
190 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
191 this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
195 void ModuleBase_Operation::abort()
200 disconnect(myPropertyPanel, 0, this, 0);
204 ModelAPI_Session::get()->abortOperation();
208 bool ModuleBase_Operation::commit()
210 if (canBeCommitted()) {
215 disconnect(myPropertyPanel, 0, this, 0);
218 ModelAPI_Session::get()->finishOperation();
222 afterCommitOperation();
228 void ModuleBase_Operation::setRunning(bool theState)
235 void ModuleBase_Operation::activateByPreselection()
237 if (!myPropertyPanel)
239 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
240 if ((myPreSelection.size() > 0) && aActiveWgt) {
241 const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
242 ModuleBase_WidgetValueFeature aValue;
243 aValue.setObject(aPrs.object());
244 if (aActiveWgt->setValue(&aValue)) {
245 myPreSelection.removeOne(aPrs);
246 myPropertyPanel->activateNextWidget();
248 // If preselection is enough to make a valid feature - apply it immediately
252 void ModuleBase_Operation::initSelection(
253 const QList<ModuleBase_ViewerPrs>& theSelected,
254 const QList<ModuleBase_ViewerPrs>& /*theHighlighted*/)
256 myPreSelection = theSelected;
259 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
261 activateByPreselection();
262 //if (theWidget && myPropertyPanel) {
263 // myPropertyPanel->activateNextWidget();
264 //// //emit activateNextWidget(myActiveWidget);
268 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
270 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
273 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
274 aValue->setObject(theFeature);
275 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
276 bool isApplyed = aActiveWgt->setValue(aValue);
279 myIsModified = (myIsModified || isApplyed);
284 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
286 myPropertyPanel = theProp;
287 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
288 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));