]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
b7f2995afc00f3f3339b5642c4e9ab8c2fa2ac8f
[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   //foreach(QModelIndex aIndex, aIndexes) {
330   //  aIndex.row();
331   //}
332
333   // refill attribute by the items which indices are not in the list of ids
334   bool aDone = false;
335   DataPtr aData = myFeature->data();
336   AttributePtr anAttribute = aData->attribute(attributeID());
337   std::string aType = anAttribute->attributeType();
338   aDone = !anAttributeIds.empty();
339   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
340     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
341     aSelectionListAttr->remove(anAttributeIds);
342
343   }
344   else if (aType == ModelAPI_AttributeRefList::typeId()) {
345     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
346     aRefListAttr->remove(anAttributeIds);
347   }
348   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
349     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
350     aRefAttrListAttr->remove(anAttributeIds);
351   }
352
353   if (aDone) {
354     // update object is necessary to flush update signal. It leads to objects references map update
355     // and the operation presentation will not contain deleted items visualized as parameters of
356     // the feature.
357     updateObject(myFeature);
358
359     restoreValue();
360     myWorkshop->setSelected(getAttributeSelection());
361
362     // may be the feature's result is not displayed, but attributes should be
363     myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
364                                           true); /// hope that something is redisplayed by object updated
365   }
366   foreach(QModelIndex aIndex, aIndexes) {
367     myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
368   }
369   return aDone;
370 }
371
372 //********************************************************************
373 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
374 {
375   QList<QWidget*> result;
376   //result << myTypeCombo;
377   result << myListControl;
378   return result;
379 }
380
381 //********************************************************************
382 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
383 {
384   activateSelectionAndFilters(true);
385   QList<ModuleBase_ViewerPrsPtr> anEmptyList;
386   // This method will call Selection changed event which will call onSelectionChanged
387   // To clear mySelection, myListControl and storeValue()
388   // So, we don't need to call it
389   myWorkshop->setSelected(anEmptyList);
390 }
391
392 //********************************************************************
393 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
394 {
395   if (!myIsNeutralPointClear) {
396     QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
397     if (aSelected.size() == 0)
398       return;
399   }
400   ModuleBase_WidgetSelector::onSelectionChanged();
401 }
402
403 void ModuleBase_WidgetMultiSelector::updateFocus()
404 {
405   // Set focus to List control in order to make possible 
406   // to use Tab key for transfer the focus to next widgets
407   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
408   ModuleBase_Tools::setFocus(myListControl,
409                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
410 }
411
412 //********************************************************************
413 void ModuleBase_WidgetMultiSelector::updateSelectionName()
414 {
415 }
416
417 //********************************************************************
418 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
419 {
420   QIntList aShapeTypes;
421
422   if (myTypeCombo->count() > 1 && myIsUseChoice) {
423     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
424   }
425   else {
426     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
427       TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
428       aShapeTypes.append(aType);
429       if (aType == TopAbs_SOLID)
430         aShapeTypes.append(TopAbs_COMPSOLID);
431     }
432   }
433   return aShapeTypes;
434 }
435
436 //********************************************************************
437 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
438 {
439   QString aShapeTypeName;
440   
441   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
442     aShapeTypeName = myTypeCombo->itemText(idx);
443     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
444     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
445       bool aWasActivated = activateSelectionAndFilters(false);
446       bool isBlocked = myTypeCombo->blockSignals(true);
447       myTypeCombo->setCurrentIndex(idx);
448       myTypeCombo->blockSignals(isBlocked);
449       if (aWasActivated)
450         activateSelectionAndFilters(true);
451       break;
452     }
453   }
454 }
455
456 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
457 {
458   QList<ModuleBase_ViewerPrsPtr> aSelected;
459   convertIndicesToViewerSelection(std::set<int>(), aSelected);
460   return aSelected;
461 }
462
463 //********************************************************************
464 void ModuleBase_WidgetMultiSelector::updateSelectionList()
465 {
466   myListControl->clear();
467
468   DataPtr aData = myFeature->data();
469   AttributePtr anAttribute = aData->attribute(attributeID());
470   std::string aType = anAttribute->attributeType();
471   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
472     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
473     for (int i = 0; i < aSelectionListAttr->size(); i++) {
474       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
475       QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
476       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
477       myListControl->addItem(anItem);
478     }
479   }
480   else if (aType == ModelAPI_AttributeRefList::typeId()) {
481     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
482     for (int i = 0; i < aRefListAttr->size(); i++) {
483       ObjectPtr anObject = aRefListAttr->object(i);
484       if (anObject.get()) {
485         QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
486                                                       myListControl);
487         anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
488         myListControl->addItem(anItem);
489       }
490     }
491   }
492   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
493     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
494     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
495       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
496       QString aName;
497       if (anAttribute.get()) {
498         std::string anAttrName = generateName(anAttribute, myWorkshop);
499         aName = QString::fromStdString(anAttrName);
500       }
501       else {
502         ObjectPtr anObject = aRefAttrListAttr->object(i);
503         if (anObject.get()) {
504           aName = anObject->data()->name().c_str();
505         }
506       }
507       QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
508       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
509       myListControl->addItem(anItem);
510     }
511   }
512
513   // We have to call repaint because sometimes the List control is not updated
514   myListControl->repaint();
515 }
516
517 //********************************************************************
518 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
519 {
520   std::string aType;
521
522   if (theType == "Vertices")
523     aType = "vertex";
524   else if (theType == "Edges")
525     aType = "edge";
526   else if (theType == "Faces")
527     aType = "face";
528   else if (theType == "Solids")
529     aType = "solid";
530
531   return aType;
532 }
533
534 //********************************************************************
535 void ModuleBase_WidgetMultiSelector::onCopyItem()
536 {
537   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
538   QString aRes;
539   foreach(QListWidgetItem* aItem, aItems) {
540     if (!aRes.isEmpty())
541       aRes += "\n";
542     aRes += aItem->text();
543   }
544   if (!aRes.isEmpty()) {
545     QClipboard *clipboard = QApplication::clipboard();
546     clipboard->setText(aRes);
547   }
548 }
549
550 //********************************************************************
551 void ModuleBase_WidgetMultiSelector::onDeleteItem()
552 {
553   processDelete();
554 }
555
556 //********************************************************************
557 void ModuleBase_WidgetMultiSelector::onListSelection()
558 {
559   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
560   myCopyAction->setEnabled(!aItems.isEmpty());
561   myDeleteAction->setEnabled(!aItems.isEmpty());
562   
563   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
564                                         true);
565 }
566
567 //********************************************************************
568 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
569 {
570   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
571   foreach(QListWidgetItem* anItem, aItems) {
572     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
573     if (theAttributeIds.find(anIndex) == theAttributeIds.end())
574       theAttributeIds.insert(anIndex);
575   }
576 }
577
578 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
579                                                       QList<ModuleBase_ViewerPrsPtr>& theValues) const
580 {
581   if(myFeature.get() == NULL)
582     return;
583
584   DataPtr aData = myFeature->data();
585   AttributePtr anAttribute = aData->attribute(attributeID());
586   std::string aType = anAttribute->attributeType();
587   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
588     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
589     for (int i = 0; i < aSelectionListAttr->size(); i++) {
590       // filter by attribute indices only if the container is not empty otherwise return all items
591       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
592         continue;
593       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
594       ResultPtr anObject = anAttr->context();
595       if (anObject.get())
596         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
597                new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
598     }
599   }
600   else if (aType == ModelAPI_AttributeRefList::typeId()) {
601     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
602     for (int i = 0; i < aRefListAttr->size(); i++) {
603       // filter by attribute indices only if the container is not empty otherwise return all items
604       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
605         continue;
606       ObjectPtr anObject = aRefListAttr->object(i);
607       if (anObject.get()) {
608         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
609                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
610       }
611     }
612   }
613   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
614     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
615     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
616       // filter by attribute indices only if the container is not empty otherwise return all items
617       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
618         continue;
619       ObjectPtr anObject = aRefAttrListAttr->object(i);
620       if (!anObject.get())
621         continue;
622       TopoDS_Shape aShape;
623       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
624       if (anAttribute.get()) {
625         GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
626         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
627                new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
628       }
629     }
630   }
631 }
632
633 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
634                                                  (QList<ModuleBase_ViewerPrsPtr>& theValues)
635 {
636   bool isDone = false;
637
638   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
639   DataPtr aData = myFeature->data();
640   AttributePtr anAttribute = aData->attribute(attributeID());
641   std::string aType = anAttribute->attributeType();
642   std::set<GeomShapePtr> aShapes;
643   std::set<int> anIndicesToBeRemoved;
644   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
645     // iteration through data model to find not selected elements to remove them
646     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
647     for (int i = 0; i < aSelectionListAttr->size(); i++) {
648       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
649       bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
650       if (!aFound)
651         anIndicesToBeRemoved.insert(i);
652     }
653     isDone = anIndicesToBeRemoved.size() > 0;
654     aSelectionListAttr->remove(anIndicesToBeRemoved);
655   }
656   else if (aType == ModelAPI_AttributeRefList::typeId()) {
657     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
658     for (int i = 0; i < aRefListAttr->size(); i++) {
659       ObjectPtr anObject = aRefListAttr->object(i);
660       if (anObject.get()) {
661         bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
662         if (!aFound)
663           anIndicesToBeRemoved.insert(i);
664       }
665     }
666     isDone = anIndicesToBeRemoved.size() > 0;
667     aRefListAttr->remove(anIndicesToBeRemoved);
668   }
669   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
670     std::set<AttributePtr> anAttributes;
671     QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
672     ObjectPtr anObject;
673     GeomShapePtr aShape;
674     for (; anIt != aLast; anIt++) {
675       ModuleBase_ViewerPrsPtr aPrs = *anIt;
676       getGeomSelection(aPrs, anObject, aShape);
677       AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
678       if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
679         anAttributes.insert(anAttr);
680     }
681
682     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
683     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
684       bool aFound = false;
685       if (aRefAttrListAttr->isAttribute(i)) {
686         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
687         aFound = anAttributes.find(anAttribute) != anAttributes.end();
688       }
689       else {
690         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
691       }
692       if (!aFound)
693         anIndicesToBeRemoved.insert(i);
694     }
695     isDone = anIndicesToBeRemoved.size() > 0;
696     aRefAttrListAttr->remove(anIndicesToBeRemoved);
697   }
698
699   return isDone;
700 }
701
702 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
703                                                      (QList<ModuleBase_ViewerPrsPtr>& theValues)
704 {
705   // convert prs list to objects map
706   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
707   std::set<GeomShapePtr> aShapes;
708   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
709   ObjectPtr anObject;
710   GeomShapePtr aShape;
711   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
712   for (; anIt != aLast; anIt++) {
713     ModuleBase_ViewerPrsPtr aPrs = *anIt;
714     getGeomSelection(aPrs, anObject, aShape);
715     aShapes.clear();
716     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
717       aShapes = aGeomSelection[anObject];
718     // we need to know if there was an empty shape in selection for the object
719     if (!aShape.get())
720       aShape = anEmptyShape;
721     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
722       aShapes.insert(aShape);
723     aGeomSelection[anObject] = aShapes;
724   }
725   return aGeomSelection;
726 }
727
728 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
729                               const GeomShapePtr& theShape,
730                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
731 {
732   bool aFound = false;
733   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
734   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
735   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
736     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
737     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
738     for (; anIt != aLast && !aFound; anIt++) {
739       GeomShapePtr aCShape = *anIt;
740       if (aCShape.get())
741         aFound = aCShape->isEqual(aShape);
742     }
743   }
744   return aFound;
745 }