]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
563cc18780fa6d01231a8b41a78bb38d3cb47eee
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetMultiSelector.cpp
1 /*
2  * ModuleBase_WidgetMultiSelector.cpp
3  *
4  *  Created on: Aug 28, 2014
5  *      Author: sbh
6  */
7
8 #include <ModuleBase_WidgetMultiSelector.h>
9 #include <ModuleBase_WidgetShapeSelector.h>
10 #include <ModuleBase_ISelection.h>
11 #include <ModuleBase_IWorkshop.h>
12 #include <ModuleBase_Tools.h>
13
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Object.h>
17 #include <ModelAPI_Validator.h>
18 #include <ModelAPI_AttributeSelectionList.h>
19
20 #include <Config_WidgetAPI.h>
21
22 #include <QGridLayout>
23 #include <QLabel>
24 #include <QListWidget>
25 #include <QObject>
26 #include <QString>
27 #include <QComboBox>
28 #include <QEvent>
29
30 #include <boost/smart_ptr/shared_ptr.hpp>
31 #include <string>
32
33 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
34                                                                ModuleBase_IWorkshop* theWorkshop,
35                                                                const Config_WidgetAPI* theData,
36                                                                const std::string& theParentId)
37     : ModuleBase_ModelWidget(theParent, theData, theParentId),
38       myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
39 {
40   myMainWidget = new QWidget(theParent);
41   QGridLayout* aMainLay = new QGridLayout(myMainWidget);
42   ModuleBase_Tools::adjustMargins(aMainLay);
43
44   QLabel* aTypeLabel = new QLabel(tr("Type"), myMainWidget);
45   aMainLay->addWidget(aTypeLabel, 0, 0);
46
47   myTypeCombo = new QComboBox(myMainWidget);
48   std::string aTypes = theData->getProperty("type_choice");
49   myShapeTypes = QString::fromStdString(aTypes).split(' ');
50   myTypeCombo->addItems(myShapeTypes);
51   aMainLay->addWidget(myTypeCombo, 0, 1);
52
53   QLabel* aListLabel = new QLabel(tr("Selected objects:"), myMainWidget);
54   aMainLay->addWidget(aListLabel, 1, 0, 1, -1);
55
56   myListControl = new QListWidget(myMainWidget);
57   aMainLay->addWidget(myListControl, 2, 0, 2, -1);
58   aMainLay->setColumnStretch(1, 1);
59   myMainWidget->setLayout(aMainLay);
60
61   //TODO: Move into the base class
62   myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
63   //TODO_END
64   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
65   activateSelection(true);
66 }
67
68 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
69 {
70   activateSelection(false);
71 }
72
73 bool ModuleBase_WidgetMultiSelector::storeValue() const
74 {
75   // A rare case when plugin was not loaded. 
76   if(!myFeature)
77     return false;
78   DataPtr aData = myFeature->data();
79   AttributeSelectionListPtr aSelectionListAttr = 
80     boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
81
82   if (aSelectionListAttr && (mySelection.size() > 0)) {
83     aSelectionListAttr->clear();
84     foreach (GeomSelection aSelec, mySelection) {
85       aSelectionListAttr->append(aSelec.first, aSelec.second);
86     }
87     updateObject(myFeature);
88     return true;
89   }
90   return false;
91 }
92
93 bool ModuleBase_WidgetMultiSelector::restoreValue()
94 {
95   return false;
96   // A rare case when plugin was not loaded. 
97   if(!myFeature)
98     return false;
99   DataPtr aData = myFeature->data();
100   AttributeSelectionListPtr aSelectionListAttr = 
101     boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
102
103   if (aSelectionListAttr) {
104     mySelection.clear();
105     for (int i = 0; i < aSelectionListAttr->size(); i++) {
106       AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
107       mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
108     }
109     updateSelectionList();
110     return true;
111   }
112   return false;
113 }
114
115 QWidget* ModuleBase_WidgetMultiSelector::getControl() const
116 {
117   return myMainWidget;
118 }
119
120 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
121 {
122   QList<QWidget*> result;
123   result << myTypeCombo;
124   result << myListControl;
125   return result;
126 }
127
128 //********************************************************************
129 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
130 {
131   if (theObj == myListControl) {
132     if (theEvent->type() == QEvent::FocusIn)
133       activateSelection(true);
134   }
135   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
136 }
137
138 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
139 {
140   ModuleBase_ISelection* aSelection = myWorkshop->selection();
141   NCollection_List<TopoDS_Shape> aSelectedShapes; //, aFilteredShapes;
142   std::list<ObjectPtr> aOwnersList;
143   aSelection->selectedShapes(aSelectedShapes, aOwnersList);
144
145   mySelection.clear();
146   std::list<ObjectPtr>::const_iterator aIt;
147   NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
148   GeomShapePtr aShape;
149   for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) {
150     ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
151     aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
152     aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
153     mySelection.append(GeomSelection(aResult, aShape));
154   }
155   updateSelectionList();
156   emit valuesChanged();
157 }
158
159
160 void ModuleBase_WidgetMultiSelector::updateSelectionList()
161 {
162   myListControl->clear();
163   int i = 1;
164   foreach (GeomSelection aSel, mySelection) {
165     QString aName(aSel.first->data()->name().c_str());
166     aName += ":" + myTypeCombo->currentText() + QString::number(i);
167     myListControl->addItem(aName);
168     i++;
169   }
170 }
171
172
173 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
174                                                   NCollection_List<TopoDS_Shape>& theResult)
175 {
176   if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
177     return;
178   TopAbs_ShapeEnum aReferenceType =
179       ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
180   NCollection_List<TopoDS_Shape>::Iterator anIter(theShapesToFilter);
181   for (; anIter.More(); anIter.Next()) {
182     TopoDS_Shape aShape = anIter.Value();
183     if (aShape.IsNull() || aShape.ShapeType() != aReferenceType)
184       continue;
185     theResult.Append(aShape);
186   }
187 }
188
189 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
190 {
191   myIsActive = toActivate;
192   if (myIsActive) {
193     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
194     onSelectionTypeChanged();
195   } else {
196     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
197     myWorkshop->deactivateSubShapesSelection();
198   }
199 }
200
201 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
202 {
203   QString aNewType = myTypeCombo->currentText();
204   QIntList aList;
205   aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
206   myWorkshop->activateSubShapesSelection(aList);
207 }