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>
19 #include <Config_WidgetAPI.h>
21 #include <QGridLayout>
29 #include <boost/smart_ptr/shared_ptr.hpp>
32 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
33 ModuleBase_IWorkshop* theWorkshop,
34 const Config_WidgetAPI* theData,
35 const std::string& theParentId)
36 : ModuleBase_ModelWidget(theParent, theData, theParentId),
37 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
39 myMainWidget = new QWidget(theParent);
40 QGridLayout* aMainLay = new QGridLayout(myMainWidget);
41 ModuleBase_Tools::adjustMargins(aMainLay);
42 QLabel* aTypeLabel = new QLabel(tr("Type"), myMainWidget);
43 aMainLay->addWidget(aTypeLabel, 0, 0);
44 myTypeCombo = new QComboBox(myMainWidget);
45 std::string aTypes = theData->getProperty("type_choice");
46 myShapeTypes = QString::fromStdString(aTypes).split(' ');
47 myTypeCombo->addItems(myShapeTypes);
48 aMainLay->addWidget(myTypeCombo, 0, 1);
49 QLabel* aListLabel = new QLabel(tr("Selected objects:"), myMainWidget);
50 aMainLay->addWidget(aListLabel, 1, 0, 1, -1);
51 myListControl = new QTextEdit(myMainWidget);
52 myListControl->setReadOnly(true);
53 aMainLay->addWidget(myListControl, 2, 0, 2, -1);
54 aMainLay->setColumnStretch(1, 1);
55 myMainWidget->setLayout(aMainLay);
57 //TODO: Move into the base class
58 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
60 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
61 activateSelection(true);
64 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
66 activateSelection(false);
69 bool ModuleBase_WidgetMultiSelector::storeValue() const
71 // A rare case when plugin was not loaded.
74 DataPtr aData = myFeature->data();
75 AttributeStringPtr aStringAttr = aData->string(attributeID());
76 QString aWidgetValue = myListControl->toPlainText();
77 aStringAttr->setValue(aWidgetValue.toStdString());
78 updateObject(myFeature);
82 bool ModuleBase_WidgetMultiSelector::restoreValue()
85 // A rare case when plugin was not loaded.
88 DataPtr aData = myFeature->data();
89 AttributeStringPtr aStringAttr = aData->string(attributeID());
91 bool isBlocked = myListControl->blockSignals(true);
92 myListControl->setText(QString::fromStdString(aStringAttr->value()));
93 myListControl->blockSignals(isBlocked);
98 QWidget* ModuleBase_WidgetMultiSelector::getControl() const
103 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
105 QList<QWidget*> result;
106 result << myTypeCombo;
107 result << myListControl;
111 //********************************************************************
112 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
114 if (theObj == myListControl) {
115 if (theEvent->type() == QEvent::FocusIn)
116 activateSelection(true);
118 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
121 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
123 ModuleBase_ISelection* aSelection = myWorkshop->selection();
124 NCollection_List<TopoDS_Shape> aSelectedShapes, aFilteredShapes;
125 aSelection->selectedShapes(aSelectedShapes);
127 if (!aSelectedShapes.IsEmpty()) {
128 filterShapes(aSelectedShapes, aFilteredShapes);
129 aText = QString("Items selected: %1").arg(aFilteredShapes.Size());
131 myListControl->setText(aText);
134 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
135 NCollection_List<TopoDS_Shape>& theResult)
137 if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
139 TopAbs_ShapeEnum aReferenceType =
140 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
141 NCollection_List<TopoDS_Shape>::Iterator anIter(theShapesToFilter);
142 for (; anIter.More(); anIter.Next()) {
143 TopoDS_Shape aShape = anIter.Value();
144 if (aShape.IsNull() || aShape.ShapeType() != aReferenceType)
146 theResult.Append(aShape);
150 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
152 myIsActive = toActivate;
154 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
155 onSelectionTypeChanged();
157 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
158 myWorkshop->deactivateSubShapesSelection();
162 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
164 QString aNewType = myTypeCombo->currentText();
166 aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
167 myWorkshop->activateSubShapesSelection(aList);