Salome HOME
1. Compound selection choice is provided for sketch selection in Extrusion operation...
[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 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent,
13                                                      ModuleBase_IWorkshop* theWorkshop,
14                                                      const Config_WidgetAPI* theData,
15                                                      const std::string& theParentId)
16  : ModuleBase_WidgetValidated(theParent, theData, theParentId),
17    myWorkshop(theWorkshop)
18 {
19 }
20
21 //********************************************************************
22 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
23 {
24 }
25
26 //TODO: nds stabilization hotfix
27 void ModuleBase_WidgetSelector::disconnectSignals()
28 {
29   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
30 }
31
32 //********************************************************************
33 void ModuleBase_WidgetSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
34                                                       ObjectPtr& theObject,
35                                                       GeomShapePtr& theShape)
36 {
37   ModuleBase_ISelection* aSelection = myWorkshop->selection();
38   theObject = aSelection->getResult(thePrs);
39   theShape = aSelection->getShape(thePrs);
40 }
41
42 //********************************************************************
43 void ModuleBase_WidgetSelector::onSelectionChanged()
44 {
45   clearAttribute();
46
47   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
48                                                               ModuleBase_ISelection::AllControls);
49   setSelection(aSelected);
50
51   emit valuesChanged();
52   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
53   // calls validators for the feature and, as a result, updates the Apply button state.
54   updateObject(myFeature);
55
56   updateFocus();
57 }
58
59 //********************************************************************
60 bool ModuleBase_WidgetSelector::acceptSubShape(const TopoDS_Shape& theShape) const
61 {
62   bool aValid = true;
63   if (theShape.IsNull()) {
64     aValid = true; // do not check the shape type if the shape is empty
65     // extrusion uses a sketch object selectected in Object browser
66   }
67   else {
68     aValid = false;
69     TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
70     QIntList aShapeTypes = getShapeTypes();
71
72     QIntList::const_iterator anIt = aShapeTypes.begin(), aLast = aShapeTypes.end();
73     for (; anIt != aLast; anIt++) {
74       if (aShapeType == *anIt)
75         aValid = true;
76     }
77   }
78   return aValid;
79 }
80
81 //********************************************************************
82 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
83 {
84   updateSelectionName();
85
86   if (toActivate) {
87     myWorkshop->activateSubShapesSelection(getShapeTypes());
88   } else {
89     myWorkshop->deactivateSubShapesSelection();
90   }
91 }
92
93 //********************************************************************
94 void ModuleBase_WidgetSelector::activateCustom()
95 {
96   connect(myWorkshop, SIGNAL(selectionChanged()), this,
97           SLOT(onSelectionChanged()), Qt::UniqueConnection);
98   
99   activateSelection(true);
100
101   // Restore selection in the viewer by the attribute selection list
102   myWorkshop->setSelected(getAttributeSelection());
103
104   activateFilters(myWorkshop, true);
105 }
106
107 //********************************************************************
108 bool ModuleBase_WidgetSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
109 {
110   GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
111   bool aValid = true;
112   if (!aShape.get()) {
113     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
114     if (aResult.get())
115       aShape = aResult->shape();
116   }
117   if (aShape.get()) {
118     // Check that the selection corresponds to selection type
119     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
120     aValid = acceptSubShape(aTopoShape);
121   }
122   if (aValid) {
123     // In order to avoid selection of the same object
124     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
125     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
126     aValid = aSelectedFeature != myFeature;
127   }
128   return aValid;
129 }
130
131 //********************************************************************
132 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
133 {
134   ObjectPtr anObject;
135   GeomShapePtr aShape;
136   getGeomSelection(thePrs, anObject, aShape);
137
138   setObject(anObject, aShape);
139   return true;
140 }
141
142 //********************************************************************
143 void ModuleBase_WidgetSelector::deactivate()
144 {
145   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
146   activateSelection(false);
147   activateFilters(myWorkshop, false);
148 }