]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
Issue #1037 Time performance on a big model: do not perform validating for objects...
[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   /// remove unused objects from the model attribute.
224   /// It should be performed before new attributes append.
225   removeUnusedAttributeObjects(theValues);
226
227   QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
228   QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
229   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
230   for (; anIt != aLast; anIt++) {
231     ModuleBase_ViewerPrsPtr aValue = *anIt;
232     // do not validate and append to attribute selection presentation if it exists in the attribute
233     ObjectPtr anObject;
234     GeomShapePtr aShape;
235     getGeomSelection(aValue, anObject, aShape);
236     if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
237       anAttributeValues.append(aValue);
238       continue;
239     }
240     if (theToValidate && !isValidInFilters(aValue))
241       anInvalidValues.append(aValue);
242   }
243   bool aHasInvalidValues = anInvalidValues.size() > 0;
244
245   bool isDone = false;
246   for (anIt = theValues.begin(); anIt != aLast; anIt++) {
247     ModuleBase_ViewerPrsPtr aValue = *anIt;
248     bool aProcessed = false;
249     if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
250         anAttributeValues.contains(aValue))
251       continue;
252     aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
253     // if there is at least one set, the result is true
254     isDone = isDone || aProcessed;
255   }
256   // updateObject - to update/redisplay feature
257   // it is commented in order to perfom it outside the method
258   //if (isDone) {
259     //updateObject(myFeature);
260     // this emit is necessary to call store/restore method an restore type of selection
261     //emit valuesChanged();
262   //}
263
264   theValues.clear();
265   if (!anInvalidValues.empty())
266     theValues.append(anInvalidValues);
267
268   return isDone;
269 }
270
271 //********************************************************************
272 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
273 {
274   std::set<int> anAttributeIds;
275   getSelectedAttributeIndices(anAttributeIds);
276   if (!anAttributeIds.empty())
277     convertIndicesToViewerSelection(anAttributeIds, theValues);
278 }
279
280 //********************************************************************
281 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
282 {
283   bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
284   if (aValid) {
285     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
286     aValid = aResult.get() != NULL;
287     if (aValid) {
288       if (myFeature) {
289         // We can not select a result of our feature
290         const std::list<ResultPtr>& aResList = myFeature->results();
291         std::list<ResultPtr>::const_iterator aIt;
292         bool isSkipSelf = false;
293         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
294           if ((*aIt) == aResult) {
295             isSkipSelf = true;
296             break;
297           }
298         }
299         if (isSkipSelf)
300           aValid = false;
301       }
302     }
303   }
304   return aValid;
305 }
306
307 //********************************************************************
308 bool ModuleBase_WidgetMultiSelector::processDelete()
309 {
310   // find attribute indices to delete
311   std::set<int> anAttributeIds;
312   getSelectedAttributeIndices(anAttributeIds);
313
314   // refill attribute by the items which indices are not in the list of ids
315   bool aDone = false;
316   DataPtr aData = myFeature->data();
317   AttributePtr anAttribute = aData->attribute(attributeID());
318   std::string aType = anAttribute->attributeType();
319   aDone = !anAttributeIds.empty();
320   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
321     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
322     aSelectionListAttr->remove(anAttributeIds);
323
324   }
325   else if (aType == ModelAPI_AttributeRefList::typeId()) {
326     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
327     aRefListAttr->remove(anAttributeIds);
328   }
329   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
330     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
331     aRefAttrListAttr->remove(anAttributeIds);
332   }
333
334   if (aDone) {
335     // update object is necessary to flush update signal. It leads to objects references map update
336     // and the operation presentation will not contain deleted items visualized as parameters of
337     // the feature.
338     updateObject(myFeature);
339
340     restoreValue();
341     myWorkshop->setSelected(getAttributeSelection());
342   }
343   return aDone;
344 }
345
346 //********************************************************************
347 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
348 {
349   QList<QWidget*> result;
350   //result << myTypeCombo;
351   result << myListControl;
352   return result;
353 }
354
355 //********************************************************************
356 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
357 {
358   activateSelectionAndFilters(true);
359   QList<ModuleBase_ViewerPrsPtr> anEmptyList;
360   // This method will call Selection changed event which will call onSelectionChanged
361   // To clear mySelection, myListControl and storeValue()
362   // So, we don't need to call it
363   myWorkshop->setSelected(anEmptyList);
364 }
365
366 void ModuleBase_WidgetMultiSelector::updateFocus()
367 {
368   // Set focus to List control in order to make possible 
369   // to use Tab key for transfer the focus to next widgets
370   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
371   ModuleBase_Tools::setFocus(myListControl,
372                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
373 }
374
375 //********************************************************************
376 void ModuleBase_WidgetMultiSelector::updateSelectionName()
377 {
378 }
379
380 //********************************************************************
381 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
382 {
383   QIntList aShapeTypes;
384
385   if (myTypeCombo->count() > 1 && myIsUseChoice) {
386     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
387   }
388   else {
389     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
390       TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
391       aShapeTypes.append(aType);
392       if (aType == TopAbs_SOLID)
393         aShapeTypes.append(TopAbs_COMPSOLID);
394     }
395   }
396   return aShapeTypes;
397 }
398
399 //********************************************************************
400 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
401 {
402   QString aShapeTypeName;
403   
404   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
405     aShapeTypeName = myTypeCombo->itemText(idx);
406     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
407     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
408       bool aWasActivated = activateSelectionAndFilters(false);
409       bool isBlocked = myTypeCombo->blockSignals(true);
410       myTypeCombo->setCurrentIndex(idx);
411       myTypeCombo->blockSignals(isBlocked);
412       if (aWasActivated)
413         activateSelectionAndFilters(true);
414       break;
415     }
416   }
417 }
418
419 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
420 {
421   QList<ModuleBase_ViewerPrsPtr> aSelected;
422   convertIndicesToViewerSelection(std::set<int>(), aSelected);
423   return aSelected;
424 }
425
426 //********************************************************************
427 void ModuleBase_WidgetMultiSelector::updateSelectionList()
428 {
429   myListControl->clear();
430
431   DataPtr aData = myFeature->data();
432   AttributePtr anAttribute = aData->attribute(attributeID());
433   std::string aType = anAttribute->attributeType();
434   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
435     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
436     for (int i = 0; i < aSelectionListAttr->size(); i++) {
437       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
438       QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
439       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
440       myListControl->addItem(anItem);
441     }
442   }
443   else if (aType == ModelAPI_AttributeRefList::typeId()) {
444     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
445     for (int i = 0; i < aRefListAttr->size(); i++) {
446       ObjectPtr anObject = aRefListAttr->object(i);
447       if (anObject.get()) {
448         QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
449                                                       myListControl);
450         anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
451         myListControl->addItem(anItem);
452       }
453     }
454   }
455   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
456     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
457     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
458       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
459       QString aName;
460       if (anAttribute.get()) {
461         std::string anAttrName = generateName(anAttribute, myWorkshop);
462         aName = QString::fromStdString(anAttrName);
463       }
464       else {
465         ObjectPtr anObject = aRefAttrListAttr->object(i);
466         if (anObject.get()) {
467           aName = anObject->data()->name().c_str();
468         }
469       }
470       QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
471       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
472       myListControl->addItem(anItem);
473     }
474   }
475
476   // We have to call repaint because sometimes the List control is not updated
477   myListControl->repaint();
478 }
479
480 //********************************************************************
481 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
482 {
483   std::string aType;
484
485   if (theType == "Vertices")
486     aType = "vertex";
487   else if (theType == "Edges")
488     aType = "edge";
489   else if (theType == "Faces")
490     aType = "face";
491   else if (theType == "Solids")
492     aType = "solid";
493
494   return aType;
495 }
496
497 //********************************************************************
498 void ModuleBase_WidgetMultiSelector::onCopyItem()
499 {
500   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
501   QString aRes;
502   foreach(QListWidgetItem* aItem, aItems) {
503     if (!aRes.isEmpty())
504       aRes += "\n";
505     aRes += aItem->text();
506   }
507   if (!aRes.isEmpty()) {
508     QClipboard *clipboard = QApplication::clipboard();
509     clipboard->setText(aRes);
510   }
511 }
512
513 //********************************************************************
514 void ModuleBase_WidgetMultiSelector::onDeleteItem()
515 {
516   processDelete();
517 }
518
519 //********************************************************************
520 void ModuleBase_WidgetMultiSelector::onListSelection()
521 {
522   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
523   myCopyAction->setEnabled(!aItems.isEmpty());
524   myDeleteAction->setEnabled(!aItems.isEmpty());
525   
526   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
527                                         true);
528 }
529
530 //********************************************************************
531 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
532 {
533   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
534   foreach(QListWidgetItem* anItem, aItems) {
535     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
536     if (theAttributeIds.find(anIndex) == theAttributeIds.end())
537       theAttributeIds.insert(anIndex);
538   }
539 }
540
541 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
542                                                       QList<ModuleBase_ViewerPrsPtr>& theValues) const
543 {
544   if(myFeature.get() == NULL)
545     return;
546
547   DataPtr aData = myFeature->data();
548   AttributePtr anAttribute = aData->attribute(attributeID());
549   std::string aType = anAttribute->attributeType();
550   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
551     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
552     for (int i = 0; i < aSelectionListAttr->size(); i++) {
553       // filter by attribute indices only if the container is not empty otherwise return all items
554       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
555         continue;
556       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
557       ResultPtr anObject = anAttr->context();
558       if (anObject.get())
559         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
560                new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
561     }
562   }
563   else if (aType == ModelAPI_AttributeRefList::typeId()) {
564     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
565     for (int i = 0; i < aRefListAttr->size(); i++) {
566       // filter by attribute indices only if the container is not empty otherwise return all items
567       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
568         continue;
569       ObjectPtr anObject = aRefListAttr->object(i);
570       if (anObject.get()) {
571         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
572                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
573       }
574     }
575   }
576   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
577     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
578     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
579       // filter by attribute indices only if the container is not empty otherwise return all items
580       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
581         continue;
582       ObjectPtr anObject = aRefAttrListAttr->object(i);
583       if (!anObject.get())
584         continue;
585       TopoDS_Shape aShape;
586       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
587       if (anAttribute.get()) {
588         GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
589         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
590                new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
591       }
592     }
593   }
594 }
595
596 void ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
597                                                  (QList<ModuleBase_ViewerPrsPtr>& theValues)
598 {
599   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
600   DataPtr aData = myFeature->data();
601   AttributePtr anAttribute = aData->attribute(attributeID());
602   std::string aType = anAttribute->attributeType();
603   std::set<GeomShapePtr> aShapes;
604   std::set<int> anIndicesToBeRemoved;
605   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
606     // iteration through data model to find not selected elements to remove them
607     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
608     for (int i = 0; i < aSelectionListAttr->size(); i++) {
609       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
610       bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
611       if (!aFound)
612         anIndicesToBeRemoved.insert(i);
613     }
614     aSelectionListAttr->remove(anIndicesToBeRemoved);
615   }
616   else if (aType == ModelAPI_AttributeRefList::typeId()) {
617     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
618     for (int i = 0; i < aRefListAttr->size(); i++) {
619       ObjectPtr anObject = aRefListAttr->object(i);
620       if (anObject.get()) {
621         bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
622         if (!aFound)
623           anIndicesToBeRemoved.insert(i);
624       }
625     }
626     aRefListAttr->remove(anIndicesToBeRemoved);
627   }
628   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
629     std::set<AttributePtr> anAttributes;
630     QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
631     ObjectPtr anObject;
632     GeomShapePtr aShape;
633     for (; anIt != aLast; anIt++) {
634       ModuleBase_ViewerPrsPtr aPrs = *anIt;
635       getGeomSelection(aPrs, anObject, aShape);
636       AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
637       if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
638         anAttributes.insert(anAttr);
639     }
640
641     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
642     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
643       bool aFound = false;
644       if (aRefAttrListAttr->isAttribute(i)) {
645         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
646         aFound = anAttributes.find(anAttribute) != anAttributes.end();
647       }
648       else {
649         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
650       }
651       if (!aFound)
652         anIndicesToBeRemoved.insert(i);
653     }
654     aRefAttrListAttr->remove(anIndicesToBeRemoved);
655   }
656 }
657
658 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
659                                                      (QList<ModuleBase_ViewerPrsPtr>& theValues)
660 {
661   // convert prs list to objects map
662   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
663   std::set<GeomShapePtr> aShapes;
664   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
665   ObjectPtr anObject;
666   GeomShapePtr aShape;
667   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
668   for (; anIt != aLast; anIt++) {
669     ModuleBase_ViewerPrsPtr aPrs = *anIt;
670     getGeomSelection(aPrs, anObject, aShape);
671     aShapes.clear();
672     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
673       aShapes = aGeomSelection[anObject];
674     // we need to know if there was an empty shape in selection for the object
675     if (!aShape.get())
676       aShape = anEmptyShape;
677     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
678       aShapes.insert(aShape);
679     aGeomSelection[anObject] = aShapes;
680   }
681   return aGeomSelection;
682 }
683
684 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
685                               const GeomShapePtr& theShape,
686                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
687 {
688   bool aFound = false;
689   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
690   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
691   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
692     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
693     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
694     for (; anIt != aLast && !aFound; anIt++) {
695       GeomShapePtr aCShape = *anIt;
696       if (aCShape.get())
697         aFound = aCShape->isEqual(aShape);
698     }
699   }
700   return aFound;
701 }