Salome HOME
Issue #1508 Selection mode "objects"
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetSelector.cpp
4 // Created:     19 June 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include <ModuleBase_WidgetSelector.h>
8
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>
18
19 #include <ModelAPI_ResultConstruction.h>
20
21 #include <TopoDS_Iterator.hxx>
22
23 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
24                                                      ModuleBase_IWorkshop* theWorkshop,
25                                                      const Config_WidgetAPI* theData)
26 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
27 {
28 }
29
30 //********************************************************************
31 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
32 {
33 }
34
35 //********************************************************************
36 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
37                                                       ObjectPtr& theObject,
38                                                       GeomShapePtr& theShape)
39 {
40   ModuleBase_ISelection* aSelection = myWorkshop->selection();
41   theObject = aSelection->getResult(thePrs);
42   theShape = aSelection->getShape(thePrs);
43 }
44
45 //********************************************************************
46 void ModuleBase_WidgetSelector::onSelectionChanged()
47 {
48   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
49   // equal vertices should not be used here
50   ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
51
52   bool isDone = setSelection(aSelected, true/*false*/);
53   updateOnSelectionChanged(isDone);
54 }
55
56 //********************************************************************
57 void ModuleBase_WidgetSelector::updateOnSelectionChanged(const bool theDone)
58 {
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.
63   emit valuesChanged();
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);
67
68   // we need to forget about previous validation result as the current selection can influence on it
69   clearValidatedCash();
70
71   if (theDone)
72     updateFocus();
73 }
74
75 //********************************************************************
76 QIntList ModuleBase_WidgetSelector::getShapeTypes() const
77 {
78   QIntList aShapeTypes = shapeTypes();
79   if (aShapeTypes.contains(TopAbs_SOLID) || aShapeTypes.contains(TopAbs_SHAPE)) {
80     // it should be selectable for both, "solids" and "objects" types
81     aShapeTypes.append(TopAbs_COMPSOLID);
82   }
83   return aShapeTypes;
84 }
85
86 //********************************************************************
87 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetSelector::getAttributeSelection() const
88 {
89   return QList<ModuleBase_ViewerPrsPtr>();
90 }
91
92 //********************************************************************
93 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
94                                                const ResultPtr& theResult) const
95 {
96   bool aValid = false;
97
98   GeomShapePtr aShape = theShape;
99
100   QIntList aShapeTypes = getShapeTypes();
101   if (aShapeTypes.empty()) {
102     aValid = true;
103     return aValid;
104   }
105   // when the SHAPE type is provided by XML, the hole result shape can be selected.
106   if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
107     aValid = true;
108     return aValid;
109   }
110
111   if (!aShape.get() && theResult.get()) {
112     if (theResult.get())
113       aShape = theResult->shape();
114   }
115   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
116   if (aShape.get()) {
117     // Check that the selection corresponds to selection type
118     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
119     aShapeType = aTopoShape.ShapeType();
120     // for compounds check sub-shapes: it may be compound of needed type:
121     // Booleans may produce compounds of Solids
122     if (aShapeType == TopAbs_COMPOUND) {
123       aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
124     }
125   }
126
127   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
128   for (; anIt != aLast && !aValid; anIt++) {
129     TopAbs_ShapeEnum aCurrentShapeType = (TopAbs_ShapeEnum)*anIt;
130     if (aShapeType == aCurrentShapeType)
131       aValid = true;
132     else if (aCurrentShapeType == TopAbs_FACE) {
133       // try to process the construction shape only if there is no a selected shape in the viewer
134       if (!theShape.get() && theResult.get()) {
135         ResultConstructionPtr aCResult =
136                                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
137         aValid = aCResult.get() && aCResult->facesNum() > 0;
138       }
139       if (!aValid) {
140         // the compared shape types are not equal but presentation might allow some type for another
141         // exactly it allow Wire type of the shape for Face XML shape type
142         aValid = ModuleBase_ResultPrs::isValidShapeType(aCurrentShapeType, aShapeType);
143       }
144     }
145   }
146   return aValid;
147 }
148
149 //********************************************************************
150 bool ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
151 {
152   updateSelectionName();
153
154   if (toActivate) {
155     myWorkshop->activateSubShapesSelection(getShapeTypes());
156   } else {
157     myWorkshop->deactivateSubShapesSelection();
158   }
159   return activateFilters(toActivate);
160 }
161
162 //********************************************************************
163 void ModuleBase_WidgetSelector::activateCustom()
164 {
165   connect(myWorkshop, SIGNAL(selectionChanged()), this,
166           SLOT(onSelectionChanged()), Qt::UniqueConnection);
167   
168   activateSelectionAndFilters(true);
169
170   // Restore selection in the viewer by the attribute selection list
171   myWorkshop->setSelected(getAttributeSelection());
172 }
173
174 //********************************************************************
175 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
176 {
177   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
178   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
179   bool aValid = acceptSubShape(aShape, aResult);
180
181   if (aValid) {
182     // In order to avoid selection of the same object
183     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
184     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
185     aValid = aSelectedFeature != myFeature;
186   }
187   return aValid;
188 }
189
190 //********************************************************************
191 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
192 {
193   ObjectPtr anObject;
194   GeomShapePtr aShape;
195   getGeomSelection(thePrs, anObject, aShape);
196
197   // the last flag is to be depending on hasObject is called before. To be corrected later
198   ModuleBase_Tools::setObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate, true);
199   return true;
200 }
201
202 //********************************************************************
203 void ModuleBase_WidgetSelector::deactivate()
204 {
205   ModuleBase_ModelWidget::deactivate();
206   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
207   activateSelectionAndFilters(false);
208   ModuleBase_ModelWidget::deactivate();
209 }
210
211 //********************************************************************
212 std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
213                                                     ModuleBase_IWorkshop* theWorkshop)
214 {
215   std::string aName;
216   if (theAttribute.get() != NULL) {
217     ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
218
219     FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
220     if (aFeature.get()) {
221       std::string aXmlCfg, aDescription;
222       theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
223
224       ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
225       std::string anAttributeTitle;
226       aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
227
228       std::stringstream aStreamName;
229       aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
230       aName = aStreamName.str();
231     }
232   }
233   return aName;
234 }