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 void ModuleBase_OperationFeature::setEditOperation()
54 if (isEditOperation())
58 propertyPanel()->setEditingMode(isEditOperation());
61 FeaturePtr ModuleBase_OperationFeature::feature() const
66 bool ModuleBase_OperationFeature::isValid() const
68 if (!myFeature || !myFeature->data()->isValid())
69 return true; // rename operation
70 if (myFeature->isAction())
72 //Get validators for the Id
73 SessionPtr aMgr = ModelAPI_Session::get();
74 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
75 bool aValid = aFactory->validate(myFeature);
77 // the feature exec state should be checked in order to do not apply features, which result can not
78 // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
79 bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
80 || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
82 return aValid && isDone;
85 void ModuleBase_OperationFeature::startOperation()
87 FeaturePtr aFeature = feature();
88 if (!aFeature.get() || !isEditOperation())
91 if (aFeature.get() && isEditOperation())
92 aFeature->setStable(false);
94 myVisualizedObjects.clear();
95 // store hidden result features
96 std::list<ResultPtr> aResults = aFeature->results();
97 std::list<ResultPtr>::const_iterator aIt;
98 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
99 ObjectPtr anObject = *aIt;
100 if (anObject.get() && !anObject->isDisplayed()) {
101 myVisualizedObjects.insert(*aIt);
102 anObject->setDisplayed(true);
105 if (!aFeature->isDisplayed()) {
106 myVisualizedObjects.insert(aFeature);
107 aFeature->setDisplayed(true);
109 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
112 void ModuleBase_OperationFeature::stopOperation()
114 FeaturePtr aFeature = feature();
115 if (!aFeature.get() || !isEditOperation())
118 // store hidden result features
119 std::list<ResultPtr> aResults = aFeature->results();
120 std::list<ResultPtr>::const_iterator aIt;
121 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
122 ObjectPtr anObject = *aIt;
123 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
124 anObject->setDisplayed(false);
127 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
128 aFeature->setDisplayed(false);
130 if (myVisualizedObjects.size() > 0)
131 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
134 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
136 if (myParentFeature.get()) {
137 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
139 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
140 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
142 if (myFeature) { // TODO: generate an error if feature was not created
144 // Model update should call "execute" of a feature.
145 //myFeature->execute();
146 // Init default values
147 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
148 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
149 for (; anIt != aLast; anIt++) {
150 (*anIt)->storeValue(aFeature);
155 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
159 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
161 myFeature = theFeature;
165 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
167 FeaturePtr aFeature = feature();
169 if (aFeature == theObj)
171 std::list<ResultPtr> aResults = aFeature->results();
172 std::list<ResultPtr>::const_iterator aIt;
173 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
174 if (theObj == (*aIt))
181 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
183 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
186 void ModuleBase_OperationFeature::start()
188 setIsModified(false);
189 QString anId = getDescription()->operationId();
191 anId = anId.append(EditSuffix());
193 ModelAPI_Session::get()->startOperation(anId.toStdString());
195 emit beforeStarted();
199 FeaturePtr aFeature = createFeature();
200 // if the feature is not created, there is no sense to start the operation
201 if (aFeature.get() == NULL) {
202 // it is necessary to abor the operation in the session and emit the aborted signal
203 // in order to update commands status in the workshop, to be exact the feature action
209 //Already called startOperation();
214 void ModuleBase_OperationFeature::abort()
216 emit beforeAborted();
218 // the viewer update should be blocked in order to avoid the features blinking before they are
220 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
221 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
222 Events_Loop::loop()->send(aMsg);
224 // the widgets of property panel should not process any events come from data mode
225 // after abort clicked. Some signal such as redisplay/create influence on content
226 // of the object browser and viewer context. Therefore it influence to the current
227 // selection and if the active widget listens it, the attribute value is errnoneous
229 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
231 aPropertyPanel->cleanContent();
233 myFeature->setStable(true);
238 SessionPtr aMgr = ModelAPI_Session::get();
239 aMgr->abortOperation();
241 // the viewer update should be unblocked in order to avoid the features blinking before they are
243 aMsg = std::shared_ptr<Events_Message>(
244 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
246 Events_Loop::loop()->send(aMsg);
251 bool ModuleBase_OperationFeature::commit()
253 if (canBeCommitted()) {
254 // the widgets of property panel should not process any events come from data mode
255 // after commit clicked. Some signal such as redisplay/create influence on content
256 // of the object browser and viewer context. Therefore it influence to the current
257 // selection and if the active widget listens it, the attribute value is errnoneous
259 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
261 aPropertyPanel->cleanContent();
263 myFeature->setStable(true);
265 SessionPtr aMgr = ModelAPI_Session::get();
266 /// Set current feature and remeber old current feature
268 emit beforeCommitted();
270 aMgr->finishOperation();
276 afterCommitOperation();
282 void ModuleBase_OperationFeature::activateByPreselection()
284 if (myPreSelection.empty())
287 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
289 ModuleBase_ModelWidget* aFilledWgt = 0;
290 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
291 if (aPropertyPanel) {
292 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
293 if (!aWidgets.empty()) {
294 ModuleBase_ModelWidget* aWgt = 0;
295 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
297 // 1. apply the selection to controls
298 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
300 if (!aWgt->canSetValue())
302 aPropertyPanel->setPreselectionWidget(aWgt);
303 if (!aWgt->setSelection(myPreSelection, true)) {
311 aPropertyPanel->setPreselectionWidget(NULL);
312 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
313 // it is better to perform it not in setSelection of each widget, but do it here,
314 // after the preselection is processed
315 ModuleBase_ModelWidget::updateObject(myFeature);
317 // 3. a signal should be emitted before the next widget activation
318 // because, the activation of the next widget will give a focus to the widget. As a result
319 // the value of the widget is initialized. And commit may happens until the value is entered.
321 emit activatedByPreselection();
323 // 4. activate the next obligatory widget
324 aPropertyPanel->activateNextWidget(aFilledWgt);
330 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
332 myParentFeature = theParent;
335 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
337 return myParentFeature;
340 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
342 myPreviousCurrentFeature = theFeature;
345 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
347 return myPreviousCurrentFeature;
350 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
351 ModuleBase_IViewer* theViewer)
355 QList<ModuleBase_ViewerPrs> aPreSelected;
356 // Check that the selected result are not results of operation feature
357 FeaturePtr aFeature = feature();
359 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
361 std::list<ResultPtr> aResults = aFeature->results();
362 QObjectPtrList aResList;
363 std::list<ResultPtr>::const_iterator aIt;
364 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
365 aResList.append(*aIt);
367 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
368 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
369 aPreSelected.append(aPrs);
372 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
374 myPreSelection = aPreSelected;
377 void ModuleBase_OperationFeature::clearPreselection()
379 myPreSelection.clear();
382 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
384 ModuleBase_Operation::setPropertyPanel(theProp);
386 theProp->setEditingMode(isEditOperation());
389 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
390 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
391 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
392 ModuleBase_ModelWidget* aWgt = (*aWIt);
393 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
397 // Do not activate widgets by default if the current operation is editing operation
398 // Because we don't know which widget is going to be edited.
399 if (!isEditOperation()) {
400 // 4. activate the first obligatory widget
401 theProp->activateNextWidget(NULL);
404 // set focus on Ok button in order to operation manager could process Enter press
406 theProp->setFocusOnOkButton();