Salome HOME
#839 Selection of Part sub-results
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetSelector.cpp
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 <GeomValidators_Tools.h>
15
16 #include <TopoDS_Iterator.hxx>
17
18 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
19                                                      ModuleBase_IWorkshop* theWorkshop,
20                                                      const Config_WidgetAPI* theData,
21                                                      const std::string& theParentId)
22  : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId)
23 {
24 }
25
26 //********************************************************************
27 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
28 {
29 }
30
31 //********************************************************************
32 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
33                                                       ObjectPtr& theObject,
34                                                       GeomShapePtr& theShape)
35 {
36   ModuleBase_ISelection* aSelection = myWorkshop->selection();
37   theObject = aSelection->getResult(thePrs);
38   theShape = aSelection->getShape(thePrs);
39 }
40
41 //********************************************************************
42 void ModuleBase_WidgetSelector::onSelectionChanged()
43 {
44   clearAttribute();
45
46   QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
47
48   bool isDone = setSelection(aSelected, false);
49   emit valuesChanged();
50   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
51   // calls validators for the feature and, as a result, updates the Apply button state.
52   updateObject(myFeature);
53
54   if (isDone)
55     updateFocus();
56 }
57
58 //********************************************************************
59 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
60                                                const ResultPtr& theResult) const
61 {
62   bool aValid = false;
63
64   GeomShapePtr aShape = theShape;
65   if (!aShape.get() && theResult.get()) {
66     if (theResult.get())
67       aShape = theResult->shape();
68   }
69   QIntList aShapeTypes = getShapeTypes();
70
71   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
72   if (aShape.get()) {
73     // Check that the selection corresponds to selection type
74     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
75     aShapeType = aTopoShape.ShapeType();
76     // for compounds check sub-shapes: it may be compound of needed type:
77     // Booleans may produce compounds of Solids
78     if (aShapeType == TopAbs_COMPOUND) {
79       if (aShapeTypes.contains(aShapeType))
80         return true;
81
82       aShapeType = GeomValidators_Tools::getCompoundSubType(aTopoShape);
83     }
84   }
85
86   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
87   for (; anIt != aLast; anIt++) {
88     if (aShapeType == *anIt)
89       aValid = true;
90     else if (*anIt == TopAbs_FACE) {
91       // try to process the construction shape only if there is no a selected shape in the viewer
92       if (!theShape.get() && theResult.get()) {
93         ResultConstructionPtr aCResult =
94                                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
95         aValid = aCResult.get() && aCResult->facesNum() > 0;
96       }
97     }
98   }
99   return aValid;
100 }
101
102 //********************************************************************
103 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
104 {
105   updateSelectionName();
106
107   if (toActivate) {
108     myWorkshop->activateSubShapesSelection(getShapeTypes());
109   } else {
110     myWorkshop->deactivateSubShapesSelection();
111   }
112 }
113
114 //********************************************************************
115 void ModuleBase_WidgetSelector::activateCustom()
116 {
117   connect(myWorkshop, SIGNAL(selectionChanged()), this,
118           SLOT(onSelectionChanged()), Qt::UniqueConnection);
119   
120   activateSelection(true);
121
122   // Restore selection in the viewer by the attribute selection list
123   myWorkshop->setSelected(getAttributeSelection());
124
125   activateFilters(true);
126 }
127
128 //********************************************************************
129 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
130 {
131   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
132   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
133   bool aValid = acceptSubShape(aShape, aResult);
134
135   if (aValid) {
136     // In order to avoid selection of the same object
137     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
138     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
139     aValid = aSelectedFeature != myFeature;
140   }
141   return aValid;
142 }
143
144 //********************************************************************
145 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
146 {
147   ObjectPtr anObject;
148   GeomShapePtr aShape;
149   getGeomSelection(thePrs, anObject, aShape);
150
151   setObject(anObject, aShape);
152   return true;
153 }
154
155 //********************************************************************
156 void ModuleBase_WidgetSelector::deactivate()
157 {
158   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
159   activateSelection(false);
160   activateFilters(false);
161 }
162