2 * ModuleBase_WidgetMultiSelector.cpp
4 * Created on: Aug 28, 2014
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>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_AttributeSelectionList.h>
18 #include <Config_WidgetAPI.h>
20 #include <QGridLayout>
22 #include <QListWidget>
28 #include <boost/smart_ptr/shared_ptr.hpp>
31 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
32 ModuleBase_IWorkshop* theWorkshop,
33 const Config_WidgetAPI* theData,
34 const std::string& theParentId)
35 : ModuleBase_ModelWidget(theParent, theData, theParentId),
36 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
38 myMainWidget = new QWidget(theParent);
39 QGridLayout* aMainLay = new QGridLayout(myMainWidget);
40 ModuleBase_Tools::adjustMargins(aMainLay);
42 QLabel* aTypeLabel = new QLabel(tr("Type"), myMainWidget);
43 aMainLay->addWidget(aTypeLabel, 0, 0);
45 myTypeCombo = new QComboBox(myMainWidget);
46 // There is no sence to paramerize list of types while we can not parametrize selection mode
47 QString aTypesStr("Vertices Edges Faces Solids");
48 QStringList aShapeTypes = aTypesStr.split(' ');
49 myTypeCombo->addItems(aShapeTypes);
50 aMainLay->addWidget(myTypeCombo, 0, 1);
52 QLabel* aListLabel = new QLabel(tr("Selected objects:"), myMainWidget);
53 aMainLay->addWidget(aListLabel, 1, 0, 1, -1);
55 myListControl = new QListWidget(myMainWidget);
56 aMainLay->addWidget(myListControl, 2, 0, 2, -1);
57 aMainLay->setColumnStretch(1, 1);
58 myMainWidget->setLayout(aMainLay);
60 //TODO: Move into the base class
61 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
63 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
64 activateSelection(true);
67 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
69 activateSelection(false);
72 //********************************************************************
73 bool ModuleBase_WidgetMultiSelector::storeValue() const
75 // A rare case when plugin was not loaded.
78 DataPtr aData = myFeature->data();
79 AttributeSelectionListPtr aSelectionListAttr =
80 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
82 if (aSelectionListAttr) {
83 aSelectionListAttr->clear();
85 TopAbs_ShapeEnum aCurrentType =
86 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
87 aSelectionListAttr->setSelectionType((int) aCurrentType);
88 // Store selection in the attribute
89 foreach (GeomSelection aSelec, mySelection) {
90 aSelectionListAttr->append(aSelec.first, aSelec.second);
92 updateObject(myFeature);
98 //********************************************************************
99 bool ModuleBase_WidgetMultiSelector::restoreValue()
101 // A rare case when plugin was not loaded.
104 DataPtr aData = myFeature->data();
105 AttributeSelectionListPtr aSelectionListAttr =
106 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
108 if (aSelectionListAttr) {
110 // Restore shape type
111 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum) aSelectionListAttr->selectionType();
112 setCurrentShapeType(aShapeType);
113 // Restore selection in the list
114 for (int i = 0; i < aSelectionListAttr->size(); i++) {
115 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
116 mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
118 updateSelectionList();
124 //********************************************************************
125 QWidget* ModuleBase_WidgetMultiSelector::getControl() const
130 //********************************************************************
131 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
133 QList<QWidget*> result;
134 //result << myTypeCombo;
135 result << myListControl;
139 //********************************************************************
140 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
142 if (theObj == myListControl) {
143 if (theEvent->type() == QEvent::FocusIn)
144 activateSelection(true);
146 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
149 //********************************************************************
150 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
152 ModuleBase_ISelection* aSelection = myWorkshop->selection();
153 NCollection_List<TopoDS_Shape> aSelectedShapes; //, aFilteredShapes;
154 std::list<ObjectPtr> aOwnersList;
155 aSelection->selectedShapes(aSelectedShapes, aOwnersList);
158 std::list<ObjectPtr>::const_iterator aIt;
159 NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
161 for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) {
162 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
163 aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
164 aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
165 mySelection.append(GeomSelection(aResult, aShape));
167 updateSelectionList();
168 emit valuesChanged();
172 //********************************************************************
173 void ModuleBase_WidgetMultiSelector::updateSelectionList()
176 if (myTypeCombo->currentText().toLower() == "vertices")
178 else if (myTypeCombo->currentText().toLower() == "edges")
180 else if (myTypeCombo->currentText().toLower() == "faces")
182 else if (myTypeCombo->currentText().toLower() == "solids")
185 myListControl->clear();
187 foreach (GeomSelection aSel, mySelection) {
188 QString aName(aSel.first->data()->name().c_str());
189 aName += ":" + aType + QString("_%1").arg(i);
190 myListControl->addItem(aName);
196 //********************************************************************
197 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
198 NCollection_List<TopoDS_Shape>& theResult)
200 if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
202 TopAbs_ShapeEnum aReferenceType =
203 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
204 NCollection_List<TopoDS_Shape>::Iterator anIter(theShapesToFilter);
205 for (; anIter.More(); anIter.Next()) {
206 TopoDS_Shape aShape = anIter.Value();
207 if (aShape.IsNull() || aShape.ShapeType() != aReferenceType)
209 theResult.Append(aShape);
214 //********************************************************************
215 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
217 QString aShapeTypeName;
218 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
219 aShapeTypeName = myTypeCombo->itemText(idx);
220 TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName);
221 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
222 myTypeCombo->setCurrentIndex(idx);
228 //********************************************************************
229 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
231 myIsActive = toActivate;
233 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
234 //onSelectionTypeChanged();
236 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
237 myWorkshop->deactivateSubShapesSelection();
241 void ModuleBase_WidgetMultiSelector::activateShapeSelection()
243 QString aNewType = myTypeCombo->currentText();
245 aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
246 myWorkshop->activateSubShapesSelection(aList);
249 //********************************************************************
250 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
252 QList<ObjectPtr> anEmptyList;
253 myWorkshop->setSelected(anEmptyList);
254 activateShapeSelection();
255 onSelectionChanged();