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 SessionPtr aMgr = ModelAPI_Session::get();
60 ModelAPI_Session::get()->finishOperation();
62 QString anId = getDescription()->operationId();
64 anId = anId.append(EditSuffix());
66 ModelAPI_Session::get()->startOperation(anId.toStdString());
70 propertyPanel()->setEditingMode(isEditOperation());
73 FeaturePtr ModuleBase_OperationFeature::feature() const
78 bool ModuleBase_OperationFeature::isValid() const
80 if (!myFeature || !myFeature->data()->isValid())
81 return true; // rename operation
82 if (myFeature->isAction())
84 //Get validators for the Id
85 SessionPtr aMgr = ModelAPI_Session::get();
86 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
87 bool aValid = aFactory->validate(myFeature);
89 // the feature exec state should be checked in order to do not apply features, which result can not
90 // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
91 bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
92 || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
94 return aValid && isDone;
97 void ModuleBase_OperationFeature::startOperation()
99 FeaturePtr aFeature = feature();
100 if (!aFeature.get() || !isEditOperation())
103 if (aFeature.get() && isEditOperation())
104 aFeature->setStable(false);
106 myVisualizedObjects.clear();
107 // store hidden result features
108 std::list<ResultPtr> aResults = aFeature->results();
109 std::list<ResultPtr>::const_iterator aIt;
110 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
111 ObjectPtr anObject = *aIt;
112 if (anObject.get() && !anObject->isDisplayed()) {
113 myVisualizedObjects.insert(*aIt);
114 anObject->setDisplayed(true);
117 if (!aFeature->isDisplayed()) {
118 myVisualizedObjects.insert(aFeature);
119 aFeature->setDisplayed(true);
121 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
124 void ModuleBase_OperationFeature::stopOperation()
126 FeaturePtr aFeature = feature();
127 if (!aFeature.get() || !isEditOperation())
130 // store hidden result features
131 std::list<ResultPtr> aResults = aFeature->results();
132 std::list<ResultPtr>::const_iterator aIt;
133 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
134 ObjectPtr anObject = *aIt;
135 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
136 anObject->setDisplayed(false);
139 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
140 aFeature->setDisplayed(false);
142 if (myVisualizedObjects.size() > 0)
143 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
146 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
148 if (myParentFeature.get()) {
149 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
151 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
152 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
154 if (myFeature) { // TODO: generate an error if feature was not created
156 // Model update should call "execute" of a feature.
157 //myFeature->execute();
158 // Init default values
159 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
160 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
161 for (; anIt != aLast; anIt++) {
162 (*anIt)->storeValue(aFeature);
167 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
171 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
173 myFeature = theFeature;
177 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
179 FeaturePtr aFeature = feature();
181 if (aFeature == theObj)
183 std::list<ResultPtr> aResults = aFeature->results();
184 std::list<ResultPtr>::const_iterator aIt;
185 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
186 if (theObj == (*aIt))
193 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
195 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
198 void ModuleBase_OperationFeature::start()
200 setIsModified(false);
201 QString anId = getDescription()->operationId();
203 anId = anId.append(EditSuffix());
205 ModelAPI_Session::get()->startOperation(anId.toStdString());
207 emit beforeStarted();
211 FeaturePtr aFeature = createFeature();
212 // if the feature is not created, there is no sense to start the operation
213 if (aFeature.get() == NULL) {
214 // it is necessary to abor the operation in the session and emit the aborted signal
215 // in order to update commands status in the workshop, to be exact the feature action
221 //Already called startOperation();
226 void ModuleBase_OperationFeature::abort()
228 emit beforeAborted();
230 // the viewer update should be blocked in order to avoid the features blinking before they are
232 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
233 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
234 Events_Loop::loop()->send(aMsg);
236 // the widgets of property panel should not process any events come from data mode
237 // after abort clicked. Some signal such as redisplay/create influence on content
238 // of the object browser and viewer context. Therefore it influence to the current
239 // selection and if the active widget listens it, the attribute value is errnoneous
241 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
243 aPropertyPanel->cleanContent();
245 myFeature->setStable(true);
250 SessionPtr aMgr = ModelAPI_Session::get();
251 aMgr->abortOperation();
253 // the viewer update should be unblocked in order to avoid the features blinking before they are
255 aMsg = std::shared_ptr<Events_Message>(
256 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
258 Events_Loop::loop()->send(aMsg);
263 bool ModuleBase_OperationFeature::commit()
265 if (canBeCommitted()) {
266 emit beforeCommitted();
267 // the widgets of property panel should not process any events come from data mode
268 // after commit clicked. Some signal such as redisplay/create influence on content
269 // of the object browser and viewer context. Therefore it influence to the current
270 // selection and if the active widget listens it, the attribute value is errnoneous
272 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
274 aPropertyPanel->cleanContent();
276 myFeature->setStable(true);
278 SessionPtr aMgr = ModelAPI_Session::get();
279 /// Set current feature and remeber old current feature
282 aMgr->finishOperation();
288 afterCommitOperation();
294 void ModuleBase_OperationFeature::activateByPreselection()
296 if (myPreSelection.empty())
299 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
301 ModuleBase_ModelWidget* aFilledWgt = 0;
302 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
303 if (aPropertyPanel) {
304 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
305 if (!aWidgets.empty()) {
306 ModuleBase_ModelWidget* aWgt = 0;
307 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
309 // 1. apply the selection to controls
310 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
312 if (!aWgt->canSetValue())
314 aPropertyPanel->setPreselectionWidget(aWgt);
315 if (!aWgt->setSelection(myPreSelection, true)) {
323 aPropertyPanel->setPreselectionWidget(NULL);
324 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
325 // it is better to perform it not in setSelection of each widget, but do it here,
326 // after the preselection is processed
327 ModuleBase_ModelWidget::updateObject(myFeature);
329 // 3. a signal should be emitted before the next widget activation
330 // because, the activation of the next widget will give a focus to the widget. As a result
331 // the value of the widget is initialized. And commit may happens until the value is entered.
333 emit activatedByPreselection();
335 // 4. activate the next obligatory widget
336 aPropertyPanel->activateNextWidget(aFilledWgt);
342 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
344 myParentFeature = theParent;
347 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
349 return myParentFeature;
352 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
354 myPreviousCurrentFeature = theFeature;
357 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
359 return myPreviousCurrentFeature;
362 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
363 ModuleBase_IViewer* theViewer)
367 QList<ModuleBase_ViewerPrs> aPreSelected;
368 // Check that the selected result are not results of operation feature
369 FeaturePtr aFeature = feature();
371 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
373 std::list<ResultPtr> aResults = aFeature->results();
374 QObjectPtrList aResList;
375 std::list<ResultPtr>::const_iterator aIt;
376 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
377 aResList.append(*aIt);
379 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
380 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
381 aPreSelected.append(aPrs);
384 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
386 myPreSelection = aPreSelected;
389 void ModuleBase_OperationFeature::clearPreselection()
391 myPreSelection.clear();
394 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
396 ModuleBase_Operation::setPropertyPanel(theProp);
398 theProp->setEditingMode(isEditOperation());
401 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
402 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
403 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
404 ModuleBase_ModelWidget* aWgt = (*aWIt);
405 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
409 // Do not activate widgets by default if the current operation is editing operation
410 // Because we don't know which widget is going to be edited.
411 if (!isEditOperation()) {
412 // 4. activate the first obligatory widget
413 theProp->activateNextWidget(NULL);
416 // set focus on Ok button in order to operation manager could process Enter press
418 theProp->setFocusOnOkButton();