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 void ModuleBase_OperationFeature::setEditOperation(const bool theRestartTransaction)
54 if (isEditOperation())
57 if (theRestartTransaction) {
58 // finsh previous create operation
59 emit beforeCommitted();
60 SessionPtr aMgr = ModelAPI_Session::get();
61 ModelAPI_Session::get()->finishOperation();
63 // start new edit operation
65 QString anId = getDescription()->operationId();
67 anId = anId.append(EditSuffix());
69 ModelAPI_Session::get()->startOperation(anId.toStdString());
75 propertyPanel()->setEditingMode(isEditOperation());
78 FeaturePtr ModuleBase_OperationFeature::feature() const
83 bool ModuleBase_OperationFeature::isValid() const
85 if (!myFeature || !myFeature->data()->isValid())
86 return true; // rename operation
87 if (myFeature->isAction())
89 //Get validators for the Id
90 SessionPtr aMgr = ModelAPI_Session::get();
91 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
92 bool aValid = aFactory->validate(myFeature);
94 // the feature exec state should be checked in order to do not apply features, which result can not
95 // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
96 bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
97 || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
99 return aValid && isDone;
102 void ModuleBase_OperationFeature::startOperation()
104 FeaturePtr aFeature = feature();
105 if (!aFeature.get() || !isEditOperation())
108 if (aFeature.get() && isEditOperation())
109 aFeature->setStable(false);
111 myVisualizedObjects.clear();
112 // store hidden result features
113 std::list<ResultPtr> aResults = aFeature->results();
114 std::list<ResultPtr>::const_iterator aIt;
115 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
116 ObjectPtr anObject = *aIt;
117 if (anObject.get() && !anObject->isDisplayed()) {
118 myVisualizedObjects.insert(*aIt);
119 anObject->setDisplayed(true);
122 if (!aFeature->isDisplayed()) {
123 myVisualizedObjects.insert(aFeature);
124 aFeature->setDisplayed(true);
126 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
129 void ModuleBase_OperationFeature::stopOperation()
131 FeaturePtr aFeature = feature();
132 if (!aFeature.get() || !isEditOperation())
135 // store hidden result features
136 std::list<ResultPtr> aResults = aFeature->results();
137 std::list<ResultPtr>::const_iterator aIt;
138 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
139 ObjectPtr anObject = *aIt;
140 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
141 anObject->setDisplayed(false);
144 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
145 aFeature->setDisplayed(false);
147 if (myVisualizedObjects.size() > 0)
148 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
151 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
153 if (myParentFeature.get()) {
154 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
156 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
157 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
159 if (myFeature) { // TODO: generate an error if feature was not created
161 // Model update should call "execute" of a feature.
162 //myFeature->execute();
163 // Init default values
164 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
165 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
166 for (; anIt != aLast; anIt++) {
167 (*anIt)->storeValue(aFeature);
172 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
176 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
178 myFeature = theFeature;
182 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
184 FeaturePtr aFeature = feature();
186 if (aFeature == theObj)
188 std::list<ResultPtr> aResults = aFeature->results();
189 std::list<ResultPtr>::const_iterator aIt;
190 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
191 if (theObj == (*aIt))
198 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
200 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
203 bool ModuleBase_OperationFeature::start()
205 setIsModified(false);
206 QString anId = getDescription()->operationId();
208 anId = anId.append(EditSuffix());
210 ModelAPI_Session::get()->startOperation(anId.toStdString());
212 emit beforeStarted();
216 FeaturePtr aFeature = createFeature();
217 // if the feature is not created, there is no sense to start the operation
218 if (aFeature.get() == NULL) {
219 // it is necessary to abor the operation in the session and emit the aborted signal
220 // in order to update commands status in the workshop, to be exact the feature action
226 //Already called startOperation();
231 void ModuleBase_OperationFeature::abort()
233 emit beforeAborted();
235 // the viewer update should be blocked in order to avoid the features blinking before they are
237 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
238 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
239 Events_Loop::loop()->send(aMsg);
241 // the widgets of property panel should not process any events come from data mode
242 // after abort clicked. Some signal such as redisplay/create influence on content
243 // of the object browser and viewer context. Therefore it influence to the current
244 // selection and if the active widget listens it, the attribute value is errnoneous
246 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
248 aPropertyPanel->cleanContent();
251 myFeature->setStable(true);
256 SessionPtr aMgr = ModelAPI_Session::get();
257 aMgr->abortOperation();
259 // the viewer update should be unblocked in order to avoid the features blinking before they are
261 aMsg = std::shared_ptr<Events_Message>(
262 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
264 Events_Loop::loop()->send(aMsg);
269 bool ModuleBase_OperationFeature::commit()
271 if (canBeCommitted()) {
272 emit beforeCommitted();
273 // the widgets of property panel should not process any events come from data mode
274 // after commit clicked. Some signal such as redisplay/create influence on content
275 // of the object browser and viewer context. Therefore it influence to the current
276 // selection and if the active widget listens it, the attribute value is errnoneous
278 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
280 aPropertyPanel->cleanContent();
282 myFeature->setStable(true);
284 SessionPtr aMgr = ModelAPI_Session::get();
285 /// Set current feature and remeber old current feature
288 aMgr->finishOperation();
294 afterCommitOperation();
300 void ModuleBase_OperationFeature::activateByPreselection()
302 if (myPreSelection.empty())
305 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
307 ModuleBase_ModelWidget* aFilledWgt = 0;
308 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
309 if (aPropertyPanel) {
310 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
311 if (!aWidgets.empty()) {
312 ModuleBase_ModelWidget* aWgt = 0;
313 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
315 // 1. apply the selection to controls
316 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
318 if (!aWgt->canSetValue())
320 aPropertyPanel->setPreselectionWidget(aWgt);
321 if (!aWgt->setSelection(myPreSelection, true)) {
329 aPropertyPanel->setPreselectionWidget(NULL);
330 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
331 // it is better to perform it not in setSelection of each widget, but do it here,
332 // after the preselection is processed
333 ModuleBase_ModelWidget::updateObject(myFeature);
335 // 3. a signal should be emitted before the next widget activation
336 // because, the activation of the next widget will give a focus to the widget. As a result
337 // the value of the widget is initialized. And commit may happens until the value is entered.
339 emit activatedByPreselection();
341 // 4. activate the next obligatory widget
342 aPropertyPanel->activateNextWidget(aFilledWgt);
348 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
350 myParentFeature = theParent;
353 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
355 return myParentFeature;
358 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
360 myPreviousCurrentFeature = theFeature;
363 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
365 return myPreviousCurrentFeature;
368 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
369 ModuleBase_IViewer* theViewer)
373 QList<ModuleBase_ViewerPrs> aPreSelected;
374 // Check that the selected result are not results of operation feature
375 FeaturePtr aFeature = feature();
377 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
379 std::list<ResultPtr> aResults = aFeature->results();
380 QObjectPtrList aResList;
381 std::list<ResultPtr>::const_iterator aIt;
382 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
383 aResList.append(*aIt);
385 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
386 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
387 aPreSelected.append(aPrs);
390 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
392 myPreSelection = aPreSelected;
395 void ModuleBase_OperationFeature::clearPreselection()
397 myPreSelection.clear();
400 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
402 ModuleBase_Operation::setPropertyPanel(theProp);
404 theProp->setEditingMode(isEditOperation());
407 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
408 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
409 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
410 ModuleBase_ModelWidget* aWgt = (*aWIt);
411 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
415 // Do not activate widgets by default if the current operation is editing operation
416 // Because we don't know which widget is going to be edited.
417 if (!isEditOperation()) {
418 // 4. activate the first obligatory widget
419 theProp->activateNextWidget(NULL);
422 // set focus on Ok button in order to operation manager could process Enter press
424 theProp->setFocusOnOkButton();