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 if (aShapeTypes.empty()) {
71 // when the SHAPE type is provided by XML, the hole result shape can be selected.
72 if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
77 if (!aShape.get() && theResult.get()) {
79 aShape = theResult->shape();
81 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
83 // Check that the selection corresponds to selection type
84 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
85 aShapeType = aTopoShape.ShapeType();
86 // for compounds check sub-shapes: it may be compound of needed type:
87 // Booleans may produce compounds of Solids
88 if (aShapeType == TopAbs_COMPOUND) {
89 aShapeType = GeomValidators_Tools::getCompoundSubType(aTopoShape);
93 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
94 for (; anIt != aLast; anIt++) {
95 if (aShapeType == *anIt)
97 else if (*anIt == TopAbs_FACE) {
98 // try to process the construction shape only if there is no a selected shape in the viewer
99 if (!theShape.get() && theResult.get()) {
100 ResultConstructionPtr aCResult =
101 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
102 aValid = aCResult.get() && aCResult->facesNum() > 0;
109 //********************************************************************
110 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
112 updateSelectionName();
115 myWorkshop->activateSubShapesSelection(getShapeTypes());
117 myWorkshop->deactivateSubShapesSelection();
121 //********************************************************************
122 void ModuleBase_WidgetSelector::activateCustom()
124 connect(myWorkshop, SIGNAL(selectionChanged()), this,
125 SLOT(onSelectionChanged()), Qt::UniqueConnection);
127 activateSelection(true);
129 // Restore selection in the viewer by the attribute selection list
130 myWorkshop->setSelected(getAttributeSelection());
132 activateFilters(true);
135 //********************************************************************
136 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
138 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
139 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
140 bool aValid = acceptSubShape(aShape, aResult);
143 // In order to avoid selection of the same object
144 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
145 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
146 aValid = aSelectedFeature != myFeature;
151 //********************************************************************
152 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
156 getGeomSelection(thePrs, anObject, aShape);
158 setObject(anObject, aShape);
162 //********************************************************************
163 void ModuleBase_WidgetSelector::deactivate()
165 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
166 activateSelection(false);
167 activateFilters(false);