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 if (myFeature->isAction())
71 //Get validators for the Id
72 SessionPtr aMgr = ModelAPI_Session::get();
73 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
74 return aFactory->validate(myFeature);
78 bool ModuleBase_Operation::canBeCommitted() const
83 void ModuleBase_Operation::flushUpdated()
85 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
88 void ModuleBase_Operation::flushCreated()
90 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
93 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
95 if (myParentFeature.get()) {
96 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
98 std::shared_ptr<ModelAPI_Document> aDoc = document();
99 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
101 if (myFeature) { // TODO: generate an error if feature was not created
103 // Model update should call "execute" of a feature.
104 //myFeature->execute();
105 // Init default values
106 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
107 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
108 for (; anIt != aLast; anIt++) {
109 (*anIt)->storeValue(aFeature);
118 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
120 myFeature = theFeature;
124 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
126 FeaturePtr aFeature = feature();
128 if (aFeature == theObj)
130 std::list<ResultPtr> aResults = aFeature->results();
131 std::list<ResultPtr>::const_iterator aIt;
132 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
133 if (theObj == (*aIt))
141 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
143 return ModelAPI_Session::get()->moduleDocument();
147 void ModuleBase_Operation::start()
149 ModelAPI_Session::get()->startOperation();
152 FeaturePtr aFeature = createFeature();
153 // if the feature is not created, there is no sense to start the operation
154 if (aFeature.get() == NULL) {
155 ModelAPI_Session::get()->abortOperation();
165 void ModuleBase_Operation::postpone()
171 void ModuleBase_Operation::resume()
177 void ModuleBase_Operation::abort()
184 ModelAPI_Session::get()->abortOperation();
188 bool ModuleBase_Operation::commit()
190 if (canBeCommitted()) {
192 // check whether there are modifications performed during the current operation
194 // in case if there are no modifications, do not increase the undo/redo stack
195 if (ModelAPI_Session::get()->isModified())
196 ModelAPI_Session::get()->finishOperation();
198 ModelAPI_Session::get()->abortOperation();
204 afterCommitOperation();
210 void ModuleBase_Operation::setRunning(bool theState)
217 void ModuleBase_Operation::activateByPreselection()
219 if (!myPropertyPanel || myPreSelection.empty()) {
220 myPropertyPanel->activateNextWidget(NULL);
223 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
224 if (aWidgets.empty()) {
225 myPropertyPanel->activateNextWidget(NULL);
229 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
230 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
231 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
233 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
234 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
237 ModuleBase_ViewerPrs aValue = (*aPIt);
238 if (!aWgt->canSetValue())
242 if (!aWgt->setSelection(aValue)) {
251 myPropertyPanel->activateNextWidget(aFilledWgt);
253 emit activatedByPreselection();
257 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
259 myParentFeature = theParent;
262 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
264 return myParentFeature;
267 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
268 ModuleBase_IViewer* theViewer)
272 QList<ModuleBase_ViewerPrs> aPreSelected;
273 // Check that the selected result are not results of operation feature
274 FeaturePtr aFeature = feature();
276 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
278 std::list<ResultPtr> aResults = aFeature->results();
279 QObjectPtrList aResList;
280 std::list<ResultPtr>::const_iterator aIt;
281 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
282 aResList.append(*aIt);
284 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
285 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
286 aPreSelected.append(aPrs);
289 aPreSelected = theSelection->getSelected();
291 // convert the selection values to the values, which are set to the operation widgets
293 //Handle(V3d_View) aView = theViewer->activeView();
294 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
295 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
296 // aValue->setObject(aPrs.object());
299 // if (getViewerPoint(aPrs, theViewer, aX, anY))
300 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
301 // myPreSelection.append(aValue);
303 myPreSelection = aPreSelected;
306 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
308 // //activateByPreselection();
309 // //if (theWidget && myPropertyPanel) {
310 // // myPropertyPanel->activateNextWidget();
311 // //// //emit activateNextWidget(myActiveWidget);
315 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
317 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
320 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
321 // aValue->setObject(theFeature);
322 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
323 // bool isApplyed = aActiveWgt->setValue(aValue);
326 // myIsModified = (myIsModified || isApplyed);
330 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
331 ModuleBase_IViewer* theViewer,
332 double& theX, double& theY)
337 void ModuleBase_Operation::clearPreselection()
339 myPreSelection.clear();
342 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
344 myPropertyPanel = theProp;
345 myPropertyPanel->setEditingMode(isEditOperation());
347 // Do not activate widgets by default if the current operation is editing operation
348 // Because we don't know which widget is going to be edited.
349 if (!isEditOperation())
350 activateByPreselection();
353 bool ModuleBase_Operation::isGranted(QString theId) const
355 return myNestedFeatures.contains(theId);