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