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()->isValid())
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
79 || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
81 return aValid && isDone;
85 bool ModuleBase_Operation::canBeCommitted() const
90 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
92 if (myParentFeature.get()) {
93 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
95 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
96 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
98 if (myFeature) { // TODO: generate an error if feature was not created
100 // Model update should call "execute" of a feature.
101 //myFeature->execute();
102 // Init default values
103 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
104 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
105 for (; anIt != aLast; anIt++) {
106 (*anIt)->storeValue(aFeature);
111 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
115 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
117 myFeature = theFeature;
121 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
123 FeaturePtr aFeature = feature();
125 if (aFeature == theObj)
127 std::list<ResultPtr> aResults = aFeature->results();
128 std::list<ResultPtr>::const_iterator aIt;
129 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
130 if (theObj == (*aIt))
137 void ModuleBase_Operation::start()
139 QString anId = getDescription()->operationId();
141 anId = anId.append(EditSuffix());
143 ModelAPI_Session::get()->startOperation(anId.toStdString());
146 FeaturePtr aFeature = createFeature();
147 // if the feature is not created, there is no sense to start the operation
148 if (aFeature.get() == NULL) {
149 // it is necessary to abor the operation in the session and emit the aborted signal
150 // in order to update commands status in the workshop, to be exact the feature action
156 /// Set current feature and remeber old current feature
158 SessionPtr aMgr = ModelAPI_Session::get();
159 DocumentPtr aDoc = aMgr->activeDocument();
160 myCurrentFeature = aDoc->currentFeature(true);
161 aDoc->setCurrentFeature(feature(), false);
169 void ModuleBase_Operation::postpone()
175 void ModuleBase_Operation::resume()
181 void ModuleBase_Operation::abort()
184 SessionPtr aMgr = ModelAPI_Session::get();
185 DocumentPtr aDoc = aMgr->activeDocument();
186 bool aIsOp = aMgr->isOperation();
188 aMgr->startOperation();
189 aDoc->setCurrentFeature(myCurrentFeature, true);
191 aMgr->finishOperation();
192 myCurrentFeature = FeaturePtr();
198 // is is necessary to deactivate current widgets before the model operation is aborted
199 // because abort removes the feature and activated filters should not check it
200 propertyPanel()->cleanContent();
202 ModelAPI_Session::get()->abortOperation();
206 bool ModuleBase_Operation::commit()
208 if (canBeCommitted()) {
209 SessionPtr aMgr = ModelAPI_Session::get();
210 /// Set current feature and remeber old current feature
212 DocumentPtr aDoc = aMgr->activeDocument();
213 bool aIsOp = aMgr->isOperation();
215 aMgr->startOperation();
216 aDoc->setCurrentFeature(myCurrentFeature, true);
218 aMgr->finishOperation();
219 myCurrentFeature = FeaturePtr();
222 // check whether there are modifications performed during the current operation
224 // in case if there are no modifications, do not increase the undo/redo stack
225 if (aMgr->isModified())
226 aMgr->finishOperation();
228 aMgr->abortOperation();
234 afterCommitOperation();
240 void ModuleBase_Operation::setRunning(bool theState)
242 emit triggered(theState);
245 void ModuleBase_Operation::activateByPreselection()
247 if (!myPropertyPanel || myPreSelection.empty()) {
248 myPropertyPanel->activateNextWidget(NULL);
251 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
252 if (aWidgets.empty()) {
253 myPropertyPanel->activateNextWidget(NULL);
257 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
258 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
260 // 1. apply the selection to controls
261 int aCurrentPosition = 0;
262 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
264 if (!aWgt->canSetValue())
267 if (!aWgt->setSelection(myPreSelection, aCurrentPosition/*aValue*/)) {
275 // 2. ignore not obligatory widgets
276 /*for (; aWIt != aWidgets.constEnd(); ++aWIt) {
278 if (aWgt && aWgt->isObligatory())
283 // 3. a signal should be emitted before the next widget activation
284 // because, the activation of the next widget will give a focus to the widget. As a result
285 // the value of the widget is initialized. And commit may happens until the value is entered.
287 emit activatedByPreselection();
289 // 4. activate the next obligatory widget
290 myPropertyPanel->activateNextWidget(aFilledWgt);
293 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
295 myParentFeature = theParent;
298 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
300 return myParentFeature;
303 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
304 ModuleBase_IViewer* theViewer)
308 QList<ModuleBase_ViewerPrs> aPreSelected;
309 // Check that the selected result are not results of operation feature
310 FeaturePtr aFeature = feature();
312 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
314 std::list<ResultPtr> aResults = aFeature->results();
315 QObjectPtrList aResList;
316 std::list<ResultPtr>::const_iterator aIt;
317 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
318 aResList.append(*aIt);
320 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
321 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
322 aPreSelected.append(aPrs);
325 aPreSelected = theSelection->getSelected();
327 // convert the selection values to the values, which are set to the operation widgets
329 //Handle(V3d_View) aView = theViewer->activeView();
330 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
331 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
332 // aValue->setObject(aPrs.object());
335 // if (getViewerPoint(aPrs, theViewer, aX, anY))
336 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
337 // myPreSelection.append(aValue);
339 myPreSelection = aPreSelected;
342 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
344 // //activateByPreselection();
345 // //if (theWidget && myPropertyPanel) {
346 // // myPropertyPanel->activateNextWidget();
347 // //// //emit activateNextWidget(myActiveWidget);
351 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
353 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
356 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
357 // aValue->setObject(theFeature);
358 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
359 // bool isApplyed = aActiveWgt->setValue(aValue);
362 // myIsModified = (myIsModified || isApplyed);
366 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
367 ModuleBase_IViewer* theViewer,
368 double& theX, double& theY)
373 void ModuleBase_Operation::clearPreselection()
375 myPreSelection.clear();
378 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
380 myPropertyPanel = theProp;
381 myPropertyPanel->setEditingMode(isEditOperation());
383 // Do not activate widgets by default if the current operation is editing operation
384 // Because we don't know which widget is going to be edited.
385 if (!isEditOperation())
386 activateByPreselection();
389 bool ModuleBase_Operation::isGranted(QString theId) const
391 return myNestedFeatures.contains(theId);