Salome HOME
Issue #2097 Edit group with change of type does not work
[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   clearValidatedCash();
411   activateSelectionAndFilters(true);
412
413   if (!myFeature)
414     return;
415   /// store the selected type
416   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
417   std::string aType = anAttribute->attributeType();
418   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
419     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
420     aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
421   }
422
423   // clear attribute values
424   DataPtr aData = myFeature->data();
425   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
426     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
427     aSelectionListAttr->clear();
428   }
429   else if (aType == ModelAPI_AttributeRefList::typeId()) {
430     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
431     aRefListAttr->clear();
432   }
433   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
434     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
435     aRefAttrListAttr->clear();
436   }
437
438   // update object is necessary to flush update signal. It leads to objects references map update
439   // and the operation presentation will not contain deleted items visualized as parameters of
440   // the feature.
441   updateObject(myFeature);
442   restoreValue();
443   myWorkshop->setSelected(getAttributeSelection());
444   // may be the feature's result is not displayed, but attributes should be
445   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
446                             true); /// hope that something is redisplayed by object updated
447 }
448
449 //********************************************************************
450 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
451 {
452   if (!myIsNeutralPointClear) {
453     QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
454     // do not clear selected object
455     if (aSelected.size() == 0) {
456       if (!getAttributeSelection().empty()) {
457         // Restore selection in the viewer by the attribute selection list
458         // it should be postponed to exit from the selectionChanged processing
459         static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
460         ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
461         Events_Loop::loop()->flush(anEvent);
462         return;
463       }
464     }
465   }
466   ModuleBase_WidgetSelector::onSelectionChanged();
467 }
468
469 void ModuleBase_WidgetMultiSelector::updateFocus()
470 {
471   // Set focus to List control in order to make possible
472   // to use Tab key for transfer the focus to next widgets
473   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
474   ModuleBase_Tools::setFocus(myListControl,
475                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
476 }
477
478 //********************************************************************
479 void ModuleBase_WidgetMultiSelector::updateSelectionName()
480 {
481 }
482
483 //********************************************************************
484 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
485 {
486   QIntList aShapeTypes;
487
488   if (myTypeCombo->count() > 1 && myIsUseChoice) {
489     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
490   }
491   else {
492     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
493       aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
494   }
495   return aShapeTypes;
496 }
497
498 //********************************************************************
499 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
500 {
501   QString aShapeTypeName;
502
503   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
504     aShapeTypeName = myTypeCombo->itemText(idx);
505     int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
506     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
507       bool aWasActivated = activateSelectionAndFilters(false);
508       bool isBlocked = myTypeCombo->blockSignals(true);
509       myTypeCombo->setCurrentIndex(idx);
510       myTypeCombo->blockSignals(isBlocked);
511       if (aWasActivated)
512         activateSelectionAndFilters(true);
513       break;
514     }
515   }
516 }
517
518 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
519 {
520   QList<ModuleBase_ViewerPrsPtr> aSelected;
521   convertIndicesToViewerSelection(std::set<int>(), aSelected);
522   return aSelected;
523 }
524
525 //********************************************************************
526 void ModuleBase_WidgetMultiSelector::updateSelectionList()
527 {
528   myListControl->clear();
529
530   DataPtr aData = myFeature->data();
531   AttributePtr anAttribute = aData->attribute(attributeID());
532   std::string aType = anAttribute->attributeType();
533   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
534     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
535     for (int i = 0; i < aSelectionListAttr->size(); i++) {
536       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
537       QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
538       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
539       myListControl->addItem(anItem);
540     }
541   }
542   else if (aType == ModelAPI_AttributeRefList::typeId()) {
543     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
544     for (int i = 0; i < aRefListAttr->size(); i++) {
545       ObjectPtr anObject = aRefListAttr->object(i);
546       if (anObject.get()) {
547         QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
548                                                       myListControl);
549         anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
550         myListControl->addItem(anItem);
551       }
552     }
553   }
554   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
555     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
556     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
557       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
558       QString aName;
559       if (anAttribute.get()) {
560         std::string anAttrName = generateName(anAttribute, myWorkshop);
561         aName = QString::fromStdString(anAttrName);
562       }
563       else {
564         ObjectPtr anObject = aRefAttrListAttr->object(i);
565         if (anObject.get()) {
566           aName = anObject->data()->name().c_str();
567         }
568       }
569       QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
570       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
571       myListControl->addItem(anItem);
572     }
573   }
574
575   // We have to call repaint because sometimes the List control is not updated
576   myListControl->repaint();
577 }
578
579 //********************************************************************
580 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
581 {
582   std::string aType;
583
584   if (theType == "Vertices")
585     aType = "vertex";
586   else if (theType == "Edges")
587     aType = "edge";
588   else if (theType == "Faces")
589     aType = "face";
590   else if (theType == "Solids")
591     aType = "solid";
592
593   return aType;
594 }
595
596 //********************************************************************
597 void ModuleBase_WidgetMultiSelector::clearSelection()
598 {
599   bool isClearInNeutralPoint = myIsNeutralPointClear;
600   myIsNeutralPointClear = true;
601
602   QList<ModuleBase_ViewerPrsPtr> anEmptyList;
603   // This method will call Selection changed event which will call onSelectionChanged
604   // To clear mySelection, myListControl and storeValue()
605   // So, we don't need to call it
606   myWorkshop->setSelected(anEmptyList);
607
608   myIsNeutralPointClear = isClearInNeutralPoint;
609 }
610
611 //********************************************************************
612 void ModuleBase_WidgetMultiSelector::onCopyItem()
613 {
614   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
615   QString aRes;
616   foreach(QListWidgetItem* aItem, aItems) {
617     if (!aRes.isEmpty())
618       aRes += "\n";
619     aRes += aItem->text();
620   }
621   if (!aRes.isEmpty()) {
622     QClipboard *clipboard = QApplication::clipboard();
623     clipboard->setText(aRes);
624   }
625 }
626
627 //********************************************************************
628 void ModuleBase_WidgetMultiSelector::onDeleteItem()
629 {
630   processDelete();
631 }
632
633 //********************************************************************
634 void ModuleBase_WidgetMultiSelector::onListSelection()
635 {
636   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
637   myCopyAction->setEnabled(!aItems.isEmpty());
638   myDeleteAction->setEnabled(!aItems.isEmpty());
639
640   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
641                                         true);
642 }
643
644 //********************************************************************
645 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
646 {
647   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
648   foreach(QListWidgetItem* anItem, aItems) {
649     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
650     if (theAttributeIds.find(anIndex) == theAttributeIds.end())
651       theAttributeIds.insert(anIndex);
652   }
653 }
654
655 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
656                                                    QList<ModuleBase_ViewerPrsPtr>& theValues) const
657 {
658   if(myFeature.get() == NULL)
659     return;
660
661   DataPtr aData = myFeature->data();
662   AttributePtr anAttribute = aData->attribute(attributeID());
663   std::string aType = anAttribute->attributeType();
664   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
665     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
666     for (int i = 0; i < aSelectionListAttr->size(); i++) {
667       // filter by attribute indices only if the container is not empty otherwise return all items
668       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
669         continue;
670       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
671       ResultPtr anObject = anAttr->context();
672       if (anObject.get())
673         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
674                new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
675     }
676   }
677   else if (aType == ModelAPI_AttributeRefList::typeId()) {
678     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
679     for (int i = 0; i < aRefListAttr->size(); i++) {
680       // filter by attribute indices only if the container is not empty otherwise return all items
681       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
682         continue;
683       ObjectPtr anObject = aRefListAttr->object(i);
684       if (anObject.get()) {
685         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
686                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
687       }
688     }
689   }
690   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
691     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
692     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
693       // filter by attribute indices only if the container is not empty otherwise return all items
694       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
695         continue;
696       ObjectPtr anObject = aRefAttrListAttr->object(i);
697       if (!anObject.get())
698         continue;
699       TopoDS_Shape aShape;
700       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
701       if (anAttribute.get()) {
702         GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
703         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
704                new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
705       }
706     }
707   }
708 }
709
710 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
711                                                  (QList<ModuleBase_ViewerPrsPtr>& theValues)
712 {
713   bool isDone = false;
714
715   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
716   DataPtr aData = myFeature->data();
717   AttributePtr anAttribute = aData->attribute(attributeID());
718   std::string aType = anAttribute->attributeType();
719   std::set<GeomShapePtr> aShapes;
720   std::set<int> anIndicesToBeRemoved;
721   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
722     // iteration through data model to find not selected elements to remove them
723     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
724     for (int i = 0; i < aSelectionListAttr->size(); i++) {
725       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
726       bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
727       if (!aFound)
728         anIndicesToBeRemoved.insert(i);
729     }
730     isDone = anIndicesToBeRemoved.size() > 0;
731     aSelectionListAttr->remove(anIndicesToBeRemoved);
732   }
733   else if (aType == ModelAPI_AttributeRefList::typeId()) {
734     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
735     for (int i = 0; i < aRefListAttr->size(); i++) {
736       ObjectPtr anObject = aRefListAttr->object(i);
737       if (anObject.get()) {
738         bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
739         if (!aFound)
740           anIndicesToBeRemoved.insert(i);
741       }
742     }
743     isDone = anIndicesToBeRemoved.size() > 0;
744     aRefListAttr->remove(anIndicesToBeRemoved);
745   }
746   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
747     std::set<AttributePtr> anAttributes;
748     QList<ModuleBase_ViewerPrsPtr>::const_iterator
749       anIt = theValues.begin(), aLast = theValues.end();
750     ObjectPtr anObject;
751     GeomShapePtr aShape;
752     for (; anIt != aLast; anIt++) {
753       ModuleBase_ViewerPrsPtr aPrs = *anIt;
754       getGeomSelection(aPrs, anObject, aShape);
755       AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
756       if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
757         anAttributes.insert(anAttr);
758     }
759
760     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
761     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
762       bool aFound = false;
763       if (aRefAttrListAttr->isAttribute(i)) {
764         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
765         aFound = anAttributes.find(anAttribute) != anAttributes.end();
766       }
767       else {
768         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
769       }
770       if (!aFound)
771         anIndicesToBeRemoved.insert(i);
772     }
773     isDone = anIndicesToBeRemoved.size() > 0;
774     aRefAttrListAttr->remove(anIndicesToBeRemoved);
775   }
776
777   return isDone;
778 }
779
780 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
781                                                      (QList<ModuleBase_ViewerPrsPtr>& theValues)
782 {
783   // convert prs list to objects map
784   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
785   std::set<GeomShapePtr> aShapes;
786   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
787   ObjectPtr anObject;
788   GeomShapePtr aShape;
789   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
790   for (; anIt != aLast; anIt++) {
791     ModuleBase_ViewerPrsPtr aPrs = *anIt;
792     getGeomSelection(aPrs, anObject, aShape);
793     aShapes.clear();
794     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
795       aShapes = aGeomSelection[anObject];
796     // we need to know if there was an empty shape in selection for the object
797     if (!aShape.get())
798       aShape = anEmptyShape;
799     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
800       aShapes.insert(aShape);
801     aGeomSelection[anObject] = aShapes;
802   }
803   return aGeomSelection;
804 }
805
806 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
807                               const GeomShapePtr& theShape,
808                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
809 {
810   bool aFound = false;
811   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
812   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
813   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
814     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
815     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
816     for (; anIt != aLast && !aFound; anIt++) {
817       GeomShapePtr aCShape = *anIt;
818       if (aCShape.get())
819         aFound = aCShape->isSame(aShape);
820     }
821   }
822   return aFound;
823 }