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