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>
26 #include <ModelAPI_Session.h>
28 #include <GeomAPI_Pnt2d.h>
30 #include <Events_Loop.h>
36 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
42 myDescription = new ModuleBase_OperationDescription(theId);
45 ModuleBase_Operation::~ModuleBase_Operation()
50 QString ModuleBase_Operation::id() const
52 return getDescription()->operationId();
55 FeaturePtr ModuleBase_Operation::feature() const
60 bool ModuleBase_Operation::isValid() const
63 return true; // rename operation
64 //Get validators for the Id
65 SessionPtr aMgr = ModelAPI_Session::get();
66 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
67 return aFactory->validate(myFeature);
70 bool ModuleBase_Operation::isNestedOperationsEnabled() const
75 void ModuleBase_Operation::storeCustomValue()
79 qDebug() << "ModuleBase_Operation::storeCustom: " <<
80 "trying to store value without opening a transaction.";
85 ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
87 aCustom->storeValue();
90 void ModuleBase_Operation::startOperation()
96 void ModuleBase_Operation::stopOperation()
100 void ModuleBase_Operation::abortOperation()
104 void ModuleBase_Operation::commitOperation()
108 void ModuleBase_Operation::afterCommitOperation()
112 bool ModuleBase_Operation::canBeCommitted() const
117 void ModuleBase_Operation::flushUpdated()
119 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
122 void ModuleBase_Operation::flushCreated()
124 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
127 FeaturePtr ModuleBase_Operation::createFeature(
128 const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature)
130 if (theCompositeFeature) {
131 myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString());
133 boost::shared_ptr<ModelAPI_Document> aDoc = document();
134 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
136 if (myFeature) { // TODO: generate an error if feature was not created
138 // Model update should call "execute" of a feature.
139 //myFeature->execute();
140 // Init default values
141 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
142 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
143 for (; anIt != aLast; anIt++) {
144 (*anIt)->storeValue(aFeature);
153 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
155 myFeature = theFeature;
159 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
161 FeaturePtr aFeature = feature();
163 if (aFeature == theObj)
165 std::list<ResultPtr> aResults = aFeature->results();
166 std::list<ResultPtr>::const_iterator aIt;
167 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
168 if ((*aIt) == theObj)
176 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
178 return ModelAPI_Session::get()->moduleDocument();
182 void ModuleBase_Operation::start()
184 ModelAPI_Session::get()->startOperation();
190 void ModuleBase_Operation::resume()
193 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
194 this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
198 void ModuleBase_Operation::abort()
203 disconnect(myPropertyPanel, 0, this, 0);
207 ModelAPI_Session::get()->abortOperation();
211 bool ModuleBase_Operation::commit()
213 if (canBeCommitted()) {
218 disconnect(myPropertyPanel, 0, this, 0);
221 ModelAPI_Session::get()->finishOperation();
225 afterCommitOperation();
231 void ModuleBase_Operation::setRunning(bool theState)
238 bool ModuleBase_Operation::activateByPreselection()
240 if (!myPropertyPanel)
242 if (myPreSelection.empty())
244 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
245 if (aWidgets.empty())
248 ModuleBase_ModelWidget* aWgt;
249 ModuleBase_ViewerPrs aPrs;
250 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
251 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
252 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
253 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
257 ModuleBase_WidgetValueFeature aValue;
258 aValue.setObject(aPrs.object());
259 if (!aWgt->setValue(&aValue))
262 if (canBeCommitted()) {
263 // if all widgets are filled with selection
268 //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
269 //if ((myPreSelection.size() > 0) && aActiveWgt) {
270 // const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
271 // ModuleBase_WidgetValueFeature aValue;
272 // aValue.setObject(aPrs.object());
273 // if (aActiveWgt->setValue(&aValue)) {
274 // myPreSelection.removeOne(aPrs);
275 // myPropertyPanel->activateNextWidget();
277 // // If preselection is enough to make a valid feature - apply it immediately
282 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection)
284 myPreSelection.clear();
286 // Check that the selected result are not results of operation feature
287 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
288 FeaturePtr aFeature = feature();
290 std::list<ResultPtr> aResults = aFeature->results();
291 QList<ObjectPtr> aResList;
292 std::list<ResultPtr>::const_iterator aIt;
293 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
294 aResList.append(*aIt);
296 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
297 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
298 myPreSelection.append(aPrs);
301 myPreSelection = aSelected;
304 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
306 //activateByPreselection();
307 //if (theWidget && myPropertyPanel) {
308 // myPropertyPanel->activateNextWidget();
309 //// //emit activateNextWidget(myActiveWidget);
313 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
315 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
318 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
319 aValue->setObject(theFeature);
320 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
321 bool isApplyed = aActiveWgt->setValue(aValue);
324 myIsModified = (myIsModified || isApplyed);
329 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
331 myPropertyPanel = theProp;
332 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
333 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));