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 && (mySelection.size() > 0)) {
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();
169 emit valuesChanged();
173 //********************************************************************
174 void ModuleBase_WidgetMultiSelector::updateSelectionList()
177 if (myTypeCombo->currentText().toLower() == "vertices")
179 else if (myTypeCombo->currentText().toLower() == "edges")
181 else if (myTypeCombo->currentText().toLower() == "faces")
183 else if (myTypeCombo->currentText().toLower() == "solids")
186 myListControl->clear();
188 foreach (GeomSelection aSel, mySelection) {
189 QString aName(aSel.first->data()->name().c_str());
190 aName += ":" + aType + QString("_%1").arg(i);
191 myListControl->addItem(aName);
197 //********************************************************************
198 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
199 NCollection_List<TopoDS_Shape>& theResult)
201 if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
203 TopAbs_ShapeEnum aReferenceType =
204 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
205 NCollection_List<TopoDS_Shape>::Iterator anIter(theShapesToFilter);
206 for (; anIter.More(); anIter.Next()) {
207 TopoDS_Shape aShape = anIter.Value();
208 if (aShape.IsNull() || aShape.ShapeType() != aReferenceType)
210 theResult.Append(aShape);
215 //********************************************************************
216 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
218 QString aShapeTypeName;
219 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
220 aShapeTypeName = myTypeCombo->currentText();
221 TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName);
222 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
223 myTypeCombo->setCurrentIndex(idx);
229 //********************************************************************
230 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
232 myIsActive = toActivate;
234 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
235 onSelectionTypeChanged();
237 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
238 myWorkshop->deactivateSubShapesSelection();
242 //********************************************************************
243 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
245 QString aNewType = myTypeCombo->currentText();
247 aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
248 myWorkshop->activateSubShapesSelection(aList);