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>
16 #include <ModuleBase_ResultPrs.h>
18 #include <ModelAPI_ResultConstruction.h>
20 #include <TopoDS_Iterator.hxx>
22 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
23 ModuleBase_IWorkshop* theWorkshop,
24 const Config_WidgetAPI* theData)
25 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
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()
47 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
48 bool isDone = setSelection(aSelected, true/*false*/);
51 updateOnSelectionChanged(isDone);
54 //********************************************************************
55 void ModuleBase_WidgetSelector::updateOnSelectionChanged(const bool theDone)
57 // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
58 // the same place repeatedly without mouse moved. In the case validation by filters is not
59 // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
60 // the 3rd click in the same point allow using this point.
62 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
63 // calls validators for the feature and, as a result, updates the Apply button state.
64 updateObject(myFeature);
66 // we need to forget about previous validation result as the current selection can influence on it
73 //********************************************************************
74 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetSelector::getAttributeSelection() const
76 return QList<ModuleBase_ViewerPrs>();
79 //********************************************************************
80 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
81 const ResultPtr& theResult) const
85 GeomShapePtr aShape = theShape;
87 QIntList aShapeTypes = getShapeTypes();
88 if (aShapeTypes.empty()) {
92 // when the SHAPE type is provided by XML, the hole result shape can be selected.
93 if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
98 if (!aShape.get() && theResult.get()) {
100 aShape = theResult->shape();
102 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
104 // Check that the selection corresponds to selection type
105 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
106 aShapeType = aTopoShape.ShapeType();
107 // for compounds check sub-shapes: it may be compound of needed type:
108 // Booleans may produce compounds of Solids
109 if (aShapeType == TopAbs_COMPOUND) {
110 aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
114 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
115 for (; anIt != aLast; anIt++) {
116 TopAbs_ShapeEnum aCurrentShapeType = (TopAbs_ShapeEnum)*anIt;
117 if (aShapeType == aCurrentShapeType)
119 else if (aCurrentShapeType == TopAbs_FACE) {
120 // try to process the construction shape only if there is no a selected shape in the viewer
121 if (!theShape.get() && theResult.get()) {
122 ResultConstructionPtr aCResult =
123 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
124 aValid = aCResult.get() && aCResult->facesNum() > 0;
126 aValid = ModuleBase_ResultPrs::isValidShapeType(aCurrentShapeType, aShapeType);
132 //********************************************************************
133 bool ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
135 updateSelectionName();
138 myWorkshop->activateSubShapesSelection(getShapeTypes());
140 myWorkshop->deactivateSubShapesSelection();
142 return activateFilters(toActivate);
145 //********************************************************************
146 void ModuleBase_WidgetSelector::setObject(ObjectPtr theObject,
147 GeomShapePtr theShape)
149 DataPtr aData = myFeature->data();
150 ModuleBase_Tools::setObject(aData->attribute(attributeID()), theObject, theShape,
151 myWorkshop, myIsInValidate);
154 //********************************************************************
155 void ModuleBase_WidgetSelector::activateCustom()
157 connect(myWorkshop, SIGNAL(selectionChanged()), this,
158 SLOT(onSelectionChanged()), Qt::UniqueConnection);
160 activateSelectionAndFilters(true);
162 // Restore selection in the viewer by the attribute selection list
163 myWorkshop->setSelected(getAttributeSelection());
166 //********************************************************************
167 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
169 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
170 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
171 bool aValid = acceptSubShape(aShape, aResult);
174 // In order to avoid selection of the same object
175 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
176 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
177 aValid = aSelectedFeature != myFeature;
182 //********************************************************************
183 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
187 getGeomSelection(thePrs, anObject, aShape);
189 setObject(anObject, aShape);
193 //********************************************************************
194 void ModuleBase_WidgetSelector::deactivate()
196 ModuleBase_ModelWidget::deactivate();
197 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
198 activateSelectionAndFilters(false);
199 ModuleBase_ModelWidget::deactivate();
202 //********************************************************************
203 std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
204 ModuleBase_IWorkshop* theWorkshop)
207 if (theAttribute.get() != NULL) {
208 ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
210 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
211 if (aFeature.get()) {
212 std::string aXmlCfg, aDescription;
213 theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
215 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
216 std::string anAttributeTitle;
217 aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
219 std::stringstream aStreamName;
220 aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
221 aName = aStreamName.str();