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