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 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
28 //********************************************************************
29 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
33 //********************************************************************
34 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
36 GeomShapePtr& theShape)
38 ModuleBase_ISelection* aSelection = myWorkshop->selection();
39 theObject = aSelection->getResult(thePrs);
40 theShape = aSelection->getShape(thePrs);
43 //********************************************************************
44 void ModuleBase_WidgetSelector::onSelectionChanged()
46 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
48 bool isDone = setSelection(aSelected, true/*false*/);
49 // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
50 // the same place repeatedly without mouse moved. In the case validation by filters is not
51 // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
52 // the 3rd click in the same point allow using this point.
54 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
55 // calls validators for the feature and, as a result, updates the Apply button state.
56 updateObject(myFeature);
58 // we need to forget about previous validation result as the current selection can influence on it
65 //********************************************************************
66 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetSelector::getAttributeSelection() const
68 return QList<ModuleBase_ViewerPrs>();
71 //********************************************************************
72 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
73 const ResultPtr& theResult) const
77 GeomShapePtr aShape = theShape;
79 QIntList aShapeTypes = getShapeTypes();
80 if (aShapeTypes.empty()) {
84 // when the SHAPE type is provided by XML, the hole result shape can be selected.
85 if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
90 if (!aShape.get() && theResult.get()) {
92 aShape = theResult->shape();
94 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
96 // Check that the selection corresponds to selection type
97 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
98 aShapeType = aTopoShape.ShapeType();
99 // for compounds check sub-shapes: it may be compound of needed type:
100 // Booleans may produce compounds of Solids
101 if (aShapeType == TopAbs_COMPOUND) {
102 aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
106 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
107 for (; anIt != aLast; anIt++) {
108 if (aShapeType == *anIt)
110 else if (*anIt == TopAbs_FACE) {
111 // try to process the construction shape only if there is no a selected shape in the viewer
112 if (!theShape.get() && theResult.get()) {
113 ResultConstructionPtr aCResult =
114 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
115 aValid = aCResult.get() && aCResult->facesNum() > 0;
122 //********************************************************************
123 bool ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
125 updateSelectionName();
128 myWorkshop->activateSubShapesSelection(getShapeTypes());
130 myWorkshop->deactivateSubShapesSelection();
132 return activateFilters(toActivate);
135 //********************************************************************
136 void ModuleBase_WidgetSelector::activateCustom()
138 connect(myWorkshop, SIGNAL(selectionChanged()), this,
139 SLOT(onSelectionChanged()), Qt::UniqueConnection);
141 activateSelectionAndFilters(true);
143 // Restore selection in the viewer by the attribute selection list
144 myWorkshop->setSelected(getAttributeSelection());
147 //********************************************************************
148 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
150 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
151 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
152 bool aValid = acceptSubShape(aShape, aResult);
155 // In order to avoid selection of the same object
156 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
157 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
158 aValid = aSelectedFeature != myFeature;
163 //********************************************************************
164 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
168 getGeomSelection(thePrs, anObject, aShape);
170 setObject(anObject, aShape);
174 //********************************************************************
175 void ModuleBase_WidgetSelector::deactivate()
177 ModuleBase_ModelWidget::deactivate();
178 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
179 activateSelectionAndFilters(false);
180 ModuleBase_ModelWidget::deactivate();
183 //********************************************************************
184 std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
185 ModuleBase_IWorkshop* theWorkshop)
188 if (theAttribute.get() != NULL) {
189 ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
191 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
192 if (aFeature.get()) {
193 std::string aXmlCfg, aDescription;
194 theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
196 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
197 std::string anAttributeTitle;
198 aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
200 std::stringstream aStreamName;
201 aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
202 aName = aStreamName.str();