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 #include <TopoDS_Iterator.hxx>
15
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)
21 {
22 }
23
24 //********************************************************************
25 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
26 {
27 }
28
29 //TODO: nds stabilization hotfix
30 void ModuleBase_WidgetSelector::disconnectSignals()
31 {
32   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
33 }
34
35 //********************************************************************
36 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
37                                                       ObjectPtr& theObject,
38                                                       GeomShapePtr& theShape)
39 {
40   ModuleBase_ISelection* aSelection = myWorkshop->selection();
41   theObject = aSelection->getResult(thePrs);
42   theShape = aSelection->getShape(thePrs);
43 }
44
45 //********************************************************************
46 void ModuleBase_WidgetSelector::onSelectionChanged()
47 {
48   clearAttribute();
49
50   QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
51
52   bool isDone = setSelection(aSelected, false);
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 //********************************************************************
63 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
64                                                const ResultPtr& theResult) const
65 {
66   bool aValid = false;
67
68   GeomShapePtr aShape = theShape;
69   if (!aShape.get() && theResult.get()) {
70     if (theResult.get())
71       aShape = theResult->shape();
72   }
73   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
74   if (aShape.get()) {
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;
86             break;
87           }
88           if (aShapeType == TopAbs_COMPOUND) {
89             aShapeType = aSubType;
90           } else if (aShapeType != aSubType) { // compound of shapes of different types
91             aShapeType = TopAbs_COMPOUND;
92             break;
93           }
94         }
95       }
96     }
97   }
98
99   QIntList aShapeTypes = getShapeTypes();
100   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
101   for (; anIt != aLast; anIt++) {
102     if (aShapeType == *anIt)
103       aValid = true;
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;
110       }
111     }
112   }
113   return aValid;
114 }
115
116 //********************************************************************
117 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
118 {
119   updateSelectionName();
120
121   if (toActivate) {
122     myWorkshop->activateSubShapesSelection(getShapeTypes());
123   } else {
124     myWorkshop->deactivateSubShapesSelection();
125   }
126 }
127
128 //********************************************************************
129 void ModuleBase_WidgetSelector::activateCustom()
130 {
131   connect(myWorkshop, SIGNAL(selectionChanged()), this,
132           SLOT(onSelectionChanged()), Qt::UniqueConnection);
133   
134   activateSelection(true);
135
136   // Restore selection in the viewer by the attribute selection list
137   myWorkshop->setSelected(getAttributeSelection());
138
139   activateFilters(true);
140 }
141
142 //********************************************************************
143 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
144 {
145   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
146   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
147   bool aValid = acceptSubShape(aShape, aResult);
148
149   if (aValid) {
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;
154   }
155   return aValid;
156 }
157
158 //********************************************************************
159 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
160 {
161   ObjectPtr anObject;
162   GeomShapePtr aShape;
163   getGeomSelection(thePrs, anObject, aShape);
164
165   setObject(anObject, aShape);
166   return true;
167 }
168
169 //********************************************************************
170 void ModuleBase_WidgetSelector::deactivate()
171 {
172   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
173   activateSelection(false);
174   activateFilters(false);
175 }
176