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 <TopoDS_Iterator.hxx>
16 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
17 ModuleBase_IWorkshop* theWorkshop,
18 const Config_WidgetAPI* theData,
19 const std::string& theParentId)
20 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId)
24 //********************************************************************
25 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
29 //********************************************************************
30 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
32 GeomShapePtr& theShape)
34 ModuleBase_ISelection* aSelection = myWorkshop->selection();
35 theObject = aSelection->getResult(thePrs);
36 theShape = aSelection->getShape(thePrs);
39 //********************************************************************
40 void ModuleBase_WidgetSelector::onSelectionChanged()
44 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
46 bool isDone = setSelection(aSelected, false);
48 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
49 // calls validators for the feature and, as a result, updates the Apply button state.
50 updateObject(myFeature);
56 //********************************************************************
57 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
58 const ResultPtr& theResult) const
62 GeomShapePtr aShape = theShape;
63 if (!aShape.get() && theResult.get()) {
65 aShape = theResult->shape();
67 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
69 // Check that the selection corresponds to selection type
70 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
71 aShapeType = aTopoShape.ShapeType();
72 // for compounds check sub-shapes: it may be compound of needed type:
73 // Booleans may produce compounds of Solids
74 if (aShapeType == TopAbs_COMPOUND) {
75 for(TopoDS_Iterator aSubs(aTopoShape); aSubs.More(); aSubs.Next()) {
76 if (!aSubs.Value().IsNull()) {
77 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
78 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
79 aShapeType = TopAbs_COMPOUND;
82 if (aShapeType == TopAbs_COMPOUND) {
83 aShapeType = aSubType;
84 } else if (aShapeType != aSubType) { // compound of shapes of different types
85 aShapeType = TopAbs_COMPOUND;
93 QIntList aShapeTypes = getShapeTypes();
94 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
95 for (; anIt != aLast; anIt++) {
96 if (aShapeType == *anIt)
98 else if (*anIt == TopAbs_FACE) {
99 // try to process the construction shape only if there is no a selected shape in the viewer
100 if (!theShape.get() && theResult.get()) {
101 ResultConstructionPtr aCResult =
102 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
103 aValid = aCResult.get() && aCResult->facesNum() > 0;
110 //********************************************************************
111 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
113 updateSelectionName();
116 myWorkshop->activateSubShapesSelection(getShapeTypes());
118 myWorkshop->deactivateSubShapesSelection();
122 //********************************************************************
123 void ModuleBase_WidgetSelector::activateCustom()
125 connect(myWorkshop, SIGNAL(selectionChanged()), this,
126 SLOT(onSelectionChanged()), Qt::UniqueConnection);
128 activateSelection(true);
130 // Restore selection in the viewer by the attribute selection list
131 myWorkshop->setSelected(getAttributeSelection());
133 activateFilters(true);
136 //********************************************************************
137 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
139 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
140 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
141 bool aValid = acceptSubShape(aShape, aResult);
144 // In order to avoid selection of the same object
145 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
146 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
147 aValid = aSelectedFeature != myFeature;
152 //********************************************************************
153 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
157 getGeomSelection(thePrs, anObject, aShape);
159 setObject(anObject, aShape);
163 //********************************************************************
164 void ModuleBase_WidgetSelector::deactivate()
166 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
167 activateSelection(false);
168 activateFilters(false);