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 DocumentPtr aDoc = aMgr->activeDocument();
61 FeaturePtr aFeature = aDoc->currentFeature(false);
62 ModelAPI_Session::get()->finishOperation();
63 FeaturePtr anAFeature = aDoc->currentFeature(false);
65 QString anId = getDescription()->operationId();
67 anId = anId.append(EditSuffix());
69 ModelAPI_Session::get()->startOperation(anId.toStdString());
73 propertyPanel()->setEditingMode(isEditOperation());
76 FeaturePtr ModuleBase_OperationFeature::feature() const
81 bool ModuleBase_OperationFeature::isValid() const
83 if (!myFeature || !myFeature->data()->isValid())
84 return true; // rename operation
85 if (myFeature->isAction())
87 //Get validators for the Id
88 SessionPtr aMgr = ModelAPI_Session::get();
89 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
90 bool aValid = aFactory->validate(myFeature);
92 // the feature exec state should be checked in order to do not apply features, which result can not
93 // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
94 bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
95 || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
97 return aValid && isDone;
100 void ModuleBase_OperationFeature::startOperation()
102 FeaturePtr aFeature = feature();
103 if (!aFeature.get() || !isEditOperation())
106 if (aFeature.get() && isEditOperation())
107 aFeature->setStable(false);
109 myVisualizedObjects.clear();
110 // store hidden result features
111 std::list<ResultPtr> aResults = aFeature->results();
112 std::list<ResultPtr>::const_iterator aIt;
113 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
114 ObjectPtr anObject = *aIt;
115 if (anObject.get() && !anObject->isDisplayed()) {
116 myVisualizedObjects.insert(*aIt);
117 anObject->setDisplayed(true);
120 if (!aFeature->isDisplayed()) {
121 myVisualizedObjects.insert(aFeature);
122 aFeature->setDisplayed(true);
124 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
127 void ModuleBase_OperationFeature::stopOperation()
129 FeaturePtr aFeature = feature();
130 if (!aFeature.get() || !isEditOperation())
133 // store hidden result features
134 std::list<ResultPtr> aResults = aFeature->results();
135 std::list<ResultPtr>::const_iterator aIt;
136 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
137 ObjectPtr anObject = *aIt;
138 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
139 anObject->setDisplayed(false);
142 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
143 aFeature->setDisplayed(false);
145 if (myVisualizedObjects.size() > 0)
146 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
149 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
151 if (myParentFeature.get()) {
152 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
154 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
155 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
157 if (myFeature) { // TODO: generate an error if feature was not created
159 // Model update should call "execute" of a feature.
160 //myFeature->execute();
161 // Init default values
162 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
163 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
164 for (; anIt != aLast; anIt++) {
165 (*anIt)->storeValue(aFeature);
170 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
174 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
176 myFeature = theFeature;
180 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
182 FeaturePtr aFeature = feature();
184 if (aFeature == theObj)
186 std::list<ResultPtr> aResults = aFeature->results();
187 std::list<ResultPtr>::const_iterator aIt;
188 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
189 if (theObj == (*aIt))
196 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
198 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
201 void ModuleBase_OperationFeature::start()
203 setIsModified(false);
204 QString anId = getDescription()->operationId();
206 anId = anId.append(EditSuffix());
208 ModelAPI_Session::get()->startOperation(anId.toStdString());
210 emit beforeStarted();
214 FeaturePtr aFeature = createFeature();
215 // if the feature is not created, there is no sense to start the operation
216 if (aFeature.get() == NULL) {
217 // it is necessary to abor the operation in the session and emit the aborted signal
218 // in order to update commands status in the workshop, to be exact the feature action
224 //Already called startOperation();
229 void ModuleBase_OperationFeature::abort()
231 emit beforeAborted();
233 // the viewer update should be blocked in order to avoid the features blinking before they are
235 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
236 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
237 Events_Loop::loop()->send(aMsg);
239 // the widgets of property panel should not process any events come from data mode
240 // after abort clicked. Some signal such as redisplay/create influence on content
241 // of the object browser and viewer context. Therefore it influence to the current
242 // selection and if the active widget listens it, the attribute value is errnoneous
244 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
246 aPropertyPanel->cleanContent();
248 myFeature->setStable(true);
253 SessionPtr aMgr = ModelAPI_Session::get();
254 aMgr->abortOperation();
256 // the viewer update should be unblocked in order to avoid the features blinking before they are
258 aMsg = std::shared_ptr<Events_Message>(
259 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
261 Events_Loop::loop()->send(aMsg);
266 bool ModuleBase_OperationFeature::commit()
268 if (canBeCommitted()) {
269 emit beforeCommitted();
270 // the widgets of property panel should not process any events come from data mode
271 // after commit clicked. Some signal such as redisplay/create influence on content
272 // of the object browser and viewer context. Therefore it influence to the current
273 // selection and if the active widget listens it, the attribute value is errnoneous
275 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
277 aPropertyPanel->cleanContent();
279 myFeature->setStable(true);
281 SessionPtr aMgr = ModelAPI_Session::get();
282 /// Set current feature and remeber old current feature
285 aMgr->finishOperation();
291 afterCommitOperation();
297 void ModuleBase_OperationFeature::activateByPreselection()
299 if (myPreSelection.empty())
302 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
304 ModuleBase_ModelWidget* aFilledWgt = 0;
305 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
306 if (aPropertyPanel) {
307 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
308 if (!aWidgets.empty()) {
309 ModuleBase_ModelWidget* aWgt = 0;
310 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
312 // 1. apply the selection to controls
313 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
315 if (!aWgt->canSetValue())
317 aPropertyPanel->setPreselectionWidget(aWgt);
318 if (!aWgt->setSelection(myPreSelection, true)) {
326 aPropertyPanel->setPreselectionWidget(NULL);
327 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
328 // it is better to perform it not in setSelection of each widget, but do it here,
329 // after the preselection is processed
330 ModuleBase_ModelWidget::updateObject(myFeature);
332 // 3. a signal should be emitted before the next widget activation
333 // because, the activation of the next widget will give a focus to the widget. As a result
334 // the value of the widget is initialized. And commit may happens until the value is entered.
336 emit activatedByPreselection();
338 // 4. activate the next obligatory widget
339 aPropertyPanel->activateNextWidget(aFilledWgt);
345 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
347 myParentFeature = theParent;
350 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
352 return myParentFeature;
355 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
357 myPreviousCurrentFeature = theFeature;
360 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
362 return myPreviousCurrentFeature;
365 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
366 ModuleBase_IViewer* theViewer)
370 QList<ModuleBase_ViewerPrs> aPreSelected;
371 // Check that the selected result are not results of operation feature
372 FeaturePtr aFeature = feature();
374 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
376 std::list<ResultPtr> aResults = aFeature->results();
377 QObjectPtrList aResList;
378 std::list<ResultPtr>::const_iterator aIt;
379 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
380 aResList.append(*aIt);
382 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
383 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
384 aPreSelected.append(aPrs);
387 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
389 myPreSelection = aPreSelected;
392 void ModuleBase_OperationFeature::clearPreselection()
394 myPreSelection.clear();
397 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
399 ModuleBase_Operation::setPropertyPanel(theProp);
401 theProp->setEditingMode(isEditOperation());
404 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
405 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
406 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
407 ModuleBase_ModelWidget* aWgt = (*aWIt);
408 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
412 // Do not activate widgets by default if the current operation is editing operation
413 // Because we don't know which widget is going to be edited.
414 if (!isEditOperation()) {
415 // 4. activate the first obligatory widget
416 theProp->activateNextWidget(NULL);
419 // set focus on Ok button in order to operation manager could process Enter press
421 theProp->setFocusOnOkButton();