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