Salome HOME
Merge remote-tracking branch 'origin/CEA_2019'
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelector.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ModuleBase_WidgetSelector.h>
21
22 #include <ModuleBase_Events.h>
23 #include <ModuleBase_IModule.h>
24 #include <ModuleBase_ISelection.h>
25 #include <ModuleBase_ISelectionActivate.h>
26 #include <ModuleBase_IWorkshop.h>
27 #include <ModuleBase_Operation.h>
28 #include <ModuleBase_OperationDescription.h>
29 #include <ModuleBase_ResultPrs.h>
30 #include <ModuleBase_Tools.h>
31 #include <ModuleBase_ViewerPrs.h>
32 #include <ModuleBase_WidgetFactory.h>
33
34 #include <ModelAPI_AttributeSelection.h>
35 #include <ModelAPI_AttributeSelectionList.h>
36 #include <ModelAPI_Events.h>
37 #include <ModelAPI_ResultConstruction.h>
38
39 #include <Config_WidgetAPI.h>
40
41 #include <TopoDS_Iterator.hxx>
42
43 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
44                                                      ModuleBase_IWorkshop* theWorkshop,
45                                                      const Config_WidgetAPI* theData)
46 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData),
47 myIsPointsFiltering(true)
48 {
49   QString aFiltering = QString::fromStdString(theData->getProperty("filter_points"));
50   if (aFiltering.toLower() == "false")
51     myIsPointsFiltering = false;
52 }
53
54 //********************************************************************
55 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
56 {
57 }
58
59 //********************************************************************
60 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
61                                                       ObjectPtr& theObject,
62                                                       GeomShapePtr& theShape)
63 {
64   ModuleBase_ISelection* aSelection = myWorkshop->selection();
65   theObject = aSelection->getResult(thePrs);
66   if (!theObject.get())
67     theObject = thePrs->object();
68   theShape = aSelection->getShape(thePrs);
69 }
70
71 //********************************************************************
72 bool ModuleBase_WidgetSelector::processSelection()
73 {
74   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
75   // equal vertices should not be used here
76   if (myIsPointsFiltering)
77     ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
78
79   bool isDone = setSelection(aSelected, true/*false*/);
80   updateOnSelectionChanged(isDone);
81
82   return isDone;
83 }
84
85 //********************************************************************
86 void ModuleBase_WidgetSelector::updateOnSelectionChanged(const bool theDone)
87 {
88   // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
89   // the same place repeatedly without mouse moved. In the case validation by filters is not
90   // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
91   // the 3rd click in the same point allow using this point.
92   emit valuesChanged();
93   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
94   // calls validators for the feature and, as a result, updates the Apply button state.
95   updateObject(myFeature);
96
97   // we need to forget about previous validation result as the current selection can influence on it
98   clearValidatedCash();
99
100   if (theDone)
101     updateFocus();
102 }
103
104 //********************************************************************
105 QIntList ModuleBase_WidgetSelector::getShapeTypes() const
106 {
107   QIntList aShapeTypes = shapeTypes();
108   // this type should be mentioned in XML, poor selection otherwise
109   if (/*aShapeTypes.contains(TopAbs_SOLID) ||*/
110       aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result/*TopAbs_SHAPE*/)) {
111     // it should be selectable for both, "solids" and "objects" types
112     aShapeTypes.append(TopAbs_COMPSOLID);
113   }
114   return aShapeTypes;
115 }
116
117 //********************************************************************
118 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetSelector::getAttributeSelection() const
119 {
120   return QList<ModuleBase_ViewerPrsPtr>();
121 }
122
123 //********************************************************************
124 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
125                                                const ResultPtr& theResult) const
126 {
127   bool aValid = false;
128
129   GeomShapePtr aShape = theShape;
130
131   QIntList aShapeTypes = getShapeTypes();
132   if (aShapeTypes.empty()) {
133     aValid = true;
134     return aValid;
135   }
136   // when the SHAPE type is provided by XML as Object, the whole result shape should be selected.
137   //if (!aShape.get() && aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result)) {
138   // In case of selection of a feature aShape could be not NULL, but result has to be selected
139   if (aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result)) {
140     aValid = true;
141     return aValid;
142   }
143
144   if (!aShape.get() && theResult.get()) {
145     if (theResult.get())
146       aShape = theResult->shape();
147   }
148   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
149   if (aShape.get()) {
150     // Check that the selection corresponds to selection type
151     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
152     if (!aTopoShape.IsNull()) {
153       aShapeType = aTopoShape.ShapeType();
154       // for compounds check sub-shapes: it may be compound of needed type:
155       // Booleans may produce compounds of Solids
156       if (aShapeType == TopAbs_COMPOUND) {
157         aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
158       }
159     }
160   }
161
162   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
163   for (; anIt != aLast && !aValid; anIt++) {
164     TopAbs_ShapeEnum aCurrentShapeType = (TopAbs_ShapeEnum)*anIt;
165     if (aShapeType == aCurrentShapeType)
166       aValid = true;
167     else if (aCurrentShapeType == TopAbs_FACE) {
168       // try to process the construction shape only if there is no a selected shape in the viewer
169       if (!theShape.get() && theResult.get()) {
170         ResultConstructionPtr aCResult =
171                                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
172         aValid = aCResult.get() && aCResult->facesNum() > 0;
173       }
174     }
175   }
176   return aValid;
177 }
178
179 //********************************************************************
180 void ModuleBase_WidgetSelector::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
181 {
182   theModuleSelectionModes = -1;
183   theModes.append(getShapeTypes());
184 }
185
186 //********************************************************************
187 void ModuleBase_WidgetSelector::updateSelectionModesAndFilters(bool toActivate)
188 {
189   updateSelectionName();
190
191   myWorkshop->selectionActivate()->updateSelectionFilters();
192   myWorkshop->selectionActivate()->updateSelectionModes();
193
194   if (!toActivate)
195     clearValidatedCash();
196 }
197
198 //********************************************************************
199 void ModuleBase_WidgetSelector::activateCustom()
200 {
201   // Restore selection in the viewer by the attribute selection list
202   // it should be postponed to have current widget as active to validate restored selection
203   static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
204   ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
205 }
206
207 //********************************************************************
208 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
209 {
210   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
211   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
212   bool aValid = aResult.get();
213   if (!isWholeResultAllowed())
214     aValid = acceptSubShape(aShape, aResult);
215
216   if (aValid) {
217     // In order to avoid selection of the same object
218     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
219     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
220     aValid = aSelectedFeature != myFeature;
221   }
222   return aValid;
223 }
224
225 //********************************************************************
226 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
227 {
228   ObjectPtr anObject;
229   GeomShapePtr aShape;
230   getGeomSelection(thePrs, anObject, aShape);
231
232   // the last flag is to be depending on hasObject is called before. To be corrected later
233   return ModuleBase_Tools::setObject(attribute(), anObject, aShape,
234                                      myWorkshop, myIsInValidate, true);
235 }
236
237 //********************************************************************
238 void ModuleBase_WidgetSelector::deactivate()
239 {
240   ModuleBase_WidgetValidated::deactivate();
241   /// clear temporary cash
242   AttributePtr anAttribute = attribute();
243   if (!anAttribute.get())
244     return;
245   std::string aType = anAttribute->attributeType();
246   if (anAttribute->attributeType() == ModelAPI_AttributeSelection::typeId()) {
247     AttributeSelectionPtr aSelectAttr =
248                              std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
249     aSelectAttr->removeTemporaryValues();
250   }
251   else if (anAttribute->attributeType() == ModelAPI_AttributeSelectionList::typeId()) {
252     AttributeSelectionListPtr aSelectAttr =
253                       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
254     aSelectAttr->removeTemporaryValues();
255   }
256 }
257
258 //********************************************************************
259 bool ModuleBase_WidgetSelector::isWholeResultAllowed() const
260 {
261   AttributePtr anAttribute = attribute();
262   if (anAttribute.get()) {
263     AttributeSelectionListPtr aSelAttr =
264       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
265     if (aSelAttr.get())
266       return aSelAttr->isWholeResultAllowed();
267   }
268   return false;
269 }