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>
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->setRowStretch(2, 1);
58 aMainLay->addWidget(new QLabel(myMainWidget));
59 aMainLay->setRowMinimumHeight(3, 20);
60 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()));
66 activateSelection(true);
69 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
71 activateSelection(false);
74 //********************************************************************
75 bool ModuleBase_WidgetMultiSelector::storeValue() const
77 // A rare case when plugin was not loaded.
80 DataPtr aData = myFeature->data();
81 AttributeSelectionListPtr aSelectionListAttr =
82 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
84 if (aSelectionListAttr) {
85 aSelectionListAttr->clear();
87 TopAbs_ShapeEnum aCurrentType =
88 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
89 aSelectionListAttr->setSelectionType((int) aCurrentType);
90 // Store selection in the attribute
91 foreach (GeomSelection aSelec, mySelection) {
92 aSelectionListAttr->append(aSelec.first, aSelec.second);
94 updateObject(myFeature);
100 //********************************************************************
101 bool ModuleBase_WidgetMultiSelector::restoreValue()
103 // A rare case when plugin was not loaded.
106 DataPtr aData = myFeature->data();
107 AttributeSelectionListPtr aSelectionListAttr =
108 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
110 if (aSelectionListAttr) {
112 // Restore shape type
113 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum) aSelectionListAttr->selectionType();
114 setCurrentShapeType(aShapeType);
115 // Restore selection in the list
116 for (int i = 0; i < aSelectionListAttr->size(); i++) {
117 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
118 mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
120 updateSelectionList();
126 //********************************************************************
127 QWidget* ModuleBase_WidgetMultiSelector::getControl() const
132 //********************************************************************
133 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
135 QList<QWidget*> result;
136 //result << myTypeCombo;
137 result << myListControl;
141 //********************************************************************
142 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
144 //TODO: Remove maybe?
145 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
148 //********************************************************************
149 void ModuleBase_WidgetMultiSelector::activateSelection(bool toActivate)
151 myIsActive = toActivate;
153 connect(myWorkshop, SIGNAL(selectionChanged()),
154 this, SLOT(onSelectionChanged()),
155 Qt::UniqueConnection);
156 activateShapeSelection();
158 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
159 myWorkshop->deactivateSubShapesSelection();
163 //********************************************************************
164 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
166 activateShapeSelection();
167 QObjectPtrList anEmptyList;
168 myWorkshop->setSelected(anEmptyList);
169 // Clear mySelection, myListControl and storeValue()
170 onSelectionChanged();
173 //********************************************************************
174 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
176 ModuleBase_ISelection* aSelection = myWorkshop->selection();
177 NCollection_List<TopoDS_Shape> aSelectedShapes; //, aFilteredShapes;
178 std::list<ObjectPtr> aOwnersList;
179 aSelection->selectedShapes(aSelectedShapes, aOwnersList);
182 std::list<ObjectPtr>::const_iterator aIt;
183 NCollection_List<TopoDS_Shape>::Iterator aShpIt(aSelectedShapes);
185 for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) {
186 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(*aIt);
188 // We can not select a result of our feature
189 const std::list<ResultPtr>& aResList = myFeature->results();
190 std::list<ResultPtr>::const_iterator aIt;
191 bool isSkipSelf = false;
192 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
193 if ((*aIt) == aResult) {
201 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
202 aShape->setImpl(new TopoDS_Shape(aShpIt.Value()));
203 mySelection.append(GeomSelection(aResult, aShape));
205 updateSelectionList();
206 emit valuesChanged();
209 //********************************************************************
210 void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List<TopoDS_Shape>& theShapesToFilter,
211 NCollection_List<TopoDS_Shape>& theResult)
213 if(myTypeCombo->count() == 0 || theShapesToFilter.IsEmpty())
215 TopAbs_ShapeEnum aReferenceType =
216 ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText());
217 NCollection_List<TopoDS_Shape>::Iterator anIter(theShapesToFilter);
218 for (; anIter.More(); anIter.Next()) {
219 TopoDS_Shape aShape = anIter.Value();
220 if (aShape.IsNull() || aShape.ShapeType() != aReferenceType)
222 theResult.Append(aShape);
226 //********************************************************************
227 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
229 QString aShapeTypeName;
231 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
232 aShapeTypeName = myTypeCombo->itemText(idx);
233 TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName);
234 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
235 activateSelection(false);
236 bool isBlocked = myTypeCombo->blockSignals(true);
237 myTypeCombo->setCurrentIndex(idx);
238 myTypeCombo->blockSignals(isBlocked);
239 activateSelection(true);
245 void ModuleBase_WidgetMultiSelector::activateShapeSelection()
247 QString aNewType = myTypeCombo->currentText();
249 aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType));
250 myWorkshop->activateSubShapesSelection(aList);
253 //********************************************************************
254 void ModuleBase_WidgetMultiSelector::updateSelectionList()
257 if (myTypeCombo->currentText().toLower() == "vertices")
259 else if (myTypeCombo->currentText().toLower() == "edges")
261 else if (myTypeCombo->currentText().toLower() == "faces")
263 else if (myTypeCombo->currentText().toLower() == "solids")
266 myListControl->clear();
268 foreach (GeomSelection aSel, mySelection) {
269 QString aName(aSel.first->data()->name().c_str());
270 aName += ":" + aType + QString("_%1").arg(i);
271 myListControl->addItem(aName);
274 myListControl->repaint();