1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetSelector.h
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 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
15 ModuleBase_IWorkshop* theWorkshop,
16 const Config_WidgetAPI* theData,
17 const std::string& theParentId)
18 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId)
22 //********************************************************************
23 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
27 //TODO: nds stabilization hotfix
28 void ModuleBase_WidgetSelector::disconnectSignals()
30 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
33 //********************************************************************
34 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
36 GeomShapePtr& theShape)
38 ModuleBase_ISelection* aSelection = myWorkshop->selection();
39 theObject = aSelection->getResult(thePrs);
40 theShape = aSelection->getShape(thePrs);
43 //********************************************************************
44 void ModuleBase_WidgetSelector::onSelectionChanged()
48 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
49 ModuleBase_ISelection::AllControls);
50 bool isDone = setSelection(aSelected, true);
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 #include <TopoDS_Iterator.hxx>
63 //********************************************************************
64 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
65 const ResultPtr& theResult) const
69 GeomShapePtr aShape = theShape;
70 if (!aShape.get() && theResult.get()) {
72 aShape = theResult->shape();
74 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
76 // Check that the selection corresponds to selection type
77 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
78 aShapeType = aTopoShape.ShapeType();
79 // for compounds check sub-shapes: it may be compound of needed type:
80 // Booleans may produce compounds of Solids
81 if (aShapeType == TopAbs_COMPOUND) {
82 for(TopoDS_Iterator aSubs(aTopoShape); aSubs.More(); aSubs.Next()) {
83 if (!aSubs.Value().IsNull()) {
84 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
85 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
86 aShapeType = TopAbs_COMPOUND;
89 if (aShapeType == TopAbs_COMPOUND) {
90 aShapeType = aSubType;
91 } else if (aShapeType != aSubType) { // compound of shapes of different types
92 aShapeType = TopAbs_COMPOUND;
100 QIntList aShapeTypes = getShapeTypes();
101 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
102 for (; anIt != aLast; anIt++) {
103 if (aShapeType == *anIt)
105 else if (*anIt == TopAbs_FACE) {
106 // try to process the construction shape only if there is no a selected shape in the viewer
107 if (!theShape.get() && theResult.get()) {
108 ResultConstructionPtr aCResult =
109 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
110 aValid = aCResult.get() && aCResult->facesNum() > 0;
117 //********************************************************************
118 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
120 updateSelectionName();
123 myWorkshop->activateSubShapesSelection(getShapeTypes());
125 myWorkshop->deactivateSubShapesSelection();
129 //********************************************************************
130 void ModuleBase_WidgetSelector::activateCustom()
132 connect(myWorkshop, SIGNAL(selectionChanged()), this,
133 SLOT(onSelectionChanged()), Qt::UniqueConnection);
135 activateSelection(true);
137 // Restore selection in the viewer by the attribute selection list
138 myWorkshop->setSelected(getAttributeSelection());
140 activateFilters(true);
143 //********************************************************************
144 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
146 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
147 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
148 bool aValid = acceptSubShape(aShape, aResult);
151 // In order to avoid selection of the same object
152 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
153 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
154 aValid = aSelectedFeature != myFeature;
159 //********************************************************************
160 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
164 getGeomSelection(thePrs, anObject, aShape);
166 setObject(anObject, aShape);
170 //********************************************************************
171 void ModuleBase_WidgetSelector::deactivate()
173 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
174 activateSelection(false);
175 activateFilters(false);