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_AttributeString.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Object.h>
17 #include <ModelAPI_Validator.h>
18 #include <ModelAPI_AttributeSelectionList.h>
20 #include <Config_WidgetAPI.h>
22 #include <QGridLayout>
24 #include <QListWidget>
30 #include <boost/smart_ptr/shared_ptr.hpp>
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)
40 myMainWidget = new QWidget(theParent);
41 QGridLayout* aMainLay = new QGridLayout(myMainWidget);
42 ModuleBase_Tools::adjustMargins(aMainLay);
44 QLabel* aTypeLabel = new QLabel(tr("Type"), myMainWidget);
45 aMainLay->addWidget(aTypeLabel, 0, 0);
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);
53 QLabel* aListLabel = new QLabel(tr("Selected objects:"), myMainWidget);
54 aMainLay->addWidget(aListLabel, 1, 0, 1, -1);
56 myListControl = new QListWidget(myMainWidget);
57 aMainLay->addWidget(myListControl, 2, 0, 2, -1);
58 aMainLay->setColumnStretch(1, 1);
59 myMainWidget->setLayout(aMainLay);
61 //TODO: Move into the base class
62 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
64 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
65 activateSelection(true);
68 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
70 activateSelection(false);
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();
84 foreach (GeomSelection aSelec, mySelection) {
85 aSelectionListAttr->append(aSelec.first, aSelec.second);
87 updateObject(myFeature);
93 bool ModuleBase_WidgetMultiSelector::restoreValue()
96 // A rare case when plugin was not loaded.
99 DataPtr aData = myFeature->data();
100 AttributeSelectionListPtr aSelectionListAttr =
101 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
103 if (aSelectionListAttr) {
105 for (int i = 0; i < aSelectionListAttr->size(); i++) {
106 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
107 mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
109 updateSelectionList();
115 QWidget* ModuleBase_WidgetMultiSelector::getControl() const
120 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
122 QList<QWidget*> result;
123 result << myTypeCombo;
124 result << myListControl;
128 //********************************************************************
129 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
131 if (theObj == myListControl) {
132 if (theEvent->type() == QEvent::FocusIn)
133 activateSelection(true);
135 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
138 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
140 ModuleBase_ISelection* aSelection = myWorkshop->selection();
141 NCollection_List<TopoDS_Shape> aSelectedShapes; //, aFilteredShapes;
142 std::list<ObjectPtr> aOwnersList;
143 aSelection->selectedShapes(aSelectedShapes, aOwnersList);
146 std::list<ObjectPtr>::const_iterator aIt;
147 NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
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));
155 updateSelectionList();
156 emit valuesChanged();
160 void ModuleBase_WidgetMultiSelector::updateSelectionList()
162 myListControl->clear();
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);
173 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
174 NCollection_List<TopoDS_Shape>& theResult)
176 if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
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)
185 theResult.Append(aShape);
189 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
191 myIsActive = toActivate;
193 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
194 onSelectionTypeChanged();
196 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
197 myWorkshop->deactivateSubShapesSelection();
201 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
203 QString aNewType = myTypeCombo->currentText();
205 aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
206 myWorkshop->activateSubShapesSelection(aList);