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>
17 #include <ModuleBase_ViewerPrs.h>
19 #include <ModelAPI_ResultConstruction.h>
21 #include <TopoDS_Iterator.hxx>
23 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
24 ModuleBase_IWorkshop* theWorkshop,
25 const Config_WidgetAPI* theData)
26 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
30 //********************************************************************
31 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
35 //********************************************************************
36 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
38 GeomShapePtr& theShape)
40 ModuleBase_ISelection* aSelection = myWorkshop->selection();
41 theObject = aSelection->getResult(thePrs);
42 theShape = aSelection->getShape(thePrs);
45 //********************************************************************
46 void ModuleBase_WidgetSelector::onSelectionChanged()
48 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
49 // equal vertices should not be used here
50 ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
52 bool isDone = setSelection(aSelected, true/*false*/);
53 updateOnSelectionChanged(isDone);
56 //********************************************************************
57 void ModuleBase_WidgetSelector::updateOnSelectionChanged(const bool theDone)
59 // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
60 // the same place repeatedly without mouse moved. In the case validation by filters is not
61 // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
62 // the 3rd click in the same point allow using this point.
64 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
65 // calls validators for the feature and, as a result, updates the Apply button state.
66 updateObject(myFeature);
68 // we need to forget about previous validation result as the current selection can influence on it
75 //********************************************************************
76 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetSelector::getAttributeSelection() const
78 return QList<ModuleBase_ViewerPrsPtr>();
81 //********************************************************************
82 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
83 const ResultPtr& theResult) const
87 GeomShapePtr aShape = theShape;
89 QIntList aShapeTypes = getShapeTypes();
90 if (aShapeTypes.empty()) {
94 // when the SHAPE type is provided by XML, the hole result shape can be selected.
95 if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
100 if (!aShape.get() && theResult.get()) {
102 aShape = theResult->shape();
104 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
106 // Check that the selection corresponds to selection type
107 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
108 aShapeType = aTopoShape.ShapeType();
109 // for compounds check sub-shapes: it may be compound of needed type:
110 // Booleans may produce compounds of Solids
111 if (aShapeType == TopAbs_COMPOUND) {
112 aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
116 QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
117 for (; anIt != aLast && !aValid; anIt++) {
118 TopAbs_ShapeEnum aCurrentShapeType = (TopAbs_ShapeEnum)*anIt;
119 if (aShapeType == aCurrentShapeType)
121 else if (aCurrentShapeType == TopAbs_FACE) {
122 // try to process the construction shape only if there is no a selected shape in the viewer
123 if (!theShape.get() && theResult.get()) {
124 ResultConstructionPtr aCResult =
125 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
126 aValid = aCResult.get() && aCResult->facesNum() > 0;
129 // the compared shape types are not equal but presentation might allow some type for another
130 // exactly it allow Wire type of the shape for Face XML shape type
131 aValid = ModuleBase_ResultPrs::isValidShapeType(aCurrentShapeType, aShapeType);
138 //********************************************************************
139 bool ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
141 updateSelectionName();
144 myWorkshop->activateSubShapesSelection(getShapeTypes());
146 myWorkshop->deactivateSubShapesSelection();
148 return activateFilters(toActivate);
151 //********************************************************************
152 void ModuleBase_WidgetSelector::activateCustom()
154 connect(myWorkshop, SIGNAL(selectionChanged()), this,
155 SLOT(onSelectionChanged()), Qt::UniqueConnection);
157 activateSelectionAndFilters(true);
159 // Restore selection in the viewer by the attribute selection list
160 myWorkshop->setSelected(getAttributeSelection());
163 //********************************************************************
164 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
166 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
167 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
168 bool aValid = acceptSubShape(aShape, aResult);
171 // In order to avoid selection of the same object
172 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
173 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
174 aValid = aSelectedFeature != myFeature;
179 //********************************************************************
180 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
184 getGeomSelection(thePrs, anObject, aShape);
186 // the last flag is to be depending on hasObject is called before. To be corrected later
187 ModuleBase_Tools::setObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate, true);
191 //********************************************************************
192 void ModuleBase_WidgetSelector::deactivate()
194 ModuleBase_ModelWidget::deactivate();
195 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
196 activateSelectionAndFilters(false);
197 ModuleBase_ModelWidget::deactivate();
200 //********************************************************************
201 std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
202 ModuleBase_IWorkshop* theWorkshop)
205 if (theAttribute.get() != NULL) {
206 ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
208 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
209 if (aFeature.get()) {
210 std::string aXmlCfg, aDescription;
211 theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
213 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
214 std::string anAttributeTitle;
215 aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
217 std::stringstream aStreamName;
218 aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
219 aName = aStreamName.str();