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"
15 #include "ModuleBase_ISelection.h"
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Feature.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_Result.h>
24 #include <ModelAPI_Validator.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 bool ModuleBase_Operation::activateByPreselection()
238 if (!myPropertyPanel)
240 if (myPreSelection.empty())
242 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
243 if (aWidgets.empty())
246 ModuleBase_ModelWidget* aWgt;
247 ModuleBase_ViewerPrs aPrs;
248 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
249 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
250 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
251 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
255 ModuleBase_WidgetValueFeature aValue;
256 aValue.setObject(aPrs.object());
257 if (!aWgt->setValue(&aValue))
260 if (canBeCommitted()) {
261 // if all widgets are filled with selection
266 //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
267 //if ((myPreSelection.size() > 0) && aActiveWgt) {
268 // const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
269 // ModuleBase_WidgetValueFeature aValue;
270 // aValue.setObject(aPrs.object());
271 // if (aActiveWgt->setValue(&aValue)) {
272 // myPreSelection.removeOne(aPrs);
273 // myPropertyPanel->activateNextWidget();
275 // // If preselection is enough to make a valid feature - apply it immediately
280 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection)
282 myPreSelection = theSelection->getSelected();
285 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
287 //activateByPreselection();
288 //if (theWidget && myPropertyPanel) {
289 // myPropertyPanel->activateNextWidget();
290 //// //emit activateNextWidget(myActiveWidget);
294 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
296 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
299 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
300 aValue->setObject(theFeature);
301 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
302 bool isApplyed = aActiveWgt->setValue(aValue);
305 myIsModified = (myIsModified || isApplyed);
310 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
312 myPropertyPanel = theProp;
313 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
314 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));