Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include <ModuleBase_WidgetMultiSelector.h>
21 #include <ModuleBase_WidgetShapeSelector.h>
22 #include <ModuleBase_ISelection.h>
23 #include <ModuleBase_IWorkshop.h>
24 #include <ModuleBase_IViewer.h>
25 #include <ModuleBase_Tools.h>
26 #include <ModuleBase_Definitions.h>
27 #include <ModuleBase_IModule.h>
28 #include <ModuleBase_ViewerPrs.h>
29 #include <ModuleBase_IconFactory.h>
30 #include <ModuleBase_Events.h>
31
32 #include <ModelAPI_Data.h>
33 #include <ModelAPI_Object.h>
34 #include <ModelAPI_AttributeSelectionList.h>
35 #include <ModelAPI_AttributeRefList.h>
36 #include <ModelAPI_AttributeRefAttrList.h>
37 #include <ModelAPI_Tools.h>
38 #include <ModelAPI_Events.h>
39
40 #include <Config_WidgetAPI.h>
41
42 #include <QGridLayout>
43 #include <QLabel>
44 #include <QListWidget>
45 #include <QObject>
46 #include <QString>
47 #include <QComboBox>
48 #include <QEvent>
49 #include <QAction>
50 #include <QApplication>
51 #include <QClipboard>
52 #include <QTimer>
53 #include <QMainWindow>
54
55 #include <memory>
56 #include <string>
57
58 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
59
60 /**
61 * Customization of a List Widget to make it to be placed on full width of container
62 */
63 class CustomListWidget : public QListWidget
64 {
65 public:
66   /// Constructor
67   /// \param theParent a parent widget
68   CustomListWidget( QWidget* theParent )
69     : QListWidget( theParent )
70   {
71   }
72
73   /// Redefinition of virtual method
74   virtual QSize sizeHint() const
75   {
76     int aHeight = 2*QFontMetrics( font() ).height();
77     QSize aSize = QListWidget::sizeHint();
78     return QSize( aSize.width(), aHeight );
79   }
80
81   /// Redefinition of virtual method
82   virtual QSize minimumSizeHint() const
83   {
84     int aHeight = 4/*2*/*QFontMetrics( font() ).height();
85     QSize aSize = QListWidget::minimumSizeHint();
86     return QSize( aSize.width(), aHeight );
87   }
88
89 #ifndef WIN32
90 // The code is necessary only for Linux because
91 //it can not update viewport on widget resize
92 protected:
93   void resizeEvent(QResizeEvent* theEvent)
94   {
95     QListWidget::resizeEvent(theEvent);
96     QTimer::singleShot(5, viewport(), SLOT(repaint()));
97   }
98 #endif
99 };
100
101 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
102                                                                ModuleBase_IWorkshop* theWorkshop,
103                                                                const Config_WidgetAPI* theData)
104 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData),
105   myIsSetSelectionBlocked(false)
106 {
107   QGridLayout* aMainLay = new QGridLayout(this);
108   ModuleBase_Tools::adjustMargins(aMainLay);
109
110   QLabel* aTypeLabel = new QLabel(tr("Type"), this);
111   aMainLay->addWidget(aTypeLabel, 0, 0);
112
113   myTypeCombo = new QComboBox(this);
114   // There is no sense to parameterize list of types while we can not parameterize selection mode
115
116   std::string aPropertyTypes = theData->getProperty("type_choice");
117   QString aTypesStr = aPropertyTypes.c_str();
118   QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
119
120   myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
121
122   if (!aShapeTypes.empty())
123     myTypeCombo->addItems(aShapeTypes);
124   aMainLay->addWidget(myTypeCombo, 0, 1);
125   // if the xml definition contains one type, the controls to select a type should not be shown
126   if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
127     aTypeLabel->setVisible(false);
128     myTypeCombo->setVisible(false);
129   }
130
131   QString aLabelText = translate(theData->getProperty("label"));
132   QLabel* aListLabel = new QLabel(aLabelText, this);
133   aMainLay->addWidget(aListLabel, 1, 0);
134   // if the xml definition contains one type, an information label
135   // should be shown near to the latest
136   if (aShapeTypes.size() <= 1) {
137     QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
138     if (!aLabelIcon.isEmpty()) {
139       QLabel* aSelectedLabel = new QLabel("", this);
140       aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
141       aMainLay->addWidget(aSelectedLabel, 1, 1);
142     }
143     aMainLay->setColumnStretch(2, 1);
144   }
145
146   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
147   myListControl = new CustomListWidget(this);
148   QString anObjName = QString::fromStdString(attributeID());
149   myListControl->setObjectName(anObjName);
150   myListControl->setToolTip(aToolTip);
151   myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
152
153   aMainLay->addWidget(myListControl, 2, 0, 1, -1);
154   aMainLay->setRowStretch(2, 1);
155   //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
156   //aMainLay->setRowMinimumHeight(3, 20);
157   //this->setLayout(aMainLay);
158   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
159
160   myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
161                           myWorkshop->desktop(), this, SLOT(onCopyItem()));
162   myCopyAction->setShortcut(QKeySequence::Copy);
163   myCopyAction->setEnabled(false);
164   myListControl->addAction(myCopyAction);
165
166   myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
167                           myWorkshop->desktop(), this, SLOT(onDeleteItem()));
168   myDeleteAction->setEnabled(false);
169   myListControl->addAction(myDeleteAction);
170
171   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
172   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
173
174   myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
175 }
176
177 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
178 {
179 }
180
181 //********************************************************************
182 void ModuleBase_WidgetMultiSelector::activateCustom()
183 {
184   ModuleBase_WidgetSelector::activateCustom();
185
186   myWorkshop->module()->activateCustomPrs(myFeature,
187                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
188 }
189
190 //********************************************************************
191 void ModuleBase_WidgetMultiSelector::deactivate()
192 {
193   ModuleBase_WidgetSelector::deactivate();
194
195   myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
196 }
197
198 //********************************************************************
199 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
200 {
201   // the value is stored on the selection changed signal processing
202   // A rare case when plugin was not loaded.
203   if (!myFeature)
204     return false;
205
206   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
207   std::string aType = anAttribute->attributeType();
208   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
209     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
210     aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
211   }
212   return true;
213 }
214
215 //********************************************************************
216 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
217 {
218   // A rare case when plugin was not loaded.
219   if (!myFeature)
220     return false;
221
222   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
223   std::string aType = anAttribute->attributeType();
224   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
225     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
226     // Restore shape type
227     std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
228     if (!aSelectionType.empty())
229       setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
230   }
231   updateSelectionList();
232   return true;
233 }
234
235 //********************************************************************
236 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
237                                                   const bool theToValidate)
238 {
239   if (myIsSetSelectionBlocked)
240     return false;
241
242   AttributeSelectionListPtr aSelectionListAttr;
243   if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
244     aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
245   if (aSelectionListAttr.get())
246     aSelectionListAttr->cashValues(true);
247
248   /// remove unused objects from the model attribute.
249   /// It should be performed before new attributes append.
250   bool isDone = removeUnusedAttributeObjects(theValues);
251
252   QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
253   QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
254   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
255   for (; anIt != aLast; anIt++) {
256     ModuleBase_ViewerPrsPtr aValue = *anIt;
257     // do not validate and append to attribute selection presentation if it exists in the attribute
258     ObjectPtr anObject;
259     GeomShapePtr aShape;
260     getGeomSelection(aValue, anObject, aShape);
261     if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
262       anAttributeValues.append(aValue);
263       continue;
264     }
265     if (theToValidate && !isValidInFilters(aValue))
266       anInvalidValues.append(aValue);
267   }
268   bool aHasInvalidValues = anInvalidValues.size() > 0;
269
270   for (anIt = theValues.begin(); anIt != aLast; anIt++) {
271     ModuleBase_ViewerPrsPtr aValue = *anIt;
272     bool aProcessed = false;
273     if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
274         anAttributeValues.contains(aValue))
275       continue;
276     aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
277     // if there is at least one set, the result is true
278     isDone = isDone || aProcessed;
279   }
280   // updateObject - to update/redisplay feature
281   // it is commented in order to perfom it outside the method
282   //if (isDone) {
283     //updateObject(myFeature);
284     // this emit is necessary to call store/restore method an restore type of selection
285     //emit valuesChanged();
286   //}
287
288   if (aSelectionListAttr.get())
289     aSelectionListAttr->cashValues(false);
290
291   theValues.clear();
292   if (!anInvalidValues.empty())
293     theValues.append(anInvalidValues);
294
295   if (isDone) // may be the feature's result is not displayed, but attributes should be
296     myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
297                              true);/// hope that something is redisplayed by object updated
298
299   return isDone;
300 }
301
302 //********************************************************************
303 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
304 {
305   std::set<int> anAttributeIds;
306   getSelectedAttributeIndices(anAttributeIds);
307   if (!anAttributeIds.empty())
308     convertIndicesToViewerSelection(anAttributeIds, theValues);
309 }
310
311 //********************************************************************
312 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
313 {
314   bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
315   if (aValid) {
316     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
317     aValid = aResult.get() != NULL;
318     if (aValid) {
319       if (myFeature) {
320         // We can not select a result of our feature
321         std::list<ResultPtr> aResults;
322         ModelAPI_Tools::allResults(myFeature, aResults);
323         std::list<ResultPtr>::const_iterator aIt;
324         bool isSkipSelf = false;
325         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
326           if ((*aIt) == aResult) {
327             isSkipSelf = true;
328             break;
329           }
330         }
331         if (isSkipSelf)
332           aValid = false;
333       }
334     }
335   }
336   return aValid;
337 }
338
339 //********************************************************************
340 bool ModuleBase_WidgetMultiSelector::processDelete()
341 {
342   // find attribute indices to delete
343   std::set<int> anAttributeIds;
344   getSelectedAttributeIndices(anAttributeIds);
345
346   QModelIndexList aIndexes = myListControl->selectionModel()->selectedIndexes();
347
348   // refill attribute by the items which indices are not in the list of ids
349   bool aDone = false;
350   DataPtr aData = myFeature->data();
351   AttributePtr anAttribute = aData->attribute(attributeID());
352   std::string aType = anAttribute->attributeType();
353   aDone = !anAttributeIds.empty();
354   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
355     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
356     aSelectionListAttr->remove(anAttributeIds);
357
358   }
359   else if (aType == ModelAPI_AttributeRefList::typeId()) {
360     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
361     aRefListAttr->remove(anAttributeIds);
362   }
363   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
364     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
365     aRefAttrListAttr->remove(anAttributeIds);
366   }
367
368   if (aDone) {
369     // update object is necessary to flush update signal. It leads to objects references map update
370     // and the operation presentation will not contain deleted items visualized as parameters of
371     // the feature.
372     updateObject(myFeature);
373
374     restoreValue();
375     myWorkshop->setSelected(getAttributeSelection());
376
377     // may be the feature's result is not displayed, but attributes should be
378     myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
379                               true); /// hope that something is redisplayed by object updated
380   }
381
382   // Restore selection
383   int aRows = myListControl->model()->rowCount();
384   if (aRows > 0) {
385     foreach(QModelIndex aIndex, aIndexes) {
386       if (aIndex.row() < aRows)
387         myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
388       else {
389         QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
390         myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
391       }
392     }
393   }
394   return aDone;
395 }
396
397 //********************************************************************
398 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
399 {
400   QList<QWidget*> result;
401   //result << myTypeCombo;
402   result << myListControl;
403   return result;
404 }
405
406 //********************************************************************
407 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
408 {
409   activateSelectionAndFilters(true);
410
411   if (!myFeature)
412     return;
413   /// store the selected type
414   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
415   std::string aType = anAttribute->attributeType();
416   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
417     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
418     aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
419   }
420
421   // clear attribute values
422   DataPtr aData = myFeature->data();
423   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
424     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
425     aSelectionListAttr->clear();
426   }
427   else if (aType == ModelAPI_AttributeRefList::typeId()) {
428     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
429     aRefListAttr->clear();
430   }
431   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
432     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
433     aRefAttrListAttr->clear();
434   }
435
436   // update object is necessary to flush update signal. It leads to objects references map update
437   // and the operation presentation will not contain deleted items visualized as parameters of
438   // the feature.
439   updateObject(myFeature);
440   restoreValue();
441   myWorkshop->setSelected(getAttributeSelection());
442   // may be the feature's result is not displayed, but attributes should be
443   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
444                             true); /// hope that something is redisplayed by object updated
445 }
446
447 //********************************************************************
448 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
449 {
450   if (!myIsNeutralPointClear) {
451     QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
452     // do not clear selected object
453     if (aSelected.size() == 0) {
454       if (!getAttributeSelection().empty()) {
455         // Restore selection in the viewer by the attribute selection list
456         // it should be postponed to exit from the selectionChanged processing
457         static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
458         ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
459         Events_Loop::loop()->flush(anEvent);
460         return;
461       }
462     }
463   }
464   ModuleBase_WidgetSelector::onSelectionChanged();
465 }
466
467 void ModuleBase_WidgetMultiSelector::updateFocus()
468 {
469   // Set focus to List control in order to make possible
470   // to use Tab key for transfer the focus to next widgets
471   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
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                               const GeomShapePtr& theShape,
829                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
830 {
831   bool aFound = false;
832   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
833   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
834   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
835     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
836     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
837     for (; anIt != aLast && !aFound; anIt++) {
838       GeomShapePtr aCShape = *anIt;
839       if (aCShape.get())
840         aFound = aCShape->isSame(aShape);
841     }
842   }
843   return aFound;
844 }