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 if (aFeature.get() && isEditOperation())
83 aFeature->setStable(false);
85 myVisualizedObjects.clear();
86 // store hidden result features
87 std::list<ResultPtr> aResults = aFeature->results();
88 std::list<ResultPtr>::const_iterator aIt;
89 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
90 ObjectPtr anObject = *aIt;
91 if (anObject.get() && !anObject->isDisplayed()) {
92 myVisualizedObjects.insert(*aIt);
93 anObject->setDisplayed(true);
96 if (!aFeature->isDisplayed()) {
97 myVisualizedObjects.insert(aFeature);
98 aFeature->setDisplayed(true);
100 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
103 void ModuleBase_OperationFeature::stopOperation()
105 FeaturePtr aFeature = feature();
106 if (!aFeature.get() || !isEditOperation())
109 // store hidden result features
110 std::list<ResultPtr> aResults = aFeature->results();
111 std::list<ResultPtr>::const_iterator aIt;
112 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
113 ObjectPtr anObject = *aIt;
114 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
115 anObject->setDisplayed(false);
118 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
119 aFeature->setDisplayed(false);
121 if (myVisualizedObjects.size() > 0)
122 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
125 void ModuleBase_OperationFeature::resumeOperation()
127 ModuleBase_Operation::resumeOperation();
129 setCurrentFeature(feature());
130 //SessionPtr aMgr = ModelAPI_Session::get();
131 //DocumentPtr aDoc = aMgr->activeDocument();
132 //aDoc->setCurrentFeature(feature(), false);
135 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
137 if (myParentFeature.get()) {
138 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
140 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
141 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
143 if (myFeature) { // TODO: generate an error if feature was not created
145 // Model update should call "execute" of a feature.
146 //myFeature->execute();
147 // Init default values
148 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
149 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
150 for (; anIt != aLast; anIt++) {
151 (*anIt)->storeValue(aFeature);
156 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
160 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
162 myFeature = theFeature;
166 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
168 FeaturePtr aFeature = feature();
170 if (aFeature == theObj)
172 std::list<ResultPtr> aResults = aFeature->results();
173 std::list<ResultPtr>::const_iterator aIt;
174 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
175 if (theObj == (*aIt))
182 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
184 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
187 void ModuleBase_OperationFeature::start()
189 setIsModified(false);
190 QString anId = getDescription()->operationId();
192 anId = anId.append(EditSuffix());
194 ModelAPI_Session::get()->startOperation(anId.toStdString());
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 /// Set current feature and remeber old current feature
211 SessionPtr aMgr = ModelAPI_Session::get();
212 DocumentPtr aDoc = aMgr->activeDocument();
213 // the parameter of current feature should be false, we should use all feature, not only visible
214 // in order to correctly save the previous feature of the nested operation, where the
215 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
216 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
217 // When sketch entity operation started, the sketch should be cashed here as the current.
218 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
219 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
220 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
221 myPreviousCurrentFeature = aDoc->currentFeature(false);
222 aDoc->setCurrentFeature(feature(), false);
225 //Already called startOperation();
230 void ModuleBase_OperationFeature::abort()
232 // the viewer update should be blocked in order to avoid the features blinking before they are
234 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
235 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
236 Events_Loop::loop()->send(aMsg);
238 // the widgets of property panel should not process any events come from data mode
239 // after abort clicked. Some signal such as redisplay/create influence on content
240 // of the object browser and viewer context. Therefore it influence to the current
241 // selection and if the active widget listens it, the attribute value is errnoneous
243 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
245 aPropertyPanel->cleanContent();
247 myFeature->setStable(true);
249 SessionPtr aMgr = ModelAPI_Session::get();
251 DocumentPtr aDoc = aMgr->activeDocument();
252 bool aIsOp = aMgr->isOperation();
254 aMgr->startOperation();
255 aDoc->setCurrentFeature(myPreviousCurrentFeature, false);//true);
257 aMgr->finishOperation();
258 myPreviousCurrentFeature = FeaturePtr();
264 aMgr->abortOperation();
266 // the viewer update should be unblocked in order to avoid the features blinking before they are
268 aMsg = std::shared_ptr<Events_Message>(
269 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
271 Events_Loop::loop()->send(aMsg);
276 bool ModuleBase_OperationFeature::commit()
278 if (canBeCommitted()) {
279 // the widgets of property panel should not process any events come from data mode
280 // after commit clicked. Some signal such as redisplay/create influence on content
281 // of the object browser and viewer context. Therefore it influence to the current
282 // selection and if the active widget listens it, the attribute value is errnoneous
284 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
286 aPropertyPanel->cleanContent();
288 myFeature->setStable(true);
290 SessionPtr aMgr = ModelAPI_Session::get();
291 /// Set current feature and remeber old current feature
294 setCurrentFeature(myPreviousCurrentFeature);
295 /*DocumentPtr aDoc = aMgr->activeDocument();
296 bool aIsOp = aMgr->isOperation();
298 aMgr->startOperation();
299 aDoc->setCurrentFeature(myPreviousCurrentFeature, true);
301 aMgr->finishOperation();*/
302 myPreviousCurrentFeature = FeaturePtr();
305 /*CompositeFeaturePtr aCompositeFeature = ModelAPI_Tools::compositeOwner(feature());
306 if (aCompositeFeature.get())
307 setCurrentFeature(aCompositeFeature);//myPreviousCurrentFeature);
309 // setCurrentFeature(feature());*/
312 aMgr->finishOperation();
318 afterCommitOperation();
324 void ModuleBase_OperationFeature::setCurrentFeature(const FeaturePtr& theFeature)
326 SessionPtr aMgr = ModelAPI_Session::get();
327 DocumentPtr aDoc = aMgr->activeDocument();
328 bool aIsOp = aMgr->isOperation();
330 aMgr->startOperation();
331 aDoc->setCurrentFeature(theFeature, false);//true);
333 aMgr->finishOperation();
336 void ModuleBase_OperationFeature::activateByPreselection()
338 if (myPreSelection.empty())
341 ModuleBase_ModelWidget* aFilledWgt = 0;
342 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
343 if (aPropertyPanel) {
344 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
345 if (!aWidgets.empty()) {
346 ModuleBase_ModelWidget* aWgt = 0;
347 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
349 // 1. apply the selection to controls
350 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
352 if (!aWgt->canSetValue())
354 aPropertyPanel->setPreselectionWidget(aWgt);
355 if (!aWgt->setSelection(myPreSelection, true)) {
363 aPropertyPanel->setPreselectionWidget(NULL);
364 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
365 // it is better to perform it not in setSelection of each widget, but do it here,
366 // after the preselection is processed
367 ModuleBase_ModelWidget::updateObject(myFeature);
369 // 3. a signal should be emitted before the next widget activation
370 // because, the activation of the next widget will give a focus to the widget. As a result
371 // the value of the widget is initialized. And commit may happens until the value is entered.
373 emit activatedByPreselection();
375 // 4. activate the next obligatory widget
376 aPropertyPanel->activateNextWidget(aFilledWgt);
382 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
384 myParentFeature = theParent;
387 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
389 return myParentFeature;
392 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
393 ModuleBase_IViewer* theViewer)
397 QList<ModuleBase_ViewerPrs> aPreSelected;
398 // Check that the selected result are not results of operation feature
399 FeaturePtr aFeature = feature();
401 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
403 std::list<ResultPtr> aResults = aFeature->results();
404 QObjectPtrList aResList;
405 std::list<ResultPtr>::const_iterator aIt;
406 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
407 aResList.append(*aIt);
409 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
410 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
411 aPreSelected.append(aPrs);
414 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
416 myPreSelection = aPreSelected;
419 void ModuleBase_OperationFeature::clearPreselection()
421 myPreSelection.clear();
424 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
426 ModuleBase_Operation::setPropertyPanel(theProp);
428 theProp->setEditingMode(isEditOperation());
431 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
432 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
433 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
434 ModuleBase_ModelWidget* aWgt = (*aWIt);
435 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
439 // Do not activate widgets by default if the current operation is editing operation
440 // Because we don't know which widget is going to be edited.
441 if (!isEditOperation()) {
442 // 4. activate the first obligatory widget
443 theProp->activateNextWidget(NULL);
446 // set focus on Ok button in order to operation manager could process Enter press
448 theProp->setFocusOnOkButton();