1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_WidgetMultiSelector.cpp
6 * Created on: Aug 28, 2014
10 #include <ModuleBase_WidgetMultiSelector.h>
11 #include <ModuleBase_WidgetShapeSelector.h>
12 #include <ModuleBase_ISelection.h>
13 #include <ModuleBase_IWorkshop.h>
14 #include <ModuleBase_IViewer.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_Definitions.h>
17 #include <ModuleBase_IModule.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Object.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_AttributeRefAttrList.h>
25 #include <Config_WidgetAPI.h>
27 #include <QGridLayout>
29 #include <QListWidget>
35 #include <QApplication>
42 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
45 * Customization of a List Widget to make it to be placed on full width of container
47 class CustomListWidget : public QListWidget
51 /// \param theParent a parent widget
52 CustomListWidget( QWidget* theParent )
53 : QListWidget( theParent )
57 /// Redefinition of virtual method
58 virtual QSize sizeHint() const
60 int aHeight = 2*QFontMetrics( font() ).height();
61 QSize aSize = QListWidget::sizeHint();
62 return QSize( aSize.width(), aHeight );
65 /// Redefinition of virtual method
66 virtual QSize minimumSizeHint() const
68 int aHeight = 4/*2*/*QFontMetrics( font() ).height();
69 QSize aSize = QListWidget::minimumSizeHint();
70 return QSize( aSize.width(), aHeight );
74 // The code is necessary only for Linux because
75 //it can not update viewport on widget resize
77 void resizeEvent(QResizeEvent* theEvent)
79 QListWidget::resizeEvent(theEvent);
80 QTimer::singleShot(5, viewport(), SLOT(repaint()));
85 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
86 ModuleBase_IWorkshop* theWorkshop,
87 const Config_WidgetAPI* theData,
88 const std::string& theParentId)
89 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId),
92 QGridLayout* aMainLay = new QGridLayout(this);
93 ModuleBase_Tools::adjustMargins(aMainLay);
95 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
96 aMainLay->addWidget(aTypeLabel, 0, 0);
98 myTypeCombo = new QComboBox(this);
99 // There is no sense to parameterize list of types while we can not parameterize selection mode
101 std::string aPropertyTypes = theData->getProperty("type_choice");
102 QString aTypesStr = aPropertyTypes.c_str();
103 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
105 myIsUseChoice = theData->getBooleanAttribute("use_choice", true);
107 if (!aShapeTypes.empty())
108 myTypeCombo->addItems(aShapeTypes);
109 aMainLay->addWidget(myTypeCombo, 0, 1);
110 // if the xml definition contains one type, the controls to select a type should not be shown
111 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
112 aTypeLabel->setVisible(false);
113 myTypeCombo->setVisible(false);
116 std::string aLabelText = theData->getProperty("label");
117 QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
118 : tr("Selected objects:"), this);
119 aMainLay->addWidget(aListLabel, 1, 0);
120 // if the xml definition contains one type, an information label should be shown near to the latest
121 if (aShapeTypes.size() <= 1) {
122 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
123 if (!aLabelIcon.isEmpty()) {
124 QLabel* aSelectedLabel = new QLabel("", this);
125 aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
126 aMainLay->addWidget(aSelectedLabel, 1, 1);
128 aMainLay->setColumnStretch(2, 1);
131 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
132 myListControl = new CustomListWidget(this);
133 QString anObjName = QString::fromStdString(attributeID());
134 myListControl->setObjectName(anObjName);
135 myListControl->setToolTip(aToolTip);
136 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
138 aMainLay->addWidget(myListControl, 2, 0, 1, -1);
139 aMainLay->setRowStretch(2, 1);
140 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
141 //aMainLay->setRowMinimumHeight(3, 20);
142 //this->setLayout(aMainLay);
143 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
145 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
146 myCopyAction->setShortcut(QKeySequence::Copy);
147 myCopyAction->setEnabled(false);
148 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
149 myListControl->addAction(myCopyAction);
151 myDeleteAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
152 myDeleteAction->setEnabled(false);
153 connect(myDeleteAction, SIGNAL(triggered(bool)), SLOT(onDeleteItem()));
154 myListControl->addAction(myDeleteAction);
156 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
157 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
160 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
164 //********************************************************************
165 void ModuleBase_WidgetMultiSelector::activateCustom()
167 ModuleBase_WidgetSelector::activateCustom();
169 myWorkshop->module()->activateCustomPrs(myFeature,
170 ModuleBase_IModule::CustomizeHighlightedObjects, true);
173 //********************************************************************
174 void ModuleBase_WidgetMultiSelector::deactivate()
176 ModuleBase_WidgetSelector::deactivate();
178 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
181 //********************************************************************
182 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
184 // the value is stored on the selection changed signal processing
185 // A rare case when plugin was not loaded.
189 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
190 std::string aType = anAttribute->attributeType();
191 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
192 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
193 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
198 //********************************************************************
199 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
201 // A rare case when plugin was not loaded.
205 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
206 std::string aType = anAttribute->attributeType();
207 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
208 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
209 // Restore shape type
210 std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
211 if (!aSelectionType.empty())
212 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
214 updateSelectionList();
218 //********************************************************************
219 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
221 ModuleBase_WidgetValidated::storeAttributeValue();
223 DataPtr aData = myFeature->data();
224 AttributePtr anAttribute = aData->attribute(attributeID());
225 std::string aType = anAttribute->attributeType();
226 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
227 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
228 mySelectionType = aSelectionListAttr->selectionType();
229 mySelectionCount = aSelectionListAttr->size();
231 else if (aType == ModelAPI_AttributeRefList::typeId()) {
232 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
233 mySelectionCount = aRefListAttr->size();
235 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
236 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
237 mySelectionCount = aRefAttrListAttr->size();
241 //********************************************************************
242 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool theValid)
244 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
246 DataPtr aData = myFeature->data();
247 AttributePtr anAttribute = aData->attribute(attributeID());
248 std::string aType = anAttribute->attributeType();
249 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
250 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
251 aSelectionListAttr->setSelectionType(mySelectionType);
253 // restore selection in the attribute. Indeed there is only one stored object
254 int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
255 for (int i = 0; i < aCountAppened; i++)
256 aSelectionListAttr->removeLast();
258 else if (aType == ModelAPI_AttributeRefList::typeId()) {
259 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
260 // restore objects in the attribute. Indeed there is only one stored object
261 int aCountAppened = aRefListAttr->size() - mySelectionCount;
262 for (int i = 0; i < aCountAppened; i++)
263 aRefListAttr->removeLast();
265 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
266 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
267 // restore objects in the attribute. Indeed there is only one stored object
268 int aCountAppened = aRefAttrListAttr->size() - mySelectionCount;
269 for (int i = 0; i < aCountAppened; i++)
270 aRefAttrListAttr->removeLast();
274 //********************************************************************
275 void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject,
276 GeomShapePtr theShape)
278 DataPtr aData = myFeature->data();
279 AttributePtr anAttribute = aData->attribute(attributeID());
280 std::string aType = anAttribute->attributeType();
281 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
282 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
283 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
284 if (!aSelectionListAttr->isInList(aResult, theShape, myIsInValidate))
285 aSelectionListAttr->append(aResult, theShape, myIsInValidate);
287 else if (aType == ModelAPI_AttributeRefList::typeId()) {
288 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
289 if (!aRefListAttr->isInList(theSelectedObject))
290 aRefListAttr->append(theSelectedObject);
292 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
293 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
294 AttributePtr anAttribute = myWorkshop->module()->findAttribute(theSelectedObject, theShape);
296 if (anAttribute.get()) {
297 if (!aRefAttrListAttr->isInList(anAttribute))
298 aRefAttrListAttr->append(anAttribute);
301 if (!aRefAttrListAttr->isInList(theSelectedObject))
302 aRefAttrListAttr->append(theSelectedObject);
307 //********************************************************************
308 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
309 const bool theToValidate)
311 QList<ModuleBase_ViewerPrs> aSkippedValues;
313 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
315 for (; anIt != aLast; anIt++) {
316 ModuleBase_ViewerPrs aValue = *anIt;
317 bool aProcessed = false;
318 if (!theToValidate || isValidInFilters(aValue)) {
319 aProcessed = setSelectionCustom(aValue);
322 aSkippedValues.append(aValue);
323 // if there is at least one set, the result is true
324 isDone = isDone || aProcessed;
326 // updateObject - to update/redisplay feature
327 // it is commented in order to perfom it outside the method
329 //updateObject(myFeature);
330 // this emit is necessary to call store/restore method an restore type of selection
331 //emit valuesChanged();
334 /// remove unused objects from the model attribute
335 removeUnusedAttributeObjects(theValues);
338 if (!aSkippedValues.empty())
339 theValues.append(aSkippedValues);
344 //********************************************************************
345 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrs>& theValues)
347 std::set<int> anAttributeIds;
348 getSelectedAttributeIndices(anAttributeIds);
349 if (!anAttributeIds.empty())
350 convertIndicesToViewerSelection(anAttributeIds, theValues);
353 //********************************************************************
354 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
356 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
358 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
359 aValid = aResult.get() != NULL;
362 // We can not select a result of our feature
363 const std::list<ResultPtr>& aResList = myFeature->results();
364 std::list<ResultPtr>::const_iterator aIt;
365 bool isSkipSelf = false;
366 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
367 if ((*aIt) == aResult) {
380 //********************************************************************
381 bool ModuleBase_WidgetMultiSelector::processDelete()
383 // find attribute indices to delete
384 std::set<int> anAttributeIds;
385 getSelectedAttributeIndices(anAttributeIds);
387 // refill attribute by the items which indices are not in the list of ids
389 DataPtr aData = myFeature->data();
390 AttributePtr anAttribute = aData->attribute(attributeID());
391 std::string aType = anAttribute->attributeType();
392 aDone = !anAttributeIds.empty();
393 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
394 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
395 aSelectionListAttr->remove(anAttributeIds);
398 else if (aType == ModelAPI_AttributeRefList::typeId()) {
399 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
400 aRefListAttr->remove(anAttributeIds);
402 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
403 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
404 aRefAttrListAttr->remove(anAttributeIds);
408 // update object is necessary to flush update signal. It leads to objects references map update
409 // and the operation presentation will not contain deleted items visualized as parameters of
411 updateObject(myFeature);
414 myWorkshop->setSelected(getAttributeSelection());
419 //********************************************************************
420 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
422 QList<QWidget*> result;
423 //result << myTypeCombo;
424 result << myListControl;
428 //********************************************************************
429 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
431 activateSelectionAndFilters(true);
432 QList<ModuleBase_ViewerPrs> anEmptyList;
433 // This method will call Selection changed event which will call onSelectionChanged
434 // To clear mySelection, myListControl and storeValue()
435 // So, we don't need to call it
436 myWorkshop->setSelected(anEmptyList);
439 void ModuleBase_WidgetMultiSelector::updateFocus()
441 // Set focus to List control in order to make possible
442 // to use Tab key for transfer the focus to next widgets
443 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
444 ModuleBase_Tools::setFocus(myListControl,
445 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
448 //********************************************************************
449 void ModuleBase_WidgetMultiSelector::updateSelectionName()
453 //********************************************************************
454 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
456 QIntList aShapeTypes;
458 if (myTypeCombo->count() > 1 && myIsUseChoice) {
459 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
462 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
463 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
464 aShapeTypes.append(aType);
465 if (aType == TopAbs_SOLID)
466 aShapeTypes.append(TopAbs_COMPSOLID);
472 //********************************************************************
473 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
475 QString aShapeTypeName;
477 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
478 aShapeTypeName = myTypeCombo->itemText(idx);
479 TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
480 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
481 activateSelectionAndFilters(false);
482 bool isBlocked = myTypeCombo->blockSignals(true);
483 myTypeCombo->setCurrentIndex(idx);
484 myTypeCombo->blockSignals(isBlocked);
486 activateSelectionAndFilters(true);
492 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
494 QList<ModuleBase_ViewerPrs> aSelected;
495 convertIndicesToViewerSelection(std::set<int>(), aSelected);
499 //********************************************************************
500 void ModuleBase_WidgetMultiSelector::updateSelectionList()
502 myListControl->clear();
504 DataPtr aData = myFeature->data();
505 AttributePtr anAttribute = aData->attribute(attributeID());
506 std::string aType = anAttribute->attributeType();
507 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
508 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
509 for (int i = 0; i < aSelectionListAttr->size(); i++) {
510 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
511 QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
512 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
513 myListControl->addItem(anItem);
516 else if (aType == ModelAPI_AttributeRefList::typeId()) {
517 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
518 for (int i = 0; i < aRefListAttr->size(); i++) {
519 ObjectPtr anObject = aRefListAttr->object(i);
520 if (anObject.get()) {
521 QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
523 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
524 myListControl->addItem(anItem);
528 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
529 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
530 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
531 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
533 if (anAttribute.get()) {
534 std::string anAttrName = generateName(anAttribute, myWorkshop);
535 aName = QString::fromStdString(anAttrName);
538 ObjectPtr anObject = aRefAttrListAttr->object(i);
539 if (anObject.get()) {
540 aName = anObject->data()->name().c_str();
543 QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
544 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
545 myListControl->addItem(anItem);
549 // We have to call repaint because sometimes the List control is not updated
550 myListControl->repaint();
553 //********************************************************************
554 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
558 if (theType == "Vertices")
560 else if (theType == "Edges")
562 else if (theType == "Faces")
564 else if (theType == "Solids")
570 //********************************************************************
571 void ModuleBase_WidgetMultiSelector::onCopyItem()
573 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
575 foreach(QListWidgetItem* aItem, aItems) {
578 aRes += aItem->text();
580 if (!aRes.isEmpty()) {
581 QClipboard *clipboard = QApplication::clipboard();
582 clipboard->setText(aRes);
586 //********************************************************************
587 void ModuleBase_WidgetMultiSelector::onDeleteItem()
592 //********************************************************************
593 void ModuleBase_WidgetMultiSelector::onListSelection()
595 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
596 myCopyAction->setEnabled(!aItems.isEmpty());
597 myDeleteAction->setEnabled(!aItems.isEmpty());
599 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
603 //********************************************************************
604 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
606 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
607 foreach(QListWidgetItem* anItem, aItems) {
608 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
609 if (theAttributeIds.find(anIndex) == theAttributeIds.end())
610 theAttributeIds.insert(anIndex);
614 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
615 QList<ModuleBase_ViewerPrs>& theValues) const
617 if(myFeature.get() == NULL)
620 DataPtr aData = myFeature->data();
621 AttributePtr anAttribute = aData->attribute(attributeID());
622 std::string aType = anAttribute->attributeType();
623 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
624 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
625 for (int i = 0; i < aSelectionListAttr->size(); i++) {
626 // filter by attribute indices only if the container is not empty otherwise return all items
627 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
629 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
630 ResultPtr anObject = anAttr->context();
631 if (anObject.get()) {
633 std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
634 if (aShapePtr.get()) {
635 aShape = aShapePtr->impl<TopoDS_Shape>();
637 theValues.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
641 else if (aType == ModelAPI_AttributeRefList::typeId()) {
642 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
643 for (int i = 0; i < aRefListAttr->size(); i++) {
644 // filter by attribute indices only if the container is not empty otherwise return all items
645 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
647 ObjectPtr anObject = aRefListAttr->object(i);
648 if (anObject.get()) {
649 theValues.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
653 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
654 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
655 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
656 // filter by attribute indices only if the container is not empty otherwise return all items
657 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
659 ObjectPtr anObject = aRefAttrListAttr->object(i);
663 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
664 if (anAttribute.get()) {
665 GeomShapePtr aGeomShape = myWorkshop->module()->findShape(anAttribute);
666 if (aGeomShape.get()) {
667 aShape = aGeomShape->impl<TopoDS_Shape>();
669 theValues.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
675 void ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
676 (QList<ModuleBase_ViewerPrs>& theValues)
678 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
679 DataPtr aData = myFeature->data();
680 AttributePtr anAttribute = aData->attribute(attributeID());
681 std::string aType = anAttribute->attributeType();
682 std::set<GeomShapePtr> aShapes;
683 std::set<int> anIndicesToBeRemoved;
684 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
685 // iteration through data model to find not selected elements to remove them
686 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
687 for (int i = 0; i < aSelectionListAttr->size(); i++) {
688 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
689 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
691 anIndicesToBeRemoved.insert(i);
693 aSelectionListAttr->remove(anIndicesToBeRemoved);
695 else if (aType == ModelAPI_AttributeRefList::typeId()) {
696 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
697 for (int i = 0; i < aRefListAttr->size(); i++) {
698 ObjectPtr anObject = aRefListAttr->object(i);
699 if (anObject.get()) {
700 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
702 anIndicesToBeRemoved.insert(i);
705 aRefListAttr->remove(anIndicesToBeRemoved);
707 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
708 std::set<AttributePtr> anAttributes;
709 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
712 for (; anIt != aLast; anIt++) {
713 ModuleBase_ViewerPrs aPrs = *anIt;
714 getGeomSelection(aPrs, anObject, aShape);
715 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
716 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
717 anAttributes.insert(anAttr);
720 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
721 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
723 if (aRefAttrListAttr->isAttribute(i)) {
724 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
725 aFound = anAttributes.find(anAttribute) != anAttributes.end();
728 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
731 anIndicesToBeRemoved.insert(i);
733 aRefAttrListAttr->remove(anIndicesToBeRemoved);
737 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
738 (QList<ModuleBase_ViewerPrs>& theValues)
740 // convert prs list to objects map
741 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
742 std::set<GeomShapePtr> aShapes;
743 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
746 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
747 for (; anIt != aLast; anIt++) {
748 ModuleBase_ViewerPrs aPrs = *anIt;
749 getGeomSelection(aPrs, anObject, aShape);
751 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
752 aShapes = aGeomSelection[anObject];
753 // we need to know if there was an empty shape in selection for the object
755 aShape = anEmptyShape;
756 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
757 aShapes.insert(aShape);
758 aGeomSelection[anObject] = aShapes;
760 return aGeomSelection;
763 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
764 const GeomShapePtr& theShape,
765 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
768 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
769 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
770 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
771 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
772 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
773 for (; anIt != aLast && !aFound; anIt++) {
774 GeomShapePtr aCShape = *anIt;
776 aFound = aCShape->isEqual(aShape);