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"
18 #include "ModuleBase_Tools.h"
20 #include <ModelAPI_AttributeDouble.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Result.h>
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
32 #include <GeomAPI_Pnt2d.h>
34 #include <Events_Loop.h>
38 // the define to check the activated object as a sub-feature by argument of
39 // the operation feature. E.g. rectangle feature(operation), line(in argument) to be not activated
40 #define DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
41 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
42 #include <ModelAPI_AttributeRefList.h>
49 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
50 : ModuleBase_Operation(theId, theParent),
55 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
60 void ModuleBase_OperationFeature::setEditOperation(const bool& isEditState
61 /*const bool theRestartTransaction*/)
63 bool isCurrentEditState = isEditOperation();
64 if (isCurrentEditState == isEditState)
68 // this case is obsolete as it was not approved for reentrant sketch operation
69 // it was implemented when isEditState did not exist and only edit operation can be set
70 if (theRestartTransaction) {
71 // finsh previous create operation
72 emit beforeCommitted();
73 SessionPtr aMgr = ModelAPI_Session::get();
74 ModelAPI_Session::get()->finishOperation();
76 // start new edit operation
78 QString anId = getDescription()->operationId();
80 anId = anId.append(EditSuffix());
82 ModelAPI_Session::get()->startOperation(anId.toStdString());
85 myIsEditing = isEditState;
87 propertyPanel()->setEditingMode(isEditOperation());
90 FeaturePtr ModuleBase_OperationFeature::feature() const
95 bool ModuleBase_OperationFeature::isValid() const
97 if (!myFeature || !myFeature->data()->isValid())
98 return true; // rename operation
99 if (myFeature->isAction())
102 std::string anError = ModelAPI_Tools::getFeatureError(myFeature);
103 return anError.empty();
106 void ModuleBase_OperationFeature::startOperation()
108 FeaturePtr aFeature = feature();
109 if (!aFeature.get() || !isEditOperation())
112 if (aFeature.get() && isEditOperation())
113 aFeature->setStable(false);
115 myVisualizedObjects.clear();
116 // store hidden result features
117 std::list<ResultPtr> aResults = aFeature->results();
118 std::list<ResultPtr>::const_iterator aIt;
119 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
120 ObjectPtr anObject = *aIt;
121 if (anObject.get() && !anObject->isDisplayed()) {
122 myVisualizedObjects.insert(*aIt);
123 anObject->setDisplayed(true);
126 if (!aFeature->isDisplayed()) {
127 myVisualizedObjects.insert(aFeature);
128 aFeature->setDisplayed(true);
130 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
133 void ModuleBase_OperationFeature::stopOperation()
135 FeaturePtr aFeature = feature();
136 if (!aFeature.get() || !isEditOperation())
139 // store hidden result features
140 std::list<ResultPtr> aResults = aFeature->results();
141 std::list<ResultPtr>::const_iterator aIt;
142 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
143 ObjectPtr anObject = *aIt;
144 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
145 anObject->setDisplayed(false);
148 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
149 aFeature->setDisplayed(false);
151 if (myVisualizedObjects.size() > 0)
152 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
155 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
157 if (myParentFeature.get()) {
158 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
160 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
161 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
163 if (myFeature) { // TODO: generate an error if feature was not created
165 // Model update should call "execute" of a feature.
166 //myFeature->execute();
167 // Init default values
168 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
169 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
170 for (; anIt != aLast; anIt++) {
171 (*anIt)->storeValue(aFeature);
175 if (theFlushMessage) {
176 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
177 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
182 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
184 myFeature = theFeature;
188 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
190 FeaturePtr aFeature = feature();
192 if (aFeature == theObj)
194 std::list<ResultPtr> aResults = aFeature->results();
195 std::list<ResultPtr>::const_iterator aIt;
196 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
197 if (theObj == (*aIt))
200 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
201 if (aFeature->isMacro()) {
202 // macro feature may refers to sub-features, which also should be deactivated when the operation
203 // is active, e.g. rectangle'lines.
204 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
205 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
206 ModelAPI_AttributeRefList::typeId());
207 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
208 bool aFoundObject = false;
209 for (; anIt != aLast && !aFoundObject; anIt++) {
210 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
211 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
212 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
213 ObjectPtr anObject = aCurSelList->object(i);
214 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
215 if (aFeature.get()) {
216 aFoundObject = anObjectFeature == aFeature;
227 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
229 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
232 bool ModuleBase_OperationFeature::start()
234 setIsModified(false);
235 QString anId = getDescription()->operationId();
237 anId = anId.append(EditSuffix());
239 ModelAPI_Session::get()->startOperation(anId.toStdString());
241 emit beforeStarted();
245 FeaturePtr aFeature = createFeature();
246 // if the feature is not created, there is no sense to start the operation
247 if (aFeature.get() == NULL) {
248 // it is necessary to abor the operation in the session and emit the aborted signal
249 // in order to update commands status in the workshop, to be exact the feature action
255 //Already called startOperation();
260 void ModuleBase_OperationFeature::abort()
262 emit beforeAborted();
264 // the viewer update should be blocked in order to avoid the features blinking before they are
266 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
267 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
268 Events_Loop::loop()->send(aMsg);
270 // the widgets of property panel should not process any events come from data mode
271 // after abort 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();
280 myFeature->setStable(true);
285 SessionPtr aMgr = ModelAPI_Session::get();
286 aMgr->abortOperation();
288 // the viewer update should be unblocked in order to avoid the features blinking before they are
290 aMsg = std::shared_ptr<Events_Message>(
291 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
293 Events_Loop::loop()->send(aMsg);
298 bool ModuleBase_OperationFeature::commit()
300 ModuleBase_IPropertyPanel* aPanel = propertyPanel();
302 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
303 if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
304 anActiveWidget->storeValue();
307 if (canBeCommitted()) {
308 emit beforeCommitted();
309 // the widgets of property panel should not process any events come from data mode
310 // after commit clicked. Some signal such as redisplay/create influence on content
311 // of the object browser and viewer context. Therefore it influence to the current
312 // selection and if the active widget listens it, the attribute value is errnoneous
314 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
316 aPropertyPanel->cleanContent();
318 myFeature->setStable(true);
320 SessionPtr aMgr = ModelAPI_Session::get();
321 /// Set current feature and remeber old current feature
324 aMgr->finishOperation();
330 afterCommitOperation();
336 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
337 const std::string& theGreedAttributeId)
339 ModuleBase_ModelWidget* aWidget = 0;
340 if (myPreSelection.empty())
342 // equal vertices should not be used here
343 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
345 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
346 ModuleBase_ModelWidget* aFilledWgt = 0;
347 if (aPropertyPanel) {
348 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
349 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
350 ModuleBase_ModelWidget* aWgt = 0;
351 if (!aWidgets.empty()) {
352 if (!theGreedAttributeId.empty()) {
353 // set preselection to greed widget
354 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
356 if (aWgt->attributeID() == theGreedAttributeId) {
357 aPropertyPanel->setPreselectionWidget(aWgt);
358 aWgt->setSelection(myPreSelection, true);
359 aPropertyPanel->setPreselectionWidget(NULL);
367 // 1. apply the selection to controls
368 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
370 if (!aWgt->canAcceptFocus())
372 aPropertyPanel->setPreselectionWidget(aWgt);
373 if (!aWgt->setSelection(myPreSelection, true)) {
382 aPropertyPanel->setPreselectionWidget(NULL);
383 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
384 // it is better to perform it not in setSelection of each widget, but do it here,
385 // after the preselection is processed
386 ModuleBase_Tools::flushUpdated(myFeature);
394 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
396 myParentFeature = theParent;
399 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
401 return myParentFeature;
404 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
406 myPreviousCurrentFeature = theFeature;
409 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
411 return myPreviousCurrentFeature;
414 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
415 ModuleBase_IViewer* theViewer)
417 QList<ModuleBase_ViewerPrsPtr> aPreSelected;
418 // Check that the selected result are not results of operation feature
419 FeaturePtr aFeature = feature();
421 QList<ModuleBase_ViewerPrsPtr> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
423 std::list<ResultPtr> aResults = aFeature->results();
424 QObjectPtrList aResList;
425 std::list<ResultPtr>::const_iterator aIt;
426 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
427 aResList.append(*aIt);
429 foreach (ModuleBase_ViewerPrsPtr aPrs, aSelected) {
430 if ((!aResList.contains(aPrs->object())) && (aPrs->object() != aFeature))
431 aPreSelected.append(aPrs);
434 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
436 setPreselection(aPreSelected);
439 void ModuleBase_OperationFeature::setPreselection(const QList<ModuleBase_ViewerPrsPtr>& theValues)
442 myPreSelection = theValues;
445 void ModuleBase_OperationFeature::clearPreselection()
447 myPreSelection.clear();
450 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
452 ModuleBase_Operation::setPropertyPanel(theProp);
454 theProp->setEditingMode(isEditOperation());
457 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
458 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
459 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
460 ModuleBase_ModelWidget* aWgt = (*aWIt);
461 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
465 // Do not activate widgets by default if the current operation is editing operation
466 // Because we don't know which widget is going to be edited.
467 if (!isEditOperation()) {
468 // 4. activate the first obligatory widget
469 theProp->activateNextWidget(NULL);
472 // set focus on Ok button in order to operation manager could process Enter press
474 theProp->setFocusOnOkButton();