1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModuleBase_WidgetSelector.h>
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>
35 #include <ModelAPI_AttributeSelection.h>
36 #include <ModelAPI_AttributeSelectionList.h>
37 #include <ModelAPI_Events.h>
38 #include <ModelAPI_ResultConstruction.h>
40 #include <TopoDS_Iterator.hxx>
42 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
43 ModuleBase_IWorkshop* theWorkshop,
44 const Config_WidgetAPI* theData)
45 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
49 //********************************************************************
50 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
54 //********************************************************************
55 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
57 GeomShapePtr& theShape)
59 ModuleBase_ISelection* aSelection = myWorkshop->selection();
60 theObject = aSelection->getResult(thePrs);
61 theShape = aSelection->getShape(thePrs);
64 //********************************************************************
65 bool ModuleBase_WidgetSelector::processSelection()
67 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
68 // equal vertices should not be used here
69 ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
71 bool isDone = setSelection(aSelected, true/*false*/);
72 updateOnSelectionChanged(isDone);
77 //********************************************************************
78 void ModuleBase_WidgetSelector::updateOnSelectionChanged(const bool theDone)
80 // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
81 // the same place repeatedly without mouse moved. In the case validation by filters is not
82 // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
83 // the 3rd click in the same point allow using this point.
85 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
86 // calls validators for the feature and, as a result, updates the Apply button state.
87 updateObject(myFeature);
89 // we need to forget about previous validation result as the current selection can influence on it
96 //********************************************************************
97 QIntList ModuleBase_WidgetSelector::getShapeTypes() const
99 QIntList aShapeTypes = shapeTypes();
100 // this type should be mentioned in XML, poor selection otherwise
101 if (/*aShapeTypes.contains(TopAbs_SOLID) ||*/
102 aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result/*TopAbs_SHAPE*/)) {
103 // it should be selectable for both, "solids" and "objects" types
104 aShapeTypes.append(TopAbs_COMPSOLID);
109 //********************************************************************
110 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetSelector::getAttributeSelection() const
112 return QList<ModuleBase_ViewerPrsPtr>();
115 //********************************************************************
116 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
117 const ResultPtr& theResult) const
121 GeomShapePtr aShape = theShape;
123 QIntList aShapeTypes = getShapeTypes();
124 if (aShapeTypes.empty()) {
128 // when the SHAPE type is provided by XML as Object, the whole result shape should be selected.
129 if (!aShape.get() && aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result)) {
134 if (!aShape.get() && theResult.get()) {
136 aShape = theResult->shape();
138 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
140 // Check that the selection corresponds to selection type
141 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
142 aShapeType = aTopoShape.ShapeType();
143 // for compounds check sub-shapes: it may be compound of needed type:
144 // Booleans may produce compounds of Solids
145 if (aShapeType == TopAbs_COMPOUND) {
146 aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
150 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
151 for (; anIt != aLast && !aValid; anIt++) {
152 TopAbs_ShapeEnum aCurrentShapeType = (TopAbs_ShapeEnum)*anIt;
153 if (aShapeType == aCurrentShapeType)
155 else if (aCurrentShapeType == TopAbs_FACE) {
156 // try to process the construction shape only if there is no a selected shape in the viewer
157 if (!theShape.get() && theResult.get()) {
158 ResultConstructionPtr aCResult =
159 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
160 aValid = aCResult.get() && aCResult->facesNum() > 0;
167 //********************************************************************
168 void ModuleBase_WidgetSelector::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
170 theModuleSelectionModes = -1;
171 theModes.append(getShapeTypes());
174 //********************************************************************
175 void ModuleBase_WidgetSelector::updateSelectionModesAndFilters(bool toActivate)
177 updateSelectionName();
179 myWorkshop->selectionActivate()->updateSelectionFilters();
180 myWorkshop->selectionActivate()->updateSelectionModes();
183 clearValidatedCash();
186 //********************************************************************
187 void ModuleBase_WidgetSelector::activateCustom()
189 // Restore selection in the viewer by the attribute selection list
190 // it should be postponed to have current widget as active to validate restored selection
191 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
192 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
195 //********************************************************************
196 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
198 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
199 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
200 bool aValid = acceptSubShape(aShape, aResult);
203 // In order to avoid selection of the same object
204 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
205 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
206 aValid = aSelectedFeature != myFeature;
211 //********************************************************************
212 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
216 getGeomSelection(thePrs, anObject, aShape);
218 // the last flag is to be depending on hasObject is called before. To be corrected later
219 return ModuleBase_Tools::setObject(attribute(), anObject, aShape,
220 myWorkshop, myIsInValidate, true);
223 //********************************************************************
224 void ModuleBase_WidgetSelector::deactivate()
226 ModuleBase_WidgetValidated::deactivate();
227 /// clear temporary cash
228 AttributePtr anAttribute = attribute();
229 if (!anAttribute.get())
231 std::string aType = anAttribute->attributeType();
232 if (anAttribute->attributeType() == ModelAPI_AttributeSelection::typeId()) {
233 AttributeSelectionPtr aSelectAttr =
234 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
235 aSelectAttr->removeTemporaryValues();
237 else if (anAttribute->attributeType() == ModelAPI_AttributeSelectionList::typeId()) {
238 AttributeSelectionListPtr aSelectAttr =
239 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
240 aSelectAttr->removeTemporaryValues();
244 //********************************************************************
245 std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
246 ModuleBase_IWorkshop* theWorkshop)
249 if (theAttribute.get() != NULL) {
250 ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
252 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
253 if (aFeature.get()) {
254 std::string aXmlCfg, aDescription;
255 theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
257 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
258 std::string anAttributeTitle;
259 aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
261 std::stringstream aStreamName;
262 aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
263 aName = aStreamName.str();