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::activateSelectionAndFilters(bool toActivate)
115 updateSelectionName();
118 myWorkshop->activateSubShapesSelection(getShapeTypes());
120 myWorkshop->deactivateSubShapesSelection();
122 activateFilters(toActivate);
125 //********************************************************************
126 void ModuleBase_WidgetSelector::activateCustom()
128 connect(myWorkshop, SIGNAL(selectionChanged()), this,
129 SLOT(onSelectionChanged()), Qt::UniqueConnection);
131 activateSelectionAndFilters(true);
133 // Restore selection in the viewer by the attribute selection list
134 myWorkshop->setSelected(getAttributeSelection());
137 //********************************************************************
138 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
140 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
141 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
142 bool aValid = acceptSubShape(aShape, aResult);
145 // In order to avoid selection of the same object
146 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
147 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
148 aValid = aSelectedFeature != myFeature;
153 //********************************************************************
154 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
158 getGeomSelection(thePrs, anObject, aShape);
160 setObject(anObject, aShape);
164 //********************************************************************
165 void ModuleBase_WidgetSelector::deactivate()
167 ModuleBase_ModelWidget::deactivate();
168 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
169 activateSelectionAndFilters(false);
170 ModuleBase_ModelWidget::deactivate();