Salome HOME
Issue #1424: Make impossible to create non-valid parameters more the a one
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetMultiSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetMultiSelector.cpp
5  *
6  *  Created on: Aug 28, 2014
7  *      Author: sbh
8  */
9
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
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeRefList.h>
25 #include <ModelAPI_AttributeRefAttrList.h>
26 #include <ModelAPI_Tools.h>
27
28 #include <Config_WidgetAPI.h>
29
30 #include <QGridLayout>
31 #include <QLabel>
32 #include <QListWidget>
33 #include <QObject>
34 #include <QString>
35 #include <QComboBox>
36 #include <QEvent>
37 #include <QAction>
38 #include <QApplication>
39 #include <QClipboard>
40 #include <QTimer>
41
42 #include <memory>
43 #include <string>
44
45 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
46
47 /**
48 * Customization of a List Widget to make it to be placed on full width of container
49 */
50 class CustomListWidget : public QListWidget
51 {
52 public:
53   /// Constructor
54   /// \param theParent a parent widget
55   CustomListWidget( QWidget* theParent )
56     : QListWidget( theParent )
57   {
58   }
59
60   /// Redefinition of virtual method
61   virtual QSize sizeHint() const
62   {
63     int aHeight = 2*QFontMetrics( font() ).height();
64     QSize aSize = QListWidget::sizeHint();
65     return QSize( aSize.width(), aHeight );
66   }
67
68   /// Redefinition of virtual method
69   virtual QSize minimumSizeHint() const
70   {
71     int aHeight = 4/*2*/*QFontMetrics( font() ).height();
72     QSize aSize = QListWidget::minimumSizeHint();
73     return QSize( aSize.width(), aHeight );
74   }
75
76 #ifndef WIN32
77 // The code is necessary only for Linux because
78 //it can not update viewport on widget resize
79 protected:
80   void resizeEvent(QResizeEvent* theEvent)
81   {
82     QListWidget::resizeEvent(theEvent);
83     QTimer::singleShot(5, viewport(), SLOT(repaint()));
84   }
85 #endif
86 };
87
88 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
89                                                                ModuleBase_IWorkshop* theWorkshop,
90                                                                const Config_WidgetAPI* theData)
91 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
92 {
93   QGridLayout* aMainLay = new QGridLayout(this);
94   ModuleBase_Tools::adjustMargins(aMainLay);
95
96   QLabel* aTypeLabel = new QLabel(tr("Type"), this);
97   aMainLay->addWidget(aTypeLabel, 0, 0);
98
99   myTypeCombo = new QComboBox(this);
100   // There is no sense to parameterize list of types while we can not parameterize selection mode
101
102   std::string aPropertyTypes = theData->getProperty("type_choice");
103   QString aTypesStr = aPropertyTypes.c_str();
104   QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
105
106   myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
107
108   if (!aShapeTypes.empty())
109     myTypeCombo->addItems(aShapeTypes);
110   aMainLay->addWidget(myTypeCombo, 0, 1);
111   // if the xml definition contains one type, the controls to select a type should not be shown
112   if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
113     aTypeLabel->setVisible(false);
114     myTypeCombo->setVisible(false);
115   }
116
117   std::string aLabelText = theData->getProperty("label");
118   QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
119                                                       : tr("Selected objects:"), this);
120   aMainLay->addWidget(aListLabel, 1, 0);
121   // if the xml definition contains one type, an information label should be shown near to the latest
122   if (aShapeTypes.size() <= 1) {
123     QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
124     if (!aLabelIcon.isEmpty()) {
125       QLabel* aSelectedLabel = new QLabel("", this);
126       aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
127       aMainLay->addWidget(aSelectedLabel, 1, 1);
128     }
129     aMainLay->setColumnStretch(2, 1);
130   }
131
132   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
133   myListControl = new CustomListWidget(this);
134   QString anObjName = QString::fromStdString(attributeID());
135   myListControl->setObjectName(anObjName);
136   myListControl->setToolTip(aToolTip);
137   myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
138
139   aMainLay->addWidget(myListControl, 2, 0, 1, -1);
140   aMainLay->setRowStretch(2, 1);
141   //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
142   //aMainLay->setRowMinimumHeight(3, 20);
143   //this->setLayout(aMainLay);
144   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
145
146   myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
147   myCopyAction->setShortcut(QKeySequence::Copy);
148   myCopyAction->setEnabled(false);
149   connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
150   myListControl->addAction(myCopyAction);
151
152   myDeleteAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
153   myDeleteAction->setEnabled(false);
154   connect(myDeleteAction, SIGNAL(triggered(bool)), SLOT(onDeleteItem()));
155   myListControl->addAction(myDeleteAction);
156
157   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
158   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
159 }
160
161 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
162 {
163 }
164
165 //********************************************************************
166 void ModuleBase_WidgetMultiSelector::activateCustom()
167 {
168   ModuleBase_WidgetSelector::activateCustom();
169
170   myWorkshop->module()->activateCustomPrs(myFeature,
171                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
172 }
173
174 //********************************************************************
175 void ModuleBase_WidgetMultiSelector::deactivate()
176 {
177   ModuleBase_WidgetSelector::deactivate();
178
179   myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
180 }
181
182 //********************************************************************
183 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
184 {
185   // the value is stored on the selection changed signal processing 
186   // A rare case when plugin was not loaded. 
187   if (!myFeature)
188     return false;
189
190   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
191   std::string aType = anAttribute->attributeType();
192   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
193     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
194     aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
195   }
196   return true;
197 }
198
199 //********************************************************************
200 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
201 {
202   // A rare case when plugin was not loaded. 
203   if (!myFeature)
204     return false;
205
206   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
207   std::string aType = anAttribute->attributeType();
208   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
209     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
210     // Restore shape type
211     std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
212     if (!aSelectionType.empty())
213       setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
214   }
215   updateSelectionList();
216   return true;
217 }
218
219 //********************************************************************
220 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
221                                                   const bool theToValidate)
222 {
223   AttributeSelectionListPtr aSelectionListAttr;
224   if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
225     aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
226   if (aSelectionListAttr.get())
227     aSelectionListAttr->cashValues(true);
228
229   /// remove unused objects from the model attribute.
230   /// It should be performed before new attributes append.
231   removeUnusedAttributeObjects(theValues);
232
233   QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
234   QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
235   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
236   for (; anIt != aLast; anIt++) {
237     ModuleBase_ViewerPrsPtr aValue = *anIt;
238     // do not validate and append to attribute selection presentation if it exists in the attribute
239     ObjectPtr anObject;
240     GeomShapePtr aShape;
241     getGeomSelection(aValue, anObject, aShape);
242     if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
243       anAttributeValues.append(aValue);
244       continue;
245     }
246     if (theToValidate && !isValidInFilters(aValue))
247       anInvalidValues.append(aValue);
248   }
249   bool aHasInvalidValues = anInvalidValues.size() > 0;
250
251   bool isDone = false;
252   for (anIt = theValues.begin(); anIt != aLast; anIt++) {
253     ModuleBase_ViewerPrsPtr aValue = *anIt;
254     bool aProcessed = false;
255     if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
256         anAttributeValues.contains(aValue))
257       continue;
258     aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
259     // if there is at least one set, the result is true
260     isDone = isDone || aProcessed;
261   }
262   // updateObject - to update/redisplay feature
263   // it is commented in order to perfom it outside the method
264   //if (isDone) {
265     //updateObject(myFeature);
266     // this emit is necessary to call store/restore method an restore type of selection
267     //emit valuesChanged();
268   //}
269
270   if (aSelectionListAttr.get())
271     aSelectionListAttr->cashValues(false);
272
273   theValues.clear();
274   if (!anInvalidValues.empty())
275     theValues.append(anInvalidValues);
276
277   return isDone;
278 }
279
280 //********************************************************************
281 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
282 {
283   std::set<int> anAttributeIds;
284   getSelectedAttributeIndices(anAttributeIds);
285   if (!anAttributeIds.empty())
286     convertIndicesToViewerSelection(anAttributeIds, theValues);
287 }
288
289 //********************************************************************
290 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
291 {
292   bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
293   if (aValid) {
294     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
295     aValid = aResult.get() != NULL;
296     if (aValid) {
297       if (myFeature) {
298         // We can not select a result of our feature
299         const std::list<ResultPtr>& aResList = myFeature->results();
300         std::list<ResultPtr>::const_iterator aIt;
301         bool isSkipSelf = false;
302         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
303           if ((*aIt) == aResult) {
304             isSkipSelf = true;
305             break;
306           }
307         }
308         if (isSkipSelf)
309           aValid = false;
310       }
311     }
312   }
313   return aValid;
314 }
315
316 //********************************************************************
317 bool ModuleBase_WidgetMultiSelector::processDelete()
318 {
319   // find attribute indices to delete
320   std::set<int> anAttributeIds;
321   getSelectedAttributeIndices(anAttributeIds);
322
323   // refill attribute by the items which indices are not in the list of ids
324   bool aDone = false;
325   DataPtr aData = myFeature->data();
326   AttributePtr anAttribute = aData->attribute(attributeID());
327   std::string aType = anAttribute->attributeType();
328   aDone = !anAttributeIds.empty();
329   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
330     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
331     aSelectionListAttr->remove(anAttributeIds);
332
333   }
334   else if (aType == ModelAPI_AttributeRefList::typeId()) {
335     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
336     aRefListAttr->remove(anAttributeIds);
337   }
338   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
339     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
340     aRefAttrListAttr->remove(anAttributeIds);
341   }
342
343   if (aDone) {
344     // update object is necessary to flush update signal. It leads to objects references map update
345     // and the operation presentation will not contain deleted items visualized as parameters of
346     // the feature.
347     updateObject(myFeature);
348
349     restoreValue();
350     myWorkshop->setSelected(getAttributeSelection());
351   }
352   return aDone;
353 }
354
355 //********************************************************************
356 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
357 {
358   QList<QWidget*> result;
359   //result << myTypeCombo;
360   result << myListControl;
361   return result;
362 }
363
364 //********************************************************************
365 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
366 {
367   activateSelectionAndFilters(true);
368   QList<ModuleBase_ViewerPrsPtr> anEmptyList;
369   // This method will call Selection changed event which will call onSelectionChanged
370   // To clear mySelection, myListControl and storeValue()
371   // So, we don't need to call it
372   myWorkshop->setSelected(anEmptyList);
373 }
374
375 void ModuleBase_WidgetMultiSelector::updateFocus()
376 {
377   // Set focus to List control in order to make possible 
378   // to use Tab key for transfer the focus to next widgets
379   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
380   ModuleBase_Tools::setFocus(myListControl,
381                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
382 }
383
384 //********************************************************************
385 void ModuleBase_WidgetMultiSelector::updateSelectionName()
386 {
387 }
388
389 //********************************************************************
390 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
391 {
392   QIntList aShapeTypes;
393
394   if (myTypeCombo->count() > 1 && myIsUseChoice) {
395     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
396   }
397   else {
398     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
399       TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
400       aShapeTypes.append(aType);
401       if (aType == TopAbs_SOLID)
402         aShapeTypes.append(TopAbs_COMPSOLID);
403     }
404   }
405   return aShapeTypes;
406 }
407
408 //********************************************************************
409 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
410 {
411   QString aShapeTypeName;
412   
413   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
414     aShapeTypeName = myTypeCombo->itemText(idx);
415     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
416     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
417       bool aWasActivated = activateSelectionAndFilters(false);
418       bool isBlocked = myTypeCombo->blockSignals(true);
419       myTypeCombo->setCurrentIndex(idx);
420       myTypeCombo->blockSignals(isBlocked);
421       if (aWasActivated)
422         activateSelectionAndFilters(true);
423       break;
424     }
425   }
426 }
427
428 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
429 {
430   QList<ModuleBase_ViewerPrsPtr> aSelected;
431   convertIndicesToViewerSelection(std::set<int>(), aSelected);
432   return aSelected;
433 }
434
435 //********************************************************************
436 void ModuleBase_WidgetMultiSelector::updateSelectionList()
437 {
438   myListControl->clear();
439
440   DataPtr aData = myFeature->data();
441   AttributePtr anAttribute = aData->attribute(attributeID());
442   std::string aType = anAttribute->attributeType();
443   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
444     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
445     for (int i = 0; i < aSelectionListAttr->size(); i++) {
446       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
447       QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
448       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
449       myListControl->addItem(anItem);
450     }
451   }
452   else if (aType == ModelAPI_AttributeRefList::typeId()) {
453     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
454     for (int i = 0; i < aRefListAttr->size(); i++) {
455       ObjectPtr anObject = aRefListAttr->object(i);
456       if (anObject.get()) {
457         QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
458                                                       myListControl);
459         anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
460         myListControl->addItem(anItem);
461       }
462     }
463   }
464   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
465     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
466     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
467       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
468       QString aName;
469       if (anAttribute.get()) {
470         std::string anAttrName = generateName(anAttribute, myWorkshop);
471         aName = QString::fromStdString(anAttrName);
472       }
473       else {
474         ObjectPtr anObject = aRefAttrListAttr->object(i);
475         if (anObject.get()) {
476           aName = anObject->data()->name().c_str();
477         }
478       }
479       QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
480       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
481       myListControl->addItem(anItem);
482     }
483   }
484
485   // We have to call repaint because sometimes the List control is not updated
486   myListControl->repaint();
487 }
488
489 //********************************************************************
490 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
491 {
492   std::string aType;
493
494   if (theType == "Vertices")
495     aType = "vertex";
496   else if (theType == "Edges")
497     aType = "edge";
498   else if (theType == "Faces")
499     aType = "face";
500   else if (theType == "Solids")
501     aType = "solid";
502
503   return aType;
504 }
505
506 //********************************************************************
507 void ModuleBase_WidgetMultiSelector::onCopyItem()
508 {
509   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
510   QString aRes;
511   foreach(QListWidgetItem* aItem, aItems) {
512     if (!aRes.isEmpty())
513       aRes += "\n";
514     aRes += aItem->text();
515   }
516   if (!aRes.isEmpty()) {
517     QClipboard *clipboard = QApplication::clipboard();
518     clipboard->setText(aRes);
519   }
520 }
521
522 //********************************************************************
523 void ModuleBase_WidgetMultiSelector::onDeleteItem()
524 {
525   processDelete();
526 }
527
528 //********************************************************************
529 void ModuleBase_WidgetMultiSelector::onListSelection()
530 {
531   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
532   myCopyAction->setEnabled(!aItems.isEmpty());
533   myDeleteAction->setEnabled(!aItems.isEmpty());
534   
535   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
536                                         true);
537 }
538
539 //********************************************************************
540 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
541 {
542   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
543   foreach(QListWidgetItem* anItem, aItems) {
544     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
545     if (theAttributeIds.find(anIndex) == theAttributeIds.end())
546       theAttributeIds.insert(anIndex);
547   }
548 }
549
550 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
551                                                       QList<ModuleBase_ViewerPrsPtr>& theValues) const
552 {
553   if(myFeature.get() == NULL)
554     return;
555
556   DataPtr aData = myFeature->data();
557   AttributePtr anAttribute = aData->attribute(attributeID());
558   std::string aType = anAttribute->attributeType();
559   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
560     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
561     for (int i = 0; i < aSelectionListAttr->size(); i++) {
562       // filter by attribute indices only if the container is not empty otherwise return all items
563       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
564         continue;
565       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
566       ResultPtr anObject = anAttr->context();
567       if (anObject.get())
568         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
569                new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
570     }
571   }
572   else if (aType == ModelAPI_AttributeRefList::typeId()) {
573     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
574     for (int i = 0; i < aRefListAttr->size(); i++) {
575       // filter by attribute indices only if the container is not empty otherwise return all items
576       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
577         continue;
578       ObjectPtr anObject = aRefListAttr->object(i);
579       if (anObject.get()) {
580         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
581                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
582       }
583     }
584   }
585   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
586     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
587     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
588       // filter by attribute indices only if the container is not empty otherwise return all items
589       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
590         continue;
591       ObjectPtr anObject = aRefAttrListAttr->object(i);
592       if (!anObject.get())
593         continue;
594       TopoDS_Shape aShape;
595       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
596       if (anAttribute.get()) {
597         GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
598         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
599                new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
600       }
601     }
602   }
603 }
604
605 void ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
606                                                  (QList<ModuleBase_ViewerPrsPtr>& theValues)
607 {
608   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
609   DataPtr aData = myFeature->data();
610   AttributePtr anAttribute = aData->attribute(attributeID());
611   std::string aType = anAttribute->attributeType();
612   std::set<GeomShapePtr> aShapes;
613   std::set<int> anIndicesToBeRemoved;
614   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
615     // iteration through data model to find not selected elements to remove them
616     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
617     for (int i = 0; i < aSelectionListAttr->size(); i++) {
618       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
619       bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
620       if (!aFound)
621         anIndicesToBeRemoved.insert(i);
622     }
623     aSelectionListAttr->remove(anIndicesToBeRemoved);
624   }
625   else if (aType == ModelAPI_AttributeRefList::typeId()) {
626     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
627     for (int i = 0; i < aRefListAttr->size(); i++) {
628       ObjectPtr anObject = aRefListAttr->object(i);
629       if (anObject.get()) {
630         bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
631         if (!aFound)
632           anIndicesToBeRemoved.insert(i);
633       }
634     }
635     aRefListAttr->remove(anIndicesToBeRemoved);
636   }
637   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
638     std::set<AttributePtr> anAttributes;
639     QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
640     ObjectPtr anObject;
641     GeomShapePtr aShape;
642     for (; anIt != aLast; anIt++) {
643       ModuleBase_ViewerPrsPtr aPrs = *anIt;
644       getGeomSelection(aPrs, anObject, aShape);
645       AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
646       if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
647         anAttributes.insert(anAttr);
648     }
649
650     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
651     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
652       bool aFound = false;
653       if (aRefAttrListAttr->isAttribute(i)) {
654         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
655         aFound = anAttributes.find(anAttribute) != anAttributes.end();
656       }
657       else {
658         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
659       }
660       if (!aFound)
661         anIndicesToBeRemoved.insert(i);
662     }
663     aRefAttrListAttr->remove(anIndicesToBeRemoved);
664   }
665 }
666
667 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
668                                                      (QList<ModuleBase_ViewerPrsPtr>& theValues)
669 {
670   // convert prs list to objects map
671   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
672   std::set<GeomShapePtr> aShapes;
673   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
674   ObjectPtr anObject;
675   GeomShapePtr aShape;
676   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
677   for (; anIt != aLast; anIt++) {
678     ModuleBase_ViewerPrsPtr aPrs = *anIt;
679     getGeomSelection(aPrs, anObject, aShape);
680     aShapes.clear();
681     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
682       aShapes = aGeomSelection[anObject];
683     // we need to know if there was an empty shape in selection for the object
684     if (!aShape.get())
685       aShape = anEmptyShape;
686     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
687       aShapes.insert(aShape);
688     aGeomSelection[anObject] = aShapes;
689   }
690   return aGeomSelection;
691 }
692
693 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
694                               const GeomShapePtr& theShape,
695                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
696 {
697   bool aFound = false;
698   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
699   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
700   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
701     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
702     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
703     for (; anIt != aLast && !aFound; anIt++) {
704       GeomShapePtr aCShape = *anIt;
705       if (aCShape.get())
706         aFound = aCShape->isEqual(aShape);
707     }
708   }
709   return aFound;
710 }