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