Salome HOME
Issue #2219: keep the order of remove-shape sub-elements after deselection of some...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetMultiSelector.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ModuleBase_WidgetMultiSelector.h>
22 #include <ModuleBase_WidgetShapeSelector.h>
23 #include <ModuleBase_ISelection.h>
24 #include <ModuleBase_IWorkshop.h>
25 #include <ModuleBase_IViewer.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_Definitions.h>
28 #include <ModuleBase_IModule.h>
29 #include <ModuleBase_ViewerPrs.h>
30 #include <ModuleBase_IconFactory.h>
31 #include <ModuleBase_Events.h>
32
33 #include <ModelAPI_Data.h>
34 #include <ModelAPI_Object.h>
35 #include <ModelAPI_AttributeSelectionList.h>
36 #include <ModelAPI_AttributeRefList.h>
37 #include <ModelAPI_AttributeRefAttrList.h>
38 #include <ModelAPI_Tools.h>
39 #include <ModelAPI_Events.h>
40
41 #include <Config_WidgetAPI.h>
42
43 #include <QGridLayout>
44 #include <QLabel>
45 #include <QListWidget>
46 #include <QObject>
47 #include <QString>
48 #include <QComboBox>
49 #include <QEvent>
50 #include <QAction>
51 #include <QApplication>
52 #include <QClipboard>
53 #include <QTimer>
54 #include <QMainWindow>
55
56 #include <memory>
57 #include <string>
58
59 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
60
61 /**
62 * Customization of a List Widget to make it to be placed on full width of container
63 */
64 class CustomListWidget : public QListWidget
65 {
66 public:
67   /// Constructor
68   /// \param theParent a parent widget
69   CustomListWidget( QWidget* theParent )
70     : QListWidget( theParent )
71   {
72   }
73
74   /// Redefinition of virtual method
75   virtual QSize sizeHint() const
76   {
77     int aHeight = 2*QFontMetrics( font() ).height();
78     QSize aSize = QListWidget::sizeHint();
79     return QSize( aSize.width(), aHeight );
80   }
81
82   /// Redefinition of virtual method
83   virtual QSize minimumSizeHint() const
84   {
85     int aHeight = 4/*2*/*QFontMetrics( font() ).height();
86     QSize aSize = QListWidget::minimumSizeHint();
87     return QSize( aSize.width(), aHeight );
88   }
89
90 #ifndef WIN32
91 // The code is necessary only for Linux because
92 //it can not update viewport on widget resize
93 protected:
94   void resizeEvent(QResizeEvent* theEvent)
95   {
96     QListWidget::resizeEvent(theEvent);
97     QTimer::singleShot(5, viewport(), SLOT(repaint()));
98   }
99 #endif
100 };
101
102 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
103                                                                ModuleBase_IWorkshop* theWorkshop,
104                                                                const Config_WidgetAPI* theData)
105 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData),
106   myIsSetSelectionBlocked(false)
107 {
108   QGridLayout* aMainLay = new QGridLayout(this);
109   ModuleBase_Tools::adjustMargins(aMainLay);
110
111   QLabel* aTypeLabel = new QLabel(tr("Type"), this);
112   aMainLay->addWidget(aTypeLabel, 0, 0);
113
114   myTypeCombo = new QComboBox(this);
115   // There is no sense to parameterize list of types while we can not parameterize selection mode
116
117   std::string aPropertyTypes = theData->getProperty("type_choice");
118   QString aTypesStr = aPropertyTypes.c_str();
119   QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
120
121   myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
122
123   if (!aShapeTypes.empty())
124     myTypeCombo->addItems(aShapeTypes);
125   aMainLay->addWidget(myTypeCombo, 0, 1);
126   // if the xml definition contains one type, the controls to select a type should not be shown
127   if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
128     aTypeLabel->setVisible(false);
129     myTypeCombo->setVisible(false);
130   }
131
132   QString aLabelText = translate(theData->getProperty("label"));
133   QLabel* aListLabel = new QLabel(aLabelText, this);
134   aMainLay->addWidget(aListLabel, 1, 0);
135   // if the xml definition contains one type, an information label
136   // should be shown near to the latest
137   if (aShapeTypes.size() <= 1) {
138     QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
139     if (!aLabelIcon.isEmpty()) {
140       QLabel* aSelectedLabel = new QLabel("", this);
141       aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
142       aMainLay->addWidget(aSelectedLabel, 1, 1);
143     }
144     aMainLay->setColumnStretch(2, 1);
145   }
146
147   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
148   myListControl = new CustomListWidget(this);
149   QString anObjName = QString::fromStdString(attributeID());
150   myListControl->setObjectName(anObjName);
151   myListControl->setToolTip(aToolTip);
152   myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
153
154   aMainLay->addWidget(myListControl, 2, 0, 1, -1);
155   aMainLay->setRowStretch(2, 1);
156   //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
157   //aMainLay->setRowMinimumHeight(3, 20);
158   //this->setLayout(aMainLay);
159   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
160
161   myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
162                           myWorkshop->desktop(), this, SLOT(onCopyItem()));
163   myCopyAction->setShortcut(QKeySequence::Copy);
164   myCopyAction->setEnabled(false);
165   myListControl->addAction(myCopyAction);
166
167   myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
168                           myWorkshop->desktop(), this, SLOT(onDeleteItem()));
169   myDeleteAction->setEnabled(false);
170   myListControl->addAction(myDeleteAction);
171
172   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
173   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
174
175   myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
176 }
177
178 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
179 {
180 }
181
182 //********************************************************************
183 void ModuleBase_WidgetMultiSelector::activateCustom()
184 {
185   ModuleBase_WidgetSelector::activateCustom();
186
187   myWorkshop->module()->activateCustomPrs(myFeature,
188                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
189 }
190
191 //********************************************************************
192 void ModuleBase_WidgetMultiSelector::deactivate()
193 {
194   ModuleBase_WidgetSelector::deactivate();
195
196   myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
197 }
198
199 //********************************************************************
200 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
201 {
202   // the value is stored on the selection changed signal processing
203   // A rare case when plugin was not loaded.
204   if (!myFeature)
205     return false;
206
207   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
208   std::string aType = anAttribute->attributeType();
209   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
210     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
211     aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
212   }
213   return true;
214 }
215
216 //********************************************************************
217 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
218 {
219   // A rare case when plugin was not loaded.
220   if (!myFeature)
221     return false;
222
223   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
224   std::string aType = anAttribute->attributeType();
225   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
226     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
227     // Restore shape type
228     std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
229     if (!aSelectionType.empty())
230       setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
231   }
232   updateSelectionList();
233   return true;
234 }
235
236 //********************************************************************
237 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
238                                                   const bool theToValidate)
239 {
240   if (myIsSetSelectionBlocked)
241     return false;
242
243   AttributeSelectionListPtr aSelectionListAttr;
244   if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
245     aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
246   if (aSelectionListAttr.get())
247     aSelectionListAttr->cashValues(true);
248
249   /// remove unused objects from the model attribute.
250   /// It should be performed before new attributes append.
251   bool isDone = removeUnusedAttributeObjects(theValues);
252
253   QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
254   QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
255   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
256   for (; anIt != aLast; anIt++) {
257     ModuleBase_ViewerPrsPtr aValue = *anIt;
258     // do not validate and append to attribute selection presentation if it exists in the attribute
259     ObjectPtr anObject;
260     GeomShapePtr aShape;
261     getGeomSelection(aValue, anObject, aShape);
262     if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
263       anAttributeValues.append(aValue);
264       continue;
265     }
266     if (theToValidate && !isValidInFilters(aValue))
267       anInvalidValues.append(aValue);
268   }
269   bool aHasInvalidValues = anInvalidValues.size() > 0;
270
271   for (anIt = theValues.begin(); anIt != aLast; anIt++) {
272     ModuleBase_ViewerPrsPtr aValue = *anIt;
273     bool aProcessed = false;
274     if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
275         anAttributeValues.contains(aValue))
276       continue;
277     aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
278     // if there is at least one set, the result is true
279     isDone = isDone || aProcessed;
280   }
281   // updateObject - to update/redisplay feature
282   // it is commented in order to perfom it outside the method
283   //if (isDone) {
284     //updateObject(myFeature);
285     // this emit is necessary to call store/restore method an restore type of selection
286     //emit valuesChanged();
287   //}
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   ModuleBase_Tools::setFocus(myListControl,
473                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
474 }
475
476 //********************************************************************
477 void ModuleBase_WidgetMultiSelector::updateSelectionName()
478 {
479 }
480
481 //********************************************************************
482 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
483 {
484   if (myIsSetSelectionBlocked)
485     return;
486   ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
487
488   // according to #2154 we need to update OB selection when selection in the viewer happens
489   // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
490   // because we need that Object Browser has been already updated when synchronize happens.
491
492   // Restore selection in the viewer by the attribute selection list
493   // it is possible that diring selection attribute filling, selection in Object Browser
494   // is changed(some items were removed/added) and as result, selection in the viewer
495   // differs from the selection come to this method. By next rows, we restore selection
496   // in the viewer according to content of selection attribute. Case is Edge selection in Group
497   myIsSetSelectionBlocked = true;
498   static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
499   ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
500   Events_Loop::loop()->flush(anEvent);
501   myIsSetSelectionBlocked = false;
502 }
503
504 //********************************************************************
505 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
506 {
507   QIntList aShapeTypes;
508
509   if (myTypeCombo->count() > 1 && myIsUseChoice) {
510     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
511   }
512   else {
513     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
514       aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
515   }
516   return aShapeTypes;
517 }
518
519 //********************************************************************
520 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
521 {
522   QString aShapeTypeName;
523
524   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
525     aShapeTypeName = myTypeCombo->itemText(idx);
526     int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
527     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
528       bool aWasActivated = activateSelectionAndFilters(false);
529       bool isBlocked = myTypeCombo->blockSignals(true);
530       myTypeCombo->setCurrentIndex(idx);
531       myTypeCombo->blockSignals(isBlocked);
532       if (aWasActivated)
533         activateSelectionAndFilters(true);
534       break;
535     }
536   }
537 }
538
539 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
540 {
541   QList<ModuleBase_ViewerPrsPtr> aSelected;
542   convertIndicesToViewerSelection(std::set<int>(), aSelected);
543   return aSelected;
544 }
545
546 //********************************************************************
547 void ModuleBase_WidgetMultiSelector::updateSelectionList()
548 {
549   myListControl->clear();
550
551   DataPtr aData = myFeature->data();
552   AttributePtr anAttribute = aData->attribute(attributeID());
553   std::string aType = anAttribute->attributeType();
554   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
555     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
556     for (int i = 0; i < aSelectionListAttr->size(); i++) {
557       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
558       QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
559       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
560       myListControl->addItem(anItem);
561     }
562   }
563   else if (aType == ModelAPI_AttributeRefList::typeId()) {
564     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
565     for (int i = 0; i < aRefListAttr->size(); i++) {
566       ObjectPtr anObject = aRefListAttr->object(i);
567       if (anObject.get()) {
568         QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
569                                                       myListControl);
570         anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
571         myListControl->addItem(anItem);
572       }
573     }
574   }
575   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
576     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
577     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
578       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
579       QString aName;
580       if (anAttribute.get()) {
581         std::string anAttrName = generateName(anAttribute, myWorkshop);
582         aName = QString::fromStdString(anAttrName);
583       }
584       else {
585         ObjectPtr anObject = aRefAttrListAttr->object(i);
586         if (anObject.get()) {
587           aName = anObject->data()->name().c_str();
588         }
589       }
590       QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
591       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
592       myListControl->addItem(anItem);
593     }
594   }
595
596   // We have to call repaint because sometimes the List control is not updated
597   myListControl->repaint();
598 }
599
600 //********************************************************************
601 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
602 {
603   std::string aType;
604
605   if (theType == "Vertices")
606     aType = "vertex";
607   else if (theType == "Edges")
608     aType = "edge";
609   else if (theType == "Faces")
610     aType = "face";
611   else if (theType == "Solids")
612     aType = "solid";
613
614   return aType;
615 }
616
617 //********************************************************************
618 void ModuleBase_WidgetMultiSelector::clearSelection()
619 {
620   bool isClearInNeutralPoint = myIsNeutralPointClear;
621   myIsNeutralPointClear = true;
622
623   QList<ModuleBase_ViewerPrsPtr> anEmptyList;
624   // This method will call Selection changed event which will call onSelectionChanged
625   // To clear mySelection, myListControl and storeValue()
626   // So, we don't need to call it
627   myWorkshop->setSelected(anEmptyList);
628
629   myIsNeutralPointClear = isClearInNeutralPoint;
630 }
631
632 //********************************************************************
633 void ModuleBase_WidgetMultiSelector::onCopyItem()
634 {
635   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
636   QString aRes;
637   foreach(QListWidgetItem* aItem, aItems) {
638     if (!aRes.isEmpty())
639       aRes += "\n";
640     aRes += aItem->text();
641   }
642   if (!aRes.isEmpty()) {
643     QClipboard *clipboard = QApplication::clipboard();
644     clipboard->setText(aRes);
645   }
646 }
647
648 //********************************************************************
649 void ModuleBase_WidgetMultiSelector::onDeleteItem()
650 {
651   processDelete();
652 }
653
654 //********************************************************************
655 void ModuleBase_WidgetMultiSelector::onListSelection()
656 {
657   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
658   myCopyAction->setEnabled(!aItems.isEmpty());
659   myDeleteAction->setEnabled(!aItems.isEmpty());
660
661   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
662                                         true);
663 }
664
665 //********************************************************************
666 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
667 {
668   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
669   foreach(QListWidgetItem* anItem, aItems) {
670     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
671     if (theAttributeIds.find(anIndex) == theAttributeIds.end())
672       theAttributeIds.insert(anIndex);
673   }
674 }
675
676 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
677                                                    QList<ModuleBase_ViewerPrsPtr>& theValues) const
678 {
679   if(myFeature.get() == NULL)
680     return;
681
682   DataPtr aData = myFeature->data();
683   AttributePtr anAttribute = aData->attribute(attributeID());
684   std::string aType = anAttribute->attributeType();
685   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
686     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
687     for (int i = 0; i < aSelectionListAttr->size(); i++) {
688       // filter by attribute indices only if the container is not empty otherwise return all items
689       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
690         continue;
691       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
692       ResultPtr anObject = anAttr->context();
693       if (anObject.get())
694         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
695                new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
696     }
697   }
698   else if (aType == ModelAPI_AttributeRefList::typeId()) {
699     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
700     for (int i = 0; i < aRefListAttr->size(); i++) {
701       // filter by attribute indices only if the container is not empty otherwise return all items
702       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
703         continue;
704       ObjectPtr anObject = aRefListAttr->object(i);
705       if (anObject.get()) {
706         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
707                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
708       }
709     }
710   }
711   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
712     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
713     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
714       // filter by attribute indices only if the container is not empty otherwise return all items
715       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
716         continue;
717       ObjectPtr anObject = aRefAttrListAttr->object(i);
718       if (!anObject.get())
719         continue;
720       TopoDS_Shape aShape;
721       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
722       if (anAttribute.get()) {
723         GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
724         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
725                new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
726       }
727     }
728   }
729 }
730
731 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
732                                                  (QList<ModuleBase_ViewerPrsPtr>& theValues)
733 {
734   bool isDone = false;
735
736   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
737   DataPtr aData = myFeature->data();
738   AttributePtr anAttribute = aData->attribute(attributeID());
739   std::string aType = anAttribute->attributeType();
740   std::set<GeomShapePtr> aShapes;
741   std::set<int> anIndicesToBeRemoved;
742   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
743     // iteration through data model to find not selected elements to remove them
744     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
745     for (int i = 0; i < aSelectionListAttr->size(); i++) {
746       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
747       bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
748       if (!aFound)
749         anIndicesToBeRemoved.insert(i);
750     }
751     isDone = anIndicesToBeRemoved.size() > 0;
752     aSelectionListAttr->remove(anIndicesToBeRemoved);
753   }
754   else if (aType == ModelAPI_AttributeRefList::typeId()) {
755     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
756     for (int i = 0; i < aRefListAttr->size(); i++) {
757       ObjectPtr anObject = aRefListAttr->object(i);
758       if (anObject.get()) {
759         bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
760         if (!aFound)
761           anIndicesToBeRemoved.insert(i);
762       }
763     }
764     isDone = anIndicesToBeRemoved.size() > 0;
765     aRefListAttr->remove(anIndicesToBeRemoved);
766   }
767   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
768     std::set<AttributePtr> anAttributes;
769     QList<ModuleBase_ViewerPrsPtr>::const_iterator
770       anIt = theValues.begin(), aLast = theValues.end();
771     ObjectPtr anObject;
772     GeomShapePtr aShape;
773     for (; anIt != aLast; anIt++) {
774       ModuleBase_ViewerPrsPtr aPrs = *anIt;
775       getGeomSelection(aPrs, anObject, aShape);
776       AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
777       if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
778         anAttributes.insert(anAttr);
779     }
780
781     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
782     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
783       bool aFound = false;
784       if (aRefAttrListAttr->isAttribute(i)) {
785         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
786         aFound = anAttributes.find(anAttribute) != anAttributes.end();
787       }
788       else {
789         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
790       }
791       if (!aFound)
792         anIndicesToBeRemoved.insert(i);
793     }
794     isDone = anIndicesToBeRemoved.size() > 0;
795     aRefAttrListAttr->remove(anIndicesToBeRemoved);
796   }
797
798   return isDone;
799 }
800
801 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
802                                                      (QList<ModuleBase_ViewerPrsPtr>& theValues)
803 {
804   // convert prs list to objects map
805   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
806   std::set<GeomShapePtr> aShapes;
807   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
808   ObjectPtr anObject;
809   GeomShapePtr aShape;
810   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
811   for (; anIt != aLast; anIt++) {
812     ModuleBase_ViewerPrsPtr aPrs = *anIt;
813     getGeomSelection(aPrs, anObject, aShape);
814     aShapes.clear();
815     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
816       aShapes = aGeomSelection[anObject];
817     // we need to know if there was an empty shape in selection for the object
818     if (!aShape.get())
819       aShape = anEmptyShape;
820     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
821       aShapes.insert(aShape);
822     aGeomSelection[anObject] = aShapes;
823   }
824   return aGeomSelection;
825 }
826
827 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
828                               GeomShapePtr theShape,
829                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
830 {
831   bool aFound = false;
832   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
833   if (theShape.get()) { // treat shape equal to context as null: 2219, keep order of shapes in list
834     const ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
835     if (aContext.get() && aContext->shape()->isEqual(theShape))
836       theShape.reset();
837   }
838   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
839   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
840     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
841     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
842     for (; anIt != aLast && !aFound; anIt++) {
843       GeomShapePtr aCShape = *anIt;
844       if (aCShape.get())
845         aFound = aCShape->isSame(aShape);
846     }
847   }
848   return aFound;
849 }