]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
Merge branch 'master' into cgt/devCEA
[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   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
473   ModuleBase_Tools::setFocus(myListControl,
474                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
475 }
476
477 //********************************************************************
478 void ModuleBase_WidgetMultiSelector::updateSelectionName()
479 {
480 }
481
482 //********************************************************************
483 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
484 {
485   if (myIsSetSelectionBlocked)
486     return;
487   ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
488
489   // according to #2154 we need to update OB selection when selection in the viewer happens
490   // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
491   // because we need that Object Browser has been already updated when synchronize happens.
492
493   // Restore selection in the viewer by the attribute selection list
494   // it is possible that diring selection attribute filling, selection in Object Browser
495   // is changed(some items were removed/added) and as result, selection in the viewer
496   // differs from the selection come to this method. By next rows, we restore selection
497   // in the viewer according to content of selection attribute. Case is Edge selection in Group
498   myIsSetSelectionBlocked = true;
499   static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
500   ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
501   Events_Loop::loop()->flush(anEvent);
502   myIsSetSelectionBlocked = false;
503 }
504
505 //********************************************************************
506 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
507 {
508   QIntList aShapeTypes;
509
510   if (myTypeCombo->count() > 1 && myIsUseChoice) {
511     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
512   }
513   else {
514     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
515       aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
516   }
517   return aShapeTypes;
518 }
519
520 //********************************************************************
521 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
522 {
523   QString aShapeTypeName;
524
525   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
526     aShapeTypeName = myTypeCombo->itemText(idx);
527     int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
528     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
529       bool aWasActivated = activateSelectionAndFilters(false);
530       bool isBlocked = myTypeCombo->blockSignals(true);
531       myTypeCombo->setCurrentIndex(idx);
532       myTypeCombo->blockSignals(isBlocked);
533       if (aWasActivated)
534         activateSelectionAndFilters(true);
535       break;
536     }
537   }
538 }
539
540 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
541 {
542   QList<ModuleBase_ViewerPrsPtr> aSelected;
543   convertIndicesToViewerSelection(std::set<int>(), aSelected);
544   return aSelected;
545 }
546
547 //********************************************************************
548 void ModuleBase_WidgetMultiSelector::updateSelectionList()
549 {
550   myListControl->clear();
551
552   DataPtr aData = myFeature->data();
553   AttributePtr anAttribute = aData->attribute(attributeID());
554   std::string aType = anAttribute->attributeType();
555   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
556     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
557     for (int i = 0; i < aSelectionListAttr->size(); i++) {
558       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
559       QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
560       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
561       myListControl->addItem(anItem);
562     }
563   }
564   else if (aType == ModelAPI_AttributeRefList::typeId()) {
565     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
566     for (int i = 0; i < aRefListAttr->size(); i++) {
567       ObjectPtr anObject = aRefListAttr->object(i);
568       if (anObject.get()) {
569         QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
570                                                       myListControl);
571         anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
572         myListControl->addItem(anItem);
573       }
574     }
575   }
576   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
577     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
578     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
579       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
580       QString aName;
581       if (anAttribute.get()) {
582         std::string anAttrName = generateName(anAttribute, myWorkshop);
583         aName = QString::fromStdString(anAttrName);
584       }
585       else {
586         ObjectPtr anObject = aRefAttrListAttr->object(i);
587         if (anObject.get()) {
588           aName = anObject->data()->name().c_str();
589         }
590       }
591       QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
592       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
593       myListControl->addItem(anItem);
594     }
595   }
596
597   // We have to call repaint because sometimes the List control is not updated
598   myListControl->repaint();
599 }
600
601 //********************************************************************
602 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
603 {
604   std::string aType;
605
606   if (theType == "Vertices")
607     aType = "vertex";
608   else if (theType == "Edges")
609     aType = "edge";
610   else if (theType == "Faces")
611     aType = "face";
612   else if (theType == "Solids")
613     aType = "solid";
614
615   return aType;
616 }
617
618 //********************************************************************
619 void ModuleBase_WidgetMultiSelector::clearSelection()
620 {
621   bool isClearInNeutralPoint = myIsNeutralPointClear;
622   myIsNeutralPointClear = true;
623
624   QList<ModuleBase_ViewerPrsPtr> anEmptyList;
625   // This method will call Selection changed event which will call onSelectionChanged
626   // To clear mySelection, myListControl and storeValue()
627   // So, we don't need to call it
628   myWorkshop->setSelected(anEmptyList);
629
630   myIsNeutralPointClear = isClearInNeutralPoint;
631 }
632
633 //********************************************************************
634 void ModuleBase_WidgetMultiSelector::onCopyItem()
635 {
636   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
637   QString aRes;
638   foreach(QListWidgetItem* aItem, aItems) {
639     if (!aRes.isEmpty())
640       aRes += "\n";
641     aRes += aItem->text();
642   }
643   if (!aRes.isEmpty()) {
644     QClipboard *clipboard = QApplication::clipboard();
645     clipboard->setText(aRes);
646   }
647 }
648
649 //********************************************************************
650 void ModuleBase_WidgetMultiSelector::onDeleteItem()
651 {
652   processDelete();
653 }
654
655 //********************************************************************
656 void ModuleBase_WidgetMultiSelector::onListSelection()
657 {
658   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
659   myCopyAction->setEnabled(!aItems.isEmpty());
660   myDeleteAction->setEnabled(!aItems.isEmpty());
661
662   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
663                                         true);
664 }
665
666 //********************************************************************
667 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
668 {
669   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
670   foreach(QListWidgetItem* anItem, aItems) {
671     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
672     if (theAttributeIds.find(anIndex) == theAttributeIds.end())
673       theAttributeIds.insert(anIndex);
674   }
675 }
676
677 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
678                                                    QList<ModuleBase_ViewerPrsPtr>& theValues) const
679 {
680   if(myFeature.get() == NULL)
681     return;
682
683   DataPtr aData = myFeature->data();
684   AttributePtr anAttribute = aData->attribute(attributeID());
685   std::string aType = anAttribute->attributeType();
686   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
687     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
688     for (int i = 0; i < aSelectionListAttr->size(); i++) {
689       // filter by attribute indices only if the container is not empty otherwise return all items
690       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
691         continue;
692       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
693       ResultPtr anObject = anAttr->context();
694       if (anObject.get())
695         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
696                new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
697     }
698   }
699   else if (aType == ModelAPI_AttributeRefList::typeId()) {
700     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
701     for (int i = 0; i < aRefListAttr->size(); i++) {
702       // filter by attribute indices only if the container is not empty otherwise return all items
703       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
704         continue;
705       ObjectPtr anObject = aRefListAttr->object(i);
706       if (anObject.get()) {
707         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
708                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
709       }
710     }
711   }
712   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
713     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
714     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
715       // filter by attribute indices only if the container is not empty otherwise return all items
716       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
717         continue;
718       ObjectPtr anObject = aRefAttrListAttr->object(i);
719       if (!anObject.get())
720         continue;
721       TopoDS_Shape aShape;
722       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
723       if (anAttribute.get()) {
724         GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
725         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
726                new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
727       }
728     }
729   }
730 }
731
732 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
733                                                  (QList<ModuleBase_ViewerPrsPtr>& theValues)
734 {
735   bool isDone = false;
736
737   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
738   DataPtr aData = myFeature->data();
739   AttributePtr anAttribute = aData->attribute(attributeID());
740   std::string aType = anAttribute->attributeType();
741   std::set<GeomShapePtr> aShapes;
742   std::set<int> anIndicesToBeRemoved;
743   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
744     // iteration through data model to find not selected elements to remove them
745     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
746     for (int i = 0; i < aSelectionListAttr->size(); i++) {
747       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
748       bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
749       if (!aFound)
750         anIndicesToBeRemoved.insert(i);
751     }
752     isDone = anIndicesToBeRemoved.size() > 0;
753     aSelectionListAttr->remove(anIndicesToBeRemoved);
754   }
755   else if (aType == ModelAPI_AttributeRefList::typeId()) {
756     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
757     for (int i = 0; i < aRefListAttr->size(); i++) {
758       ObjectPtr anObject = aRefListAttr->object(i);
759       if (anObject.get()) {
760         bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
761         if (!aFound)
762           anIndicesToBeRemoved.insert(i);
763       }
764     }
765     isDone = anIndicesToBeRemoved.size() > 0;
766     aRefListAttr->remove(anIndicesToBeRemoved);
767   }
768   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
769     std::set<AttributePtr> anAttributes;
770     QList<ModuleBase_ViewerPrsPtr>::const_iterator
771       anIt = theValues.begin(), aLast = theValues.end();
772     ObjectPtr anObject;
773     GeomShapePtr aShape;
774     for (; anIt != aLast; anIt++) {
775       ModuleBase_ViewerPrsPtr aPrs = *anIt;
776       getGeomSelection(aPrs, anObject, aShape);
777       AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
778       if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
779         anAttributes.insert(anAttr);
780     }
781
782     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
783     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
784       bool aFound = false;
785       if (aRefAttrListAttr->isAttribute(i)) {
786         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
787         aFound = anAttributes.find(anAttribute) != anAttributes.end();
788       }
789       else {
790         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
791       }
792       if (!aFound)
793         anIndicesToBeRemoved.insert(i);
794     }
795     isDone = anIndicesToBeRemoved.size() > 0;
796     aRefAttrListAttr->remove(anIndicesToBeRemoved);
797   }
798
799   return isDone;
800 }
801
802 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
803                                                      (QList<ModuleBase_ViewerPrsPtr>& theValues)
804 {
805   // convert prs list to objects map
806   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
807   std::set<GeomShapePtr> aShapes;
808   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
809   ObjectPtr anObject;
810   GeomShapePtr aShape;
811   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
812   for (; anIt != aLast; anIt++) {
813     ModuleBase_ViewerPrsPtr aPrs = *anIt;
814     getGeomSelection(aPrs, anObject, aShape);
815     aShapes.clear();
816     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
817       aShapes = aGeomSelection[anObject];
818     // we need to know if there was an empty shape in selection for the object
819     if (!aShape.get())
820       aShape = anEmptyShape;
821     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
822       aShapes.insert(aShape);
823     aGeomSelection[anObject] = aShapes;
824   }
825   return aGeomSelection;
826 }
827
828 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
829                               const GeomShapePtr& theShape,
830                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
831 {
832   bool aFound = false;
833   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
834   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
835   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
836     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
837     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
838     for (; anIt != aLast && !aFound; anIt++) {
839       GeomShapePtr aCShape = *anIt;
840       if (aCShape.get())
841         aFound = aCShape->isSame(aShape);
842     }
843   }
844   return aFound;
845 }