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 // store hidden result features
82 std::list<ResultPtr> aResults = aFeature->results();
83 std::list<ResultPtr>::const_iterator aIt;
84 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
85 ObjectPtr anObject = *aIt;
86 if (anObject.get() && !anObject->isDisplayed()) {
87 myVisualizedObjects.insert(*aIt);
88 anObject->setDisplayed(true);
91 if (!aFeature->isDisplayed()) {
92 myVisualizedObjects.insert(*aIt);
93 aFeature->setDisplayed(true);
95 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
98 void ModuleBase_OperationFeature::stopOperation()
100 FeaturePtr aFeature = feature();
101 if (!aFeature.get() || !isEditOperation())
104 // store hidden result features
105 std::list<ResultPtr> aResults = aFeature->results();
106 std::list<ResultPtr>::const_iterator aIt;
107 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
108 ObjectPtr anObject = *aIt;
109 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
110 anObject->setDisplayed(false);
113 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
114 aFeature->setDisplayed(false);
116 if (myVisualizedObjects.size() > 0)
117 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
120 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
122 if (myParentFeature.get()) {
123 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
125 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
126 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
128 if (myFeature) { // TODO: generate an error if feature was not created
130 // Model update should call "execute" of a feature.
131 //myFeature->execute();
132 // Init default values
133 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
134 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
135 for (; anIt != aLast; anIt++) {
136 (*anIt)->storeValue(aFeature);
141 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
145 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
147 myFeature = theFeature;
151 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
153 FeaturePtr aFeature = feature();
155 if (aFeature == theObj)
157 std::list<ResultPtr> aResults = aFeature->results();
158 std::list<ResultPtr>::const_iterator aIt;
159 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
160 if (theObj == (*aIt))
167 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
169 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
172 void ModuleBase_OperationFeature::start()
174 setIsModified(false);
175 QString anId = getDescription()->operationId();
177 anId = anId.append(EditSuffix());
179 ModelAPI_Session::get()->startOperation(anId.toStdString());
184 FeaturePtr aFeature = createFeature();
185 // if the feature is not created, there is no sense to start the operation
186 if (aFeature.get() == NULL) {
187 // it is necessary to abor the operation in the session and emit the aborted signal
188 // in order to update commands status in the workshop, to be exact the feature action
194 /// Set current feature and remeber old current feature
196 SessionPtr aMgr = ModelAPI_Session::get();
197 DocumentPtr aDoc = aMgr->activeDocument();
198 // the parameter of current feature should be false, we should use all feature, not only visible
199 // in order to correctly save the previous feature of the nested operation, where the
200 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
201 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
202 // When sketch entity operation started, the sketch should be cashed here as the current.
203 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
204 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
205 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
206 myPreviousCurrentFeature = aDoc->currentFeature(false);
207 aDoc->setCurrentFeature(feature(), false);
215 void ModuleBase_OperationFeature::abort()
217 // the viewer update should be blocked in order to avoid the features blinking before they are
219 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
220 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
221 Events_Loop::loop()->send(aMsg);
223 // the widgets of property panel should not process any events come from data mode
224 // after abort clicked. Some signal such as redisplay/create influence on content
225 // of the object browser and viewer context. Therefore it influence to the current
226 // selection and if the active widget listens it, the attribute value is errnoneous
228 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
230 aPropertyPanel->cleanContent();
232 SessionPtr aMgr = ModelAPI_Session::get();
234 DocumentPtr aDoc = aMgr->activeDocument();
235 bool aIsOp = aMgr->isOperation();
237 aMgr->startOperation();
238 aDoc->setCurrentFeature(myPreviousCurrentFeature, true);
240 aMgr->finishOperation();
241 myPreviousCurrentFeature = FeaturePtr();
247 aMgr->abortOperation();
249 // the viewer update should be unblocked in order to avoid the features blinking before they are
251 aMsg = std::shared_ptr<Events_Message>(
252 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
254 Events_Loop::loop()->send(aMsg);
259 bool ModuleBase_OperationFeature::commit()
261 if (canBeCommitted()) {
262 // the widgets of property panel should not process any events come from data mode
263 // after commit clicked. Some signal such as redisplay/create influence on content
264 // of the object browser and viewer context. Therefore it influence to the current
265 // selection and if the active widget listens it, the attribute value is errnoneous
267 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
269 aPropertyPanel->cleanContent();
271 SessionPtr aMgr = ModelAPI_Session::get();
272 /// Set current feature and remeber old current feature
274 DocumentPtr aDoc = aMgr->activeDocument();
275 bool aIsOp = aMgr->isOperation();
277 aMgr->startOperation();
278 aDoc->setCurrentFeature(myPreviousCurrentFeature, true);
280 aMgr->finishOperation();
281 myPreviousCurrentFeature = FeaturePtr();
284 aMgr->finishOperation();
290 afterCommitOperation();
296 void ModuleBase_OperationFeature::activateByPreselection()
298 if (myPreSelection.empty())
301 ModuleBase_ModelWidget* aFilledWgt = 0;
302 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
303 if (aPropertyPanel) {
304 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
305 if (!aWidgets.empty()) {
306 ModuleBase_ModelWidget* aWgt = 0;
307 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
309 // 1. apply the selection to controls
310 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
312 if (!aWgt->canSetValue())
314 aPropertyPanel->setPreselectionWidget(aWgt);
315 if (!aWgt->setSelection(myPreSelection, true)) {
323 aPropertyPanel->setPreselectionWidget(NULL);
324 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
325 // it is better to perform it not in setSelection of each widget, but do it here,
326 // after the preselection is processed
327 ModuleBase_ModelWidget::updateObject(myFeature);
329 // 3. a signal should be emitted before the next widget activation
330 // because, the activation of the next widget will give a focus to the widget. As a result
331 // the value of the widget is initialized. And commit may happens until the value is entered.
333 emit activatedByPreselection();
335 // 4. activate the next obligatory widget
336 aPropertyPanel->activateNextWidget(aFilledWgt);
342 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
344 myParentFeature = theParent;
347 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
349 return myParentFeature;
352 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
353 ModuleBase_IViewer* theViewer)
357 QList<ModuleBase_ViewerPrs> aPreSelected;
358 // Check that the selected result are not results of operation feature
359 FeaturePtr aFeature = feature();
361 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
363 std::list<ResultPtr> aResults = aFeature->results();
364 QObjectPtrList aResList;
365 std::list<ResultPtr>::const_iterator aIt;
366 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
367 aResList.append(*aIt);
369 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
370 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
371 aPreSelected.append(aPrs);
374 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
376 myPreSelection = aPreSelected;
379 void ModuleBase_OperationFeature::clearPreselection()
381 myPreSelection.clear();
384 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
386 ModuleBase_Operation::setPropertyPanel(theProp);
388 theProp->setEditingMode(isEditOperation());
391 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
392 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
393 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
394 ModuleBase_ModelWidget* aWgt = (*aWIt);
395 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
399 // Do not activate widgets by default if the current operation is editing operation
400 // Because we don't know which widget is going to be edited.
401 if (!isEditOperation()) {
402 // 4. activate the first obligatory widget
403 theProp->activateNextWidget(NULL);
406 // set focus on Ok button in order to operation manager could process Enter press
408 theProp->setFocusOnOkButton();