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