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