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 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
85 if (myParentFeature.get()) {
86 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
88 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
89 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
91 if (myFeature) { // TODO: generate an error if feature was not created
93 // Model update should call "execute" of a feature.
94 //myFeature->execute();
95 // Init default values
96 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
97 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
98 for (; anIt != aLast; anIt++) {
99 (*anIt)->storeValue(aFeature);
104 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
108 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
110 myFeature = theFeature;
114 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
116 FeaturePtr aFeature = feature();
118 if (aFeature == theObj)
120 std::list<ResultPtr> aResults = aFeature->results();
121 std::list<ResultPtr>::const_iterator aIt;
122 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
123 if (theObj == (*aIt))
130 void ModuleBase_Operation::start()
132 QString anId = getDescription()->operationId();
134 anId = anId.append(EditSuffix());
136 ModelAPI_Session::get()->startOperation(anId.toStdString());
139 FeaturePtr aFeature = createFeature();
140 // if the feature is not created, there is no sense to start the operation
141 if (aFeature.get() == NULL) {
142 // it is necessary to abor the operation in the session and emit the aborted signal
143 // in order to update commands status in the workshop, to be exact the feature action
155 void ModuleBase_Operation::postpone()
161 void ModuleBase_Operation::resume()
167 void ModuleBase_Operation::abort()
174 ModelAPI_Session::get()->abortOperation();
178 bool ModuleBase_Operation::commit()
180 if (canBeCommitted()) {
182 // check whether there are modifications performed during the current operation
184 // in case if there are no modifications, do not increase the undo/redo stack
185 if (ModelAPI_Session::get()->isModified())
186 ModelAPI_Session::get()->finishOperation();
188 ModelAPI_Session::get()->abortOperation();
194 afterCommitOperation();
200 void ModuleBase_Operation::setRunning(bool theState)
207 void ModuleBase_Operation::activateByPreselection()
209 if (!myPropertyPanel || myPreSelection.empty()) {
210 myPropertyPanel->activateNextWidget(NULL);
213 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
214 if (aWidgets.empty()) {
215 myPropertyPanel->activateNextWidget(NULL);
219 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
220 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
221 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
223 // 1. apply the selection to controls
224 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
225 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
228 ModuleBase_ViewerPrs aValue = (*aPIt);
229 if (!aWgt->canSetValue())
233 if (!aWgt->setSelection(aValue)) {
241 // 2. ignore not obligatory widgets
242 for (; aWIt != aWidgets.constEnd(); ++aWIt) {
244 if (aWgt && aWgt->isObligatory())
249 // 3. activate the next obligatory widget
250 myPropertyPanel->activateNextWidget(aFilledWgt);
252 emit activatedByPreselection();
256 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
258 myParentFeature = theParent;
261 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
263 return myParentFeature;
266 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
267 ModuleBase_IViewer* theViewer)
271 QList<ModuleBase_ViewerPrs> aPreSelected;
272 // Check that the selected result are not results of operation feature
273 FeaturePtr aFeature = feature();
275 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
277 std::list<ResultPtr> aResults = aFeature->results();
278 QObjectPtrList aResList;
279 std::list<ResultPtr>::const_iterator aIt;
280 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
281 aResList.append(*aIt);
283 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
284 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
285 aPreSelected.append(aPrs);
288 aPreSelected = theSelection->getSelected();
290 // convert the selection values to the values, which are set to the operation widgets
292 //Handle(V3d_View) aView = theViewer->activeView();
293 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
294 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
295 // aValue->setObject(aPrs.object());
298 // if (getViewerPoint(aPrs, theViewer, aX, anY))
299 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
300 // myPreSelection.append(aValue);
302 myPreSelection = aPreSelected;
305 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
307 // //activateByPreselection();
308 // //if (theWidget && myPropertyPanel) {
309 // // myPropertyPanel->activateNextWidget();
310 // //// //emit activateNextWidget(myActiveWidget);
314 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
316 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
319 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
320 // aValue->setObject(theFeature);
321 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
322 // bool isApplyed = aActiveWgt->setValue(aValue);
325 // myIsModified = (myIsModified || isApplyed);
329 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
330 ModuleBase_IViewer* theViewer,
331 double& theX, double& theY)
336 void ModuleBase_Operation::clearPreselection()
338 myPreSelection.clear();
341 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
343 myPropertyPanel = theProp;
344 myPropertyPanel->setEditingMode(isEditOperation());
346 // Do not activate widgets by default if the current operation is editing operation
347 // Because we don't know which widget is going to be edited.
348 if (!isEditOperation())
349 activateByPreselection();
352 bool ModuleBase_Operation::isGranted(QString theId) const
354 return myNestedFeatures.contains(theId);