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 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
77 if (myParentFeature.get()) {
78 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
80 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
81 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
83 if (myFeature) { // TODO: generate an error if feature was not created
85 // Model update should call "execute" of a feature.
86 //myFeature->execute();
87 // Init default values
88 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
89 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
90 for (; anIt != aLast; anIt++) {
91 (*anIt)->storeValue(aFeature);
96 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
100 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
102 myFeature = theFeature;
106 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
108 FeaturePtr aFeature = feature();
110 if (aFeature == theObj)
112 std::list<ResultPtr> aResults = aFeature->results();
113 std::list<ResultPtr>::const_iterator aIt;
114 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
115 if (theObj == (*aIt))
122 void ModuleBase_OperationFeature::start()
124 setIsModified(false);
125 QString anId = getDescription()->operationId();
127 anId = anId.append(EditSuffix());
129 ModelAPI_Session::get()->startOperation(anId.toStdString());
134 FeaturePtr aFeature = createFeature();
135 // if the feature is not created, there is no sense to start the operation
136 if (aFeature.get() == NULL) {
137 // it is necessary to abor the operation in the session and emit the aborted signal
138 // in order to update commands status in the workshop, to be exact the feature action
144 /// Set current feature and remeber old current feature
146 SessionPtr aMgr = ModelAPI_Session::get();
147 DocumentPtr aDoc = aMgr->activeDocument();
148 // the parameter of current feature should be false, we should use all feature, not only visible
149 // in order to correctly save the previous feature of the nested operation, where the
150 // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
151 // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
152 // When sketch entity operation started, the sketch should be cashed here as the current.
153 // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
154 // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
155 // As a result the sketch disappears from the viewer. However after commit it is displayed back.
156 myPreviousCurrentFeature = aDoc->currentFeature(false);
157 aDoc->setCurrentFeature(feature(), false);
165 void ModuleBase_OperationFeature::abort()
167 // the viewer update should be blocked in order to avoid the features blinking before they are
169 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
170 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
171 Events_Loop::loop()->send(aMsg);
173 // the widgets of property panel should not process any events come from data mode
174 // after abort clicked. Some signal such as redisplay/create influence on content
175 // of the object browser and viewer context. Therefore it influence to the current
176 // selection and if the active widget listens it, the attribute value is errnoneous
178 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
180 aPropertyPanel->cleanContent();
182 SessionPtr aMgr = ModelAPI_Session::get();
184 DocumentPtr aDoc = aMgr->activeDocument();
185 bool aIsOp = aMgr->isOperation();
187 aMgr->startOperation();
188 aDoc->setCurrentFeature(myPreviousCurrentFeature, true);
190 aMgr->finishOperation();
191 myPreviousCurrentFeature = FeaturePtr();
196 // is is necessary to deactivate current widgets before the model operation is aborted
197 // because abort removes the feature and activated filters should not check it
198 propertyPanel()->cleanContent();
200 aMgr->abortOperation();
202 // the viewer update should be unblocked in order to avoid the features blinking before they are
204 aMsg = std::shared_ptr<Events_Message>(
205 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
207 Events_Loop::loop()->send(aMsg);
212 bool ModuleBase_OperationFeature::commit()
214 if (canBeCommitted()) {
215 // the widgets of property panel should not process any events come from data mode
216 // after commit clicked. Some signal such as redisplay/create influence on content
217 // of the object browser and viewer context. Therefore it influence to the current
218 // selection and if the active widget listens it, the attribute value is errnoneous
220 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
222 aPropertyPanel->cleanContent();
224 SessionPtr aMgr = ModelAPI_Session::get();
225 /// Set current feature and remeber old current feature
227 DocumentPtr aDoc = aMgr->activeDocument();
228 bool aIsOp = aMgr->isOperation();
230 aMgr->startOperation();
231 aDoc->setCurrentFeature(myPreviousCurrentFeature, true);
233 aMgr->finishOperation();
234 myPreviousCurrentFeature = FeaturePtr();
237 aMgr->finishOperation();
243 afterCommitOperation();
249 void ModuleBase_OperationFeature::activateByPreselection()
251 if (myPreSelection.empty())
254 ModuleBase_ModelWidget* aFilledWgt = 0;
255 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
256 if (aPropertyPanel) {
257 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
258 if (!aWidgets.empty()) {
259 ModuleBase_ModelWidget* aWgt = 0;
260 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
262 // 1. apply the selection to controls
263 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
265 if (!aWgt->canSetValue())
267 aPropertyPanel->setPreselectionWidget(aWgt);
268 if (!aWgt->setSelection(myPreSelection, true)) {
276 aPropertyPanel->setPreselectionWidget(NULL);
277 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
278 // it is better to perform it not in setSelection of each widget, but do it here,
279 // after the preselection is processed
280 ModuleBase_ModelWidget::updateObject(myFeature);
282 // 3. a signal should be emitted before the next widget activation
283 // because, the activation of the next widget will give a focus to the widget. As a result
284 // the value of the widget is initialized. And commit may happens until the value is entered.
286 emit activatedByPreselection();
288 // 4. activate the next obligatory widget
289 aPropertyPanel->activateNextWidget(aFilledWgt);
295 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
297 myParentFeature = theParent;
300 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
302 return myParentFeature;
305 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
306 ModuleBase_IViewer* theViewer)
310 QList<ModuleBase_ViewerPrs> aPreSelected;
311 // Check that the selected result are not results of operation feature
312 FeaturePtr aFeature = feature();
314 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
316 std::list<ResultPtr> aResults = aFeature->results();
317 QObjectPtrList aResList;
318 std::list<ResultPtr>::const_iterator aIt;
319 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
320 aResList.append(*aIt);
322 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
323 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
324 aPreSelected.append(aPrs);
327 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
329 myPreSelection = aPreSelected;
332 void ModuleBase_OperationFeature::clearPreselection()
334 myPreSelection.clear();
337 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
339 ModuleBase_Operation::setPropertyPanel(theProp);
341 theProp->setEditingMode(isEditOperation());
344 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
345 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
346 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
347 ModuleBase_ModelWidget* aWgt = (*aWIt);
348 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
352 // Do not activate widgets by default if the current operation is editing operation
353 // Because we don't know which widget is going to be edited.
354 if (!isEditOperation()) {
355 // 4. activate the first obligatory widget
356 theProp->activateNextWidget(NULL);