1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_Operation.cpp
6 * Created on: Apr 2, 2014
10 #include "ModuleBase_Operation.h"
12 #include "ModuleBase_OperationDescription.h"
13 #include "ModuleBase_ModelWidget.h"
14 #include "ModuleBase_ViewerPrs.h"
15 #include "ModuleBase_IPropertyPanel.h"
16 #include "ModuleBase_ISelection.h"
17 #include "ModuleBase_IViewer.h"
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Feature.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Result.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
30 #include <GeomAPI_Pnt2d.h>
32 #include <Events_Loop.h>
40 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
46 myDescription = new ModuleBase_OperationDescription(theId);
49 ModuleBase_Operation::~ModuleBase_Operation()
55 QString ModuleBase_Operation::id() const
57 return getDescription()->operationId();
60 FeaturePtr ModuleBase_Operation::feature() const
65 bool ModuleBase_Operation::isValid() const
68 return true; // rename operation
69 //Get validators for the Id
70 SessionPtr aMgr = ModelAPI_Session::get();
71 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
72 return aFactory->validate(myFeature);
75 bool ModuleBase_Operation::isNestedOperationsEnabled() const
80 //void ModuleBase_Operation::storeCustomValue()
84 // qDebug() << "ModuleBase_Operation::storeCustom: " <<
85 // "trying to store value without opening a transaction.";
90 // ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
92 // aCustom->storeValue();
96 bool ModuleBase_Operation::canBeCommitted() const
101 void ModuleBase_Operation::flushUpdated()
103 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
106 void ModuleBase_Operation::flushCreated()
108 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
111 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
113 if (myParentFeature) {
114 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
116 std::shared_ptr<ModelAPI_Document> aDoc = document();
117 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
119 if (myFeature) { // TODO: generate an error if feature was not created
121 // Model update should call "execute" of a feature.
122 //myFeature->execute();
123 // Init default values
124 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
125 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
126 for (; anIt != aLast; anIt++) {
127 (*anIt)->storeValue(aFeature);
136 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
138 myFeature = theFeature;
142 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
144 FeaturePtr aFeature = feature();
146 if (aFeature == theObj)
148 std::list<ResultPtr> aResults = aFeature->results();
149 std::list<ResultPtr>::const_iterator aIt;
150 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
151 if (theObj == (*aIt))
159 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
161 return ModelAPI_Session::get()->moduleDocument();
165 void ModuleBase_Operation::start()
167 ModelAPI_Session::get()->startOperation();
176 void ModuleBase_Operation::postpone()
179 disconnect(myPropertyPanel, 0, this, 0);
183 void ModuleBase_Operation::resume()
185 // connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
186 // this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
190 void ModuleBase_Operation::abort()
195 disconnect(myPropertyPanel, 0, this, 0);
199 ModelAPI_Session::get()->abortOperation();
203 bool ModuleBase_Operation::commit()
205 if (canBeCommitted()) {
207 disconnect(myPropertyPanel, 0, this, 0);
210 // check whether there are modifications performed during the current operation
212 // in case if there are no modifications, do not increase the undo/redo stack
213 if (ModelAPI_Session::get()->isModified())
214 ModelAPI_Session::get()->finishOperation();
216 ModelAPI_Session::get()->abortOperation();
222 afterCommitOperation();
228 void ModuleBase_Operation::setRunning(bool theState)
235 bool ModuleBase_Operation::activateByPreselection()
237 if (!myPropertyPanel || myPreSelection.empty()) {
238 myPropertyPanel->activateNextWidget(NULL);
241 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
242 if (aWidgets.empty()) {
243 myPropertyPanel->activateNextWidget(NULL);
247 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
248 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
249 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
251 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
252 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
255 ModuleBase_ViewerPrs aValue = (*aPIt);
256 if (!aWgt->canSetValue())
260 if (!aWgt->setSelection(aValue)) {
270 myPropertyPanel->activateNextWidget(aFilledWgt);
271 emit activatedByPreselection();
275 myPropertyPanel->activateNextWidget(NULL);
279 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
280 ModuleBase_IViewer* theViewer)
284 QList<ModuleBase_ViewerPrs> aPreSelected;
285 // Check that the selected result are not results of operation feature
286 FeaturePtr aFeature = feature();
288 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
290 std::list<ResultPtr> aResults = aFeature->results();
291 QObjectPtrList 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 aPreSelected.append(aPrs);
301 aPreSelected = theSelection->getSelected();
303 // convert the selection values to the values, which are set to the operation widgets
305 //Handle(V3d_View) aView = theViewer->activeView();
306 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
307 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
308 // aValue->setObject(aPrs.object());
311 // if (getViewerPoint(aPrs, theViewer, aX, anY))
312 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
313 // myPreSelection.append(aValue);
315 myPreSelection = aPreSelected;
318 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
320 // //activateByPreselection();
321 // //if (theWidget && myPropertyPanel) {
322 // // myPropertyPanel->activateNextWidget();
323 // //// //emit activateNextWidget(myActiveWidget);
327 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
329 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
332 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
333 // aValue->setObject(theFeature);
334 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
335 // bool isApplyed = aActiveWgt->setValue(aValue);
338 // myIsModified = (myIsModified || isApplyed);
342 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
343 ModuleBase_IViewer* theViewer,
344 double& theX, double& theY)
349 void ModuleBase_Operation::clearPreselection()
351 myPreSelection.clear();
354 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
356 myPropertyPanel = theProp;
357 myPropertyPanel->setEditingMode(isEditOperation());
358 //connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
359 // SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
362 bool ModuleBase_Operation::isGranted(QString theId) const
364 return myNestedFeatures.contains(theId);