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