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>
12 #include <ModelAPI_ResultConstruction.h>
14 #include <GeomValidators_Tools.h>
16 #include <TopoDS_Iterator.hxx>
18 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
19 ModuleBase_IWorkshop* theWorkshop,
20 const Config_WidgetAPI* theData,
21 const std::string& theParentId)
22 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId)
26 //********************************************************************
27 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
31 //********************************************************************
32 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
34 GeomShapePtr& theShape)
36 ModuleBase_ISelection* aSelection = myWorkshop->selection();
37 theObject = aSelection->getResult(thePrs);
38 theShape = aSelection->getShape(thePrs);
41 //********************************************************************
42 void ModuleBase_WidgetSelector::onSelectionChanged()
46 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
48 bool isDone = setSelection(aSelected, true/*false*/);
49 // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
50 // the same place repeatedly without mouse moved. In the case validation by filters is not
51 // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
52 // the 3rd click in the same point allow using this point.
54 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
55 // calls validators for the feature and, as a result, updates the Apply button state.
56 updateObject(myFeature);
62 //********************************************************************
63 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
64 const ResultPtr& theResult) const
68 GeomShapePtr aShape = theShape;
70 QIntList aShapeTypes = getShapeTypes();
71 if (aShapeTypes.empty()) {
75 // when the SHAPE type is provided by XML, the hole result shape can be selected.
76 if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
81 if (!aShape.get() && theResult.get()) {
83 aShape = theResult->shape();
85 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
87 // Check that the selection corresponds to selection type
88 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
89 aShapeType = aTopoShape.ShapeType();
90 // for compounds check sub-shapes: it may be compound of needed type:
91 // Booleans may produce compounds of Solids
92 if (aShapeType == TopAbs_COMPOUND) {
93 aShapeType = GeomValidators_Tools::getCompoundSubType(aTopoShape);
97 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
98 for (; anIt != aLast; anIt++) {
99 if (aShapeType == *anIt)
101 else if (*anIt == TopAbs_FACE) {
102 // try to process the construction shape only if there is no a selected shape in the viewer
103 if (!theShape.get() && theResult.get()) {
104 ResultConstructionPtr aCResult =
105 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
106 aValid = aCResult.get() && aCResult->facesNum() > 0;
113 //********************************************************************
114 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
116 updateSelectionName();
119 myWorkshop->activateSubShapesSelection(getShapeTypes());
121 myWorkshop->deactivateSubShapesSelection();
125 //********************************************************************
126 void ModuleBase_WidgetSelector::activateCustom()
128 connect(myWorkshop, SIGNAL(selectionChanged()), this,
129 SLOT(onSelectionChanged()), Qt::UniqueConnection);
131 activateSelection(true);
133 // Restore selection in the viewer by the attribute selection list
134 myWorkshop->setSelected(getAttributeSelection());
136 activateFilters(true);
139 //********************************************************************
140 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
142 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
143 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
144 bool aValid = acceptSubShape(aShape, aResult);
147 // In order to avoid selection of the same object
148 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
149 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
150 aValid = aSelectedFeature != myFeature;
155 //********************************************************************
156 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
160 getGeomSelection(thePrs, anObject, aShape);
162 setObject(anObject, aShape);
166 //********************************************************************
167 void ModuleBase_WidgetSelector::deactivate()
169 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
170 activateSelection(false);
171 activateFilters(false);