1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetSelector.cpp
4 // Created: 19 June 2015
5 // Author: Natalia ERMOLAEVA
7 #include <ModuleBase_WidgetSelector.h>
9 #include <ModuleBase_ISelection.h>
10 #include <ModuleBase_IWorkshop.h>
11 #include <ModuleBase_Tools.h>
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_OperationDescription.h>
14 #include <ModuleBase_WidgetFactory.h>
15 #include <ModuleBase_IModule.h>
17 #include <ModelAPI_ResultConstruction.h>
19 #include <TopoDS_Iterator.hxx>
21 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
22 ModuleBase_IWorkshop* theWorkshop,
23 const Config_WidgetAPI* theData,
24 const std::string& theParentId)
25 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId)
29 //********************************************************************
30 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
34 //********************************************************************
35 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
37 GeomShapePtr& theShape)
39 ModuleBase_ISelection* aSelection = myWorkshop->selection();
40 theObject = aSelection->getResult(thePrs);
41 theShape = aSelection->getShape(thePrs);
44 //********************************************************************
45 void ModuleBase_WidgetSelector::onSelectionChanged()
49 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
51 bool isDone = setSelection(aSelected, true/*false*/);
52 // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
53 // the same place repeatedly without mouse moved. In the case validation by filters is not
54 // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
55 // the 3rd click in the same point allow using this point.
57 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
58 // calls validators for the feature and, as a result, updates the Apply button state.
59 updateObject(myFeature);
64 // we need to forget about previous validation result as the current selection can influence on it
68 //********************************************************************
69 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
70 const ResultPtr& theResult) const
74 GeomShapePtr aShape = theShape;
76 QIntList aShapeTypes = getShapeTypes();
77 if (aShapeTypes.empty()) {
81 // when the SHAPE type is provided by XML, the hole result shape can be selected.
82 if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
87 if (!aShape.get() && theResult.get()) {
89 aShape = theResult->shape();
91 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
93 // Check that the selection corresponds to selection type
94 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
95 aShapeType = aTopoShape.ShapeType();
96 // for compounds check sub-shapes: it may be compound of needed type:
97 // Booleans may produce compounds of Solids
98 if (aShapeType == TopAbs_COMPOUND) {
99 aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
103 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
104 for (; anIt != aLast; anIt++) {
105 if (aShapeType == *anIt)
107 else if (*anIt == TopAbs_FACE) {
108 // try to process the construction shape only if there is no a selected shape in the viewer
109 if (!theShape.get() && theResult.get()) {
110 ResultConstructionPtr aCResult =
111 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
112 aValid = aCResult.get() && aCResult->facesNum() > 0;
119 //********************************************************************
120 void ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
122 updateSelectionName();
125 myWorkshop->activateSubShapesSelection(getShapeTypes());
127 myWorkshop->deactivateSubShapesSelection();
129 activateFilters(toActivate);
132 //********************************************************************
133 void ModuleBase_WidgetSelector::activateCustom()
135 connect(myWorkshop, SIGNAL(selectionChanged()), this,
136 SLOT(onSelectionChanged()), Qt::UniqueConnection);
138 activateSelectionAndFilters(true);
140 // Restore selection in the viewer by the attribute selection list
141 myWorkshop->setSelected(getAttributeSelection());
144 //********************************************************************
145 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
147 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
148 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
149 bool aValid = acceptSubShape(aShape, aResult);
152 // In order to avoid selection of the same object
153 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
154 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
155 aValid = aSelectedFeature != myFeature;
160 //********************************************************************
161 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
165 getGeomSelection(thePrs, anObject, aShape);
167 setObject(anObject, aShape);
171 //********************************************************************
172 void ModuleBase_WidgetSelector::deactivate()
174 ModuleBase_ModelWidget::deactivate();
175 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
176 activateSelectionAndFilters(false);
177 ModuleBase_ModelWidget::deactivate();
180 //********************************************************************
181 std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
182 ModuleBase_IWorkshop* theWorkshop)
185 if (theAttribute.get() != NULL) {
186 ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
188 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
189 if (aFeature.get()) {
190 std::string aXmlCfg, aDescription;
191 theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
193 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
194 std::string anAttributeTitle;
195 aFactory.getAttributeTitle(aFeature->getKind(), theAttribute->id(), anAttributeTitle);
197 std::stringstream aStreamName;
198 aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
199 aName = aStreamName.str();