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