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 myVisualizedObjects.clear();
83 // store hidden result features
84 std::list<ResultPtr> aResults = aFeature->results();
85 std::list<ResultPtr>::const_iterator aIt;
86 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
87 ObjectPtr anObject = *aIt;
88 if (anObject.get() && !anObject->isDisplayed()) {
89 myVisualizedObjects.insert(*aIt);
90 anObject->setDisplayed(true);
93 if (!aFeature->isDisplayed()) {
94 myVisualizedObjects.insert(aFeature);
95 aFeature->setDisplayed(true);
97 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
100 void ModuleBase_OperationFeature::stopOperation()
102 FeaturePtr aFeature = feature();
103 if (!aFeature.get() || !isEditOperation())
106 // store hidden result features
107 std::list<ResultPtr> aResults = aFeature->results();
108 std::list<ResultPtr>::const_iterator aIt;
109 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
110 ObjectPtr anObject = *aIt;
111 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
112 anObject->setDisplayed(false);
115 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
116 aFeature->setDisplayed(false);
118 aFeature->setStable(true);
119 if (myVisualizedObjects.size() > 0)
120 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
123 void ModuleBase_OperationFeature::resumeOperation()
125 ModuleBase_Operation::resumeOperation();
127 setCurrentFeature(feature());
128 //SessionPtr aMgr = ModelAPI_Session::get();
129 //DocumentPtr aDoc = aMgr->activeDocument();
130 //aDoc->setCurrentFeature(feature(), false);
133 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
135 if (myParentFeature.get()) {
136 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
138 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
139 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
141 if (myFeature) { // TODO: generate an error if feature was not created
143 // Model update should call "execute" of a feature.
144 //myFeature->execute();
145 // Init default values
146 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
147 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
148 for (; anIt != aLast; anIt++) {
149 (*anIt)->storeValue(aFeature);
154 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
158 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
160 myFeature = theFeature;
161 myFeature->setStable(false);
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());
198 FeaturePtr aFeature = createFeature();
199 // if the feature is not created, there is no sense to start the operation
200 if (aFeature.get() == NULL) {
201 // it is necessary to abor the operation in the session and emit the aborted signal
202 // in order to update commands status in the workshop, to be exact the feature action
208 /// Set current feature and remeber old current feature
210 SessionPtr aMgr = ModelAPI_Session::get();
211 DocumentPtr aDoc = aMgr->activeDocument();
212 // the parameter of current feature should be false, we should use all feature, not only visible
213 // in order to correctly save the previous feature of the nested operation, where the
214 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
215 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
216 // When sketch entity operation started, the sketch should be cashed here as the current.
217 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
218 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
219 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
220 myPreviousCurrentFeature = aDoc->currentFeature(false);
221 aDoc->setCurrentFeature(feature(), false);
229 void ModuleBase_OperationFeature::abort()
231 // the viewer update should be blocked in order to avoid the features blinking before they are
233 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
234 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
235 Events_Loop::loop()->send(aMsg);
237 // the widgets of property panel should not process any events come from data mode
238 // after abort clicked. Some signal such as redisplay/create influence on content
239 // of the object browser and viewer context. Therefore it influence to the current
240 // selection and if the active widget listens it, the attribute value is errnoneous
242 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
244 aPropertyPanel->cleanContent();
246 SessionPtr aMgr = ModelAPI_Session::get();
248 DocumentPtr aDoc = aMgr->activeDocument();
249 bool aIsOp = aMgr->isOperation();
251 aMgr->startOperation();
252 aDoc->setCurrentFeature(myPreviousCurrentFeature, false);//true);
254 aMgr->finishOperation();
255 myPreviousCurrentFeature = FeaturePtr();
261 aMgr->abortOperation();
263 // the viewer update should be unblocked in order to avoid the features blinking before they are
265 aMsg = std::shared_ptr<Events_Message>(
266 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
268 Events_Loop::loop()->send(aMsg);
273 bool ModuleBase_OperationFeature::commit()
275 if (canBeCommitted()) {
276 // the widgets of property panel should not process any events come from data mode
277 // after commit clicked. Some signal such as redisplay/create influence on content
278 // of the object browser and viewer context. Therefore it influence to the current
279 // selection and if the active widget listens it, the attribute value is errnoneous
281 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
283 aPropertyPanel->cleanContent();
285 SessionPtr aMgr = ModelAPI_Session::get();
286 /// Set current feature and remeber old current feature
289 setCurrentFeature(myPreviousCurrentFeature);
290 /*DocumentPtr aDoc = aMgr->activeDocument();
291 bool aIsOp = aMgr->isOperation();
293 aMgr->startOperation();
294 aDoc->setCurrentFeature(myPreviousCurrentFeature, true);
296 aMgr->finishOperation();*/
297 myPreviousCurrentFeature = FeaturePtr();
300 /*CompositeFeaturePtr aCompositeFeature = ModelAPI_Tools::compositeOwner(feature());
301 if (aCompositeFeature.get())
302 setCurrentFeature(aCompositeFeature);//myPreviousCurrentFeature);
304 // setCurrentFeature(feature());*/
307 aMgr->finishOperation();
313 afterCommitOperation();
319 void ModuleBase_OperationFeature::setCurrentFeature(const FeaturePtr& theFeature)
321 SessionPtr aMgr = ModelAPI_Session::get();
322 DocumentPtr aDoc = aMgr->activeDocument();
323 bool aIsOp = aMgr->isOperation();
325 aMgr->startOperation();
326 aDoc->setCurrentFeature(theFeature, false);//true);
328 aMgr->finishOperation();
331 void ModuleBase_OperationFeature::activateByPreselection()
333 if (myPreSelection.empty())
336 ModuleBase_ModelWidget* aFilledWgt = 0;
337 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
338 if (aPropertyPanel) {
339 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
340 if (!aWidgets.empty()) {
341 ModuleBase_ModelWidget* aWgt = 0;
342 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
344 // 1. apply the selection to controls
345 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
347 if (!aWgt->canSetValue())
349 aPropertyPanel->setPreselectionWidget(aWgt);
350 if (!aWgt->setSelection(myPreSelection, true)) {
358 aPropertyPanel->setPreselectionWidget(NULL);
359 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
360 // it is better to perform it not in setSelection of each widget, but do it here,
361 // after the preselection is processed
362 ModuleBase_ModelWidget::updateObject(myFeature);
364 // 3. a signal should be emitted before the next widget activation
365 // because, the activation of the next widget will give a focus to the widget. As a result
366 // the value of the widget is initialized. And commit may happens until the value is entered.
368 emit activatedByPreselection();
370 // 4. activate the next obligatory widget
371 aPropertyPanel->activateNextWidget(aFilledWgt);
377 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
379 myParentFeature = theParent;
382 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
384 return myParentFeature;
387 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
388 ModuleBase_IViewer* theViewer)
392 QList<ModuleBase_ViewerPrs> aPreSelected;
393 // Check that the selected result are not results of operation feature
394 FeaturePtr aFeature = feature();
396 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
398 std::list<ResultPtr> aResults = aFeature->results();
399 QObjectPtrList aResList;
400 std::list<ResultPtr>::const_iterator aIt;
401 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
402 aResList.append(*aIt);
404 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
405 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
406 aPreSelected.append(aPrs);
409 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
411 myPreSelection = aPreSelected;
414 void ModuleBase_OperationFeature::clearPreselection()
416 myPreSelection.clear();
419 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
421 ModuleBase_Operation::setPropertyPanel(theProp);
423 theProp->setEditingMode(isEditOperation());
426 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
427 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
428 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
429 ModuleBase_ModelWidget* aWgt = (*aWIt);
430 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
434 // Do not activate widgets by default if the current operation is editing operation
435 // Because we don't know which widget is going to be edited.
436 if (!isEditOperation()) {
437 // 4. activate the first obligatory widget
438 theProp->activateNextWidget(NULL);
441 // set focus on Ok button in order to operation manager could process Enter press
443 theProp->setFocusOnOkButton();