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, theData, theParentId),
19 myWorkshop(theWorkshop)
23 //********************************************************************
24 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
28 //TODO: nds stabilization hotfix
29 void ModuleBase_WidgetSelector::disconnectSignals()
31 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
34 //********************************************************************
35 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
37 GeomShapePtr& theShape)
39 ModuleBase_ISelection* aSelection = myWorkshop->selection();
40 theObject = aSelection->getResult(thePrs);
41 theShape = aSelection->getShape(thePrs);
44 //********************************************************************
45 void ModuleBase_WidgetSelector::onSelectionChanged()
49 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
50 ModuleBase_ISelection::AllControls);
51 bool isDone = setSelection(aSelected);
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 #include <TopoDS_Iterator.hxx>
64 //********************************************************************
65 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
66 const ResultPtr& theResult) const
70 GeomShapePtr aShape = theShape;
71 if (!aShape.get() && theResult.get()) {
73 aShape = theResult->shape();
75 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
77 // Check that the selection corresponds to selection type
78 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
79 aShapeType = aTopoShape.ShapeType();
80 // for compounds check sub-shapes: it may be compound of needed type:
81 // Booleans may produce compounds of Solids
82 if (aShapeType == TopAbs_COMPOUND) {
83 for(TopoDS_Iterator aSubs(aTopoShape); aSubs.More(); aSubs.Next()) {
84 if (!aSubs.Value().IsNull()) {
85 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
86 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
87 aShapeType = TopAbs_COMPOUND;
90 if (aShapeType == TopAbs_COMPOUND) {
91 aShapeType = aSubType;
92 } else if (aShapeType != aSubType) { // compound of shapes of different types
93 aShapeType = TopAbs_COMPOUND;
101 QIntList aShapeTypes = getShapeTypes();
102 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
103 for (; anIt != aLast; anIt++) {
104 if (aShapeType == *anIt)
106 else if (*anIt == TopAbs_FACE) {
107 // try to process the construction shape only if there is no a selected shape in the viewer
108 if (!theShape.get() && theResult.get()) {
109 ResultConstructionPtr aCResult =
110 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
111 aValid = aCResult.get() && aCResult->facesNum() > 0;
118 //********************************************************************
119 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
121 updateSelectionName();
124 myWorkshop->activateSubShapesSelection(getShapeTypes());
126 myWorkshop->deactivateSubShapesSelection();
130 //********************************************************************
131 void ModuleBase_WidgetSelector::activateCustom()
133 connect(myWorkshop, SIGNAL(selectionChanged()), this,
134 SLOT(onSelectionChanged()), Qt::UniqueConnection);
136 activateSelection(true);
138 // Restore selection in the viewer by the attribute selection list
139 myWorkshop->setSelected(getAttributeSelection());
141 activateFilters(myWorkshop, true);
144 //********************************************************************
145 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
147 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
148 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
149 bool aValid = acceptSubShape(aShape, aResult);
152 // In order to avoid selection of the same object
153 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
154 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
155 aValid = aSelectedFeature != myFeature;
160 //********************************************************************
161 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
165 getGeomSelection(thePrs, anObject, aShape);
167 setObject(anObject, aShape);
171 //********************************************************************
172 void ModuleBase_WidgetSelector::deactivate()
174 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
175 activateSelection(false);
176 activateFilters(myWorkshop, false);