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
67 if (!myFeature || !myFeature->data().get())
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 bool aValid = aFactory->validate(myFeature);
76 // the feature exec state should be checked in order to do not apply features, which result can not
77 // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
78 bool isDone = myFeature->data()->execState() == ModelAPI_StateDone;
80 return aValid && isDone;
84 bool ModuleBase_Operation::canBeCommitted() const
89 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
91 if (myParentFeature.get()) {
92 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
94 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
95 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
97 if (myFeature) { // TODO: generate an error if feature was not created
99 // Model update should call "execute" of a feature.
100 //myFeature->execute();
101 // Init default values
102 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
103 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
104 for (; anIt != aLast; anIt++) {
105 (*anIt)->storeValue(aFeature);
110 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
114 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
116 myFeature = theFeature;
120 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
122 FeaturePtr aFeature = feature();
124 if (aFeature == theObj)
126 std::list<ResultPtr> aResults = aFeature->results();
127 std::list<ResultPtr>::const_iterator aIt;
128 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
129 if (theObj == (*aIt))
136 void ModuleBase_Operation::start()
138 QString anId = getDescription()->operationId();
140 anId = anId.append(EditSuffix());
142 ModelAPI_Session::get()->startOperation(anId.toStdString());
145 FeaturePtr aFeature = createFeature();
146 // if the feature is not created, there is no sense to start the operation
147 if (aFeature.get() == NULL) {
148 // it is necessary to abor the operation in the session and emit the aborted signal
149 // in order to update commands status in the workshop, to be exact the feature action
155 /// Set current feature and remeber old current feature
157 SessionPtr aMgr = ModelAPI_Session::get();
158 DocumentPtr aDoc = aMgr->activeDocument();
159 myCurrentFeature = aDoc->currentFeature(true);
160 aDoc->setCurrentFeature(feature(), false);
168 void ModuleBase_Operation::postpone()
174 void ModuleBase_Operation::resume()
180 void ModuleBase_Operation::abort()
183 SessionPtr aMgr = ModelAPI_Session::get();
184 DocumentPtr aDoc = aMgr->activeDocument();
185 aDoc->setCurrentFeature(myCurrentFeature, true);
186 myCurrentFeature = FeaturePtr();
192 // is is necessary to deactivate current widgets before the model operation is aborted
193 // because abort removes the feature and activated filters should not check it
194 propertyPanel()->cleanContent();
196 ModelAPI_Session::get()->abortOperation();
200 bool ModuleBase_Operation::commit()
202 if (canBeCommitted()) {
203 SessionPtr aMgr = ModelAPI_Session::get();
204 /// Set current feature and remeber old current feature
206 DocumentPtr aDoc = aMgr->activeDocument();
207 bool aIsOp = aMgr->isOperation();
209 aMgr->startOperation();
210 aDoc->setCurrentFeature(myCurrentFeature, true);
212 aMgr->finishOperation();
213 myCurrentFeature = FeaturePtr();
216 // check whether there are modifications performed during the current operation
218 // in case if there are no modifications, do not increase the undo/redo stack
219 if (aMgr->isModified())
220 aMgr->finishOperation();
222 aMgr->abortOperation();
228 afterCommitOperation();
234 void ModuleBase_Operation::setRunning(bool theState)
236 emit triggered(theState);
239 void ModuleBase_Operation::activateByPreselection()
241 if (!myPropertyPanel || myPreSelection.empty()) {
242 myPropertyPanel->activateNextWidget(NULL);
245 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
246 if (aWidgets.empty()) {
247 myPropertyPanel->activateNextWidget(NULL);
251 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
252 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
254 // 1. apply the selection to controls
255 int aCurrentPosition = 0;
256 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
258 if (!aWgt->canSetValue())
261 if (!aWgt->setSelection(myPreSelection, aCurrentPosition/*aValue*/)) {
269 // 2. ignore not obligatory widgets
270 /*for (; aWIt != aWidgets.constEnd(); ++aWIt) {
272 if (aWgt && aWgt->isObligatory())
277 // 3. activate the next obligatory widget
278 myPropertyPanel->activateNextWidget(aFilledWgt);
280 emit activatedByPreselection();
284 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
286 myParentFeature = theParent;
289 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
291 return myParentFeature;
294 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
295 ModuleBase_IViewer* theViewer)
299 QList<ModuleBase_ViewerPrs> aPreSelected;
300 // Check that the selected result are not results of operation feature
301 FeaturePtr aFeature = feature();
303 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
305 std::list<ResultPtr> aResults = aFeature->results();
306 QObjectPtrList aResList;
307 std::list<ResultPtr>::const_iterator aIt;
308 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
309 aResList.append(*aIt);
311 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
312 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
313 aPreSelected.append(aPrs);
316 aPreSelected = theSelection->getSelected();
318 // convert the selection values to the values, which are set to the operation widgets
320 //Handle(V3d_View) aView = theViewer->activeView();
321 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
322 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
323 // aValue->setObject(aPrs.object());
326 // if (getViewerPoint(aPrs, theViewer, aX, anY))
327 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
328 // myPreSelection.append(aValue);
330 myPreSelection = aPreSelected;
333 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
335 // //activateByPreselection();
336 // //if (theWidget && myPropertyPanel) {
337 // // myPropertyPanel->activateNextWidget();
338 // //// //emit activateNextWidget(myActiveWidget);
342 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
344 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
347 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
348 // aValue->setObject(theFeature);
349 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
350 // bool isApplyed = aActiveWgt->setValue(aValue);
353 // myIsModified = (myIsModified || isApplyed);
357 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
358 ModuleBase_IViewer* theViewer,
359 double& theX, double& theY)
364 void ModuleBase_Operation::clearPreselection()
366 myPreSelection.clear();
369 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
371 myPropertyPanel = theProp;
372 myPropertyPanel->setEditingMode(isEditOperation());
374 // Do not activate widgets by default if the current operation is editing operation
375 // Because we don't know which widget is going to be edited.
376 if (!isEditOperation())
377 activateByPreselection();
380 bool ModuleBase_Operation::isGranted(QString theId) const
382 return myNestedFeatures.contains(theId);