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_Object.h>
25 #include <ModelAPI_Validator.h>
27 #include <GeomAPI_Pnt2d.h>
29 #include <Events_Loop.h>
35 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
41 myDescription = new ModuleBase_OperationDescription(theId);
44 ModuleBase_Operation::~ModuleBase_Operation()
49 QString ModuleBase_Operation::id() const
51 return getDescription()->operationId();
54 FeaturePtr ModuleBase_Operation::feature() const
59 bool ModuleBase_Operation::isValid() const
62 return true; // rename operation
63 //Get validators for the Id
64 SessionPtr aMgr = ModelAPI_Session::get();
65 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
66 return aFactory->validate(myFeature);
69 bool ModuleBase_Operation::isNestedOperationsEnabled() const
74 void ModuleBase_Operation::storeCustomValue()
78 qDebug() << "ModuleBase_Operation::storeCustom: " <<
79 "trying to store value without opening a transaction.";
84 ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
86 aCustom->storeValue();
89 void ModuleBase_Operation::startOperation()
95 void ModuleBase_Operation::stopOperation()
99 void ModuleBase_Operation::abortOperation()
103 void ModuleBase_Operation::commitOperation()
107 void ModuleBase_Operation::afterCommitOperation()
111 bool ModuleBase_Operation::canBeCommitted() const
116 void ModuleBase_Operation::flushUpdated()
118 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
121 void ModuleBase_Operation::flushCreated()
123 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
126 FeaturePtr ModuleBase_Operation::createFeature(
127 const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature)
129 if (theCompositeFeature) {
130 myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString());
132 boost::shared_ptr<ModelAPI_Document> aDoc = document();
133 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
135 if (myFeature) { // TODO: generate an error if feature was not created
137 // Model update should call "execute" of a feature.
138 //myFeature->execute();
139 // Init default values
140 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
141 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
142 for (; anIt != aLast; anIt++) {
143 (*anIt)->storeValue(aFeature);
152 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
154 myFeature = theFeature;
158 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
160 FeaturePtr aFeature = feature();
162 if (aFeature == theObj)
164 std::list<ResultPtr> aResults = aFeature->results();
165 std::list<ResultPtr>::const_iterator aIt;
166 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
167 if ((*aIt) == theObj)
175 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
177 return ModelAPI_Session::get()->moduleDocument();
181 void ModuleBase_Operation::start()
183 ModelAPI_Session::get()->startOperation();
189 void ModuleBase_Operation::resume()
192 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
193 this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
197 void ModuleBase_Operation::abort()
202 disconnect(myPropertyPanel, 0, this, 0);
206 ModelAPI_Session::get()->abortOperation();
210 bool ModuleBase_Operation::commit()
212 if (canBeCommitted()) {
217 disconnect(myPropertyPanel, 0, this, 0);
220 ModelAPI_Session::get()->finishOperation();
224 afterCommitOperation();
230 void ModuleBase_Operation::setRunning(bool theState)
237 bool ModuleBase_Operation::activateByPreselection()
239 if (!myPropertyPanel)
241 if (myPreSelection.empty())
243 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
244 if (aWidgets.empty())
247 ModuleBase_ModelWidget* aWgt;
248 ModuleBase_ViewerPrs aPrs;
249 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
250 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
251 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
252 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
256 ModuleBase_WidgetValueFeature aValue;
257 aValue.setObject(aPrs.object());
258 if (!aWgt->setValue(&aValue))
261 if (canBeCommitted()) {
262 // if all widgets are filled with selection
267 //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
268 //if ((myPreSelection.size() > 0) && aActiveWgt) {
269 // const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
270 // ModuleBase_WidgetValueFeature aValue;
271 // aValue.setObject(aPrs.object());
272 // if (aActiveWgt->setValue(&aValue)) {
273 // myPreSelection.removeOne(aPrs);
274 // myPropertyPanel->activateNextWidget();
276 // // If preselection is enough to make a valid feature - apply it immediately
281 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection)
283 myPreSelection.clear();
285 // Check that the selected result are not results of operation feature
286 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
287 FeaturePtr aFeature = feature();
289 std::list<ResultPtr> aResults = aFeature->results();
290 QList<ObjectPtr> aResList;
291 std::list<ResultPtr>::const_iterator aIt;
292 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
293 aResList.append(*aIt);
295 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
296 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
297 myPreSelection.append(aPrs);
300 myPreSelection = aSelected;
303 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
305 //activateByPreselection();
306 //if (theWidget && myPropertyPanel) {
307 // myPropertyPanel->activateNextWidget();
308 //// //emit activateNextWidget(myActiveWidget);
312 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
314 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
317 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
318 aValue->setObject(theFeature);
319 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
320 bool isApplyed = aActiveWgt->setValue(aValue);
323 myIsModified = (myIsModified || isApplyed);
328 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
330 myPropertyPanel = theProp;
331 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
332 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));