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 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData),
91 QGridLayout* aMainLay = new QGridLayout(this);
92 ModuleBase_Tools::adjustMargins(aMainLay);
94 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
95 aMainLay->addWidget(aTypeLabel, 0, 0);
97 myTypeCombo = new QComboBox(this);
98 // There is no sense to parameterize list of types while we can not parameterize selection mode
100 std::string aPropertyTypes = theData->getProperty("type_choice");
101 QString aTypesStr = aPropertyTypes.c_str();
102 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
104 myIsUseChoice = theData->getBooleanAttribute("use_choice", true);
106 if (!aShapeTypes.empty())
107 myTypeCombo->addItems(aShapeTypes);
108 aMainLay->addWidget(myTypeCombo, 0, 1);
109 // if the xml definition contains one type, the controls to select a type should not be shown
110 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
111 aTypeLabel->setVisible(false);
112 myTypeCombo->setVisible(false);
115 std::string aLabelText = theData->getProperty("label");
116 QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
117 : tr("Selected objects:"), this);
118 aMainLay->addWidget(aListLabel, 1, 0);
119 // if the xml definition contains one type, an information label should be shown near to the latest
120 if (aShapeTypes.size() <= 1) {
121 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
122 if (!aLabelIcon.isEmpty()) {
123 QLabel* aSelectedLabel = new QLabel("", this);
124 aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
125 aMainLay->addWidget(aSelectedLabel, 1, 1);
127 aMainLay->setColumnStretch(2, 1);
130 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
131 myListControl = new CustomListWidget(this);
132 QString anObjName = QString::fromStdString(attributeID());
133 myListControl->setObjectName(anObjName);
134 myListControl->setToolTip(aToolTip);
135 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
137 aMainLay->addWidget(myListControl, 2, 0, 1, -1);
138 aMainLay->setRowStretch(2, 1);
139 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
140 //aMainLay->setRowMinimumHeight(3, 20);
141 //this->setLayout(aMainLay);
142 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
144 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
145 myCopyAction->setShortcut(QKeySequence::Copy);
146 myCopyAction->setEnabled(false);
147 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
148 myListControl->addAction(myCopyAction);
150 myDeleteAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
151 myDeleteAction->setEnabled(false);
152 connect(myDeleteAction, SIGNAL(triggered(bool)), SLOT(onDeleteItem()));
153 myListControl->addAction(myDeleteAction);
155 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
156 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
159 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
163 //********************************************************************
164 void ModuleBase_WidgetMultiSelector::activateCustom()
166 ModuleBase_WidgetSelector::activateCustom();
168 myWorkshop->module()->activateCustomPrs(myFeature,
169 ModuleBase_IModule::CustomizeHighlightedObjects, true);
172 //********************************************************************
173 void ModuleBase_WidgetMultiSelector::deactivate()
175 ModuleBase_WidgetSelector::deactivate();
177 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
180 //********************************************************************
181 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
183 // the value is stored on the selection changed signal processing
184 // A rare case when plugin was not loaded.
188 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
189 std::string aType = anAttribute->attributeType();
190 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
191 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
192 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
197 //********************************************************************
198 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
200 // A rare case when plugin was not loaded.
204 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
205 std::string aType = anAttribute->attributeType();
206 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
207 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
208 // Restore shape type
209 std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
210 if (!aSelectionType.empty())
211 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
213 updateSelectionList();
217 //********************************************************************
218 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
220 ModuleBase_WidgetValidated::storeAttributeValue();
222 DataPtr aData = myFeature->data();
223 AttributePtr anAttribute = aData->attribute(attributeID());
224 std::string aType = anAttribute->attributeType();
225 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
226 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
227 mySelectionType = aSelectionListAttr->selectionType();
228 mySelectionCount = aSelectionListAttr->size();
230 else if (aType == ModelAPI_AttributeRefList::typeId()) {
231 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
232 mySelectionCount = aRefListAttr->size();
234 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
235 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
236 mySelectionCount = aRefAttrListAttr->size();
240 //********************************************************************
241 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool theValid)
243 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
245 DataPtr aData = myFeature->data();
246 AttributePtr anAttribute = aData->attribute(attributeID());
247 std::string aType = anAttribute->attributeType();
248 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
249 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
250 aSelectionListAttr->setSelectionType(mySelectionType);
252 // restore selection in the attribute. Indeed there is only one stored object
253 int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
254 for (int i = 0; i < aCountAppened; i++)
255 aSelectionListAttr->removeLast();
257 else if (aType == ModelAPI_AttributeRefList::typeId()) {
258 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
259 // restore objects in the attribute. Indeed there is only one stored object
260 int aCountAppened = aRefListAttr->size() - mySelectionCount;
261 for (int i = 0; i < aCountAppened; i++)
262 aRefListAttr->removeLast();
264 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
265 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
266 // restore objects in the attribute. Indeed there is only one stored object
267 int aCountAppened = aRefAttrListAttr->size() - mySelectionCount;
268 for (int i = 0; i < aCountAppened; i++)
269 aRefAttrListAttr->removeLast();
273 //********************************************************************
274 void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject,
275 GeomShapePtr theShape)
277 DataPtr aData = myFeature->data();
278 AttributePtr anAttribute = aData->attribute(attributeID());
279 std::string aType = anAttribute->attributeType();
280 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
281 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
282 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
283 if (!aSelectionListAttr->isInList(aResult, theShape, myIsInValidate))
284 aSelectionListAttr->append(aResult, theShape, myIsInValidate);
286 else if (aType == ModelAPI_AttributeRefList::typeId()) {
287 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
288 if (!aRefListAttr->isInList(theSelectedObject))
289 aRefListAttr->append(theSelectedObject);
291 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
292 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
293 AttributePtr anAttribute = myWorkshop->module()->findAttribute(theSelectedObject, theShape);
295 if (anAttribute.get()) {
296 if (!aRefAttrListAttr->isInList(anAttribute))
297 aRefAttrListAttr->append(anAttribute);
300 if (!aRefAttrListAttr->isInList(theSelectedObject))
301 aRefAttrListAttr->append(theSelectedObject);
306 //********************************************************************
307 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
308 const bool theToValidate)
310 QList<ModuleBase_ViewerPrs> aSkippedValues;
312 /// remove unused objects from the model attribute.
313 /// It should be performed before new attributes append.
314 removeUnusedAttributeObjects(theValues);
316 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
318 for (; anIt != aLast; anIt++) {
319 ModuleBase_ViewerPrs aValue = *anIt;
320 bool aProcessed = false;
321 if (!theToValidate || isValidInFilters(aValue)) {
322 aProcessed = setSelectionCustom(aValue);
325 aSkippedValues.append(aValue);
326 // if there is at least one set, the result is true
327 isDone = isDone || aProcessed;
329 // updateObject - to update/redisplay feature
330 // it is commented in order to perfom it outside the method
332 //updateObject(myFeature);
333 // this emit is necessary to call store/restore method an restore type of selection
334 //emit valuesChanged();
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 bool aWasActivated = 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);