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);
176 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
180 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
182 myFeature = theFeature;
186 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
188 FeaturePtr aFeature = feature();
190 if (aFeature == theObj)
192 std::list<ResultPtr> aResults = aFeature->results();
193 std::list<ResultPtr>::const_iterator aIt;
194 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
195 if (theObj == (*aIt))
198 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
199 if (aFeature->isMacro()) {
200 // macro feature may refers to sub-features, which also should be deactivated when the operation
201 // is active, e.g. rectangle'lines.
202 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
203 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
204 ModelAPI_AttributeRefList::typeId());
205 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
206 bool aFoundObject = false;
207 for (; anIt != aLast && !aFoundObject; anIt++) {
208 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
209 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
210 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
211 ObjectPtr anObject = aCurSelList->object(i);
212 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
213 if (aFeature.get()) {
214 aFoundObject = anObjectFeature == aFeature;
225 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
227 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
230 bool ModuleBase_OperationFeature::start()
232 setIsModified(false);
233 QString anId = getDescription()->operationId();
235 anId = anId.append(EditSuffix());
237 ModelAPI_Session::get()->startOperation(anId.toStdString());
239 emit beforeStarted();
243 FeaturePtr aFeature = createFeature();
244 // if the feature is not created, there is no sense to start the operation
245 if (aFeature.get() == NULL) {
246 // it is necessary to abor the operation in the session and emit the aborted signal
247 // in order to update commands status in the workshop, to be exact the feature action
253 //Already called startOperation();
258 void ModuleBase_OperationFeature::abort()
260 emit beforeAborted();
262 // the viewer update should be blocked in order to avoid the features blinking before they are
264 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
265 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
266 Events_Loop::loop()->send(aMsg);
268 // the widgets of property panel should not process any events come from data mode
269 // after abort clicked. Some signal such as redisplay/create influence on content
270 // of the object browser and viewer context. Therefore it influence to the current
271 // selection and if the active widget listens it, the attribute value is errnoneous
273 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
275 aPropertyPanel->cleanContent();
278 myFeature->setStable(true);
283 SessionPtr aMgr = ModelAPI_Session::get();
284 aMgr->abortOperation();
286 // the viewer update should be unblocked in order to avoid the features blinking before they are
288 aMsg = std::shared_ptr<Events_Message>(
289 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
291 Events_Loop::loop()->send(aMsg);
296 bool ModuleBase_OperationFeature::commit()
298 ModuleBase_IPropertyPanel* aPanel = propertyPanel();
300 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
301 if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
302 anActiveWidget->storeValue();
305 if (canBeCommitted()) {
306 emit beforeCommitted();
307 // the widgets of property panel should not process any events come from data mode
308 // after commit clicked. Some signal such as redisplay/create influence on content
309 // of the object browser and viewer context. Therefore it influence to the current
310 // selection and if the active widget listens it, the attribute value is errnoneous
312 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
314 aPropertyPanel->cleanContent();
316 myFeature->setStable(true);
318 SessionPtr aMgr = ModelAPI_Session::get();
319 /// Set current feature and remeber old current feature
322 aMgr->finishOperation();
328 afterCommitOperation();
334 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
335 const std::string& theGreedAttributeId)
337 ModuleBase_ModelWidget* aWidget = 0;
338 if (myPreSelection.empty())
340 // equal vertices should not be used here
341 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
343 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
344 ModuleBase_ModelWidget* aFilledWgt = 0;
345 if (aPropertyPanel) {
346 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
347 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
348 ModuleBase_ModelWidget* aWgt = 0;
349 if (!aWidgets.empty()) {
350 if (!theGreedAttributeId.empty()) {
351 // set preselection to greed widget
352 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
354 if (aWgt->attributeID() == theGreedAttributeId) {
355 aPropertyPanel->setPreselectionWidget(aWgt);
356 aWgt->setSelection(myPreSelection, true);
357 aPropertyPanel->setPreselectionWidget(NULL);
365 // 1. apply the selection to controls
366 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
368 if (!aWgt->canSetValue())
370 aPropertyPanel->setPreselectionWidget(aWgt);
371 if (!aWgt->setSelection(myPreSelection, true)) {
380 aPropertyPanel->setPreselectionWidget(NULL);
381 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
382 // it is better to perform it not in setSelection of each widget, but do it here,
383 // after the preselection is processed
384 ModuleBase_ModelWidget::updateObject(myFeature);
392 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
394 myParentFeature = theParent;
397 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
399 return myParentFeature;
402 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
404 myPreviousCurrentFeature = theFeature;
407 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
409 return myPreviousCurrentFeature;
412 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
413 ModuleBase_IViewer* theViewer)
417 QList<ModuleBase_ViewerPrs> aPreSelected;
418 // Check that the selected result are not results of operation feature
419 FeaturePtr aFeature = feature();
421 QList<ModuleBase_ViewerPrs> 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_ViewerPrs aPrs, aSelected) {
430 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
431 aPreSelected.append(aPrs);
434 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
436 myPreSelection = aPreSelected;
439 void ModuleBase_OperationFeature::clearPreselection()
441 myPreSelection.clear();
444 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
446 ModuleBase_Operation::setPropertyPanel(theProp);
448 theProp->setEditingMode(isEditOperation());
451 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
452 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
453 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
454 ModuleBase_ModelWidget* aWgt = (*aWIt);
455 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
459 // Do not activate widgets by default if the current operation is editing operation
460 // Because we don't know which widget is going to be edited.
461 if (!isEditOperation()) {
462 // 4. activate the first obligatory widget
463 theProp->activateNextWidget(NULL);
466 // set focus on Ok button in order to operation manager could process Enter press
468 theProp->setFocusOnOkButton();