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>
30 #include <GeomAPI_Pnt2d.h>
32 #include <Events_Loop.h>
40 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
41 : ModuleBase_Operation(theId, theParent),
46 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
51 FeaturePtr ModuleBase_OperationFeature::feature() const
56 bool ModuleBase_OperationFeature::isValid() const
58 if (!myFeature || !myFeature->data()->isValid())
59 return true; // rename operation
60 if (myFeature->isAction())
62 //Get validators for the Id
63 SessionPtr aMgr = ModelAPI_Session::get();
64 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
65 bool aValid = aFactory->validate(myFeature);
67 // the feature exec state should be checked in order to do not apply features, which result can not
68 // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
69 bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
70 || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
72 return aValid && isDone;
75 void ModuleBase_OperationFeature::startOperation()
77 FeaturePtr aFeature = feature();
78 if (!aFeature.get() || !isEditOperation())
81 myVisualizedObjects.clear();
82 // store hidden result features
83 std::list<ResultPtr> aResults = aFeature->results();
84 std::list<ResultPtr>::const_iterator aIt;
85 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
86 ObjectPtr anObject = *aIt;
87 if (anObject.get() && !anObject->isDisplayed()) {
88 myVisualizedObjects.insert(*aIt);
89 anObject->setDisplayed(true);
92 if (!aFeature->isDisplayed()) {
93 myVisualizedObjects.insert(aFeature);
94 aFeature->setDisplayed(true);
96 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
99 void ModuleBase_OperationFeature::stopOperation()
101 FeaturePtr aFeature = feature();
102 if (!aFeature.get() || !isEditOperation())
105 // store hidden result features
106 std::list<ResultPtr> aResults = aFeature->results();
107 std::list<ResultPtr>::const_iterator aIt;
108 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
109 ObjectPtr anObject = *aIt;
110 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
111 anObject->setDisplayed(false);
114 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
115 aFeature->setDisplayed(false);
117 if (myVisualizedObjects.size() > 0)
118 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
121 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
123 if (myParentFeature.get()) {
124 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
126 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
127 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
129 if (myFeature) { // TODO: generate an error if feature was not created
131 // Model update should call "execute" of a feature.
132 //myFeature->execute();
133 // Init default values
134 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
135 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
136 for (; anIt != aLast; anIt++) {
137 (*anIt)->storeValue(aFeature);
142 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
146 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
148 myFeature = theFeature;
152 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
154 FeaturePtr aFeature = feature();
156 if (aFeature == theObj)
158 std::list<ResultPtr> aResults = aFeature->results();
159 std::list<ResultPtr>::const_iterator aIt;
160 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
161 if (theObj == (*aIt))
168 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
170 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
173 void ModuleBase_OperationFeature::start()
175 setIsModified(false);
176 QString anId = getDescription()->operationId();
178 anId = anId.append(EditSuffix());
180 ModelAPI_Session::get()->startOperation(anId.toStdString());
185 FeaturePtr aFeature = createFeature();
186 // if the feature is not created, there is no sense to start the operation
187 if (aFeature.get() == NULL) {
188 // it is necessary to abor the operation in the session and emit the aborted signal
189 // in order to update commands status in the workshop, to be exact the feature action
195 /// Set current feature and remeber old current feature
197 SessionPtr aMgr = ModelAPI_Session::get();
198 DocumentPtr aDoc = aMgr->activeDocument();
199 // the parameter of current feature should be false, we should use all feature, not only visible
200 // in order to correctly save the previous feature of the nested operation, where the
201 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
202 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
203 // When sketch entity operation started, the sketch should be cashed here as the current.
204 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
205 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
206 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
207 myPreviousCurrentFeature = aDoc->currentFeature(false);
208 aDoc->setCurrentFeature(feature(), false);
216 void ModuleBase_OperationFeature::abort()
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 SessionPtr aMgr = ModelAPI_Session::get();
235 DocumentPtr aDoc = aMgr->activeDocument();
236 bool aIsOp = aMgr->isOperation();
238 aMgr->startOperation();
239 aDoc->setCurrentFeature(myPreviousCurrentFeature, true);
241 aMgr->finishOperation();
242 myPreviousCurrentFeature = FeaturePtr();
248 aMgr->abortOperation();
250 // the viewer update should be unblocked in order to avoid the features blinking before they are
252 aMsg = std::shared_ptr<Events_Message>(
253 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
255 Events_Loop::loop()->send(aMsg);
260 bool ModuleBase_OperationFeature::commit()
262 if (canBeCommitted()) {
263 // the widgets of property panel should not process any events come from data mode
264 // after commit clicked. Some signal such as redisplay/create influence on content
265 // of the object browser and viewer context. Therefore it influence to the current
266 // selection and if the active widget listens it, the attribute value is errnoneous
268 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
270 aPropertyPanel->cleanContent();
272 SessionPtr aMgr = ModelAPI_Session::get();
273 /// Set current feature and remeber old current feature
275 DocumentPtr aDoc = aMgr->activeDocument();
276 bool aIsOp = aMgr->isOperation();
278 aMgr->startOperation();
279 aDoc->setCurrentFeature(myPreviousCurrentFeature, true);
281 aMgr->finishOperation();
282 myPreviousCurrentFeature = FeaturePtr();
285 aMgr->finishOperation();
291 afterCommitOperation();
297 void ModuleBase_OperationFeature::activateByPreselection()
299 if (myPreSelection.empty())
302 ModuleBase_ModelWidget* aFilledWgt = 0;
303 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
304 if (aPropertyPanel) {
305 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
306 if (!aWidgets.empty()) {
307 ModuleBase_ModelWidget* aWgt = 0;
308 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
310 // 1. apply the selection to controls
311 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
313 if (!aWgt->canSetValue())
315 aPropertyPanel->setPreselectionWidget(aWgt);
316 if (!aWgt->setSelection(myPreSelection, true)) {
324 aPropertyPanel->setPreselectionWidget(NULL);
325 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
326 // it is better to perform it not in setSelection of each widget, but do it here,
327 // after the preselection is processed
328 ModuleBase_ModelWidget::updateObject(myFeature);
330 // 3. a signal should be emitted before the next widget activation
331 // because, the activation of the next widget will give a focus to the widget. As a result
332 // the value of the widget is initialized. And commit may happens until the value is entered.
334 emit activatedByPreselection();
336 // 4. activate the next obligatory widget
337 aPropertyPanel->activateNextWidget(aFilledWgt);
343 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
345 myParentFeature = theParent;
348 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
350 return myParentFeature;
353 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
354 ModuleBase_IViewer* theViewer)
358 QList<ModuleBase_ViewerPrs> aPreSelected;
359 // Check that the selected result are not results of operation feature
360 FeaturePtr aFeature = feature();
362 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
364 std::list<ResultPtr> aResults = aFeature->results();
365 QObjectPtrList aResList;
366 std::list<ResultPtr>::const_iterator aIt;
367 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
368 aResList.append(*aIt);
370 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
371 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
372 aPreSelected.append(aPrs);
375 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
377 myPreSelection = aPreSelected;
380 void ModuleBase_OperationFeature::clearPreselection()
382 myPreSelection.clear();
385 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
387 ModuleBase_Operation::setPropertyPanel(theProp);
389 theProp->setEditingMode(isEditOperation());
392 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
393 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
394 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
395 ModuleBase_ModelWidget* aWgt = (*aWIt);
396 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
400 // Do not activate widgets by default if the current operation is editing operation
401 // Because we don't know which widget is going to be edited.
402 if (!isEditOperation()) {
403 // 4. activate the first obligatory widget
404 theProp->activateNextWidget(NULL);
407 // set focus on Ok button in order to operation manager could process Enter press
409 theProp->setFocusOnOkButton();