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