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 = myWorkshop->selection()->getSelected(
51                                                               ModuleBase_ISelection::AllControls);
52   bool isDone = setSelection(aSelected, true);
53
54   emit valuesChanged();
55   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
56   // calls validators for the feature and, as a result, updates the Apply button state.
57   updateObject(myFeature);
58
59   if (isDone)
60     updateFocus();
61 }
62
63 //********************************************************************
64 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
65                                                const ResultPtr& theResult) const
66 {
67   bool aValid = false;
68
69   GeomShapePtr aShape = theShape;
70   if (!aShape.get() && theResult.get()) {
71     if (theResult.get())
72       aShape = theResult->shape();
73   }
74   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
75   if (aShape.get()) {
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;
87             break;
88           }
89           if (aShapeType == TopAbs_COMPOUND) {
90             aShapeType = aSubType;
91           } else if (aShapeType != aSubType) { // compound of shapes of different types
92             aShapeType = TopAbs_COMPOUND;
93             break;
94           }
95         }
96       }
97     }
98   }
99
100   QIntList aShapeTypes = getShapeTypes();
101   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
102   for (; anIt != aLast; anIt++) {
103     if (aShapeType == *anIt)
104       aValid = true;
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;
111       }
112     }
113   }
114   return aValid;
115 }
116
117 //********************************************************************
118 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
119 {
120   updateSelectionName();
121
122   if (toActivate) {
123     myWorkshop->activateSubShapesSelection(getShapeTypes());
124   } else {
125     myWorkshop->deactivateSubShapesSelection();
126   }
127 }
128
129 //********************************************************************
130 void ModuleBase_WidgetSelector::activateCustom()
131 {
132   connect(myWorkshop, SIGNAL(selectionChanged()), this,
133           SLOT(onSelectionChanged()), Qt::UniqueConnection);
134   
135   activateSelection(true);
136
137   // Restore selection in the viewer by the attribute selection list
138   myWorkshop->setSelected(getAttributeSelection());
139
140   activateFilters(true);
141 }
142
143 //********************************************************************
144 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
145 {
146   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
147   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
148   bool aValid = acceptSubShape(aShape, aResult);
149
150   if (aValid) {
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;
155   }
156   return aValid;
157 }
158
159 //********************************************************************
160 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
161 {
162   ObjectPtr anObject;
163   GeomShapePtr aShape;
164   getGeomSelection(thePrs, anObject, aShape);
165
166   setObject(anObject, aShape);
167   return true;
168 }
169
170 //********************************************************************
171 void ModuleBase_WidgetSelector::deactivate()
172 {
173   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
174   activateSelection(false);
175   activateFilters(false);
176 }