]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetSelector.cpp
Salome HOME
Issue #811 Be able to export the whole part
[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, false);
49   emit valuesChanged();
50   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
51   // calls validators for the feature and, as a result, updates the Apply button state.
52   updateObject(myFeature);
53
54   if (isDone)
55     updateFocus();
56 }
57
58 //********************************************************************
59 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
60                                                const ResultPtr& theResult) const
61 {
62   bool aValid = false;
63
64   GeomShapePtr aShape = theShape;
65
66   QIntList aShapeTypes = getShapeTypes();
67   // when the SHAPE type is provided by XML, the hole result shape can be selected.
68   if (!aShape.get() && aShapeTypes.contains(TopAbs_SHAPE)) {
69     aValid = true;
70     return aValid;
71   }
72
73   if (!aShape.get() && theResult.get()) {
74     if (theResult.get())
75       aShape = theResult->shape();
76   }
77   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
78   if (aShape.get()) {
79     // Check that the selection corresponds to selection type
80     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
81     aShapeType = aTopoShape.ShapeType();
82     // for compounds check sub-shapes: it may be compound of needed type:
83     // Booleans may produce compounds of Solids
84     if (aShapeType == TopAbs_COMPOUND) {
85       aShapeType = GeomValidators_Tools::getCompoundSubType(aTopoShape);
86     }
87   }
88
89   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
90   for (; anIt != aLast; anIt++) {
91     if (aShapeType == *anIt)
92       aValid = true;
93     else if (*anIt == TopAbs_FACE) {
94       // try to process the construction shape only if there is no a selected shape in the viewer
95       if (!theShape.get() && theResult.get()) {
96         ResultConstructionPtr aCResult =
97                                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
98         aValid = aCResult.get() && aCResult->facesNum() > 0;
99       }
100     }
101   }
102   return aValid;
103 }
104
105 //********************************************************************
106 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
107 {
108   updateSelectionName();
109
110   if (toActivate) {
111     myWorkshop->activateSubShapesSelection(getShapeTypes());
112   } else {
113     myWorkshop->deactivateSubShapesSelection();
114   }
115 }
116
117 //********************************************************************
118 void ModuleBase_WidgetSelector::activateCustom()
119 {
120   connect(myWorkshop, SIGNAL(selectionChanged()), this,
121           SLOT(onSelectionChanged()), Qt::UniqueConnection);
122   
123   activateSelection(true);
124
125   // Restore selection in the viewer by the attribute selection list
126   myWorkshop->setSelected(getAttributeSelection());
127
128   activateFilters(true);
129 }
130
131 //********************************************************************
132 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
133 {
134   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
135   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
136   bool aValid = acceptSubShape(aShape, aResult);
137
138   if (aValid) {
139     // In order to avoid selection of the same object
140     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
141     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
142     aValid = aSelectedFeature != myFeature;
143   }
144   return aValid;
145 }
146
147 //********************************************************************
148 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
149 {
150   ObjectPtr anObject;
151   GeomShapePtr aShape;
152   getGeomSelection(thePrs, anObject, aShape);
153
154   setObject(anObject, aShape);
155   return true;
156 }
157
158 //********************************************************************
159 void ModuleBase_WidgetSelector::deactivate()
160 {
161   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
162   activateSelection(false);
163   activateFilters(false);
164 }
165