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 /// remove unused objects from the model attribute.
314 /// It should be performed before new attributes append.
315 removeUnusedAttributeObjects(theValues);
317 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
319 for (; anIt != aLast; anIt++) {
320 ModuleBase_ViewerPrs aValue = *anIt;
321 bool aProcessed = false;
322 if (!theToValidate || isValidInFilters(aValue)) {
323 aProcessed = setSelectionCustom(aValue);
326 aSkippedValues.append(aValue);
327 // if there is at least one set, the result is true
328 isDone = isDone || aProcessed;
330 // updateObject - to update/redisplay feature
331 // it is commented in order to perfom it outside the method
333 //updateObject(myFeature);
334 // this emit is necessary to call store/restore method an restore type of selection
335 //emit valuesChanged();
339 if (!aSkippedValues.empty())
340 theValues.append(aSkippedValues);
345 //********************************************************************
346 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrs>& theValues)
348 std::set<int> anAttributeIds;
349 getSelectedAttributeIndices(anAttributeIds);
350 if (!anAttributeIds.empty())
351 convertIndicesToViewerSelection(anAttributeIds, theValues);
354 //********************************************************************
355 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
357 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
359 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
360 aValid = aResult.get() != NULL;
363 // We can not select a result of our feature
364 const std::list<ResultPtr>& aResList = myFeature->results();
365 std::list<ResultPtr>::const_iterator aIt;
366 bool isSkipSelf = false;
367 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
368 if ((*aIt) == aResult) {
381 //********************************************************************
382 bool ModuleBase_WidgetMultiSelector::processDelete()
384 // find attribute indices to delete
385 std::set<int> anAttributeIds;
386 getSelectedAttributeIndices(anAttributeIds);
388 // refill attribute by the items which indices are not in the list of ids
390 DataPtr aData = myFeature->data();
391 AttributePtr anAttribute = aData->attribute(attributeID());
392 std::string aType = anAttribute->attributeType();
393 aDone = !anAttributeIds.empty();
394 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
395 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
396 aSelectionListAttr->remove(anAttributeIds);
399 else if (aType == ModelAPI_AttributeRefList::typeId()) {
400 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
401 aRefListAttr->remove(anAttributeIds);
403 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
404 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
405 aRefAttrListAttr->remove(anAttributeIds);
409 // update object is necessary to flush update signal. It leads to objects references map update
410 // and the operation presentation will not contain deleted items visualized as parameters of
412 updateObject(myFeature);
415 myWorkshop->setSelected(getAttributeSelection());
420 //********************************************************************
421 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
423 QList<QWidget*> result;
424 //result << myTypeCombo;
425 result << myListControl;
429 //********************************************************************
430 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
432 activateSelectionAndFilters(true);
433 QList<ModuleBase_ViewerPrs> anEmptyList;
434 // This method will call Selection changed event which will call onSelectionChanged
435 // To clear mySelection, myListControl and storeValue()
436 // So, we don't need to call it
437 myWorkshop->setSelected(anEmptyList);
440 void ModuleBase_WidgetMultiSelector::updateFocus()
442 // Set focus to List control in order to make possible
443 // to use Tab key for transfer the focus to next widgets
444 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
445 ModuleBase_Tools::setFocus(myListControl,
446 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
449 //********************************************************************
450 void ModuleBase_WidgetMultiSelector::updateSelectionName()
454 //********************************************************************
455 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
457 QIntList aShapeTypes;
459 if (myTypeCombo->count() > 1 && myIsUseChoice) {
460 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
463 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
464 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
465 aShapeTypes.append(aType);
466 if (aType == TopAbs_SOLID)
467 aShapeTypes.append(TopAbs_COMPSOLID);
473 //********************************************************************
474 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
476 QString aShapeTypeName;
478 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
479 aShapeTypeName = myTypeCombo->itemText(idx);
480 TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
481 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
482 activateSelectionAndFilters(false);
483 bool isBlocked = myTypeCombo->blockSignals(true);
484 myTypeCombo->setCurrentIndex(idx);
485 myTypeCombo->blockSignals(isBlocked);
487 activateSelectionAndFilters(true);
493 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
495 QList<ModuleBase_ViewerPrs> aSelected;
496 convertIndicesToViewerSelection(std::set<int>(), aSelected);
500 //********************************************************************
501 void ModuleBase_WidgetMultiSelector::updateSelectionList()
503 myListControl->clear();
505 DataPtr aData = myFeature->data();
506 AttributePtr anAttribute = aData->attribute(attributeID());
507 std::string aType = anAttribute->attributeType();
508 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
509 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
510 for (int i = 0; i < aSelectionListAttr->size(); i++) {
511 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
512 QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
513 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
514 myListControl->addItem(anItem);
517 else if (aType == ModelAPI_AttributeRefList::typeId()) {
518 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
519 for (int i = 0; i < aRefListAttr->size(); i++) {
520 ObjectPtr anObject = aRefListAttr->object(i);
521 if (anObject.get()) {
522 QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
524 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
525 myListControl->addItem(anItem);
529 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
530 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
531 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
532 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
534 if (anAttribute.get()) {
535 std::string anAttrName = generateName(anAttribute, myWorkshop);
536 aName = QString::fromStdString(anAttrName);
539 ObjectPtr anObject = aRefAttrListAttr->object(i);
540 if (anObject.get()) {
541 aName = anObject->data()->name().c_str();
544 QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
545 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
546 myListControl->addItem(anItem);
550 // We have to call repaint because sometimes the List control is not updated
551 myListControl->repaint();
554 //********************************************************************
555 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
559 if (theType == "Vertices")
561 else if (theType == "Edges")
563 else if (theType == "Faces")
565 else if (theType == "Solids")
571 //********************************************************************
572 void ModuleBase_WidgetMultiSelector::onCopyItem()
574 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
576 foreach(QListWidgetItem* aItem, aItems) {
579 aRes += aItem->text();
581 if (!aRes.isEmpty()) {
582 QClipboard *clipboard = QApplication::clipboard();
583 clipboard->setText(aRes);
587 //********************************************************************
588 void ModuleBase_WidgetMultiSelector::onDeleteItem()
593 //********************************************************************
594 void ModuleBase_WidgetMultiSelector::onListSelection()
596 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
597 myCopyAction->setEnabled(!aItems.isEmpty());
598 myDeleteAction->setEnabled(!aItems.isEmpty());
600 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
604 //********************************************************************
605 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
607 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
608 foreach(QListWidgetItem* anItem, aItems) {
609 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
610 if (theAttributeIds.find(anIndex) == theAttributeIds.end())
611 theAttributeIds.insert(anIndex);
615 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
616 QList<ModuleBase_ViewerPrs>& theValues) const
618 if(myFeature.get() == NULL)
621 DataPtr aData = myFeature->data();
622 AttributePtr anAttribute = aData->attribute(attributeID());
623 std::string aType = anAttribute->attributeType();
624 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
625 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
626 for (int i = 0; i < aSelectionListAttr->size(); i++) {
627 // filter by attribute indices only if the container is not empty otherwise return all items
628 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
630 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
631 ResultPtr anObject = anAttr->context();
632 if (anObject.get()) {
634 std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
635 if (aShapePtr.get()) {
636 aShape = aShapePtr->impl<TopoDS_Shape>();
638 theValues.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
642 else if (aType == ModelAPI_AttributeRefList::typeId()) {
643 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
644 for (int i = 0; i < aRefListAttr->size(); i++) {
645 // filter by attribute indices only if the container is not empty otherwise return all items
646 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
648 ObjectPtr anObject = aRefListAttr->object(i);
649 if (anObject.get()) {
650 theValues.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
654 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
655 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
656 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
657 // filter by attribute indices only if the container is not empty otherwise return all items
658 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
660 ObjectPtr anObject = aRefAttrListAttr->object(i);
664 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
665 if (anAttribute.get()) {
666 GeomShapePtr aGeomShape = myWorkshop->module()->findShape(anAttribute);
667 if (aGeomShape.get()) {
668 aShape = aGeomShape->impl<TopoDS_Shape>();
670 theValues.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
676 void ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
677 (QList<ModuleBase_ViewerPrs>& theValues)
679 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
680 DataPtr aData = myFeature->data();
681 AttributePtr anAttribute = aData->attribute(attributeID());
682 std::string aType = anAttribute->attributeType();
683 std::set<GeomShapePtr> aShapes;
684 std::set<int> anIndicesToBeRemoved;
685 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
686 // iteration through data model to find not selected elements to remove them
687 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
688 for (int i = 0; i < aSelectionListAttr->size(); i++) {
689 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
690 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
692 anIndicesToBeRemoved.insert(i);
694 aSelectionListAttr->remove(anIndicesToBeRemoved);
696 else if (aType == ModelAPI_AttributeRefList::typeId()) {
697 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
698 for (int i = 0; i < aRefListAttr->size(); i++) {
699 ObjectPtr anObject = aRefListAttr->object(i);
700 if (anObject.get()) {
701 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
703 anIndicesToBeRemoved.insert(i);
706 aRefListAttr->remove(anIndicesToBeRemoved);
708 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
709 std::set<AttributePtr> anAttributes;
710 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
713 for (; anIt != aLast; anIt++) {
714 ModuleBase_ViewerPrs aPrs = *anIt;
715 getGeomSelection(aPrs, anObject, aShape);
716 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
717 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
718 anAttributes.insert(anAttr);
721 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
722 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
724 if (aRefAttrListAttr->isAttribute(i)) {
725 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
726 aFound = anAttributes.find(anAttribute) != anAttributes.end();
729 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
732 anIndicesToBeRemoved.insert(i);
734 aRefAttrListAttr->remove(anIndicesToBeRemoved);
738 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
739 (QList<ModuleBase_ViewerPrs>& theValues)
741 // convert prs list to objects map
742 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
743 std::set<GeomShapePtr> aShapes;
744 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
747 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
748 for (; anIt != aLast; anIt++) {
749 ModuleBase_ViewerPrs aPrs = *anIt;
750 getGeomSelection(aPrs, anObject, aShape);
752 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
753 aShapes = aGeomSelection[anObject];
754 // we need to know if there was an empty shape in selection for the object
756 aShape = anEmptyShape;
757 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
758 aShapes.insert(aShape);
759 aGeomSelection[anObject] = aShapes;
761 return aGeomSelection;
764 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
765 const GeomShapePtr& theShape,
766 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
769 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
770 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
771 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
772 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
773 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
774 for (; anIt != aLast && !aFound; anIt++) {
775 GeomShapePtr aCShape = *anIt;
777 aFound = aCShape->isEqual(aShape);