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 //TODO: nds stabilization hotfix
30 void ModuleBase_WidgetSelector::disconnectSignals()
32 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
35 //********************************************************************
36 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
38 GeomShapePtr& theShape)
40 ModuleBase_ISelection* aSelection = myWorkshop->selection();
41 theObject = aSelection->getResult(thePrs);
42 theShape = aSelection->getShape(thePrs);
45 //********************************************************************
46 void ModuleBase_WidgetSelector::onSelectionChanged()
50 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
52 bool isDone = setSelection(aSelected, false);
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;
69 if (!aShape.get() && theResult.get()) {
71 aShape = theResult->shape();
73 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
75 // Check that the selection corresponds to selection type
76 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
77 aShapeType = aTopoShape.ShapeType();
78 // for compounds check sub-shapes: it may be compound of needed type:
79 // Booleans may produce compounds of Solids
80 if (aShapeType == TopAbs_COMPOUND) {
81 for(TopoDS_Iterator aSubs(aTopoShape); aSubs.More(); aSubs.Next()) {
82 if (!aSubs.Value().IsNull()) {
83 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
84 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
85 aShapeType = TopAbs_COMPOUND;
88 if (aShapeType == TopAbs_COMPOUND) {
89 aShapeType = aSubType;
90 } else if (aShapeType != aSubType) { // compound of shapes of different types
91 aShapeType = TopAbs_COMPOUND;
99 QIntList aShapeTypes = getShapeTypes();
100 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
101 for (; anIt != aLast; anIt++) {
102 if (aShapeType == *anIt)
104 else if (*anIt == TopAbs_FACE) {
105 // try to process the construction shape only if there is no a selected shape in the viewer
106 if (!theShape.get() && theResult.get()) {
107 ResultConstructionPtr aCResult =
108 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
109 aValid = aCResult.get() && aCResult->facesNum() > 0;
116 //********************************************************************
117 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
119 updateSelectionName();
122 myWorkshop->activateSubShapesSelection(getShapeTypes());
124 myWorkshop->deactivateSubShapesSelection();
128 //********************************************************************
129 void ModuleBase_WidgetSelector::activateCustom()
131 connect(myWorkshop, SIGNAL(selectionChanged()), this,
132 SLOT(onSelectionChanged()), Qt::UniqueConnection);
134 activateSelection(true);
136 // Restore selection in the viewer by the attribute selection list
137 myWorkshop->setSelected(getAttributeSelection());
139 activateFilters(true);
142 //********************************************************************
143 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
145 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
146 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
147 bool aValid = acceptSubShape(aShape, aResult);
150 // In order to avoid selection of the same object
151 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
152 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
153 aValid = aSelectedFeature != myFeature;
158 //********************************************************************
159 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
163 getGeomSelection(thePrs, anObject, aShape);
165 setObject(anObject, aShape);
169 //********************************************************************
170 void ModuleBase_WidgetSelector::deactivate()
172 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
173 activateSelection(false);
174 activateFilters(false);