Salome HOME
A separation of WidgetSelector class in order to unite the logic of shape/multi shape...
[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;
112   // if there is no selected shape, the method returns true
113   if (!aShape.get())
114     aValid = true;
115   else {
116     // Check that the selection corresponds to selection type
117     TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
118     aValid = acceptSubShape(aTopoShape);
119   }
120   if (aValid) {
121     // In order to avoid selection of the same object
122     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
123     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aResult);
124     aValid = aSelectedFeature != myFeature;
125   }
126   return aValid;
127 }
128
129 //********************************************************************
130 bool ModuleBase_WidgetSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
131 {
132   ObjectPtr anObject;
133   GeomShapePtr aShape;
134   getGeomSelection(thePrs, anObject, aShape);
135
136   setObject(anObject, aShape);
137   return true;
138 }
139
140 //********************************************************************
141 void ModuleBase_WidgetSelector::deactivate()
142 {
143   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
144   activateSelection(false);
145   activateFilters(myWorkshop, false);
146 }