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 #include <ModuleBase_IViewer.h>
146 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
147 #include <StdSelect_BRepOwner.hxx>
148 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
150 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
151 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
152 bool aValid = acceptSubShape(aShape, aResult);
155 // In order to avoid selection of the same object
156 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
157 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
158 aValid = aSelectedFeature != myFeature;
161 // selection happens in the Object browser.
162 // it creates a selection owner and check whether the viewer filters process it
163 if (thePrs.owner().IsNull() && thePrs.object().get()) {
164 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
166 aShape = aResult->shape();
168 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
170 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
171 Handle(StdSelect_BRepOwner) aShapeOwner = new StdSelect_BRepOwner(aTDShape, anIO);
173 const SelectMgr_ListOfFilter& aFilters = myWorkshop->viewer()->AISContext()->Filters();
174 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
175 for (; anIt.More() && aValid; anIt.Next()) {
176 Handle(SelectMgr_Filter) aFilter = anIt.Value();
177 if (aFilter == myWorkshop->validatorFilter())
180 aValid = aFilter->IsOk(aShapeOwner);
182 aShapeOwner.Nullify();
187 //********************************************************************
188 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
192 getGeomSelection(thePrs, anObject, aShape);
194 setObject(anObject, aShape);
198 //********************************************************************
199 void ModuleBase_WidgetSelector::deactivate()
201 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
202 activateSelection(false);
203 activateFilters(myWorkshop, false);