Salome HOME
Preselection for extrusion/revolution
[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 //********************************************************************
63 bool ModuleBase_WidgetSelector::acceptSubShape(const GeomShapePtr& theShape,
64                                                const ResultPtr& theResult) const
65 {
66   bool aValid = false;
67
68   GeomShapePtr aShape = theShape;
69   if (!aShape.get() && theResult.get()) {
70     if (theResult.get())
71       aShape = theResult->shape();
72   }
73   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
74   if (aShape.get()) {
75     // Check that the selection corresponds to selection type
76     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
77     aShapeType = aTopoShape.ShapeType();
78   }
79
80   QIntList aShapeTypes = getShapeTypes();
81   QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
82   for (; anIt != aLast; anIt++) {
83     if (aShapeType == *anIt)
84       aValid = true;
85     else if (*anIt == TopAbs_FACE) {
86       // try to process the construction shape only if there is no a selected shape in the viewer
87       if (!theShape.get() && theResult.get()) {
88         ResultConstructionPtr aCResult =
89                                 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
90         aValid = aCResult.get() && aCResult->facesNum() > 0;
91       }
92     }
93   }
94   return aValid;
95 }
96
97 //********************************************************************
98 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
99 {
100   updateSelectionName();
101
102   if (toActivate) {
103     myWorkshop->activateSubShapesSelection(getShapeTypes());
104   } else {
105     myWorkshop->deactivateSubShapesSelection();
106   }
107 }
108
109 //********************************************************************
110 void ModuleBase_WidgetSelector::activateCustom()
111 {
112   connect(myWorkshop, SIGNAL(selectionChanged()), this,
113           SLOT(onSelectionChanged()), Qt::UniqueConnection);
114   
115   activateSelection(true);
116
117   // Restore selection in the viewer by the attribute selection list
118   myWorkshop->setSelected(getAttributeSelection());
119
120   activateFilters(myWorkshop, true);
121 }
122
123 //********************************************************************
124 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
125 {
126   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
127   ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
128   bool aValid = acceptSubShape(aShape, aResult);
129
130   if (aValid) {
131     // In order to avoid selection of the same object
132     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
133     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
134     aValid = aSelectedFeature != myFeature;
135   }
136   return aValid;
137 }
138
139 //********************************************************************
140 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
141 {
142   ObjectPtr anObject;
143   GeomShapePtr aShape;
144   getGeomSelection(thePrs, anObject, aShape);
145
146   setObject(anObject, aShape);
147   return true;
148 }
149
150 //********************************************************************
151 void ModuleBase_WidgetSelector::deactivate()
152 {
153   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
154   activateSelection(false);
155   activateFilters(myWorkshop, false);
156 }