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