Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelector.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ModuleBase_WidgetSelector.h>
22
23 #include <ModuleBase_ISelection.h>
24 #include <ModuleBase_IWorkshop.h>
25 #include <ModuleBase_Tools.h>
26 #include <ModuleBase_Operation.h>
27 #include <ModuleBase_OperationDescription.h>
28 #include <ModuleBase_WidgetFactory.h>
29 #include <ModuleBase_IModule.h>
30 #include <ModuleBase_ResultPrs.h>
31 #include <ModuleBase_ViewerPrs.h>
32 #include <ModuleBase_Events.h>
33
34 #include <ModelAPI_ResultConstruction.h>
35 #include <ModelAPI_Events.h>
36 #include <ModelAPI_AttributeSelection.h>
37 #include <ModelAPI_AttributeSelectionList.h>
38
39 #include <TopoDS_Iterator.hxx>
40
41 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
42                                                      ModuleBase_IWorkshop* theWorkshop,
43                                                      const Config_WidgetAPI* theData)
44 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
45 {
46 }
47
48 //********************************************************************
49 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
50 {
51 }
52
53 //********************************************************************
54 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrsPtr& thePrs,
55                                                       ObjectPtr& theObject,
56                                                       GeomShapePtr& theShape)
57 {
58   ModuleBase_ISelection* aSelection = myWorkshop->selection();
59   theObject = aSelection->getResult(thePrs);
60   theShape = aSelection->getShape(thePrs);
61 }
62
63 //********************************************************************
64 void ModuleBase_WidgetSelector::onSelectionChanged()
65 {
66   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
67   // equal vertices should not be used here
68   ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
69
70   bool isDone = setSelection(aSelected, true/*false*/);
71   updateOnSelectionChanged(isDone);
72 }
73
74 //********************************************************************
75 void ModuleBase_WidgetSelector::updateOnSelectionChanged(const bool theDone)
76 {
77   // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
78   // the same place repeatedly without mouse moved. In the case validation by filters is not
79   // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
80   // the 3rd click in the same point allow using this point.
81   emit valuesChanged();
82   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
83   // calls validators for the feature and, as a result, updates the Apply button state.
84   updateObject(myFeature);
85
86   // we need to forget about previous validation result as the current selection can influence on it
87   clearValidatedCash();
88
89   if (theDone)
90     updateFocus();
91 }
92
93 //********************************************************************
94 QIntList ModuleBase_WidgetSelector::getShapeTypes() const
95 {
96   QIntList aShapeTypes = shapeTypes();
97   // this type should be mentioned in XML, poor selection otherwise
98   if (/*aShapeTypes.contains(TopAbs_SOLID) ||*/
99       aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result/*TopAbs_SHAPE*/)) {
100     // it should be selectable for both, "solids" and "objects" types
101     aShapeTypes.append(TopAbs_COMPSOLID);
102   }
103   return aShapeTypes;
104 }
105
106 //********************************************************************
107 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetSelector::getAttributeSelection() const
108 {
109   return QList<ModuleBase_ViewerPrsPtr>();
110 }
111
112 //********************************************************************
113 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
114                                                const ResultPtr& theResult) const
115 {
116   bool aValid = false;
117
118   GeomShapePtr aShape = theShape;
119
120   QIntList aShapeTypes = getShapeTypes();
121   if (aShapeTypes.empty()) {
122     aValid = true;
123     return aValid;
124   }
125   // when the SHAPE type is provided by XML as Object, the whole result shape should be selected.
126   if (!aShape.get() && aShapeTypes.contains(ModuleBase_ResultPrs::Sel_Result)) {
127     aValid = true;
128     return aValid;
129   }
130
131   if (!aShape.get() && theResult.get()) {
132     if (theResult.get())
133       aShape = theResult->shape();
134   }
135   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
136   if (aShape.get()) {
137     // Check that the selection corresponds to selection type
138     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
139     aShapeType = aTopoShape.ShapeType();
140     // for compounds check sub-shapes: it may be compound of needed type:
141     // Booleans may produce compounds of Solids
142     if (aShapeType == TopAbs_COMPOUND) {
143       aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
144     }
145   }
146
147   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
148   for (; anIt != aLast && !aValid; anIt++) {
149     TopAbs_ShapeEnum aCurrentShapeType = (TopAbs_ShapeEnum)*anIt;
150     if (aShapeType == aCurrentShapeType)
151       aValid = true;
152     else if (aCurrentShapeType == TopAbs_FACE) {
153       // try to process the construction shape only if there is no a selected shape in the viewer
154       if (!theShape.get() && theResult.get()) {
155         ResultConstructionPtr aCResult =
156                                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
157         aValid = aCResult.get() && aCResult->facesNum() > 0;
158       }
159     }
160   }
161   return aValid;
162 }
163
164 //********************************************************************
165 bool ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
166 {
167   updateSelectionName();
168
169   if (toActivate) {
170     myWorkshop->activateSubShapesSelection(getShapeTypes());
171   } else {
172     myWorkshop->deactivateSubShapesSelection();
173   }
174   return activateFilters(toActivate);
175 }
176
177 //********************************************************************
178 void ModuleBase_WidgetSelector::activateCustom()
179 {
180   connect(myWorkshop, SIGNAL(selectionChanged()), this,
181           SLOT(onSelectionChanged()), Qt::UniqueConnection);
182
183   activateSelectionAndFilters(true);
184
185   // Restore selection in the viewer by the attribute selection list
186   // it should be postponed to have current widget as active to validate restored selection
187   static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
188   ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
189 }
190
191 //********************************************************************
192 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
193 {
194   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
195   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
196   bool aValid = acceptSubShape(aShape, aResult);
197
198   if (aValid) {
199     // In order to avoid selection of the same object
200     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
201     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
202     aValid = aSelectedFeature != myFeature;
203   }
204   return aValid;
205 }
206
207 //********************************************************************
208 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
209 {
210   ObjectPtr anObject;
211   GeomShapePtr aShape;
212   getGeomSelection(thePrs, anObject, aShape);
213
214   // the last flag is to be depending on hasObject is called before. To be corrected later
215   return ModuleBase_Tools::setObject(attribute(), anObject, aShape,
216                                      myWorkshop, myIsInValidate, true);
217 }
218
219 //********************************************************************
220 void ModuleBase_WidgetSelector::deactivate()
221 {
222   ModuleBase_ModelWidget::deactivate();
223   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
224   activateSelectionAndFilters(false);
225
226   /// clear temporary cash
227   AttributePtr anAttribute = attribute();
228   if (!anAttribute.get())
229     return;
230   std::string aType = anAttribute->attributeType();
231   if (anAttribute->attributeType() == ModelAPI_AttributeSelection::typeId()) {
232     AttributeSelectionPtr aSelectAttr =
233                              std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttribute);
234     aSelectAttr->removeTemporaryValues();
235   }
236   else if (anAttribute->attributeType() == ModelAPI_AttributeSelectionList::typeId()) {
237     AttributeSelectionListPtr aSelectAttr =
238                       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(anAttribute);
239     aSelectAttr->removeTemporaryValues();
240   }
241 }
242
243 //********************************************************************
244 std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
245                                                     ModuleBase_IWorkshop* theWorkshop)
246 {
247   std::string aName;
248   if (theAttribute.get() != NULL) {
249     ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
250
251     FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
252     if (aFeature.get()) {
253       std::string aXmlCfg, aDescription;
254       theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
255
256       ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
257       std::string anAttributeTitle;
258       aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
259
260       std::stringstream aStreamName;
261       aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
262       aName = aStreamName.str();
263     }
264   }
265   return aName;
266 }