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 #define APPLY_BY_ENTER_OR_TAB
43 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
44 : ModuleBase_Operation(theId, theParent),
49 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
54 FeaturePtr ModuleBase_OperationFeature::feature() const
59 bool ModuleBase_OperationFeature::isValid() const
61 if (!myFeature || !myFeature->data()->isValid())
62 return true; // rename operation
63 if (myFeature->isAction())
65 //Get validators for the Id
66 SessionPtr aMgr = ModelAPI_Session::get();
67 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
68 bool aValid = aFactory->validate(myFeature);
70 // the feature exec state should be checked in order to do not apply features, which result can not
71 // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
72 bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
73 || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
75 return aValid && isDone;
78 void ModuleBase_OperationFeature::startOperation()
80 FeaturePtr aFeature = feature();
81 if (!aFeature.get() || !isEditOperation())
84 if (aFeature.get() && isEditOperation())
85 aFeature->setStable(false);
87 myVisualizedObjects.clear();
88 // store hidden result features
89 std::list<ResultPtr> aResults = aFeature->results();
90 std::list<ResultPtr>::const_iterator aIt;
91 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
92 ObjectPtr anObject = *aIt;
93 if (anObject.get() && !anObject->isDisplayed()) {
94 myVisualizedObjects.insert(*aIt);
95 anObject->setDisplayed(true);
98 if (!aFeature->isDisplayed()) {
99 myVisualizedObjects.insert(aFeature);
100 aFeature->setDisplayed(true);
102 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
105 void ModuleBase_OperationFeature::stopOperation()
107 FeaturePtr aFeature = feature();
108 if (!aFeature.get() || !isEditOperation())
111 // store hidden result features
112 std::list<ResultPtr> aResults = aFeature->results();
113 std::list<ResultPtr>::const_iterator aIt;
114 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
115 ObjectPtr anObject = *aIt;
116 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
117 anObject->setDisplayed(false);
120 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
121 aFeature->setDisplayed(false);
123 if (myVisualizedObjects.size() > 0)
124 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
127 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
129 if (myParentFeature.get()) {
130 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
132 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
133 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
135 if (myFeature) { // TODO: generate an error if feature was not created
137 // Model update should call "execute" of a feature.
138 //myFeature->execute();
139 // Init default values
140 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
141 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
142 for (; anIt != aLast; anIt++) {
143 (*anIt)->storeValue(aFeature);
148 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
152 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
154 myFeature = theFeature;
158 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
160 FeaturePtr aFeature = feature();
162 if (aFeature == theObj)
164 std::list<ResultPtr> aResults = aFeature->results();
165 std::list<ResultPtr>::const_iterator aIt;
166 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
167 if (theObj == (*aIt))
174 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
176 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
179 void ModuleBase_OperationFeature::start()
181 setIsModified(false);
182 QString anId = getDescription()->operationId();
184 anId = anId.append(EditSuffix());
186 ModelAPI_Session::get()->startOperation(anId.toStdString());
188 emit beforeStarted();
192 FeaturePtr aFeature = createFeature();
193 // if the feature is not created, there is no sense to start the operation
194 if (aFeature.get() == NULL) {
195 // it is necessary to abor the operation in the session and emit the aborted signal
196 // in order to update commands status in the workshop, to be exact the feature action
202 //Already called startOperation();
207 void ModuleBase_OperationFeature::abort()
209 emit beforeAborted();
211 // the viewer update should be blocked in order to avoid the features blinking before they are
213 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
214 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
215 Events_Loop::loop()->send(aMsg);
217 // the widgets of property panel should not process any events come from data mode
218 // after abort clicked. Some signal such as redisplay/create influence on content
219 // of the object browser and viewer context. Therefore it influence to the current
220 // selection and if the active widget listens it, the attribute value is errnoneous
222 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
224 aPropertyPanel->cleanContent();
226 myFeature->setStable(true);
231 SessionPtr aMgr = ModelAPI_Session::get();
232 aMgr->abortOperation();
234 // the viewer update should be unblocked in order to avoid the features blinking before they are
236 aMsg = std::shared_ptr<Events_Message>(
237 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
239 Events_Loop::loop()->send(aMsg);
244 bool ModuleBase_OperationFeature::commit()
246 if (canBeCommitted()) {
247 // the widgets of property panel should not process any events come from data mode
248 // after commit clicked. Some signal such as redisplay/create influence on content
249 // of the object browser and viewer context. Therefore it influence to the current
250 // selection and if the active widget listens it, the attribute value is errnoneous
252 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
254 aPropertyPanel->cleanContent();
256 myFeature->setStable(true);
258 SessionPtr aMgr = ModelAPI_Session::get();
259 /// Set current feature and remeber old current feature
261 emit beforeCommitted();
263 aMgr->finishOperation();
269 afterCommitOperation();
275 void ModuleBase_OperationFeature::activateByPreselection()
277 if (myPreSelection.empty())
280 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
282 ModuleBase_ModelWidget* aFilledWgt = 0;
283 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
284 if (aPropertyPanel) {
285 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
286 if (!aWidgets.empty()) {
287 ModuleBase_ModelWidget* aWgt = 0;
288 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
290 // 1. apply the selection to controls
291 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
293 if (!aWgt->canSetValue())
295 aPropertyPanel->setPreselectionWidget(aWgt);
296 if (!aWgt->setSelection(myPreSelection, true)) {
304 aPropertyPanel->setPreselectionWidget(NULL);
305 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
306 // it is better to perform it not in setSelection of each widget, but do it here,
307 // after the preselection is processed
308 ModuleBase_ModelWidget::updateObject(myFeature);
310 // 3. a signal should be emitted before the next widget activation
311 // because, the activation of the next widget will give a focus to the widget. As a result
312 // the value of the widget is initialized. And commit may happens until the value is entered.
314 emit activatedByPreselection();
316 // 4. activate the next obligatory widget
317 aPropertyPanel->activateNextWidget(aFilledWgt);
323 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
325 myParentFeature = theParent;
328 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
330 return myParentFeature;
333 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
335 myPreviousCurrentFeature = theFeature;
338 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
340 return myPreviousCurrentFeature;
343 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
344 ModuleBase_IViewer* theViewer)
348 QList<ModuleBase_ViewerPrs> aPreSelected;
349 // Check that the selected result are not results of operation feature
350 FeaturePtr aFeature = feature();
352 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
354 std::list<ResultPtr> aResults = aFeature->results();
355 QObjectPtrList aResList;
356 std::list<ResultPtr>::const_iterator aIt;
357 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
358 aResList.append(*aIt);
360 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
361 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
362 aPreSelected.append(aPrs);
365 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
367 myPreSelection = aPreSelected;
370 void ModuleBase_OperationFeature::clearPreselection()
372 myPreSelection.clear();
375 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
377 ModuleBase_Operation::setPropertyPanel(theProp);
379 theProp->setEditingMode(isEditOperation());
382 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
383 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
384 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
385 ModuleBase_ModelWidget* aWgt = (*aWIt);
386 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
390 // Do not activate widgets by default if the current operation is editing operation
391 // Because we don't know which widget is going to be edited.
392 if (!isEditOperation()) {
393 // 4. activate the first obligatory widget
394 theProp->activateNextWidget(NULL);
397 // set focus on Ok button in order to operation manager could process Enter press
399 theProp->setFocusOnOkButton();