1 // Copyright (C) 2014-2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ModuleBase_WidgetSelector.h>
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>
34 #include <ModelAPI_AttributeSelection.h>
35 #include <ModelAPI_AttributeSelectionList.h>
36 #include <ModelAPI_Events.h>
37 #include <ModelAPI_ResultConstruction.h>
38 #include <ModelAPI_ResultGroup.h>
40 #include <Config_WidgetAPI.h>
42 #include <TopoDS_Iterator.hxx>
44 // Get object from group
45 // Return true if find object
46 static bool getObjectFromGroup(ObjectPtr& theObject, GeomShapePtr& theShape);
48 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
49 ModuleBase_IWorkshop* theWorkshop,
50 const Config_WidgetAPI* theData)
51 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData),
52 myIsPointsFiltering(true)
54 QString aFiltering = QString::fromStdString(theData->getProperty("filter_points"));
55 if (aFiltering.toLower() == "false")
56 myIsPointsFiltering = false;
59 //********************************************************************
60 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
64 //********************************************************************
65 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
67 GeomShapePtr& theShape)
69 ModuleBase_ISelection* aSelection = myWorkshop->selection();
70 theObject = aSelection->getResult(thePrs);
72 theObject = thePrs->object();
73 theShape = aSelection->getShape(thePrs);
75 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
76 while (aFeature && aFeature->lastResult()->groupName() == ModelAPI_ResultGroup::group()) {
77 if (!getObjectFromGroup(theObject, theShape))
79 aFeature = ModelAPI_Feature::feature(theObject);
81 thePrs->setObject(theObject);
85 //********************************************************************
86 bool ModuleBase_WidgetSelector::processSelection()
88 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
89 // equal vertices should not be used here
90 if (myIsPointsFiltering)
91 ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
93 bool isDone = setSelection(aSelected, true/*false*/);
94 updateOnSelectionChanged(isDone);
99 //********************************************************************
100 void ModuleBase_WidgetSelector::updateOnSelectionChanged(const bool theDone)
102 // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
103 // the same place repeatedly without mouse moved. In the case validation by filters is not
104 // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
105 // the 3rd click in the same point allow using this point.
106 emit valuesChanged();
107 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
108 // calls validators for the feature and, as a result, updates the Apply button state.
109 updateObject(myFeature);
111 // we need to forget about previous validation result as the current selection can influence on it
112 clearValidatedCash();
118 //********************************************************************
119 QIntList ModuleBase_WidgetSelector::getShapeTypes() const
121 QIntList aShapeTypes = shapeTypes();
122 // this type should be mentioned in XML, poor selection otherwise
123 if (/*aShapeTypes.contains(TopAbs_SOLID) ||*/
124 aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result/*TopAbs_SHAPE*/)) {
125 // it should be selectable for both, "solids" and "objects" types
126 aShapeTypes.append(TopAbs_COMPSOLID);
131 //********************************************************************
132 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetSelector::getAttributeSelection() const
134 return QList<ModuleBase_ViewerPrsPtr>();
137 //********************************************************************
138 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
139 const ResultPtr& theResult) const
143 GeomShapePtr aShape = theShape;
145 QIntList aShapeTypes = getShapeTypes();
146 if (aShapeTypes.empty()) {
150 // when the SHAPE type is provided by XML as Object, the whole result shape should be selected.
151 //if (!aShape.get() && aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result)) {
152 // In case of selection of a feature aShape could be not NULL, but result has to be selected
153 if (aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result)) {
158 if (!aShape.get() && theResult.get()) {
160 aShape = theResult->shape();
162 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
164 // Check that the selection corresponds to selection type
165 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
166 if (!aTopoShape.IsNull()) {
167 aShapeType = aTopoShape.ShapeType();
168 // for compounds check sub-shapes: it may be compound of needed type:
169 // Booleans may produce compounds of Solids
170 if (aShapeType == TopAbs_COMPOUND) {
171 aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
176 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
177 for (; anIt != aLast && !aValid; anIt++) {
178 TopAbs_ShapeEnum aCurrentShapeType = (TopAbs_ShapeEnum)*anIt;
179 if (aShapeType == aCurrentShapeType)
181 else if (aCurrentShapeType == TopAbs_FACE) {
182 // try to process the construction shape only if there is no a selected shape in the viewer
183 if (!theShape.get() && theResult.get()) {
184 ResultConstructionPtr aCResult =
185 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
186 aValid = aCResult.get() && aCResult->facesNum() > 0;
193 //********************************************************************
194 void ModuleBase_WidgetSelector::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
196 theModuleSelectionModes = -1;
197 theModes.append(getShapeTypes());
200 //********************************************************************
201 void ModuleBase_WidgetSelector::updateSelectionModesAndFilters(bool toActivate)
203 updateSelectionName();
205 myWorkshop->selectionActivate()->updateSelectionFilters();
206 myWorkshop->selectionActivate()->updateSelectionModes();
209 clearValidatedCash();
212 //********************************************************************
213 void ModuleBase_WidgetSelector::activateCustom()
215 // Restore selection in the viewer by the attribute selection list
216 // it should be postponed to have current widget as active to validate restored selection
217 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
218 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
221 //********************************************************************
222 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
224 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
225 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
226 bool aValid = aResult.get();
227 if (!isWholeResultAllowed())
228 aValid = acceptSubShape(aShape, aResult);
231 // In order to avoid selection of the same object
232 aResult = myWorkshop->selection()->getResult(thePrs);
233 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
234 aValid = aSelectedFeature != myFeature;
239 //********************************************************************
240 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
244 getGeomSelection(thePrs, anObject, aShape);
246 // the last flag is to be depending on hasObject is called before. To be corrected later
247 return ModuleBase_Tools::setObject(attribute(), anObject, aShape,
248 myWorkshop, myIsInValidate, true);
251 //********************************************************************
252 void ModuleBase_WidgetSelector::deactivate()
254 ModuleBase_WidgetValidated::deactivate();
255 /// clear temporary cash
256 AttributePtr anAttribute = attribute();
257 if (!anAttribute.get())
259 std::string aType = anAttribute->attributeType();
260 if (anAttribute->attributeType() == ModelAPI_AttributeSelection::typeId()) {
261 AttributeSelectionPtr aSelectAttr =
262 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
263 aSelectAttr->removeTemporaryValues();
265 else if (anAttribute->attributeType() == ModelAPI_AttributeSelectionList::typeId()) {
266 AttributeSelectionListPtr aSelectAttr =
267 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
268 aSelectAttr->removeTemporaryValues();
272 //********************************************************************
273 bool ModuleBase_WidgetSelector::isWholeResultAllowed() const
275 AttributePtr anAttribute = attribute();
276 if (anAttribute.get()) {
277 AttributeSelectionListPtr aSelAttr =
278 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
280 return aSelAttr->isWholeResultAllowed();
285 bool getObjectFromGroup(ObjectPtr& theObject, GeomShapePtr& theShape)
287 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
289 AttributeSelectionListPtr anAttrList = aFeature->selectionList("group_list");
291 for (int anIndex = 0; anIndex < anAttrList->size(); ++anIndex) {
292 AttributeSelectionPtr aSelect = anAttrList->value(anIndex);
293 if (aSelect->context()->shape()->isSubShape(theShape) ||
294 aSelect->context()->shape()->isEqual(theShape)) {
295 theObject = aSelect->contextObject();