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