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) {
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))
139 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
141 return ModelAPI_Session::get()->moduleDocument();
145 void ModuleBase_Operation::start()
147 ModelAPI_Session::get()->startOperation();
157 void ModuleBase_Operation::postpone()
163 void ModuleBase_Operation::resume()
169 void ModuleBase_Operation::abort()
176 ModelAPI_Session::get()->abortOperation();
180 bool ModuleBase_Operation::commit()
182 if (canBeCommitted()) {
184 // check whether there are modifications performed during the current operation
186 // in case if there are no modifications, do not increase the undo/redo stack
187 if (ModelAPI_Session::get()->isModified())
188 ModelAPI_Session::get()->finishOperation();
190 ModelAPI_Session::get()->abortOperation();
196 afterCommitOperation();
202 void ModuleBase_Operation::setRunning(bool theState)
209 void ModuleBase_Operation::activateByPreselection()
211 if (!myPropertyPanel || myPreSelection.empty()) {
212 myPropertyPanel->activateNextWidget(NULL);
215 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
216 if (aWidgets.empty()) {
217 myPropertyPanel->activateNextWidget(NULL);
221 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
222 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
223 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
225 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
226 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
229 ModuleBase_ViewerPrs aValue = (*aPIt);
230 if (!aWgt->canSetValue())
234 if (!aWgt->setSelection(aValue)) {
243 myPropertyPanel->activateNextWidget(aFilledWgt);
245 emit activatedByPreselection();
249 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
250 ModuleBase_IViewer* theViewer)
254 QList<ModuleBase_ViewerPrs> aPreSelected;
255 // Check that the selected result are not results of operation feature
256 FeaturePtr aFeature = feature();
258 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
260 std::list<ResultPtr> aResults = aFeature->results();
261 QObjectPtrList aResList;
262 std::list<ResultPtr>::const_iterator aIt;
263 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
264 aResList.append(*aIt);
266 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
267 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
268 aPreSelected.append(aPrs);
271 aPreSelected = theSelection->getSelected();
273 // convert the selection values to the values, which are set to the operation widgets
275 //Handle(V3d_View) aView = theViewer->activeView();
276 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
277 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
278 // aValue->setObject(aPrs.object());
281 // if (getViewerPoint(aPrs, theViewer, aX, anY))
282 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
283 // myPreSelection.append(aValue);
285 myPreSelection = aPreSelected;
288 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
290 // //activateByPreselection();
291 // //if (theWidget && myPropertyPanel) {
292 // // myPropertyPanel->activateNextWidget();
293 // //// //emit activateNextWidget(myActiveWidget);
297 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
299 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
302 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
303 // aValue->setObject(theFeature);
304 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
305 // bool isApplyed = aActiveWgt->setValue(aValue);
308 // myIsModified = (myIsModified || isApplyed);
312 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
313 ModuleBase_IViewer* theViewer,
314 double& theX, double& theY)
319 void ModuleBase_Operation::clearPreselection()
321 myPreSelection.clear();
324 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
326 myPropertyPanel = theProp;
327 myPropertyPanel->setEditingMode(isEditOperation());
329 // Do not activate widgets by default if the current operation is editing operation
330 // Because we don't know which widget is going to be edited.
331 if (!isEditOperation())
332 activateByPreselection();
335 bool ModuleBase_Operation::isGranted(QString theId) const
337 return myNestedFeatures.contains(theId);