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