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>
18 #include <ModuleBase_ViewerPrs.h>
19 #include <ModuleBase_IconFactory.h>
20 #include <ModuleBase_Events.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Object.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_AttributeRefAttrList.h>
27 #include <ModelAPI_Tools.h>
28 #include <ModelAPI_Events.h>
30 #include <Config_WidgetAPI.h>
32 #include <QGridLayout>
34 #include <QListWidget>
40 #include <QApplication>
43 #include <QMainWindow>
48 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
51 * Customization of a List Widget to make it to be placed on full width of container
53 class CustomListWidget : public QListWidget
57 /// \param theParent a parent widget
58 CustomListWidget( QWidget* theParent )
59 : QListWidget( theParent )
63 /// Redefinition of virtual method
64 virtual QSize sizeHint() const
66 int aHeight = 2*QFontMetrics( font() ).height();
67 QSize aSize = QListWidget::sizeHint();
68 return QSize( aSize.width(), aHeight );
71 /// Redefinition of virtual method
72 virtual QSize minimumSizeHint() const
74 int aHeight = 4/*2*/*QFontMetrics( font() ).height();
75 QSize aSize = QListWidget::minimumSizeHint();
76 return QSize( aSize.width(), aHeight );
80 // The code is necessary only for Linux because
81 //it can not update viewport on widget resize
83 void resizeEvent(QResizeEvent* theEvent)
85 QListWidget::resizeEvent(theEvent);
86 QTimer::singleShot(5, viewport(), SLOT(repaint()));
91 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
92 ModuleBase_IWorkshop* theWorkshop,
93 const Config_WidgetAPI* theData)
94 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
96 QGridLayout* aMainLay = new QGridLayout(this);
97 ModuleBase_Tools::adjustMargins(aMainLay);
99 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
100 aMainLay->addWidget(aTypeLabel, 0, 0);
102 myTypeCombo = new QComboBox(this);
103 // There is no sense to parameterize list of types while we can not parameterize selection mode
105 std::string aPropertyTypes = theData->getProperty("type_choice");
106 QString aTypesStr = aPropertyTypes.c_str();
107 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
109 myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
111 if (!aShapeTypes.empty())
112 myTypeCombo->addItems(aShapeTypes);
113 aMainLay->addWidget(myTypeCombo, 0, 1);
114 // if the xml definition contains one type, the controls to select a type should not be shown
115 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
116 aTypeLabel->setVisible(false);
117 myTypeCombo->setVisible(false);
120 QString aLabelText = translate(theData->getProperty("label"));
121 QLabel* aListLabel = new QLabel(aLabelText, this);
122 aMainLay->addWidget(aListLabel, 1, 0);
123 // if the xml definition contains one type, an information label
124 // should be shown near to the latest
125 if (aShapeTypes.size() <= 1) {
126 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
127 if (!aLabelIcon.isEmpty()) {
128 QLabel* aSelectedLabel = new QLabel("", this);
129 aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
130 aMainLay->addWidget(aSelectedLabel, 1, 1);
132 aMainLay->setColumnStretch(2, 1);
135 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
136 myListControl = new CustomListWidget(this);
137 QString anObjName = QString::fromStdString(attributeID());
138 myListControl->setObjectName(anObjName);
139 myListControl->setToolTip(aToolTip);
140 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
142 aMainLay->addWidget(myListControl, 2, 0, 1, -1);
143 aMainLay->setRowStretch(2, 1);
144 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
145 //aMainLay->setRowMinimumHeight(3, 20);
146 //this->setLayout(aMainLay);
147 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
149 myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
150 myWorkshop->desktop(), this, SLOT(onCopyItem()));
151 myCopyAction->setShortcut(QKeySequence::Copy);
152 myCopyAction->setEnabled(false);
153 myListControl->addAction(myCopyAction);
155 myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
156 myWorkshop->desktop(), this, SLOT(onDeleteItem()));
157 myDeleteAction->setEnabled(false);
158 myListControl->addAction(myDeleteAction);
160 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
161 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
163 myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
166 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
170 //********************************************************************
171 void ModuleBase_WidgetMultiSelector::activateCustom()
173 ModuleBase_WidgetSelector::activateCustom();
175 myWorkshop->module()->activateCustomPrs(myFeature,
176 ModuleBase_IModule::CustomizeHighlightedObjects, true);
179 //********************************************************************
180 void ModuleBase_WidgetMultiSelector::deactivate()
182 ModuleBase_WidgetSelector::deactivate();
184 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
187 //********************************************************************
188 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
190 // the value is stored on the selection changed signal processing
191 // A rare case when plugin was not loaded.
195 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
196 std::string aType = anAttribute->attributeType();
197 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
198 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
199 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
204 //********************************************************************
205 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
207 // A rare case when plugin was not loaded.
211 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
212 std::string aType = anAttribute->attributeType();
213 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
214 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
215 // Restore shape type
216 std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
217 if (!aSelectionType.empty())
218 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
220 updateSelectionList();
224 //********************************************************************
225 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
226 const bool theToValidate)
228 AttributeSelectionListPtr aSelectionListAttr;
229 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
230 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
231 if (aSelectionListAttr.get())
232 aSelectionListAttr->cashValues(true);
234 /// remove unused objects from the model attribute.
235 /// It should be performed before new attributes append.
236 bool isDone = removeUnusedAttributeObjects(theValues);
238 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
239 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
240 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
241 for (; anIt != aLast; anIt++) {
242 ModuleBase_ViewerPrsPtr aValue = *anIt;
243 // do not validate and append to attribute selection presentation if it exists in the attribute
246 getGeomSelection(aValue, anObject, aShape);
247 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
248 anAttributeValues.append(aValue);
251 if (theToValidate && !isValidInFilters(aValue))
252 anInvalidValues.append(aValue);
254 bool aHasInvalidValues = anInvalidValues.size() > 0;
256 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
257 ModuleBase_ViewerPrsPtr aValue = *anIt;
258 bool aProcessed = false;
259 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
260 anAttributeValues.contains(aValue))
262 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
263 // if there is at least one set, the result is true
264 isDone = isDone || aProcessed;
266 // updateObject - to update/redisplay feature
267 // it is commented in order to perfom it outside the method
269 //updateObject(myFeature);
270 // this emit is necessary to call store/restore method an restore type of selection
271 //emit valuesChanged();
274 if (aSelectionListAttr.get())
275 aSelectionListAttr->cashValues(false);
278 if (!anInvalidValues.empty())
279 theValues.append(anInvalidValues);
281 if (isDone) // may be the feature's result is not displayed, but attributes should be
282 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
283 true);/// hope that something is redisplayed by object updated
288 //********************************************************************
289 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
291 std::set<int> anAttributeIds;
292 getSelectedAttributeIndices(anAttributeIds);
293 if (!anAttributeIds.empty())
294 convertIndicesToViewerSelection(anAttributeIds, theValues);
297 //********************************************************************
298 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
300 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
302 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
303 aValid = aResult.get() != NULL;
306 // We can not select a result of our feature
307 std::list<ResultPtr> aResults;
308 ModelAPI_Tools::allResults(myFeature, aResults);
309 std::list<ResultPtr>::const_iterator aIt;
310 bool isSkipSelf = false;
311 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
312 if ((*aIt) == aResult) {
325 //********************************************************************
326 bool ModuleBase_WidgetMultiSelector::processDelete()
328 // find attribute indices to delete
329 std::set<int> anAttributeIds;
330 getSelectedAttributeIndices(anAttributeIds);
332 QModelIndexList aIndexes = myListControl->selectionModel()->selectedIndexes();
334 // refill attribute by the items which indices are not in the list of ids
336 DataPtr aData = myFeature->data();
337 AttributePtr anAttribute = aData->attribute(attributeID());
338 std::string aType = anAttribute->attributeType();
339 aDone = !anAttributeIds.empty();
340 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
341 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
342 aSelectionListAttr->remove(anAttributeIds);
345 else if (aType == ModelAPI_AttributeRefList::typeId()) {
346 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
347 aRefListAttr->remove(anAttributeIds);
349 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
350 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
351 aRefAttrListAttr->remove(anAttributeIds);
355 // update object is necessary to flush update signal. It leads to objects references map update
356 // and the operation presentation will not contain deleted items visualized as parameters of
358 updateObject(myFeature);
361 myWorkshop->setSelected(getAttributeSelection());
363 // may be the feature's result is not displayed, but attributes should be
364 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
365 true); /// hope that something is redisplayed by object updated
369 int aRows = myListControl->model()->rowCount();
371 foreach(QModelIndex aIndex, aIndexes) {
372 if (aIndex.row() < aRows)
373 myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
375 QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
376 myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
383 //********************************************************************
384 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
386 QList<QWidget*> result;
387 //result << myTypeCombo;
388 result << myListControl;
392 //********************************************************************
393 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
395 activateSelectionAndFilters(true);
399 /// store the selected type
400 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
401 std::string aType = anAttribute->attributeType();
402 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
403 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
404 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
407 // clear attribute values
408 DataPtr aData = myFeature->data();
409 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
410 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
411 aSelectionListAttr->clear();
413 else if (aType == ModelAPI_AttributeRefList::typeId()) {
414 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
415 aRefListAttr->clear();
417 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
418 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
419 aRefAttrListAttr->clear();
422 // update object is necessary to flush update signal. It leads to objects references map update
423 // and the operation presentation will not contain deleted items visualized as parameters of
425 updateObject(myFeature);
427 myWorkshop->setSelected(getAttributeSelection());
428 // may be the feature's result is not displayed, but attributes should be
429 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
430 true); /// hope that something is redisplayed by object updated
433 //********************************************************************
434 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
436 if (!myIsNeutralPointClear) {
437 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
438 // do not clear selected object
439 if (aSelected.size() == 0) {
440 if (!getAttributeSelection().empty()) {
441 // Restore selection in the viewer by the attribute selection list
442 // it should be postponed to exit from the selectionChanged processing
443 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
444 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
445 Events_Loop::loop()->flush(anEvent);
450 ModuleBase_WidgetSelector::onSelectionChanged();
453 void ModuleBase_WidgetMultiSelector::updateFocus()
455 // Set focus to List control in order to make possible
456 // to use Tab key for transfer the focus to next widgets
457 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
458 ModuleBase_Tools::setFocus(myListControl,
459 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
462 //********************************************************************
463 void ModuleBase_WidgetMultiSelector::updateSelectionName()
467 //********************************************************************
468 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
470 QIntList aShapeTypes;
472 if (myTypeCombo->count() > 1 && myIsUseChoice) {
473 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
476 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
477 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
482 //********************************************************************
483 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
485 QString aShapeTypeName;
487 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
488 aShapeTypeName = myTypeCombo->itemText(idx);
489 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
490 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
491 bool aWasActivated = activateSelectionAndFilters(false);
492 bool isBlocked = myTypeCombo->blockSignals(true);
493 myTypeCombo->setCurrentIndex(idx);
494 myTypeCombo->blockSignals(isBlocked);
496 activateSelectionAndFilters(true);
502 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
504 QList<ModuleBase_ViewerPrsPtr> aSelected;
505 convertIndicesToViewerSelection(std::set<int>(), aSelected);
509 //********************************************************************
510 void ModuleBase_WidgetMultiSelector::updateSelectionList()
512 myListControl->clear();
514 DataPtr aData = myFeature->data();
515 AttributePtr anAttribute = aData->attribute(attributeID());
516 std::string aType = anAttribute->attributeType();
517 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
518 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
519 for (int i = 0; i < aSelectionListAttr->size(); i++) {
520 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
521 QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
522 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
523 myListControl->addItem(anItem);
526 else if (aType == ModelAPI_AttributeRefList::typeId()) {
527 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
528 for (int i = 0; i < aRefListAttr->size(); i++) {
529 ObjectPtr anObject = aRefListAttr->object(i);
530 if (anObject.get()) {
531 QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
533 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
534 myListControl->addItem(anItem);
538 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
539 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
540 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
541 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
543 if (anAttribute.get()) {
544 std::string anAttrName = generateName(anAttribute, myWorkshop);
545 aName = QString::fromStdString(anAttrName);
548 ObjectPtr anObject = aRefAttrListAttr->object(i);
549 if (anObject.get()) {
550 aName = anObject->data()->name().c_str();
553 QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
554 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
555 myListControl->addItem(anItem);
559 // We have to call repaint because sometimes the List control is not updated
560 myListControl->repaint();
563 //********************************************************************
564 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
568 if (theType == "Vertices")
570 else if (theType == "Edges")
572 else if (theType == "Faces")
574 else if (theType == "Solids")
580 //********************************************************************
581 void ModuleBase_WidgetMultiSelector::clearSelection()
583 bool isClearInNeutralPoint = myIsNeutralPointClear;
584 myIsNeutralPointClear = true;
586 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
587 // This method will call Selection changed event which will call onSelectionChanged
588 // To clear mySelection, myListControl and storeValue()
589 // So, we don't need to call it
590 myWorkshop->setSelected(anEmptyList);
592 myIsNeutralPointClear = isClearInNeutralPoint;
595 //********************************************************************
596 void ModuleBase_WidgetMultiSelector::onCopyItem()
598 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
600 foreach(QListWidgetItem* aItem, aItems) {
603 aRes += aItem->text();
605 if (!aRes.isEmpty()) {
606 QClipboard *clipboard = QApplication::clipboard();
607 clipboard->setText(aRes);
611 //********************************************************************
612 void ModuleBase_WidgetMultiSelector::onDeleteItem()
617 //********************************************************************
618 void ModuleBase_WidgetMultiSelector::onListSelection()
620 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
621 myCopyAction->setEnabled(!aItems.isEmpty());
622 myDeleteAction->setEnabled(!aItems.isEmpty());
624 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
628 //********************************************************************
629 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
631 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
632 foreach(QListWidgetItem* anItem, aItems) {
633 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
634 if (theAttributeIds.find(anIndex) == theAttributeIds.end())
635 theAttributeIds.insert(anIndex);
639 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
640 QList<ModuleBase_ViewerPrsPtr>& theValues) const
642 if(myFeature.get() == NULL)
645 DataPtr aData = myFeature->data();
646 AttributePtr anAttribute = aData->attribute(attributeID());
647 std::string aType = anAttribute->attributeType();
648 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
649 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
650 for (int i = 0; i < aSelectionListAttr->size(); i++) {
651 // filter by attribute indices only if the container is not empty otherwise return all items
652 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
654 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
655 ResultPtr anObject = anAttr->context();
657 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
658 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
661 else if (aType == ModelAPI_AttributeRefList::typeId()) {
662 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
663 for (int i = 0; i < aRefListAttr->size(); i++) {
664 // filter by attribute indices only if the container is not empty otherwise return all items
665 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
667 ObjectPtr anObject = aRefListAttr->object(i);
668 if (anObject.get()) {
669 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
670 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
674 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
675 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
676 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
677 // filter by attribute indices only if the container is not empty otherwise return all items
678 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
680 ObjectPtr anObject = aRefAttrListAttr->object(i);
684 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
685 if (anAttribute.get()) {
686 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
687 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
688 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
694 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
695 (QList<ModuleBase_ViewerPrsPtr>& theValues)
699 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
700 DataPtr aData = myFeature->data();
701 AttributePtr anAttribute = aData->attribute(attributeID());
702 std::string aType = anAttribute->attributeType();
703 std::set<GeomShapePtr> aShapes;
704 std::set<int> anIndicesToBeRemoved;
705 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
706 // iteration through data model to find not selected elements to remove them
707 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
708 for (int i = 0; i < aSelectionListAttr->size(); i++) {
709 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
710 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
712 anIndicesToBeRemoved.insert(i);
714 isDone = anIndicesToBeRemoved.size() > 0;
715 aSelectionListAttr->remove(anIndicesToBeRemoved);
717 else if (aType == ModelAPI_AttributeRefList::typeId()) {
718 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
719 for (int i = 0; i < aRefListAttr->size(); i++) {
720 ObjectPtr anObject = aRefListAttr->object(i);
721 if (anObject.get()) {
722 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
724 anIndicesToBeRemoved.insert(i);
727 isDone = anIndicesToBeRemoved.size() > 0;
728 aRefListAttr->remove(anIndicesToBeRemoved);
730 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
731 std::set<AttributePtr> anAttributes;
732 QList<ModuleBase_ViewerPrsPtr>::const_iterator
733 anIt = theValues.begin(), aLast = theValues.end();
736 for (; anIt != aLast; anIt++) {
737 ModuleBase_ViewerPrsPtr aPrs = *anIt;
738 getGeomSelection(aPrs, anObject, aShape);
739 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
740 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
741 anAttributes.insert(anAttr);
744 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
745 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
747 if (aRefAttrListAttr->isAttribute(i)) {
748 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
749 aFound = anAttributes.find(anAttribute) != anAttributes.end();
752 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
755 anIndicesToBeRemoved.insert(i);
757 isDone = anIndicesToBeRemoved.size() > 0;
758 aRefAttrListAttr->remove(anIndicesToBeRemoved);
764 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
765 (QList<ModuleBase_ViewerPrsPtr>& theValues)
767 // convert prs list to objects map
768 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
769 std::set<GeomShapePtr> aShapes;
770 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
773 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
774 for (; anIt != aLast; anIt++) {
775 ModuleBase_ViewerPrsPtr aPrs = *anIt;
776 getGeomSelection(aPrs, anObject, aShape);
778 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
779 aShapes = aGeomSelection[anObject];
780 // we need to know if there was an empty shape in selection for the object
782 aShape = anEmptyShape;
783 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
784 aShapes.insert(aShape);
785 aGeomSelection[anObject] = aShapes;
787 return aGeomSelection;
790 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
791 const GeomShapePtr& theShape,
792 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
795 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
796 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
797 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
798 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
799 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
800 for (; anIt != aLast && !aFound; anIt++) {
801 GeomShapePtr aCShape = *anIt;
803 aFound = aCShape->isEqual(aShape);