Salome HOME
Issue #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
66   QIntList aShapeTypes = getShapeTypes();
67   if (aShapeTypes.empty()) {
68     aValid = true;
69     return aValid;
70   }
71   // when the SHAPE type is provided by XML, the hole result shape can be selected.
72   if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
73     aValid = true;
74     return aValid;
75   }
76
77   if (!aShape.get() && theResult.get()) {
78     if (theResult.get())
79       aShape = theResult->shape();
80   }
81   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
82   if (aShape.get()) {
83     // Check that the selection corresponds to selection type
84     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
85     aShapeType = aTopoShape.ShapeType();
86     // for compounds check sub-shapes: it may be compound of needed type:
87     // Booleans may produce compounds of Solids
88     if (aShapeType == TopAbs_COMPOUND) {
89       aShapeType = GeomValidators_Tools::getCompoundSubType(aTopoShape);
90     }
91   }
92
93   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
94   for (; anIt != aLast; anIt++) {
95     if (aShapeType == *anIt)
96       aValid = true;
97     else if (*anIt == TopAbs_FACE) {
98       // try to process the construction shape only if there is no a selected shape in the viewer
99       if (!theShape.get() && theResult.get()) {
100         ResultConstructionPtr aCResult =
101                                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
102         aValid = aCResult.get() && aCResult->facesNum() > 0;
103       }
104     }
105   }
106   return aValid;
107 }
108
109 //********************************************************************
110 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
111 {
112   updateSelectionName();
113
114   if (toActivate) {
115     myWorkshop->activateSubShapesSelection(getShapeTypes());
116   } else {
117     myWorkshop->deactivateSubShapesSelection();
118   }
119 }
120
121 //********************************************************************
122 void ModuleBase_WidgetSelector::activateCustom()
123 {
124   connect(myWorkshop, SIGNAL(selectionChanged()), this,
125           SLOT(onSelectionChanged()), Qt::UniqueConnection);
126   
127   activateSelection(true);
128
129   // Restore selection in the viewer by the attribute selection list
130   myWorkshop->setSelected(getAttributeSelection());
131
132   activateFilters(true);
133 }
134
135 //********************************************************************
136 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
137 {
138   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
139   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
140   bool aValid = acceptSubShape(aShape, aResult);
141
142   if (aValid) {
143     // In order to avoid selection of the same object
144     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
145     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
146     aValid = aSelectedFeature != myFeature;
147   }
148   return aValid;
149 }
150
151 //********************************************************************
152 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
153 {
154   ObjectPtr anObject;
155   GeomShapePtr aShape;
156   getGeomSelection(thePrs, anObject, aShape);
157
158   setObject(anObject, aShape);
159   return true;
160 }
161
162 //********************************************************************
163 void ModuleBase_WidgetSelector::deactivate()
164 {
165   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
166   activateSelection(false);
167   activateFilters(false);
168 }
169