]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetSelector.cpp
Salome HOME
Shape plane filter should process shapes of objects selected in object browser.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetSelector.h
4 // Created:     19 June 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include <ModuleBase_WidgetSelector.h>
8
9 #include <ModuleBase_ISelection.h>
10 #include <ModuleBase_IWorkshop.h>
11
12 #include <ModelAPI_ResultConstruction.h>
13
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)
20 {
21 }
22
23 //********************************************************************
24 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
25 {
26 }
27
28 //TODO: nds stabilization hotfix
29 void ModuleBase_WidgetSelector::disconnectSignals()
30 {
31   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
32 }
33
34 //********************************************************************
35 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
36                                                       ObjectPtr& theObject,
37                                                       GeomShapePtr& theShape)
38 {
39   ModuleBase_ISelection* aSelection = myWorkshop->selection();
40   theObject = aSelection->getResult(thePrs);
41   theShape = aSelection->getShape(thePrs);
42 }
43
44 //********************************************************************
45 void ModuleBase_WidgetSelector::onSelectionChanged()
46 {
47   clearAttribute();
48
49   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
50                                                               ModuleBase_ISelection::AllControls);
51   bool isDone = setSelection(aSelected);
52
53   emit valuesChanged();
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);
57
58   if (isDone)
59     updateFocus();
60 }
61
62 #include <TopoDS_Iterator.hxx>
63
64 //********************************************************************
65 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
66                                                const ResultPtr& theResult) const
67 {
68   bool aValid = false;
69
70   GeomShapePtr aShape = theShape;
71   if (!aShape.get() && theResult.get()) {
72     if (theResult.get())
73       aShape = theResult->shape();
74   }
75   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
76   if (aShape.get()) {
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;
88             break;
89           }
90           if (aShapeType == TopAbs_COMPOUND) {
91             aShapeType = aSubType;
92           } else if (aShapeType != aSubType) { // compound of shapes of different types
93             aShapeType = TopAbs_COMPOUND;
94             break;
95           }
96         }
97       }
98     }
99   }
100
101   QIntList aShapeTypes = getShapeTypes();
102   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
103   for (; anIt != aLast; anIt++) {
104     if (aShapeType == *anIt)
105       aValid = true;
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;
112       }
113     }
114   }
115   return aValid;
116 }
117
118 //********************************************************************
119 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
120 {
121   updateSelectionName();
122
123   if (toActivate) {
124     myWorkshop->activateSubShapesSelection(getShapeTypes());
125   } else {
126     myWorkshop->deactivateSubShapesSelection();
127   }
128 }
129
130 //********************************************************************
131 void ModuleBase_WidgetSelector::activateCustom()
132 {
133   connect(myWorkshop, SIGNAL(selectionChanged()), this,
134           SLOT(onSelectionChanged()), Qt::UniqueConnection);
135   
136   activateSelection(true);
137
138   // Restore selection in the viewer by the attribute selection list
139   myWorkshop->setSelected(getAttributeSelection());
140
141   activateFilters(myWorkshop, true);
142 }
143
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)
149 {
150   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
151   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
152   bool aValid = acceptSubShape(aShape, aResult);
153
154   if (aValid) {
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;
159   }
160   
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);
165     if (aResult.get())
166       aShape = aResult->shape();
167
168     const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
169
170     Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
171     Handle(StdSelect_BRepOwner) aShapeOwner = new StdSelect_BRepOwner(aTDShape, anIO);
172
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())
178         continue;
179
180       aValid = aFilter->IsOk(aShapeOwner);
181     }
182     aShapeOwner.Nullify();
183   }
184   return aValid;
185 }
186
187 //********************************************************************
188 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
189 {
190   ObjectPtr anObject;
191   GeomShapePtr aShape;
192   getGeomSelection(thePrs, anObject, aShape);
193
194   setObject(anObject, aShape);
195   return true;
196 }
197
198 //********************************************************************
199 void ModuleBase_WidgetSelector::deactivate()
200 {
201   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
202   activateSelection(false);
203   activateFilters(myWorkshop, false);
204 }