]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetSelector.cpp
Salome HOME
e07b726b22d17ab17f93955932b6445288e736f8
[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
49   bool isDone = setSelection(aSelected, true/*false*/);
50   // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in 
51   // the same place repeatedly without mouse moved. In the case validation by filters is not
52   // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
53   // the 3rd click in the same point allow using this point.
54   emit valuesChanged();
55   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
56   // calls validators for the feature and, as a result, updates the Apply button state.
57   updateObject(myFeature);
58
59   // we need to forget about previous validation result as the current selection can influence on it
60   clearValidatedCash();
61
62   if (isDone)
63     updateFocus();
64 }
65
66 //********************************************************************
67 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetSelector::getAttributeSelection() const
68 {
69   return QList<ModuleBase_ViewerPrs>();
70 }
71
72 //********************************************************************
73 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
74                                                const ResultPtr& theResult) const
75 {
76   bool aValid = false;
77
78   GeomShapePtr aShape = theShape;
79
80   QIntList aShapeTypes = getShapeTypes();
81   if (aShapeTypes.empty()) {
82     aValid = true;
83     return aValid;
84   }
85   // when the SHAPE type is provided by XML, the hole result shape can be selected.
86   if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
87     aValid = true;
88     return aValid;
89   }
90
91   if (!aShape.get() && theResult.get()) {
92     if (theResult.get())
93       aShape = theResult->shape();
94   }
95   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
96   if (aShape.get()) {
97     // Check that the selection corresponds to selection type
98     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
99     aShapeType = aTopoShape.ShapeType();
100     // for compounds check sub-shapes: it may be compound of needed type:
101     // Booleans may produce compounds of Solids
102     if (aShapeType == TopAbs_COMPOUND) {
103       aShapeType = ModuleBase_Tools::getCompoundSubType(aTopoShape);
104     }
105   }
106
107   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
108   for (; anIt != aLast; anIt++) {
109     TopAbs_ShapeEnum aCurrentShapeType = (TopAbs_ShapeEnum)*anIt;
110     if (aShapeType == aCurrentShapeType)
111       aValid = true;
112     else if (aCurrentShapeType == TopAbs_FACE) {
113       // try to process the construction shape only if there is no a selected shape in the viewer
114       if (!theShape.get() && theResult.get()) {
115         ResultConstructionPtr aCResult =
116                                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
117         aValid = aCResult.get() && aCResult->facesNum() > 0;
118       }
119       aValid = ModuleBase_ResultPrs::isValidShapeType(aCurrentShapeType, aShapeType);
120     }
121   }
122   return aValid;
123 }
124
125 //********************************************************************
126 bool ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
127 {
128   updateSelectionName();
129
130   if (toActivate) {
131     myWorkshop->activateSubShapesSelection(getShapeTypes());
132   } else {
133     myWorkshop->deactivateSubShapesSelection();
134   }
135   return activateFilters(toActivate);
136 }
137
138 //********************************************************************
139 void ModuleBase_WidgetSelector::activateCustom()
140 {
141   connect(myWorkshop, SIGNAL(selectionChanged()), this,
142           SLOT(onSelectionChanged()), Qt::UniqueConnection);
143   
144   activateSelectionAndFilters(true);
145
146   // Restore selection in the viewer by the attribute selection list
147   myWorkshop->setSelected(getAttributeSelection());
148 }
149
150 //********************************************************************
151 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
152 {
153   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
154   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
155   bool aValid = acceptSubShape(aShape, aResult);
156
157   if (aValid) {
158     // In order to avoid selection of the same object
159     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
160     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
161     aValid = aSelectedFeature != myFeature;
162   }
163   return aValid;
164 }
165
166 //********************************************************************
167 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
168 {
169   ObjectPtr anObject;
170   GeomShapePtr aShape;
171   getGeomSelection(thePrs, anObject, aShape);
172
173   setObject(anObject, aShape);
174   return true;
175 }
176
177 //********************************************************************
178 void ModuleBase_WidgetSelector::deactivate()
179 {
180   ModuleBase_ModelWidget::deactivate();
181   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
182   activateSelectionAndFilters(false);
183   ModuleBase_ModelWidget::deactivate();
184 }
185
186 //********************************************************************
187 std::string ModuleBase_WidgetSelector::generateName(const AttributePtr& theAttribute,
188                                                     ModuleBase_IWorkshop* theWorkshop)
189 {
190   std::string aName;
191   if (theAttribute.get() != NULL) {
192     ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
193
194     FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
195     if (aFeature.get()) {
196       std::string aXmlCfg, aDescription;
197       theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
198
199       ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
200       std::string anAttributeTitle;
201       aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
202
203       std::stringstream aStreamName;
204       aStreamName << theAttribute->owner()->data()->name() << "/"<< anAttributeTitle.c_str();
205       aName = aStreamName.str();
206     }
207   }
208   return aName;
209 }