1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_OperationFeature.cpp
6 * Created on: Apr 2, 2014
10 #include "ModuleBase_OperationFeature.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>
29 #include <ModelAPI_Tools.h>
31 #include <GeomAPI_Pnt2d.h>
33 #include <Events_Loop.h>
41 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
42 : ModuleBase_Operation(theId, theParent),
47 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
52 FeaturePtr ModuleBase_OperationFeature::feature() const
57 bool ModuleBase_OperationFeature::isValid() const
59 if (!myFeature || !myFeature->data()->isValid())
60 return true; // rename operation
61 if (myFeature->isAction())
63 //Get validators for the Id
64 SessionPtr aMgr = ModelAPI_Session::get();
65 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
66 bool aValid = aFactory->validate(myFeature);
68 // the feature exec state should be checked in order to do not apply features, which result can not
69 // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
70 bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
71 || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
73 return aValid && isDone;
76 void ModuleBase_OperationFeature::startOperation()
78 FeaturePtr aFeature = feature();
79 if (!aFeature.get() || !isEditOperation())
82 if (aFeature.get() && isEditOperation())
83 aFeature->setStable(false);
85 myVisualizedObjects.clear();
86 // store hidden result features
87 std::list<ResultPtr> aResults = aFeature->results();
88 std::list<ResultPtr>::const_iterator aIt;
89 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
90 ObjectPtr anObject = *aIt;
91 if (anObject.get() && !anObject->isDisplayed()) {
92 myVisualizedObjects.insert(*aIt);
93 anObject->setDisplayed(true);
96 if (!aFeature->isDisplayed()) {
97 myVisualizedObjects.insert(aFeature);
98 aFeature->setDisplayed(true);
100 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
103 void ModuleBase_OperationFeature::stopOperation()
105 FeaturePtr aFeature = feature();
106 if (!aFeature.get() || !isEditOperation())
109 // store hidden result features
110 std::list<ResultPtr> aResults = aFeature->results();
111 std::list<ResultPtr>::const_iterator aIt;
112 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
113 ObjectPtr anObject = *aIt;
114 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
115 anObject->setDisplayed(false);
118 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
119 aFeature->setDisplayed(false);
121 if (myVisualizedObjects.size() > 0)
122 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
125 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
127 if (myParentFeature.get()) {
128 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
130 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
131 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
133 if (myFeature) { // TODO: generate an error if feature was not created
135 // Model update should call "execute" of a feature.
136 //myFeature->execute();
137 // Init default values
138 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
139 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
140 for (; anIt != aLast; anIt++) {
141 (*anIt)->storeValue(aFeature);
146 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
150 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
152 myFeature = theFeature;
156 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
158 FeaturePtr aFeature = feature();
160 if (aFeature == theObj)
162 std::list<ResultPtr> aResults = aFeature->results();
163 std::list<ResultPtr>::const_iterator aIt;
164 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
165 if (theObj == (*aIt))
172 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
174 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
177 void ModuleBase_OperationFeature::start()
179 setIsModified(false);
180 QString anId = getDescription()->operationId();
182 anId = anId.append(EditSuffix());
184 ModelAPI_Session::get()->startOperation(anId.toStdString());
186 emit beforeStarted();
190 FeaturePtr aFeature = createFeature();
191 // if the feature is not created, there is no sense to start the operation
192 if (aFeature.get() == NULL) {
193 // it is necessary to abor the operation in the session and emit the aborted signal
194 // in order to update commands status in the workshop, to be exact the feature action
200 //Already called startOperation();
205 void ModuleBase_OperationFeature::abort()
207 emit beforeAborted();
209 // the viewer update should be blocked in order to avoid the features blinking before they are
211 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
212 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
213 Events_Loop::loop()->send(aMsg);
215 // the widgets of property panel should not process any events come from data mode
216 // after abort clicked. Some signal such as redisplay/create influence on content
217 // of the object browser and viewer context. Therefore it influence to the current
218 // selection and if the active widget listens it, the attribute value is errnoneous
220 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
222 aPropertyPanel->cleanContent();
224 myFeature->setStable(true);
229 SessionPtr aMgr = ModelAPI_Session::get();
230 aMgr->abortOperation();
232 // the viewer update should be unblocked in order to avoid the features blinking before they are
234 aMsg = std::shared_ptr<Events_Message>(
235 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
237 Events_Loop::loop()->send(aMsg);
242 bool ModuleBase_OperationFeature::commit()
244 if (canBeCommitted()) {
245 // the widgets of property panel should not process any events come from data mode
246 // after commit clicked. Some signal such as redisplay/create influence on content
247 // of the object browser and viewer context. Therefore it influence to the current
248 // selection and if the active widget listens it, the attribute value is errnoneous
250 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
252 aPropertyPanel->cleanContent();
254 myFeature->setStable(true);
256 SessionPtr aMgr = ModelAPI_Session::get();
257 /// Set current feature and remeber old current feature
259 emit beforeCommitted();
261 aMgr->finishOperation();
267 afterCommitOperation();
273 void ModuleBase_OperationFeature::activateByPreselection()
275 if (myPreSelection.empty())
278 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
280 ModuleBase_ModelWidget* aFilledWgt = 0;
281 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
282 if (aPropertyPanel) {
283 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
284 if (!aWidgets.empty()) {
285 ModuleBase_ModelWidget* aWgt = 0;
286 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
288 // 1. apply the selection to controls
289 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
291 if (!aWgt->canSetValue())
293 aPropertyPanel->setPreselectionWidget(aWgt);
294 if (!aWgt->setSelection(myPreSelection, true)) {
302 aPropertyPanel->setPreselectionWidget(NULL);
303 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
304 // it is better to perform it not in setSelection of each widget, but do it here,
305 // after the preselection is processed
306 ModuleBase_ModelWidget::updateObject(myFeature);
308 // 3. a signal should be emitted before the next widget activation
309 // because, the activation of the next widget will give a focus to the widget. As a result
310 // the value of the widget is initialized. And commit may happens until the value is entered.
312 emit activatedByPreselection();
314 // 4. activate the next obligatory widget
315 aPropertyPanel->activateNextWidget(aFilledWgt);
321 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
323 myParentFeature = theParent;
326 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
328 return myParentFeature;
331 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
333 myPreviousCurrentFeature = theFeature;
336 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
338 return myPreviousCurrentFeature;
341 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
342 ModuleBase_IViewer* theViewer)
346 QList<ModuleBase_ViewerPrs> aPreSelected;
347 // Check that the selected result are not results of operation feature
348 FeaturePtr aFeature = feature();
350 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
352 std::list<ResultPtr> aResults = aFeature->results();
353 QObjectPtrList aResList;
354 std::list<ResultPtr>::const_iterator aIt;
355 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
356 aResList.append(*aIt);
358 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
359 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
360 aPreSelected.append(aPrs);
363 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
365 myPreSelection = aPreSelected;
368 void ModuleBase_OperationFeature::clearPreselection()
370 myPreSelection.clear();
373 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
375 ModuleBase_Operation::setPropertyPanel(theProp);
377 theProp->setEditingMode(isEditOperation());
380 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
381 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
382 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
383 ModuleBase_ModelWidget* aWgt = (*aWIt);
384 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
388 // Do not activate widgets by default if the current operation is editing operation
389 // Because we don't know which widget is going to be edited.
390 if (!isEditOperation()) {
391 // 4. activate the first obligatory widget
392 theProp->activateNextWidget(NULL);
395 // set focus on Ok button in order to operation manager could process Enter press
397 theProp->setFocusOnOkButton();