Salome HOME
d4ed903ec4b882de9328a8118efc852085ad1854
[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
12 #include <ModelAPI_ResultConstruction.h>
13
14 #include <GeomValidators_Tools.h>
15
16 #include <TopoDS_Iterator.hxx>
17
18 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
19                                                      ModuleBase_IWorkshop* theWorkshop,
20                                                      const Config_WidgetAPI* theData,
21                                                      const std::string& theParentId)
22  : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId)
23 {
24 }
25
26 //********************************************************************
27 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
28 {
29 }
30
31 //********************************************************************
32 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
33                                                       ObjectPtr& theObject,
34                                                       GeomShapePtr& theShape)
35 {
36   ModuleBase_ISelection* aSelection = myWorkshop->selection();
37   theObject = aSelection->getResult(thePrs);
38   theShape = aSelection->getShape(thePrs);
39 }
40
41 //********************************************************************
42 void ModuleBase_WidgetSelector::onSelectionChanged()
43 {
44   clearAttribute();
45
46   QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
47
48   bool isDone = setSelection(aSelected, true/*false*/);
49   // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in 
50   // the same place repeatedly without mouse moved. In the case validation by filters is not
51   // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
52   // the 3rd click in the same point allow using this point.
53   emit valuesChanged();
54   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
55   // calls validators for the feature and, as a result, updates the Apply button state.
56   updateObject(myFeature);
57
58   if (isDone)
59     updateFocus();
60 }
61
62 //********************************************************************
63 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
64                                                const ResultPtr& theResult) const
65 {
66   bool aValid = false;
67
68   GeomShapePtr aShape = theShape;
69
70   QIntList aShapeTypes = getShapeTypes();
71   if (aShapeTypes.empty()) {
72     aValid = true;
73     return aValid;
74   }
75   // when the SHAPE type is provided by XML, the hole result shape can be selected.
76   if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
77     aValid = true;
78     return aValid;
79   }
80
81   if (!aShape.get() && theResult.get()) {
82     if (theResult.get())
83       aShape = theResult->shape();
84   }
85   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
86   if (aShape.get()) {
87     // Check that the selection corresponds to selection type
88     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
89     aShapeType = aTopoShape.ShapeType();
90     // for compounds check sub-shapes: it may be compound of needed type:
91     // Booleans may produce compounds of Solids
92     if (aShapeType == TopAbs_COMPOUND) {
93       aShapeType = GeomValidators_Tools::getCompoundSubType(aTopoShape);
94     }
95   }
96
97   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
98   for (; anIt != aLast; anIt++) {
99     if (aShapeType == *anIt)
100       aValid = true;
101     else if (*anIt == TopAbs_FACE) {
102       // try to process the construction shape only if there is no a selected shape in the viewer
103       if (!theShape.get() && theResult.get()) {
104         ResultConstructionPtr aCResult =
105                                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
106         aValid = aCResult.get() && aCResult->facesNum() > 0;
107       }
108     }
109   }
110   return aValid;
111 }
112
113 //********************************************************************
114 void ModuleBase_WidgetSelector::activateSelectionAndFilters(bool toActivate)
115 {
116   updateSelectionName();
117
118   if (toActivate) {
119     myWorkshop->activateSubShapesSelection(getShapeTypes());
120   } else {
121     myWorkshop->deactivateSubShapesSelection();
122   }
123   activateFilters(toActivate);
124 }
125
126 //********************************************************************
127 void ModuleBase_WidgetSelector::activateCustom()
128 {
129   connect(myWorkshop, SIGNAL(selectionChanged()), this,
130           SLOT(onSelectionChanged()), Qt::UniqueConnection);
131   
132   activateSelectionAndFilters(true);
133
134   // Restore selection in the viewer by the attribute selection list
135   myWorkshop->setSelected(getAttributeSelection());
136 }
137
138 //********************************************************************
139 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
140 {
141   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
142   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
143   bool aValid = acceptSubShape(aShape, aResult);
144
145   if (aValid) {
146     // In order to avoid selection of the same object
147     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
148     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
149     aValid = aSelectedFeature != myFeature;
150   }
151   return aValid;
152 }
153
154 //********************************************************************
155 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
156 {
157   ObjectPtr anObject;
158   GeomShapePtr aShape;
159   getGeomSelection(thePrs, anObject, aShape);
160
161   setObject(anObject, aShape);
162   return true;
163 }
164
165 //********************************************************************
166 void ModuleBase_WidgetSelector::deactivate()
167 {
168   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
169   activateSelectionAndFilters(false);
170   ModuleBase_ModelWidget::deactivate();
171 }
172