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