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>
13 #include <ModelAPI_ResultConstruction.h>
15 #include <TopoDS_Iterator.hxx>
17 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
18 ModuleBase_IWorkshop* theWorkshop,
19 const Config_WidgetAPI* theData,
20 const std::string& theParentId)
21 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId)
25 //********************************************************************
26 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
30 //********************************************************************
31 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
33 GeomShapePtr& theShape)
35 ModuleBase_ISelection* aSelection = myWorkshop->selection();
36 theObject = aSelection->getResult(thePrs);
37 theShape = aSelection->getShape(thePrs);
40 //********************************************************************
41 void ModuleBase_WidgetSelector::onSelectionChanged()
45 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
47 bool isDone = setSelection(aSelected, true/*false*/);
48 // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
49 // the same place repeatedly without mouse moved. In the case validation by filters is not
50 // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
51 // the 3rd click in the same point allow using this point.
53 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
54 // calls validators for the feature and, as a result, updates the Apply button state.
55 updateObject(myFeature);
61 //********************************************************************
62 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
63 const ResultPtr& theResult) const
67 GeomShapePtr aShape = theShape;
69 QIntList aShapeTypes = getShapeTypes();
70 if (aShapeTypes.empty()) {
74 // when the SHAPE type is provided by XML, the hole result shape can be selected.
75 if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
80 if (!aShape.get() && theResult.get()) {
82 aShape = theResult->shape();
84 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
86 // Check that the selection corresponds to selection type
87 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
88 aShapeType = aTopoShape.ShapeType();
89 // for compounds check sub-shapes: it may be compound of needed type:
90 // Booleans may produce compounds of Solids
91 if (aShapeType == TopAbs_COMPOUND) {
92 aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
96 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
97 for (; anIt != aLast; anIt++) {
98 if (aShapeType == *anIt)
100 else if (*anIt == TopAbs_FACE) {
101 // try to process the construction shape only if there is no a selected shape in the viewer
102 if (!theShape.get() && theResult.get()) {
103 ResultConstructionPtr aCResult =
104 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
105 aValid = aCResult.get() && aCResult->facesNum() > 0;
112 //********************************************************************
113 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
115 updateSelectionName();
118 myWorkshop->activateSubShapesSelection(getShapeTypes());
120 myWorkshop->deactivateSubShapesSelection();
124 //********************************************************************
125 void ModuleBase_WidgetSelector::activateCustom()
127 connect(myWorkshop, SIGNAL(selectionChanged()), this,
128 SLOT(onSelectionChanged()), Qt::UniqueConnection);
130 activateSelection(true);
132 // Restore selection in the viewer by the attribute selection list
133 myWorkshop->setSelected(getAttributeSelection());
135 activateFilters(true);
138 //********************************************************************
139 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
141 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
142 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
143 bool aValid = acceptSubShape(aShape, aResult);
146 // In order to avoid selection of the same object
147 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
148 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
149 aValid = aSelectedFeature != myFeature;
154 //********************************************************************
155 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
159 getGeomSelection(thePrs, anObject, aShape);
161 setObject(anObject, aShape);
165 //********************************************************************
166 void ModuleBase_WidgetSelector::deactivate()
168 ModuleBase_ModelWidget::deactivate();
169 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
170 activateSelection(false);
171 activateFilters(false);