]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
#1330 Multiply selection when Create fillet using Shift button
[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   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
314   bool isDone = false;
315   for (; anIt != aLast; anIt++) {
316     ModuleBase_ViewerPrs aValue = *anIt;
317     bool aProcessed = false;
318     if (!theToValidate || isValidInFilters(aValue)) {
319       aProcessed = setSelectionCustom(aValue);
320     }
321     else
322       aSkippedValues.append(aValue);
323     // if there is at least one set, the result is true
324     isDone = isDone || aProcessed;
325   }
326   // updateObject - to update/redisplay feature
327   // it is commented in order to perfom it outside the method
328   //if (isDone) {
329     //updateObject(myFeature);
330     // this emit is necessary to call store/restore method an restore type of selection
331     //emit valuesChanged();
332   //}
333
334   /// remove unused objects from the model attribute
335   removeUnusedAttributeObjects(theValues);
336
337   theValues.clear();
338   if (!aSkippedValues.empty())
339     theValues.append(aSkippedValues);
340
341   return isDone;
342 }
343
344 //********************************************************************
345 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrs>& theValues)
346 {
347   std::set<int> anAttributeIds;
348   getSelectedAttributeIndices(anAttributeIds);
349   if (!anAttributeIds.empty())
350     convertIndicesToViewerSelection(anAttributeIds, theValues);
351 }
352
353 //********************************************************************
354 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
355 {
356   bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
357   if (aValid) {
358     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
359     aValid = aResult.get() != NULL;
360     if (aValid) {
361       if (myFeature) {
362         // We can not select a result of our feature
363         const std::list<ResultPtr>& aResList = myFeature->results();
364         std::list<ResultPtr>::const_iterator aIt;
365         bool isSkipSelf = false;
366         for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
367           if ((*aIt) == aResult) {
368             isSkipSelf = true;
369             break;
370           }
371         }
372         if (isSkipSelf)
373           aValid = false;
374       }
375     }
376   }
377   return aValid;
378 }
379
380 //********************************************************************
381 bool ModuleBase_WidgetMultiSelector::processDelete()
382 {
383   // find attribute indices to delete
384   std::set<int> anAttributeIds;
385   getSelectedAttributeIndices(anAttributeIds);
386
387   // refill attribute by the items which indices are not in the list of ids
388   bool aDone = false;
389   DataPtr aData = myFeature->data();
390   AttributePtr anAttribute = aData->attribute(attributeID());
391   std::string aType = anAttribute->attributeType();
392   aDone = !anAttributeIds.empty();
393   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
394     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
395     aSelectionListAttr->remove(anAttributeIds);
396
397   }
398   else if (aType == ModelAPI_AttributeRefList::typeId()) {
399     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
400     aRefListAttr->remove(anAttributeIds);
401   }
402   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
403     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
404     aRefAttrListAttr->remove(anAttributeIds);
405   }
406
407   if (aDone) {
408     // update object is necessary to flush update signal. It leads to objects references map update
409     // and the operation presentation will not contain deleted items visualized as parameters of
410     // the feature.
411     updateObject(myFeature);
412
413     restoreValue();
414     myWorkshop->setSelected(getAttributeSelection());
415   }
416   return aDone;
417 }
418
419 //********************************************************************
420 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
421 {
422   QList<QWidget*> result;
423   //result << myTypeCombo;
424   result << myListControl;
425   return result;
426 }
427
428 //********************************************************************
429 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
430 {
431   activateSelectionAndFilters(true);
432   QList<ModuleBase_ViewerPrs> anEmptyList;
433   // This method will call Selection changed event which will call onSelectionChanged
434   // To clear mySelection, myListControl and storeValue()
435   // So, we don't need to call it
436   myWorkshop->setSelected(anEmptyList);
437 }
438
439 void ModuleBase_WidgetMultiSelector::updateFocus()
440 {
441   // Set focus to List control in order to make possible 
442   // to use Tab key for transfer the focus to next widgets
443   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
444   ModuleBase_Tools::setFocus(myListControl,
445                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
446 }
447
448 //********************************************************************
449 void ModuleBase_WidgetMultiSelector::updateSelectionName()
450 {
451 }
452
453 //********************************************************************
454 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
455 {
456   QIntList aShapeTypes;
457
458   if (myTypeCombo->count() > 1 && myIsUseChoice) {
459     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
460   }
461   else {
462     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
463       TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
464       aShapeTypes.append(aType);
465       if (aType == TopAbs_SOLID)
466         aShapeTypes.append(TopAbs_COMPSOLID);
467     }
468   }
469   return aShapeTypes;
470 }
471
472 //********************************************************************
473 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
474 {
475   QString aShapeTypeName;
476   
477   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
478     aShapeTypeName = myTypeCombo->itemText(idx);
479     TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
480     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
481       activateSelectionAndFilters(false);
482       bool isBlocked = myTypeCombo->blockSignals(true);
483       myTypeCombo->setCurrentIndex(idx);
484       myTypeCombo->blockSignals(isBlocked);
485
486       activateSelectionAndFilters(true);
487       break;
488     }
489   }
490 }
491
492 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
493 {
494   QList<ModuleBase_ViewerPrs> aSelected;
495   convertIndicesToViewerSelection(std::set<int>(), aSelected);
496   return aSelected;
497 }
498
499 //********************************************************************
500 void ModuleBase_WidgetMultiSelector::updateSelectionList()
501 {
502   myListControl->clear();
503
504   DataPtr aData = myFeature->data();
505   AttributePtr anAttribute = aData->attribute(attributeID());
506   std::string aType = anAttribute->attributeType();
507   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
508     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
509     for (int i = 0; i < aSelectionListAttr->size(); i++) {
510       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
511       QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
512       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
513       myListControl->addItem(anItem);
514     }
515   }
516   else if (aType == ModelAPI_AttributeRefList::typeId()) {
517     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
518     for (int i = 0; i < aRefListAttr->size(); i++) {
519       ObjectPtr anObject = aRefListAttr->object(i);
520       if (anObject.get()) {
521         QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
522                                                       myListControl);
523         anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
524         myListControl->addItem(anItem);
525       }
526     }
527   }
528   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
529     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
530     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
531       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
532       QString aName;
533       if (anAttribute.get()) {
534         std::string anAttrName = generateName(anAttribute, myWorkshop);
535         aName = QString::fromStdString(anAttrName);
536       }
537       else {
538         ObjectPtr anObject = aRefAttrListAttr->object(i);
539         if (anObject.get()) {
540           aName = anObject->data()->name().c_str();
541         }
542       }
543       QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
544       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
545       myListControl->addItem(anItem);
546     }
547   }
548
549   // We have to call repaint because sometimes the List control is not updated
550   myListControl->repaint();
551 }
552
553 //********************************************************************
554 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
555 {
556   std::string aType;
557
558   if (theType == "Vertices")
559     aType = "vertex";
560   else if (theType == "Edges")
561     aType = "edge";
562   else if (theType == "Faces")
563     aType = "face";
564   else if (theType == "Solids")
565     aType = "solid";
566
567   return aType;
568 }
569
570 //********************************************************************
571 void ModuleBase_WidgetMultiSelector::onCopyItem()
572 {
573   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
574   QString aRes;
575   foreach(QListWidgetItem* aItem, aItems) {
576     if (!aRes.isEmpty())
577       aRes += "\n";
578     aRes += aItem->text();
579   }
580   if (!aRes.isEmpty()) {
581     QClipboard *clipboard = QApplication::clipboard();
582     clipboard->setText(aRes);
583   }
584 }
585
586 //********************************************************************
587 void ModuleBase_WidgetMultiSelector::onDeleteItem()
588 {
589   processDelete();
590 }
591
592 //********************************************************************
593 void ModuleBase_WidgetMultiSelector::onListSelection()
594 {
595   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
596   myCopyAction->setEnabled(!aItems.isEmpty());
597   myDeleteAction->setEnabled(!aItems.isEmpty());
598   
599   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
600                                         true);
601 }
602
603 //********************************************************************
604 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
605 {
606   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
607   foreach(QListWidgetItem* anItem, aItems) {
608     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
609     if (theAttributeIds.find(anIndex) == theAttributeIds.end())
610       theAttributeIds.insert(anIndex);
611   }
612 }
613
614 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
615                                                       QList<ModuleBase_ViewerPrs>& theValues) const
616 {
617   if(myFeature.get() == NULL)
618     return;
619
620   DataPtr aData = myFeature->data();
621   AttributePtr anAttribute = aData->attribute(attributeID());
622   std::string aType = anAttribute->attributeType();
623   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
624     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
625     for (int i = 0; i < aSelectionListAttr->size(); i++) {
626       // filter by attribute indices only if the container is not empty otherwise return all items
627       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
628         continue;
629       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
630       ResultPtr anObject = anAttr->context();
631       if (anObject.get()) {
632         TopoDS_Shape aShape;
633         std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
634         if (aShapePtr.get()) {
635           aShape = aShapePtr->impl<TopoDS_Shape>();
636         }
637         theValues.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
638       }
639     }
640   }
641   else if (aType == ModelAPI_AttributeRefList::typeId()) {
642     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
643     for (int i = 0; i < aRefListAttr->size(); i++) {
644       // filter by attribute indices only if the container is not empty otherwise return all items
645       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
646         continue;
647       ObjectPtr anObject = aRefListAttr->object(i);
648       if (anObject.get()) {
649         theValues.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
650       }
651     }
652   }
653   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
654     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
655     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
656       // filter by attribute indices only if the container is not empty otherwise return all items
657       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
658         continue;
659       ObjectPtr anObject = aRefAttrListAttr->object(i);
660       if (!anObject.get())
661         continue;
662       TopoDS_Shape aShape;
663       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
664       if (anAttribute.get()) {
665         GeomShapePtr aGeomShape = myWorkshop->module()->findShape(anAttribute);
666         if (aGeomShape.get()) {
667           aShape = aGeomShape->impl<TopoDS_Shape>();
668         }
669         theValues.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
670       }
671     }
672   }
673 }
674
675 void ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
676                                                  (QList<ModuleBase_ViewerPrs>& theValues)
677 {
678   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
679
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     /*AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
710     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
711       bool aFound = false;
712       if (aRefAttrListAttr->isAttribute(i)) {
713         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
714         ObjectPtr anAttrObject = anAttribute->owner();
715         if (aGeomSelection.find(anAttrObject) != aGeomSelection.end()) {
716           std::set<GeomShapePtr> aShapes = aGeomSelection[anAttrObject];
717           std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
718           for (; anIt != aLast && !aFound; anIt++) {
719             GeomShapePtr aCShape = *anIt;
720             if (aCShape.get()) {
721               AttributePtr aCAttr = myWorkshop->module()->findAttribute(anAttrObject, aCShape);
722               aFound = aCAttr == anAttribute;
723             }
724           }
725         }
726       }
727       else {
728         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
729       }
730       if (!aFound)
731         anIndicesToBeRemoved.insert(i);
732     }
733     aRefAttrListAttr->remove(anIndicesToBeRemoved);*/
734   }
735 }
736
737 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
738                                                      (QList<ModuleBase_ViewerPrs>& theValues)
739 {
740   // convert prs list to objects map
741   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
742   std::set<GeomShapePtr> aShapes;
743   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
744   ObjectPtr anObject;
745   GeomShapePtr aShape;
746   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
747   for (; anIt != aLast; anIt++) {
748     ModuleBase_ViewerPrs aPrs = *anIt;
749     getGeomSelection(aPrs, anObject, aShape);
750     aShapes.clear();
751     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
752       aShapes = aGeomSelection[anObject];
753     // we need to know if there was an empty shape in selection for the object
754     if (!aShape.get())
755       aShape = anEmptyShape;
756     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
757       aShapes.insert(aShape);
758     aGeomSelection[anObject] = aShapes;
759   }
760   return aGeomSelection;
761 }
762
763 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
764                               const GeomShapePtr& theShape,
765                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
766 {
767   bool aFound = false;
768   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
769   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
770   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
771     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
772     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
773     for (; anIt != aLast && !aFound; anIt++) {
774       GeomShapePtr aCShape = *anIt;
775       if (aCShape.get())
776         aFound = aCShape->isEqual(aShape);
777     }
778   }
779   return aFound;
780 }