1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetSelector.cpp
4 // Created: 19 June 2015
5 // Author: Natalia ERMOLAEVA
7 #include <ModuleBase_WidgetSelector.h>
9 #include <ModuleBase_ISelection.h>
10 #include <ModuleBase_IWorkshop.h>
11 #include <ModuleBase_Tools.h>
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_OperationDescription.h>
14 #include <ModuleBase_WidgetFactory.h>
15 #include <ModuleBase_IModule.h>
17 #include <ModelAPI_ResultConstruction.h>
19 #include <TopoDS_Iterator.hxx>
21 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
22 ModuleBase_IWorkshop* theWorkshop,
23 const Config_WidgetAPI* theData,
24 const std::string& theParentId)
25 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId)
29 //********************************************************************
30 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
34 //********************************************************************
35 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
37 GeomShapePtr& theShape)
39 ModuleBase_ISelection* aSelection = myWorkshop->selection();
40 theObject = aSelection->getResult(thePrs);
41 theShape = aSelection->getShape(thePrs);
44 //********************************************************************
45 void ModuleBase_WidgetSelector::onSelectionChanged()
49 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
51 bool isDone = setSelection(aSelected, true/*false*/);
52 // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
53 // the same place repeatedly without mouse moved. In the case validation by filters is not
54 // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
55 // the 3rd click in the same point allow using this point.
57 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
58 // calls validators for the feature and, as a result, updates the Apply button state.
59 updateObject(myFeature);
65 //********************************************************************
66 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
67 const ResultPtr& theResult) const
71 GeomShapePtr aShape = theShape;
73 QIntList aShapeTypes = getShapeTypes();
74 if (aShapeTypes.empty()) {
78 // when the SHAPE type is provided by XML, the hole result shape can be selected.
79 if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
84 if (!aShape.get() && theResult.get()) {
86 aShape = theResult->shape();
88 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
90 // Check that the selection corresponds to selection type
91 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
92 aShapeType = aTopoShape.ShapeType();
93 // for compounds check sub-shapes: it may be compound of needed type:
94 // Booleans may produce compounds of Solids
95 if (aShapeType == TopAbs_COMPOUND) {
96 aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
100 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
101 for (; anIt != aLast; anIt++) {
102 if (aShapeType == *anIt)
104 else if (*anIt == TopAbs_FACE) {
105 // try to process the construction shape only if there is no a selected shape in the viewer
106 if (!theShape.get() && theResult.get()) {
107 ResultConstructionPtr aCResult =
108 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
109 aValid = aCResult.get() && aCResult->facesNum() > 0;
116 //********************************************************************
117 void ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
119 updateSelectionName();
122 myWorkshop->activateSubShapesSelection(getShapeTypes());
124 myWorkshop->deactivateSubShapesSelection();
126 activateFilters(toActivate);
129 //********************************************************************
130 void ModuleBase_WidgetSelector::activateCustom()
132 connect(myWorkshop, SIGNAL(selectionChanged()), this,
133 SLOT(onSelectionChanged()), Qt::UniqueConnection);
135 activateSelectionAndFilters(true);
137 // Restore selection in the viewer by the attribute selection list
138 myWorkshop->setSelected(getAttributeSelection());
141 //********************************************************************
142 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
144 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
145 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
146 bool aValid = acceptSubShape(aShape, aResult);
149 // In order to avoid selection of the same object
150 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
151 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
152 aValid = aSelectedFeature != myFeature;
157 //********************************************************************
158 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
162 getGeomSelection(thePrs, anObject, aShape);
164 setObject(anObject, aShape);
168 //********************************************************************
169 void ModuleBase_WidgetSelector::deactivate()
171 ModuleBase_ModelWidget::deactivate();
172 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
173 activateSelectionAndFilters(false);
174 ModuleBase_ModelWidget::deactivate();
177 //********************************************************************
178 std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
179 ModuleBase_IWorkshop* theWorkshop)
182 if (theAttribute.get() != NULL) {
183 ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
185 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
186 if (aFeature.get()) {
187 std::string aXmlCfg, aDescription;
188 theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
190 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
191 std::string anAttributeTitle;
192 aFactory.getAttributeTitle(aFeature->getKind(), theAttribute->id(), anAttributeTitle);
194 std::stringstream aStreamName;
195 aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
196 aName = aStreamName.str();