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