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 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
125 if (myParentFeature.get()) {
126 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
128 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
129 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
131 if (myFeature) { // TODO: generate an error if feature was not created
133 // Model update should call "execute" of a feature.
134 //myFeature->execute();
135 // Init default values
136 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
137 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
138 for (; anIt != aLast; anIt++) {
139 (*anIt)->storeValue(aFeature);
144 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
148 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
150 myFeature = theFeature;
151 myFeature->setStable(false);
155 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
157 FeaturePtr aFeature = feature();
159 if (aFeature == theObj)
161 std::list<ResultPtr> aResults = aFeature->results();
162 std::list<ResultPtr>::const_iterator aIt;
163 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
164 if (theObj == (*aIt))
171 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
173 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
176 void ModuleBase_OperationFeature::start()
178 setIsModified(false);
179 QString anId = getDescription()->operationId();
181 anId = anId.append(EditSuffix());
183 ModelAPI_Session::get()->startOperation(anId.toStdString());
185 emit beforeStarted();
189 FeaturePtr aFeature = createFeature();
190 // if the feature is not created, there is no sense to start the operation
191 if (aFeature.get() == NULL) {
192 // it is necessary to abor the operation in the session and emit the aborted signal
193 // in order to update commands status in the workshop, to be exact the feature action
204 void ModuleBase_OperationFeature::abort()
206 emit beforeAborted();
208 // the viewer update should be blocked in order to avoid the features blinking before they are
210 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
211 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
212 Events_Loop::loop()->send(aMsg);
214 // the widgets of property panel should not process any events come from data mode
215 // after abort clicked. Some signal such as redisplay/create influence on content
216 // of the object browser and viewer context. Therefore it influence to the current
217 // selection and if the active widget listens it, the attribute value is errnoneous
219 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
221 aPropertyPanel->cleanContent();
226 SessionPtr aMgr = ModelAPI_Session::get();
227 aMgr->abortOperation();
229 // the viewer update should be unblocked in order to avoid the features blinking before they are
231 aMsg = std::shared_ptr<Events_Message>(
232 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
234 Events_Loop::loop()->send(aMsg);
239 bool ModuleBase_OperationFeature::commit()
241 if (canBeCommitted()) {
242 // the widgets of property panel should not process any events come from data mode
243 // after commit clicked. Some signal such as redisplay/create influence on content
244 // of the object browser and viewer context. Therefore it influence to the current
245 // selection and if the active widget listens it, the attribute value is errnoneous
247 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
249 aPropertyPanel->cleanContent();
251 SessionPtr aMgr = ModelAPI_Session::get();
252 /// Set current feature and remeber old current feature
254 emit beforeCommitted();
256 aMgr->finishOperation();
262 afterCommitOperation();
268 void ModuleBase_OperationFeature::activateByPreselection()
270 if (myPreSelection.empty())
273 ModuleBase_ModelWidget* aFilledWgt = 0;
274 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
275 if (aPropertyPanel) {
276 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
277 if (!aWidgets.empty()) {
278 ModuleBase_ModelWidget* aWgt = 0;
279 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
281 // 1. apply the selection to controls
282 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
284 if (!aWgt->canSetValue())
286 aPropertyPanel->setPreselectionWidget(aWgt);
287 if (!aWgt->setSelection(myPreSelection, true)) {
295 aPropertyPanel->setPreselectionWidget(NULL);
296 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
297 // it is better to perform it not in setSelection of each widget, but do it here,
298 // after the preselection is processed
299 ModuleBase_ModelWidget::updateObject(myFeature);
301 // 3. a signal should be emitted before the next widget activation
302 // because, the activation of the next widget will give a focus to the widget. As a result
303 // the value of the widget is initialized. And commit may happens until the value is entered.
305 emit activatedByPreselection();
307 // 4. activate the next obligatory widget
308 aPropertyPanel->activateNextWidget(aFilledWgt);
314 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
316 myParentFeature = theParent;
319 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
321 return myParentFeature;
324 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
326 myPreviousCurrentFeature = theFeature;
329 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
331 return myPreviousCurrentFeature;
334 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
335 ModuleBase_IViewer* theViewer)
339 QList<ModuleBase_ViewerPrs> aPreSelected;
340 // Check that the selected result are not results of operation feature
341 FeaturePtr aFeature = feature();
343 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
345 std::list<ResultPtr> aResults = aFeature->results();
346 QObjectPtrList aResList;
347 std::list<ResultPtr>::const_iterator aIt;
348 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
349 aResList.append(*aIt);
351 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
352 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
353 aPreSelected.append(aPrs);
356 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
358 myPreSelection = aPreSelected;
361 void ModuleBase_OperationFeature::clearPreselection()
363 myPreSelection.clear();
366 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
368 ModuleBase_Operation::setPropertyPanel(theProp);
370 theProp->setEditingMode(isEditOperation());
373 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
374 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
375 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
376 ModuleBase_ModelWidget* aWgt = (*aWIt);
377 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
381 // Do not activate widgets by default if the current operation is editing operation
382 // Because we don't know which widget is going to be edited.
383 if (!isEditOperation()) {
384 // 4. activate the first obligatory widget
385 theProp->activateNextWidget(NULL);
388 // set focus on Ok button in order to operation manager could process Enter press
390 theProp->setFocusOnOkButton();