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 std::string aLabelText = theData->getProperty("label");
121 QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
122 : tr("Selected objects:"), this);
123 aMainLay->addWidget(aListLabel, 1, 0);
124 // if the xml definition contains one type, an information label
125 // should be shown near to the latest
126 if (aShapeTypes.size() <= 1) {
127 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
128 if (!aLabelIcon.isEmpty()) {
129 QLabel* aSelectedLabel = new QLabel("", this);
130 aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
131 aMainLay->addWidget(aSelectedLabel, 1, 1);
133 aMainLay->setColumnStretch(2, 1);
136 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
137 myListControl = new CustomListWidget(this);
138 QString anObjName = QString::fromStdString(attributeID());
139 myListControl->setObjectName(anObjName);
140 myListControl->setToolTip(aToolTip);
141 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
143 aMainLay->addWidget(myListControl, 2, 0, 1, -1);
144 aMainLay->setRowStretch(2, 1);
145 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
146 //aMainLay->setRowMinimumHeight(3, 20);
147 //this->setLayout(aMainLay);
148 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
150 myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
151 myWorkshop->desktop(), this, SLOT(onCopyItem()));
152 myCopyAction->setShortcut(QKeySequence::Copy);
153 myCopyAction->setEnabled(false);
154 myListControl->addAction(myCopyAction);
156 myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
157 myWorkshop->desktop(), this, SLOT(onDeleteItem()));
158 myDeleteAction->setEnabled(false);
159 myListControl->addAction(myDeleteAction);
161 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
162 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
164 myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
167 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
171 //********************************************************************
172 void ModuleBase_WidgetMultiSelector::activateCustom()
174 ModuleBase_WidgetSelector::activateCustom();
176 myWorkshop->module()->activateCustomPrs(myFeature,
177 ModuleBase_IModule::CustomizeHighlightedObjects, true);
180 //********************************************************************
181 void ModuleBase_WidgetMultiSelector::deactivate()
183 ModuleBase_WidgetSelector::deactivate();
185 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
188 //********************************************************************
189 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
191 // the value is stored on the selection changed signal processing
192 // A rare case when plugin was not loaded.
196 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
197 std::string aType = anAttribute->attributeType();
198 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
199 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
200 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
205 //********************************************************************
206 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
208 // A rare case when plugin was not loaded.
212 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
213 std::string aType = anAttribute->attributeType();
214 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
215 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
216 // Restore shape type
217 std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
218 if (!aSelectionType.empty())
219 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
221 updateSelectionList();
225 //********************************************************************
226 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
227 const bool theToValidate)
229 AttributeSelectionListPtr aSelectionListAttr;
230 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
231 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
232 if (aSelectionListAttr.get())
233 aSelectionListAttr->cashValues(true);
235 /// remove unused objects from the model attribute.
236 /// It should be performed before new attributes append.
237 bool isDone = removeUnusedAttributeObjects(theValues);
239 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
240 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
241 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
242 for (; anIt != aLast; anIt++) {
243 ModuleBase_ViewerPrsPtr aValue = *anIt;
244 // do not validate and append to attribute selection presentation if it exists in the attribute
247 getGeomSelection(aValue, anObject, aShape);
248 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
249 anAttributeValues.append(aValue);
252 if (theToValidate && !isValidInFilters(aValue))
253 anInvalidValues.append(aValue);
255 bool aHasInvalidValues = anInvalidValues.size() > 0;
257 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
258 ModuleBase_ViewerPrsPtr aValue = *anIt;
259 bool aProcessed = false;
260 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
261 anAttributeValues.contains(aValue))
263 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
264 // if there is at least one set, the result is true
265 isDone = isDone || aProcessed;
267 // updateObject - to update/redisplay feature
268 // it is commented in order to perfom it outside the method
270 //updateObject(myFeature);
271 // this emit is necessary to call store/restore method an restore type of selection
272 //emit valuesChanged();
275 if (aSelectionListAttr.get())
276 aSelectionListAttr->cashValues(false);
279 if (!anInvalidValues.empty())
280 theValues.append(anInvalidValues);
282 if (isDone) // may be the feature's result is not displayed, but attributes should be
283 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
284 true);/// hope that something is redisplayed by object updated
289 //********************************************************************
290 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
292 std::set<int> anAttributeIds;
293 getSelectedAttributeIndices(anAttributeIds);
294 if (!anAttributeIds.empty())
295 convertIndicesToViewerSelection(anAttributeIds, theValues);
298 //********************************************************************
299 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
301 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
303 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
304 aValid = aResult.get() != NULL;
307 // We can not select a result of our feature
308 std::list<ResultPtr> aResults;
309 ModelAPI_Tools::allResults(myFeature, aResults);
310 std::list<ResultPtr>::const_iterator aIt;
311 bool isSkipSelf = false;
312 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
313 if ((*aIt) == aResult) {
326 //********************************************************************
327 bool ModuleBase_WidgetMultiSelector::processDelete()
329 // find attribute indices to delete
330 std::set<int> anAttributeIds;
331 getSelectedAttributeIndices(anAttributeIds);
333 QModelIndexList aIndexes = myListControl->selectionModel()->selectedIndexes();
335 // refill attribute by the items which indices are not in the list of ids
337 DataPtr aData = myFeature->data();
338 AttributePtr anAttribute = aData->attribute(attributeID());
339 std::string aType = anAttribute->attributeType();
340 aDone = !anAttributeIds.empty();
341 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
342 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
343 aSelectionListAttr->remove(anAttributeIds);
346 else if (aType == ModelAPI_AttributeRefList::typeId()) {
347 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
348 aRefListAttr->remove(anAttributeIds);
350 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
351 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
352 aRefAttrListAttr->remove(anAttributeIds);
356 // update object is necessary to flush update signal. It leads to objects references map update
357 // and the operation presentation will not contain deleted items visualized as parameters of
359 updateObject(myFeature);
362 myWorkshop->setSelected(getAttributeSelection());
364 // may be the feature's result is not displayed, but attributes should be
365 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
366 true); /// hope that something is redisplayed by object updated
370 int aRows = myListControl->model()->rowCount();
372 foreach(QModelIndex aIndex, aIndexes) {
373 if (aIndex.row() < aRows)
374 myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
376 QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
377 myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
384 //********************************************************************
385 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
387 QList<QWidget*> result;
388 //result << myTypeCombo;
389 result << myListControl;
393 //********************************************************************
394 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
396 activateSelectionAndFilters(true);
400 /// store the selected type
401 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
402 std::string aType = anAttribute->attributeType();
403 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
404 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
405 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
408 // clear attribute values
409 DataPtr aData = myFeature->data();
410 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
411 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
412 aSelectionListAttr->clear();
414 else if (aType == ModelAPI_AttributeRefList::typeId()) {
415 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
416 aRefListAttr->clear();
418 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
419 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
420 aRefAttrListAttr->clear();
423 // update object is necessary to flush update signal. It leads to objects references map update
424 // and the operation presentation will not contain deleted items visualized as parameters of
426 updateObject(myFeature);
428 myWorkshop->setSelected(getAttributeSelection());
429 // may be the feature's result is not displayed, but attributes should be
430 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
431 true); /// hope that something is redisplayed by object updated
434 //********************************************************************
435 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
437 if (!myIsNeutralPointClear) {
438 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
439 // do not clear selected object
440 if (aSelected.size() == 0) {
441 if (!getAttributeSelection().empty()) {
442 // Restore selection in the viewer by the attribute selection list
443 // it should be postponed to exit from the selectionChanged processing
444 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
445 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
446 Events_Loop::loop()->flush(anEvent);
451 ModuleBase_WidgetSelector::onSelectionChanged();
454 void ModuleBase_WidgetMultiSelector::updateFocus()
456 // Set focus to List control in order to make possible
457 // to use Tab key for transfer the focus to next widgets
458 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
459 ModuleBase_Tools::setFocus(myListControl,
460 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
463 //********************************************************************
464 void ModuleBase_WidgetMultiSelector::updateSelectionName()
468 //********************************************************************
469 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
471 QIntList aShapeTypes;
473 if (myTypeCombo->count() > 1 && myIsUseChoice) {
474 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
477 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
478 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
483 //********************************************************************
484 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
486 QString aShapeTypeName;
488 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
489 aShapeTypeName = myTypeCombo->itemText(idx);
490 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
491 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
492 bool aWasActivated = activateSelectionAndFilters(false);
493 bool isBlocked = myTypeCombo->blockSignals(true);
494 myTypeCombo->setCurrentIndex(idx);
495 myTypeCombo->blockSignals(isBlocked);
497 activateSelectionAndFilters(true);
503 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
505 QList<ModuleBase_ViewerPrsPtr> aSelected;
506 convertIndicesToViewerSelection(std::set<int>(), aSelected);
510 //********************************************************************
511 void ModuleBase_WidgetMultiSelector::updateSelectionList()
513 myListControl->clear();
515 DataPtr aData = myFeature->data();
516 AttributePtr anAttribute = aData->attribute(attributeID());
517 std::string aType = anAttribute->attributeType();
518 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
519 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
520 for (int i = 0; i < aSelectionListAttr->size(); i++) {
521 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
522 QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
523 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
524 myListControl->addItem(anItem);
527 else if (aType == ModelAPI_AttributeRefList::typeId()) {
528 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
529 for (int i = 0; i < aRefListAttr->size(); i++) {
530 ObjectPtr anObject = aRefListAttr->object(i);
531 if (anObject.get()) {
532 QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
534 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
535 myListControl->addItem(anItem);
539 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
540 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
541 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
542 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
544 if (anAttribute.get()) {
545 std::string anAttrName = generateName(anAttribute, myWorkshop);
546 aName = QString::fromStdString(anAttrName);
549 ObjectPtr anObject = aRefAttrListAttr->object(i);
550 if (anObject.get()) {
551 aName = anObject->data()->name().c_str();
554 QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
555 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
556 myListControl->addItem(anItem);
560 // We have to call repaint because sometimes the List control is not updated
561 myListControl->repaint();
564 //********************************************************************
565 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
569 if (theType == "Vertices")
571 else if (theType == "Edges")
573 else if (theType == "Faces")
575 else if (theType == "Solids")
581 //********************************************************************
582 void ModuleBase_WidgetMultiSelector::clearSelection()
584 bool isClearInNeutralPoint = myIsNeutralPointClear;
585 myIsNeutralPointClear = true;
587 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
588 // This method will call Selection changed event which will call onSelectionChanged
589 // To clear mySelection, myListControl and storeValue()
590 // So, we don't need to call it
591 myWorkshop->setSelected(anEmptyList);
593 myIsNeutralPointClear = isClearInNeutralPoint;
596 //********************************************************************
597 void ModuleBase_WidgetMultiSelector::onCopyItem()
599 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
601 foreach(QListWidgetItem* aItem, aItems) {
604 aRes += aItem->text();
606 if (!aRes.isEmpty()) {
607 QClipboard *clipboard = QApplication::clipboard();
608 clipboard->setText(aRes);
612 //********************************************************************
613 void ModuleBase_WidgetMultiSelector::onDeleteItem()
618 //********************************************************************
619 void ModuleBase_WidgetMultiSelector::onListSelection()
621 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
622 myCopyAction->setEnabled(!aItems.isEmpty());
623 myDeleteAction->setEnabled(!aItems.isEmpty());
625 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
629 //********************************************************************
630 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
632 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
633 foreach(QListWidgetItem* anItem, aItems) {
634 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
635 if (theAttributeIds.find(anIndex) == theAttributeIds.end())
636 theAttributeIds.insert(anIndex);
640 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
641 QList<ModuleBase_ViewerPrsPtr>& theValues) const
643 if(myFeature.get() == NULL)
646 DataPtr aData = myFeature->data();
647 AttributePtr anAttribute = aData->attribute(attributeID());
648 std::string aType = anAttribute->attributeType();
649 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
650 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
651 for (int i = 0; i < aSelectionListAttr->size(); i++) {
652 // filter by attribute indices only if the container is not empty otherwise return all items
653 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
655 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
656 ResultPtr anObject = anAttr->context();
658 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
659 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
662 else if (aType == ModelAPI_AttributeRefList::typeId()) {
663 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
664 for (int i = 0; i < aRefListAttr->size(); i++) {
665 // filter by attribute indices only if the container is not empty otherwise return all items
666 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
668 ObjectPtr anObject = aRefListAttr->object(i);
669 if (anObject.get()) {
670 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
671 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
675 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
676 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
677 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
678 // filter by attribute indices only if the container is not empty otherwise return all items
679 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
681 ObjectPtr anObject = aRefAttrListAttr->object(i);
685 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
686 if (anAttribute.get()) {
687 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
688 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
689 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
695 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
696 (QList<ModuleBase_ViewerPrsPtr>& theValues)
700 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
701 DataPtr aData = myFeature->data();
702 AttributePtr anAttribute = aData->attribute(attributeID());
703 std::string aType = anAttribute->attributeType();
704 std::set<GeomShapePtr> aShapes;
705 std::set<int> anIndicesToBeRemoved;
706 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
707 // iteration through data model to find not selected elements to remove them
708 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
709 for (int i = 0; i < aSelectionListAttr->size(); i++) {
710 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
711 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
713 anIndicesToBeRemoved.insert(i);
715 isDone = anIndicesToBeRemoved.size() > 0;
716 aSelectionListAttr->remove(anIndicesToBeRemoved);
718 else if (aType == ModelAPI_AttributeRefList::typeId()) {
719 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
720 for (int i = 0; i < aRefListAttr->size(); i++) {
721 ObjectPtr anObject = aRefListAttr->object(i);
722 if (anObject.get()) {
723 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
725 anIndicesToBeRemoved.insert(i);
728 isDone = anIndicesToBeRemoved.size() > 0;
729 aRefListAttr->remove(anIndicesToBeRemoved);
731 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
732 std::set<AttributePtr> anAttributes;
733 QList<ModuleBase_ViewerPrsPtr>::const_iterator
734 anIt = theValues.begin(), aLast = theValues.end();
737 for (; anIt != aLast; anIt++) {
738 ModuleBase_ViewerPrsPtr aPrs = *anIt;
739 getGeomSelection(aPrs, anObject, aShape);
740 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
741 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
742 anAttributes.insert(anAttr);
745 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
746 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
748 if (aRefAttrListAttr->isAttribute(i)) {
749 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
750 aFound = anAttributes.find(anAttribute) != anAttributes.end();
753 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
756 anIndicesToBeRemoved.insert(i);
758 isDone = anIndicesToBeRemoved.size() > 0;
759 aRefAttrListAttr->remove(anIndicesToBeRemoved);
765 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
766 (QList<ModuleBase_ViewerPrsPtr>& theValues)
768 // convert prs list to objects map
769 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
770 std::set<GeomShapePtr> aShapes;
771 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
774 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
775 for (; anIt != aLast; anIt++) {
776 ModuleBase_ViewerPrsPtr aPrs = *anIt;
777 getGeomSelection(aPrs, anObject, aShape);
779 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
780 aShapes = aGeomSelection[anObject];
781 // we need to know if there was an empty shape in selection for the object
783 aShape = anEmptyShape;
784 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
785 aShapes.insert(aShape);
786 aGeomSelection[anObject] = aShapes;
788 return aGeomSelection;
791 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
792 const GeomShapePtr& theShape,
793 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
796 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
797 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
798 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
799 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
800 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
801 for (; anIt != aLast && !aFound; anIt++) {
802 GeomShapePtr aCShape = *anIt;
804 aFound = aCShape->isEqual(aShape);