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);
76 bool ModuleBase_Operation::canBeCommitted() const
81 void ModuleBase_Operation::flushUpdated()
83 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
86 void ModuleBase_Operation::flushCreated()
88 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
91 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
93 if (myParentFeature.get()) {
94 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
96 std::shared_ptr<ModelAPI_Document> aDoc = document();
97 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
99 if (myFeature) { // TODO: generate an error if feature was not created
101 // Model update should call "execute" of a feature.
102 //myFeature->execute();
103 // Init default values
104 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
105 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
106 for (; anIt != aLast; anIt++) {
107 (*anIt)->storeValue(aFeature);
116 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
118 myFeature = theFeature;
122 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
124 FeaturePtr aFeature = feature();
126 if (aFeature == theObj)
128 std::list<ResultPtr> aResults = aFeature->results();
129 std::list<ResultPtr>::const_iterator aIt;
130 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
131 if (theObj == (*aIt))
134 CompositeFeaturePtr aCompFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
135 if (aCompFeature.get() != NULL) {
136 for (int i = 0; i < aCompFeature->numberOfSubs(); i++) {
137 FeaturePtr aSubFeature = aCompFeature->subFeature(i);
138 std::list<ResultPtr> aResults = aSubFeature->results();
139 std::list<ResultPtr>::const_iterator aIt;
140 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
141 if (theObj == (*aIt))
144 if (aSubFeature == theObj)
153 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
155 return ModelAPI_Session::get()->moduleDocument();
159 void ModuleBase_Operation::start()
161 ModelAPI_Session::get()->startOperation();
171 void ModuleBase_Operation::postpone()
177 void ModuleBase_Operation::resume()
183 void ModuleBase_Operation::abort()
190 ModelAPI_Session::get()->abortOperation();
194 bool ModuleBase_Operation::commit()
196 if (canBeCommitted()) {
198 // check whether there are modifications performed during the current operation
200 // in case if there are no modifications, do not increase the undo/redo stack
201 if (ModelAPI_Session::get()->isModified())
202 ModelAPI_Session::get()->finishOperation();
204 ModelAPI_Session::get()->abortOperation();
210 afterCommitOperation();
216 void ModuleBase_Operation::setRunning(bool theState)
223 void ModuleBase_Operation::activateByPreselection()
225 if (!myPropertyPanel || myPreSelection.empty()) {
226 myPropertyPanel->activateNextWidget(NULL);
229 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
230 if (aWidgets.empty()) {
231 myPropertyPanel->activateNextWidget(NULL);
235 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
236 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
237 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
239 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
240 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
243 ModuleBase_ViewerPrs aValue = (*aPIt);
244 if (!aWgt->canSetValue())
248 if (!aWgt->setSelection(aValue)) {
257 myPropertyPanel->activateNextWidget(aFilledWgt);
259 emit activatedByPreselection();
263 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
265 myParentFeature = theParent;
268 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
270 return myParentFeature;
273 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
274 ModuleBase_IViewer* theViewer)
278 QList<ModuleBase_ViewerPrs> aPreSelected;
279 // Check that the selected result are not results of operation feature
280 FeaturePtr aFeature = feature();
282 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
284 std::list<ResultPtr> aResults = aFeature->results();
285 QObjectPtrList aResList;
286 std::list<ResultPtr>::const_iterator aIt;
287 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
288 aResList.append(*aIt);
290 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
291 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
292 aPreSelected.append(aPrs);
295 aPreSelected = theSelection->getSelected();
297 // convert the selection values to the values, which are set to the operation widgets
299 //Handle(V3d_View) aView = theViewer->activeView();
300 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
301 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
302 // aValue->setObject(aPrs.object());
305 // if (getViewerPoint(aPrs, theViewer, aX, anY))
306 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
307 // myPreSelection.append(aValue);
309 myPreSelection = aPreSelected;
312 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
314 // //activateByPreselection();
315 // //if (theWidget && myPropertyPanel) {
316 // // myPropertyPanel->activateNextWidget();
317 // //// //emit activateNextWidget(myActiveWidget);
321 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
323 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
326 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
327 // aValue->setObject(theFeature);
328 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
329 // bool isApplyed = aActiveWgt->setValue(aValue);
332 // myIsModified = (myIsModified || isApplyed);
336 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
337 ModuleBase_IViewer* theViewer,
338 double& theX, double& theY)
343 void ModuleBase_Operation::clearPreselection()
345 myPreSelection.clear();
348 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
350 myPropertyPanel = theProp;
351 myPropertyPanel->setEditingMode(isEditOperation());
353 // Do not activate widgets by default if the current operation is editing operation
354 // Because we don't know which widget is going to be edited.
355 if (!isEditOperation())
356 activateByPreselection();
359 bool ModuleBase_Operation::isGranted(QString theId) const
361 return myNestedFeatures.contains(theId);