Salome HOME
9d5b50a0ad9884363fa7960114d953c8cfc85b00
[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
18 #include <ModelAPI_ResultConstruction.h>
19
20 #include <TopoDS_Iterator.hxx>
21
22 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
23                                                      ModuleBase_IWorkshop* theWorkshop,
24                                                      const Config_WidgetAPI* theData)
25 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
26 {
27 }
28
29 //********************************************************************
30 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
31 {
32 }
33
34 //********************************************************************
35 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
36                                                       ObjectPtr& theObject,
37                                                       GeomShapePtr& theShape)
38 {
39   ModuleBase_ISelection* aSelection = myWorkshop->selection();
40   theObject = aSelection->getResult(thePrs);
41   theShape = aSelection->getShape(thePrs);
42 }
43
44 //********************************************************************
45 void ModuleBase_WidgetSelector::onSelectionChanged()
46 {
47   QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
48   bool isDone = setSelection(aSelected, true/*false*/);
49
50   if (isDone)
51    updateOnSelectionChanged(isDone);
52 }
53
54 //********************************************************************
55 void ModuleBase_WidgetSelector::updateOnSelectionChanged(const bool theDone)
56 {
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.
61   emit valuesChanged();
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);
65
66   // we need to forget about previous validation result as the current selection can influence on it
67   clearValidatedCash();
68
69   if (theDone)
70     updateFocus();
71 }
72
73 //********************************************************************
74 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetSelector::getAttributeSelection() const
75 {
76   return QList<ModuleBase_ViewerPrs>();
77 }
78
79 //********************************************************************
80 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
81                                                const ResultPtr& theResult) const
82 {
83   bool aValid = false;
84
85   GeomShapePtr aShape = theShape;
86
87   QIntList aShapeTypes = getShapeTypes();
88   if (aShapeTypes.empty()) {
89     aValid = true;
90     return aValid;
91   }
92   // when the SHAPE type is provided by XML, the hole result shape can be selected.
93   if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
94     aValid = true;
95     return aValid;
96   }
97
98   if (!aShape.get() && theResult.get()) {
99     if (theResult.get())
100       aShape = theResult->shape();
101   }
102   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
103   if (aShape.get()) {
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);
111     }
112   }
113
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)
118       aValid = true;
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;
125       }
126       aValid = ModuleBase_ResultPrs::isValidShapeType(aCurrentShapeType, aShapeType);
127     }
128   }
129   return aValid;
130 }
131
132 //********************************************************************
133 bool ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
134 {
135   updateSelectionName();
136
137   if (toActivate) {
138     myWorkshop->activateSubShapesSelection(getShapeTypes());
139   } else {
140     myWorkshop->deactivateSubShapesSelection();
141   }
142   return activateFilters(toActivate);
143 }
144
145 //********************************************************************
146 void ModuleBase_WidgetSelector::setObject(ObjectPtr theObject,
147                                           GeomShapePtr theShape)
148 {
149   DataPtr aData = myFeature->data();
150   ModuleBase_Tools::setObject(aData->attribute(attributeID()), theObject, theShape,
151                               myWorkshop, myIsInValidate);
152 }
153
154 //********************************************************************
155 void ModuleBase_WidgetSelector::activateCustom()
156 {
157   connect(myWorkshop, SIGNAL(selectionChanged()), this,
158           SLOT(onSelectionChanged()), Qt::UniqueConnection);
159   
160   activateSelectionAndFilters(true);
161
162   // Restore selection in the viewer by the attribute selection list
163   myWorkshop->setSelected(getAttributeSelection());
164 }
165
166 //********************************************************************
167 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
168 {
169   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
170   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
171   bool aValid = acceptSubShape(aShape, aResult);
172
173   if (aValid) {
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;
178   }
179   return aValid;
180 }
181
182 //********************************************************************
183 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
184 {
185   ObjectPtr anObject;
186   GeomShapePtr aShape;
187   getGeomSelection(thePrs, anObject, aShape);
188
189   setObject(anObject, aShape);
190   return true;
191 }
192
193 //********************************************************************
194 void ModuleBase_WidgetSelector::deactivate()
195 {
196   ModuleBase_ModelWidget::deactivate();
197   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
198   activateSelectionAndFilters(false);
199   ModuleBase_ModelWidget::deactivate();
200 }
201
202 //********************************************************************
203 std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
204                                                     ModuleBase_IWorkshop* theWorkshop)
205 {
206   std::string aName;
207   if (theAttribute.get() != NULL) {
208     ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
209
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);
214
215       ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
216       std::string anAttributeTitle;
217       aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
218
219       std::stringstream aStreamName;
220       aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
221       aName = aStreamName.str();
222     }
223   }
224   return aName;
225 }