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),
95 myIsSetSelectionBlocked(false)
97 QGridLayout* aMainLay = new QGridLayout(this);
98 ModuleBase_Tools::adjustMargins(aMainLay);
100 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
101 aMainLay->addWidget(aTypeLabel, 0, 0);
103 myTypeCombo = new QComboBox(this);
104 // There is no sense to parameterize list of types while we can not parameterize selection mode
106 std::string aPropertyTypes = theData->getProperty("type_choice");
107 QString aTypesStr = aPropertyTypes.c_str();
108 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
110 myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
112 if (!aShapeTypes.empty())
113 myTypeCombo->addItems(aShapeTypes);
114 aMainLay->addWidget(myTypeCombo, 0, 1);
115 // if the xml definition contains one type, the controls to select a type should not be shown
116 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
117 aTypeLabel->setVisible(false);
118 myTypeCombo->setVisible(false);
121 QString aLabelText = translate(theData->getProperty("label"));
122 QLabel* aListLabel = new QLabel(aLabelText, 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 if (myIsSetSelectionBlocked)
232 AttributeSelectionListPtr aSelectionListAttr;
233 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
234 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
235 if (aSelectionListAttr.get())
236 aSelectionListAttr->cashValues(true);
238 /// remove unused objects from the model attribute.
239 /// It should be performed before new attributes append.
240 bool isDone = removeUnusedAttributeObjects(theValues);
242 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
243 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
244 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
245 for (; anIt != aLast; anIt++) {
246 ModuleBase_ViewerPrsPtr aValue = *anIt;
247 // do not validate and append to attribute selection presentation if it exists in the attribute
250 getGeomSelection(aValue, anObject, aShape);
251 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
252 anAttributeValues.append(aValue);
255 if (theToValidate && !isValidInFilters(aValue))
256 anInvalidValues.append(aValue);
258 bool aHasInvalidValues = anInvalidValues.size() > 0;
260 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
261 ModuleBase_ViewerPrsPtr aValue = *anIt;
262 bool aProcessed = false;
263 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
264 anAttributeValues.contains(aValue))
266 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
267 // if there is at least one set, the result is true
268 isDone = isDone || aProcessed;
270 // updateObject - to update/redisplay feature
271 // it is commented in order to perfom it outside the method
273 //updateObject(myFeature);
274 // this emit is necessary to call store/restore method an restore type of selection
275 //emit valuesChanged();
278 // Restore selection in the viewer by the attribute selection list
279 // it is possible that diring selection attribute filling, selection in Object Browser
280 // is changed(some items were removed/added) and as result, selection in the viewer
281 // differs from the selection come to this method. By next rows, we restore selection
282 // in the viewer according to content of selection attribute. Case is Edge selection in Group
283 myIsSetSelectionBlocked = true;
284 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
285 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
286 Events_Loop::loop()->flush(anEvent);
287 myIsSetSelectionBlocked = false;
289 if (aSelectionListAttr.get())
290 aSelectionListAttr->cashValues(false);
293 if (!anInvalidValues.empty())
294 theValues.append(anInvalidValues);
296 if (isDone) // may be the feature's result is not displayed, but attributes should be
297 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
298 true);/// hope that something is redisplayed by object updated
303 //********************************************************************
304 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
306 std::set<int> anAttributeIds;
307 getSelectedAttributeIndices(anAttributeIds);
308 if (!anAttributeIds.empty())
309 convertIndicesToViewerSelection(anAttributeIds, theValues);
312 //********************************************************************
313 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
315 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
317 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
318 aValid = aResult.get() != NULL;
321 // We can not select a result of our feature
322 std::list<ResultPtr> aResults;
323 ModelAPI_Tools::allResults(myFeature, aResults);
324 std::list<ResultPtr>::const_iterator aIt;
325 bool isSkipSelf = false;
326 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
327 if ((*aIt) == aResult) {
340 //********************************************************************
341 bool ModuleBase_WidgetMultiSelector::processDelete()
343 // find attribute indices to delete
344 std::set<int> anAttributeIds;
345 getSelectedAttributeIndices(anAttributeIds);
347 QModelIndexList aIndexes = myListControl->selectionModel()->selectedIndexes();
349 // refill attribute by the items which indices are not in the list of ids
351 DataPtr aData = myFeature->data();
352 AttributePtr anAttribute = aData->attribute(attributeID());
353 std::string aType = anAttribute->attributeType();
354 aDone = !anAttributeIds.empty();
355 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
356 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
357 aSelectionListAttr->remove(anAttributeIds);
360 else if (aType == ModelAPI_AttributeRefList::typeId()) {
361 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
362 aRefListAttr->remove(anAttributeIds);
364 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
365 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
366 aRefAttrListAttr->remove(anAttributeIds);
370 // update object is necessary to flush update signal. It leads to objects references map update
371 // and the operation presentation will not contain deleted items visualized as parameters of
373 updateObject(myFeature);
376 myWorkshop->setSelected(getAttributeSelection());
378 // may be the feature's result is not displayed, but attributes should be
379 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
380 true); /// hope that something is redisplayed by object updated
384 int aRows = myListControl->model()->rowCount();
386 foreach(QModelIndex aIndex, aIndexes) {
387 if (aIndex.row() < aRows)
388 myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
390 QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
391 myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
398 //********************************************************************
399 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
401 QList<QWidget*> result;
402 //result << myTypeCombo;
403 result << myListControl;
407 //********************************************************************
408 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
410 activateSelectionAndFilters(true);
414 /// store the selected type
415 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
416 std::string aType = anAttribute->attributeType();
417 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
418 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
419 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
422 // clear attribute values
423 DataPtr aData = myFeature->data();
424 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
425 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
426 aSelectionListAttr->clear();
428 else if (aType == ModelAPI_AttributeRefList::typeId()) {
429 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
430 aRefListAttr->clear();
432 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
433 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
434 aRefAttrListAttr->clear();
437 // update object is necessary to flush update signal. It leads to objects references map update
438 // and the operation presentation will not contain deleted items visualized as parameters of
440 updateObject(myFeature);
442 myWorkshop->setSelected(getAttributeSelection());
443 // may be the feature's result is not displayed, but attributes should be
444 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
445 true); /// hope that something is redisplayed by object updated
448 //********************************************************************
449 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
451 if (!myIsNeutralPointClear) {
452 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
453 // do not clear selected object
454 if (aSelected.size() == 0) {
455 if (!getAttributeSelection().empty()) {
456 // Restore selection in the viewer by the attribute selection list
457 // it should be postponed to exit from the selectionChanged processing
458 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
459 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
460 Events_Loop::loop()->flush(anEvent);
465 ModuleBase_WidgetSelector::onSelectionChanged();
468 void ModuleBase_WidgetMultiSelector::updateFocus()
470 // Set focus to List control in order to make possible
471 // to use Tab key for transfer the focus to next widgets
472 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
473 ModuleBase_Tools::setFocus(myListControl,
474 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
477 //********************************************************************
478 void ModuleBase_WidgetMultiSelector::updateSelectionName()
482 //********************************************************************
483 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
485 QIntList aShapeTypes;
487 if (myTypeCombo->count() > 1 && myIsUseChoice) {
488 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
491 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
492 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
497 //********************************************************************
498 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
500 QString aShapeTypeName;
502 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
503 aShapeTypeName = myTypeCombo->itemText(idx);
504 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
505 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
506 bool aWasActivated = activateSelectionAndFilters(false);
507 bool isBlocked = myTypeCombo->blockSignals(true);
508 myTypeCombo->setCurrentIndex(idx);
509 myTypeCombo->blockSignals(isBlocked);
511 activateSelectionAndFilters(true);
517 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
519 QList<ModuleBase_ViewerPrsPtr> aSelected;
520 convertIndicesToViewerSelection(std::set<int>(), aSelected);
524 //********************************************************************
525 void ModuleBase_WidgetMultiSelector::updateSelectionList()
527 myListControl->clear();
529 DataPtr aData = myFeature->data();
530 AttributePtr anAttribute = aData->attribute(attributeID());
531 std::string aType = anAttribute->attributeType();
532 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
533 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
534 for (int i = 0; i < aSelectionListAttr->size(); i++) {
535 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
536 QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
537 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
538 myListControl->addItem(anItem);
541 else if (aType == ModelAPI_AttributeRefList::typeId()) {
542 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
543 for (int i = 0; i < aRefListAttr->size(); i++) {
544 ObjectPtr anObject = aRefListAttr->object(i);
545 if (anObject.get()) {
546 QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
548 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
549 myListControl->addItem(anItem);
553 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
554 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
555 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
556 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
558 if (anAttribute.get()) {
559 std::string anAttrName = generateName(anAttribute, myWorkshop);
560 aName = QString::fromStdString(anAttrName);
563 ObjectPtr anObject = aRefAttrListAttr->object(i);
564 if (anObject.get()) {
565 aName = anObject->data()->name().c_str();
568 QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
569 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
570 myListControl->addItem(anItem);
574 // We have to call repaint because sometimes the List control is not updated
575 myListControl->repaint();
578 //********************************************************************
579 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
583 if (theType == "Vertices")
585 else if (theType == "Edges")
587 else if (theType == "Faces")
589 else if (theType == "Solids")
595 //********************************************************************
596 void ModuleBase_WidgetMultiSelector::clearSelection()
598 bool isClearInNeutralPoint = myIsNeutralPointClear;
599 myIsNeutralPointClear = true;
601 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
602 // This method will call Selection changed event which will call onSelectionChanged
603 // To clear mySelection, myListControl and storeValue()
604 // So, we don't need to call it
605 myWorkshop->setSelected(anEmptyList);
607 myIsNeutralPointClear = isClearInNeutralPoint;
610 //********************************************************************
611 void ModuleBase_WidgetMultiSelector::onCopyItem()
613 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
615 foreach(QListWidgetItem* aItem, aItems) {
618 aRes += aItem->text();
620 if (!aRes.isEmpty()) {
621 QClipboard *clipboard = QApplication::clipboard();
622 clipboard->setText(aRes);
626 //********************************************************************
627 void ModuleBase_WidgetMultiSelector::onDeleteItem()
632 //********************************************************************
633 void ModuleBase_WidgetMultiSelector::onListSelection()
635 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
636 myCopyAction->setEnabled(!aItems.isEmpty());
637 myDeleteAction->setEnabled(!aItems.isEmpty());
639 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
643 //********************************************************************
644 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
646 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
647 foreach(QListWidgetItem* anItem, aItems) {
648 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
649 if (theAttributeIds.find(anIndex) == theAttributeIds.end())
650 theAttributeIds.insert(anIndex);
654 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
655 QList<ModuleBase_ViewerPrsPtr>& theValues) const
657 if(myFeature.get() == NULL)
660 DataPtr aData = myFeature->data();
661 AttributePtr anAttribute = aData->attribute(attributeID());
662 std::string aType = anAttribute->attributeType();
663 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
664 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
665 for (int i = 0; i < aSelectionListAttr->size(); i++) {
666 // filter by attribute indices only if the container is not empty otherwise return all items
667 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
669 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
670 ResultPtr anObject = anAttr->context();
672 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
673 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
676 else if (aType == ModelAPI_AttributeRefList::typeId()) {
677 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
678 for (int i = 0; i < aRefListAttr->size(); i++) {
679 // filter by attribute indices only if the container is not empty otherwise return all items
680 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
682 ObjectPtr anObject = aRefListAttr->object(i);
683 if (anObject.get()) {
684 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
685 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
689 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
690 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
691 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
692 // filter by attribute indices only if the container is not empty otherwise return all items
693 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
695 ObjectPtr anObject = aRefAttrListAttr->object(i);
699 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
700 if (anAttribute.get()) {
701 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
702 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
703 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
709 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
710 (QList<ModuleBase_ViewerPrsPtr>& theValues)
714 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
715 DataPtr aData = myFeature->data();
716 AttributePtr anAttribute = aData->attribute(attributeID());
717 std::string aType = anAttribute->attributeType();
718 std::set<GeomShapePtr> aShapes;
719 std::set<int> anIndicesToBeRemoved;
720 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
721 // iteration through data model to find not selected elements to remove them
722 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
723 for (int i = 0; i < aSelectionListAttr->size(); i++) {
724 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
725 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
727 anIndicesToBeRemoved.insert(i);
729 isDone = anIndicesToBeRemoved.size() > 0;
730 aSelectionListAttr->remove(anIndicesToBeRemoved);
732 else if (aType == ModelAPI_AttributeRefList::typeId()) {
733 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
734 for (int i = 0; i < aRefListAttr->size(); i++) {
735 ObjectPtr anObject = aRefListAttr->object(i);
736 if (anObject.get()) {
737 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
739 anIndicesToBeRemoved.insert(i);
742 isDone = anIndicesToBeRemoved.size() > 0;
743 aRefListAttr->remove(anIndicesToBeRemoved);
745 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
746 std::set<AttributePtr> anAttributes;
747 QList<ModuleBase_ViewerPrsPtr>::const_iterator
748 anIt = theValues.begin(), aLast = theValues.end();
751 for (; anIt != aLast; anIt++) {
752 ModuleBase_ViewerPrsPtr aPrs = *anIt;
753 getGeomSelection(aPrs, anObject, aShape);
754 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
755 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
756 anAttributes.insert(anAttr);
759 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
760 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
762 if (aRefAttrListAttr->isAttribute(i)) {
763 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
764 aFound = anAttributes.find(anAttribute) != anAttributes.end();
767 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
770 anIndicesToBeRemoved.insert(i);
772 isDone = anIndicesToBeRemoved.size() > 0;
773 aRefAttrListAttr->remove(anIndicesToBeRemoved);
779 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
780 (QList<ModuleBase_ViewerPrsPtr>& theValues)
782 // convert prs list to objects map
783 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
784 std::set<GeomShapePtr> aShapes;
785 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
788 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
789 for (; anIt != aLast; anIt++) {
790 ModuleBase_ViewerPrsPtr aPrs = *anIt;
791 getGeomSelection(aPrs, anObject, aShape);
793 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
794 aShapes = aGeomSelection[anObject];
795 // we need to know if there was an empty shape in selection for the object
797 aShape = anEmptyShape;
798 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
799 aShapes.insert(aShape);
800 aGeomSelection[anObject] = aShapes;
802 return aGeomSelection;
805 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
806 const GeomShapePtr& theShape,
807 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
810 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
811 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
812 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
813 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
814 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
815 for (; anIt != aLast && !aFound; anIt++) {
816 GeomShapePtr aCShape = *anIt;
818 aFound = aCShape->isEqual(aShape);