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, false);
50 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
51 // calls validators for the feature and, as a result, updates the Apply button state.
52 updateObject(myFeature);
58 //********************************************************************
59 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
60 const ResultPtr& theResult) const
64 GeomShapePtr aShape = theShape;
66 QIntList aShapeTypes = getShapeTypes();
67 // when the SHAPE type is provided by XML, the hole result shape can be selected.
68 if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
73 if (!aShape.get() && theResult.get()) {
75 aShape = theResult->shape();
77 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
79 // Check that the selection corresponds to selection type
80 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
81 aShapeType = aTopoShape.ShapeType();
82 // for compounds check sub-shapes: it may be compound of needed type:
83 // Booleans may produce compounds of Solids
84 if (aShapeType == TopAbs_COMPOUND) {
85 aShapeType = GeomValidators_Tools::getCompoundSubType(aTopoShape);
89 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
90 for (; anIt != aLast; anIt++) {
91 if (aShapeType == *anIt)
93 else if (*anIt == TopAbs_FACE) {
94 // try to process the construction shape only if there is no a selected shape in the viewer
95 if (!theShape.get() && theResult.get()) {
96 ResultConstructionPtr aCResult =
97 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
98 aValid = aCResult.get() && aCResult->facesNum() > 0;
105 //********************************************************************
106 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
108 updateSelectionName();
111 myWorkshop->activateSubShapesSelection(getShapeTypes());
113 myWorkshop->deactivateSubShapesSelection();
117 //********************************************************************
118 void ModuleBase_WidgetSelector::activateCustom()
120 connect(myWorkshop, SIGNAL(selectionChanged()), this,
121 SLOT(onSelectionChanged()), Qt::UniqueConnection);
123 activateSelection(true);
125 // Restore selection in the viewer by the attribute selection list
126 myWorkshop->setSelected(getAttributeSelection());
128 activateFilters(true);
131 //********************************************************************
132 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
134 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
135 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
136 bool aValid = acceptSubShape(aShape, aResult);
139 // In order to avoid selection of the same object
140 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
141 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
142 aValid = aSelectedFeature != myFeature;
147 //********************************************************************
148 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
152 getGeomSelection(thePrs, anObject, aShape);
154 setObject(anObject, aShape);
158 //********************************************************************
159 void ModuleBase_WidgetSelector::deactivate()
161 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
162 activateSelection(false);
163 activateFilters(false);