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())
58 if (theRestartTransaction) {
59 FeaturePtr aPrevFeature = myPreviousCurrentFeature;
61 SessionPtr aMgr = ModelAPI_Session::get();
62 ModelAPI_Session::get()->finishOperation();
64 QString anId = getDescription()->operationId();
66 anId = anId.append(EditSuffix());
68 ModelAPI_Session::get()->startOperation(anId.toStdString());
71 myPreviousCurrentFeature = aPrevFeature;
74 propertyPanel()->setEditingMode(isEditOperation());
77 FeaturePtr ModuleBase_OperationFeature::feature() const
82 bool ModuleBase_OperationFeature::isValid() const
84 if (!myFeature || !myFeature->data()->isValid())
85 return true; // rename operation
86 if (myFeature->isAction())
88 //Get validators for the Id
89 SessionPtr aMgr = ModelAPI_Session::get();
90 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
91 bool aValid = aFactory->validate(myFeature);
93 // the feature exec state should be checked in order to do not apply features, which result can not
94 // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
95 bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
96 || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
98 return aValid && isDone;
101 void ModuleBase_OperationFeature::startOperation()
103 FeaturePtr aFeature = feature();
104 if (!aFeature.get() || !isEditOperation())
107 if (aFeature.get() && isEditOperation())
108 aFeature->setStable(false);
110 myVisualizedObjects.clear();
111 // store hidden result features
112 std::list<ResultPtr> aResults = aFeature->results();
113 std::list<ResultPtr>::const_iterator aIt;
114 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
115 ObjectPtr anObject = *aIt;
116 if (anObject.get() && !anObject->isDisplayed()) {
117 myVisualizedObjects.insert(*aIt);
118 anObject->setDisplayed(true);
121 if (!aFeature->isDisplayed()) {
122 myVisualizedObjects.insert(aFeature);
123 aFeature->setDisplayed(true);
125 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
128 void ModuleBase_OperationFeature::stopOperation()
130 FeaturePtr aFeature = feature();
131 if (!aFeature.get() || !isEditOperation())
134 // store hidden result features
135 std::list<ResultPtr> aResults = aFeature->results();
136 std::list<ResultPtr>::const_iterator aIt;
137 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
138 ObjectPtr anObject = *aIt;
139 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
140 anObject->setDisplayed(false);
143 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
144 aFeature->setDisplayed(false);
146 if (myVisualizedObjects.size() > 0)
147 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
150 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
152 if (myParentFeature.get()) {
153 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
155 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
156 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
158 if (myFeature) { // TODO: generate an error if feature was not created
160 // Model update should call "execute" of a feature.
161 //myFeature->execute();
162 // Init default values
163 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
164 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
165 for (; anIt != aLast; anIt++) {
166 (*anIt)->storeValue(aFeature);
171 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
175 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
177 myFeature = theFeature;
181 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
183 FeaturePtr aFeature = feature();
185 if (aFeature == theObj)
187 std::list<ResultPtr> aResults = aFeature->results();
188 std::list<ResultPtr>::const_iterator aIt;
189 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
190 if (theObj == (*aIt))
197 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
199 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
202 void ModuleBase_OperationFeature::start()
204 setIsModified(false);
205 QString anId = getDescription()->operationId();
207 anId = anId.append(EditSuffix());
209 ModelAPI_Session::get()->startOperation(anId.toStdString());
211 emit beforeStarted();
215 FeaturePtr aFeature = createFeature();
216 // if the feature is not created, there is no sense to start the operation
217 if (aFeature.get() == NULL) {
218 // it is necessary to abor the operation in the session and emit the aborted signal
219 // in order to update commands status in the workshop, to be exact the feature action
225 //Already called startOperation();
230 void ModuleBase_OperationFeature::abort()
232 emit beforeAborted();
234 // the viewer update should be blocked in order to avoid the features blinking before they are
236 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
237 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
238 Events_Loop::loop()->send(aMsg);
240 // the widgets of property panel should not process any events come from data mode
241 // after abort clicked. Some signal such as redisplay/create influence on content
242 // of the object browser and viewer context. Therefore it influence to the current
243 // selection and if the active widget listens it, the attribute value is errnoneous
245 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
247 aPropertyPanel->cleanContent();
249 myFeature->setStable(true);
254 SessionPtr aMgr = ModelAPI_Session::get();
255 aMgr->abortOperation();
257 // the viewer update should be unblocked in order to avoid the features blinking before they are
259 aMsg = std::shared_ptr<Events_Message>(
260 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
262 Events_Loop::loop()->send(aMsg);
267 bool ModuleBase_OperationFeature::commit()
269 if (canBeCommitted()) {
270 emit beforeCommitted();
271 // the widgets of property panel should not process any events come from data mode
272 // after commit clicked. Some signal such as redisplay/create influence on content
273 // of the object browser and viewer context. Therefore it influence to the current
274 // selection and if the active widget listens it, the attribute value is errnoneous
276 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
278 aPropertyPanel->cleanContent();
280 myFeature->setStable(true);
282 SessionPtr aMgr = ModelAPI_Session::get();
283 /// Set current feature and remeber old current feature
286 aMgr->finishOperation();
292 afterCommitOperation();
298 void ModuleBase_OperationFeature::activateByPreselection()
300 if (myPreSelection.empty())
303 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
305 ModuleBase_ModelWidget* aFilledWgt = 0;
306 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
307 if (aPropertyPanel) {
308 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
309 if (!aWidgets.empty()) {
310 ModuleBase_ModelWidget* aWgt = 0;
311 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
313 // 1. apply the selection to controls
314 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
316 if (!aWgt->canSetValue())
318 aPropertyPanel->setPreselectionWidget(aWgt);
319 if (!aWgt->setSelection(myPreSelection, true)) {
327 aPropertyPanel->setPreselectionWidget(NULL);
328 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
329 // it is better to perform it not in setSelection of each widget, but do it here,
330 // after the preselection is processed
331 ModuleBase_ModelWidget::updateObject(myFeature);
333 // 3. a signal should be emitted before the next widget activation
334 // because, the activation of the next widget will give a focus to the widget. As a result
335 // the value of the widget is initialized. And commit may happens until the value is entered.
337 emit activatedByPreselection();
339 // 4. activate the next obligatory widget
340 aPropertyPanel->activateNextWidget(aFilledWgt);
346 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
348 myParentFeature = theParent;
351 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
353 return myParentFeature;
356 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
358 myPreviousCurrentFeature = theFeature;
361 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
363 return myPreviousCurrentFeature;
366 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
367 ModuleBase_IViewer* theViewer)
371 QList<ModuleBase_ViewerPrs> aPreSelected;
372 // Check that the selected result are not results of operation feature
373 FeaturePtr aFeature = feature();
375 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
377 std::list<ResultPtr> aResults = aFeature->results();
378 QObjectPtrList aResList;
379 std::list<ResultPtr>::const_iterator aIt;
380 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
381 aResList.append(*aIt);
383 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
384 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
385 aPreSelected.append(aPrs);
388 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
390 myPreSelection = aPreSelected;
393 void ModuleBase_OperationFeature::clearPreselection()
395 myPreSelection.clear();
398 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
400 ModuleBase_Operation::setPropertyPanel(theProp);
402 theProp->setEditingMode(isEditOperation());
405 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
406 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
407 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
408 ModuleBase_ModelWidget* aWgt = (*aWIt);
409 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
413 // Do not activate widgets by default if the current operation is editing operation
414 // Because we don't know which widget is going to be edited.
415 if (!isEditOperation()) {
416 // 4. activate the first obligatory widget
417 theProp->activateNextWidget(NULL);
420 // set focus on Ok button in order to operation manager could process Enter press
422 theProp->setFocusOnOkButton();