Salome HOME
Merge branch 'master' of salome:modules/shaper
[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::shapeTypes() 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       aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
436   }
437   return aShapeTypes;
438 }
439
440 //********************************************************************
441 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
442 {
443   QString aShapeTypeName;
444   
445   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
446     aShapeTypeName = myTypeCombo->itemText(idx);
447     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
448     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
449       bool aWasActivated = activateSelectionAndFilters(false);
450       bool isBlocked = myTypeCombo->blockSignals(true);
451       myTypeCombo->setCurrentIndex(idx);
452       myTypeCombo->blockSignals(isBlocked);
453       if (aWasActivated)
454         activateSelectionAndFilters(true);
455       break;
456     }
457   }
458 }
459
460 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
461 {
462   QList<ModuleBase_ViewerPrsPtr> aSelected;
463   convertIndicesToViewerSelection(std::set<int>(), aSelected);
464   return aSelected;
465 }
466
467 //********************************************************************
468 void ModuleBase_WidgetMultiSelector::updateSelectionList()
469 {
470   myListControl->clear();
471
472   DataPtr aData = myFeature->data();
473   AttributePtr anAttribute = aData->attribute(attributeID());
474   std::string aType = anAttribute->attributeType();
475   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
476     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
477     for (int i = 0; i < aSelectionListAttr->size(); i++) {
478       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
479       QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
480       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
481       myListControl->addItem(anItem);
482     }
483   }
484   else if (aType == ModelAPI_AttributeRefList::typeId()) {
485     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
486     for (int i = 0; i < aRefListAttr->size(); i++) {
487       ObjectPtr anObject = aRefListAttr->object(i);
488       if (anObject.get()) {
489         QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
490                                                       myListControl);
491         anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
492         myListControl->addItem(anItem);
493       }
494     }
495   }
496   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
497     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
498     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
499       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
500       QString aName;
501       if (anAttribute.get()) {
502         std::string anAttrName = generateName(anAttribute, myWorkshop);
503         aName = QString::fromStdString(anAttrName);
504       }
505       else {
506         ObjectPtr anObject = aRefAttrListAttr->object(i);
507         if (anObject.get()) {
508           aName = anObject->data()->name().c_str();
509         }
510       }
511       QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
512       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
513       myListControl->addItem(anItem);
514     }
515   }
516
517   // We have to call repaint because sometimes the List control is not updated
518   myListControl->repaint();
519 }
520
521 //********************************************************************
522 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
523 {
524   std::string aType;
525
526   if (theType == "Vertices")
527     aType = "vertex";
528   else if (theType == "Edges")
529     aType = "edge";
530   else if (theType == "Faces")
531     aType = "face";
532   else if (theType == "Solids")
533     aType = "solid";
534
535   return aType;
536 }
537
538 //********************************************************************
539 void ModuleBase_WidgetMultiSelector::onCopyItem()
540 {
541   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
542   QString aRes;
543   foreach(QListWidgetItem* aItem, aItems) {
544     if (!aRes.isEmpty())
545       aRes += "\n";
546     aRes += aItem->text();
547   }
548   if (!aRes.isEmpty()) {
549     QClipboard *clipboard = QApplication::clipboard();
550     clipboard->setText(aRes);
551   }
552 }
553
554 //********************************************************************
555 void ModuleBase_WidgetMultiSelector::onDeleteItem()
556 {
557   processDelete();
558 }
559
560 //********************************************************************
561 void ModuleBase_WidgetMultiSelector::onListSelection()
562 {
563   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
564   myCopyAction->setEnabled(!aItems.isEmpty());
565   myDeleteAction->setEnabled(!aItems.isEmpty());
566   
567   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
568                                         true);
569 }
570
571 //********************************************************************
572 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
573 {
574   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
575   foreach(QListWidgetItem* anItem, aItems) {
576     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
577     if (theAttributeIds.find(anIndex) == theAttributeIds.end())
578       theAttributeIds.insert(anIndex);
579   }
580 }
581
582 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
583                                                       QList<ModuleBase_ViewerPrsPtr>& theValues) const
584 {
585   if(myFeature.get() == NULL)
586     return;
587
588   DataPtr aData = myFeature->data();
589   AttributePtr anAttribute = aData->attribute(attributeID());
590   std::string aType = anAttribute->attributeType();
591   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
592     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
593     for (int i = 0; i < aSelectionListAttr->size(); i++) {
594       // filter by attribute indices only if the container is not empty otherwise return all items
595       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
596         continue;
597       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
598       ResultPtr anObject = anAttr->context();
599       if (anObject.get())
600         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
601                new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
602     }
603   }
604   else if (aType == ModelAPI_AttributeRefList::typeId()) {
605     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
606     for (int i = 0; i < aRefListAttr->size(); i++) {
607       // filter by attribute indices only if the container is not empty otherwise return all items
608       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
609         continue;
610       ObjectPtr anObject = aRefListAttr->object(i);
611       if (anObject.get()) {
612         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
613                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
614       }
615     }
616   }
617   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
618     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
619     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
620       // filter by attribute indices only if the container is not empty otherwise return all items
621       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
622         continue;
623       ObjectPtr anObject = aRefAttrListAttr->object(i);
624       if (!anObject.get())
625         continue;
626       TopoDS_Shape aShape;
627       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
628       if (anAttribute.get()) {
629         GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
630         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
631                new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
632       }
633     }
634   }
635 }
636
637 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
638                                                  (QList<ModuleBase_ViewerPrsPtr>& theValues)
639 {
640   bool isDone = false;
641
642   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
643   DataPtr aData = myFeature->data();
644   AttributePtr anAttribute = aData->attribute(attributeID());
645   std::string aType = anAttribute->attributeType();
646   std::set<GeomShapePtr> aShapes;
647   std::set<int> anIndicesToBeRemoved;
648   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
649     // iteration through data model to find not selected elements to remove them
650     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
651     for (int i = 0; i < aSelectionListAttr->size(); i++) {
652       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
653       bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
654       if (!aFound)
655         anIndicesToBeRemoved.insert(i);
656     }
657     isDone = anIndicesToBeRemoved.size() > 0;
658     aSelectionListAttr->remove(anIndicesToBeRemoved);
659   }
660   else if (aType == ModelAPI_AttributeRefList::typeId()) {
661     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
662     for (int i = 0; i < aRefListAttr->size(); i++) {
663       ObjectPtr anObject = aRefListAttr->object(i);
664       if (anObject.get()) {
665         bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
666         if (!aFound)
667           anIndicesToBeRemoved.insert(i);
668       }
669     }
670     isDone = anIndicesToBeRemoved.size() > 0;
671     aRefListAttr->remove(anIndicesToBeRemoved);
672   }
673   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
674     std::set<AttributePtr> anAttributes;
675     QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
676     ObjectPtr anObject;
677     GeomShapePtr aShape;
678     for (; anIt != aLast; anIt++) {
679       ModuleBase_ViewerPrsPtr aPrs = *anIt;
680       getGeomSelection(aPrs, anObject, aShape);
681       AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
682       if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
683         anAttributes.insert(anAttr);
684     }
685
686     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
687     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
688       bool aFound = false;
689       if (aRefAttrListAttr->isAttribute(i)) {
690         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
691         aFound = anAttributes.find(anAttribute) != anAttributes.end();
692       }
693       else {
694         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
695       }
696       if (!aFound)
697         anIndicesToBeRemoved.insert(i);
698     }
699     isDone = anIndicesToBeRemoved.size() > 0;
700     aRefAttrListAttr->remove(anIndicesToBeRemoved);
701   }
702
703   return isDone;
704 }
705
706 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
707                                                      (QList<ModuleBase_ViewerPrsPtr>& theValues)
708 {
709   // convert prs list to objects map
710   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
711   std::set<GeomShapePtr> aShapes;
712   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
713   ObjectPtr anObject;
714   GeomShapePtr aShape;
715   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
716   for (; anIt != aLast; anIt++) {
717     ModuleBase_ViewerPrsPtr aPrs = *anIt;
718     getGeomSelection(aPrs, anObject, aShape);
719     aShapes.clear();
720     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
721       aShapes = aGeomSelection[anObject];
722     // we need to know if there was an empty shape in selection for the object
723     if (!aShape.get())
724       aShape = anEmptyShape;
725     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
726       aShapes.insert(aShape);
727     aGeomSelection[anObject] = aShapes;
728   }
729   return aGeomSelection;
730 }
731
732 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
733                               const GeomShapePtr& theShape,
734                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
735 {
736   bool aFound = false;
737   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
738   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
739   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
740     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
741     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
742     for (; anIt != aLast && !aFound; anIt++) {
743       GeomShapePtr aCShape = *anIt;
744       if (aCShape.get())
745         aFound = aCShape->isEqual(aShape);
746     }
747   }
748   return aFound;
749 }