]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetSelector.cpp
Salome HOME
e8acbcbfc750022fbd07bed0cbf5a7850bb42f05
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetSelector.h
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 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
15                                                      ModuleBase_IWorkshop* theWorkshop,
16                                                      const Config_WidgetAPI* theData,
17                                                      const std::string& theParentId)
18  : ModuleBase_WidgetValidated(theParent, theData, theParentId),
19    myWorkshop(theWorkshop)
20 {
21 }
22
23 //********************************************************************
24 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
25 {
26 }
27
28 //TODO: nds stabilization hotfix
29 void ModuleBase_WidgetSelector::disconnectSignals()
30 {
31   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
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   clearAttribute();
48
49   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
50                                                               ModuleBase_ISelection::AllControls);
51   bool isDone = setSelection(aSelected);
52
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 #include <TopoDS_Iterator.hxx>
63
64 //********************************************************************
65 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
66                                                const ResultPtr& theResult) const
67 {
68   bool aValid = false;
69
70   GeomShapePtr aShape = theShape;
71   if (!aShape.get() && theResult.get()) {
72     if (theResult.get())
73       aShape = theResult->shape();
74   }
75   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
76   if (aShape.get()) {
77     // Check that the selection corresponds to selection type
78     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
79     aShapeType = aTopoShape.ShapeType();
80     // for compounds check sub-shapes: it may be compound of needed type:
81     // Booleans may produce compounds of Solids
82     if (aShapeType == TopAbs_COMPOUND) {
83       for(TopoDS_Iterator aSubs(aTopoShape); aSubs.More(); aSubs.Next()) {
84         if (!aSubs.Value().IsNull()) {
85           TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
86           if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
87             aShapeType = TopAbs_COMPOUND;
88             break;
89           }
90           if (aShapeType == TopAbs_COMPOUND) {
91             aShapeType = aSubType;
92           } else if (aShapeType != aSubType) { // compound of shapes of different types
93             aShapeType = TopAbs_COMPOUND;
94             break;
95           }
96         }
97       }
98     }
99   }
100
101   QIntList aShapeTypes = getShapeTypes();
102   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
103   for (; anIt != aLast; anIt++) {
104     if (aShapeType == *anIt)
105       aValid = true;
106     else if (*anIt == TopAbs_FACE) {
107       // try to process the construction shape only if there is no a selected shape in the viewer
108       if (!theShape.get() && theResult.get()) {
109         ResultConstructionPtr aCResult =
110                                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
111         aValid = aCResult.get() && aCResult->facesNum() > 0;
112       }
113     }
114   }
115   return aValid;
116 }
117
118 //********************************************************************
119 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
120 {
121   updateSelectionName();
122
123   if (toActivate) {
124     myWorkshop->activateSubShapesSelection(getShapeTypes());
125   } else {
126     myWorkshop->deactivateSubShapesSelection();
127   }
128 }
129
130 //********************************************************************
131 void ModuleBase_WidgetSelector::activateCustom()
132 {
133   connect(myWorkshop, SIGNAL(selectionChanged()), this,
134           SLOT(onSelectionChanged()), Qt::UniqueConnection);
135   
136   activateSelection(true);
137
138   // Restore selection in the viewer by the attribute selection list
139   myWorkshop->setSelected(getAttributeSelection());
140
141   activateFilters(myWorkshop, true);
142 }
143
144 //********************************************************************
145 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
146 {
147   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
148   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
149   bool aValid = acceptSubShape(aShape, aResult);
150
151   if (aValid) {
152     // In order to avoid selection of the same object
153     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
154     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
155     aValid = aSelectedFeature != myFeature;
156   }
157   return aValid;
158 }
159
160 //********************************************************************
161 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
162 {
163   ObjectPtr anObject;
164   GeomShapePtr aShape;
165   getGeomSelection(thePrs, anObject, aShape);
166
167   setObject(anObject, aShape);
168   return true;
169 }
170
171 //********************************************************************
172 void ModuleBase_WidgetSelector::deactivate()
173 {
174   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
175   activateSelection(false);
176   activateFilters(myWorkshop, false);
177 }