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