]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
Issue #2340: Provide list of accessible Undo/Redo commands for widgets
[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   int aNb = mySelectedHistoryValues.count();
509   myWorkshop->updateCommandStatus();
510
511 #ifdef DEBUG_UNDO_REDO
512   printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
513 #endif
514 }
515
516 void ModuleBase_WidgetMultiSelector::clearSelectedHistory()
517 {
518   mySelectedHistoryValues.clear();
519   myCurrentHistoryIndex = -1;
520   myWorkshop->updateCommandStatus();
521
522 #ifdef DEBUG_UNDO_REDO
523   printHistoryInfo("clearSelectedHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
524 #endif
525 }
526
527 void ModuleBase_WidgetMultiSelector::updateFocus()
528 {
529   // Set focus to List control in order to make possible
530   // to use Tab key for transfer the focus to next widgets
531   ModuleBase_Tools::setFocus(myListView->getControl(),
532                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
533 }
534
535 //********************************************************************
536 void ModuleBase_WidgetMultiSelector::updateSelectionName()
537 {
538 }
539
540 //********************************************************************
541 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
542 {
543   if (myIsSetSelectionBlocked)
544     return;
545   ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
546
547   // according to #2154 we need to update OB selection when selection in the viewer happens
548   // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
549   // because we need that Object Browser has been already updated when synchronize happens.
550
551   // Restore selection in the viewer by the attribute selection list
552   // it is possible that diring selection attribute filling, selection in Object Browser
553   // is changed(some items were removed/added) and as result, selection in the viewer
554   // differs from the selection come to this method. By next rows, we restore selection
555   // in the viewer according to content of selection attribute. Case is Edge selection in Group
556   myIsSetSelectionBlocked = true;
557   static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
558   ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
559   Events_Loop::loop()->flush(anEvent);
560   myIsSetSelectionBlocked = false;
561 }
562
563 //********************************************************************
564 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
565 {
566   QIntList aShapeTypes;
567
568   if (myTypeCombo->count() > 1 && myIsUseChoice) {
569     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
570   }
571   else {
572     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
573       aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
574   }
575   return aShapeTypes;
576 }
577
578 //********************************************************************
579 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
580 {
581   QString aShapeTypeName;
582
583   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
584     aShapeTypeName = myTypeCombo->itemText(idx);
585     int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
586     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
587       bool aWasActivated = activateSelectionAndFilters(false);
588       bool isBlocked = myTypeCombo->blockSignals(true);
589       myTypeCombo->setCurrentIndex(idx);
590       myTypeCombo->blockSignals(isBlocked);
591       if (aWasActivated)
592         activateSelectionAndFilters(true);
593       break;
594     }
595   }
596 }
597
598 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
599 {
600   QList<ModuleBase_ViewerPrsPtr> aSelected;
601   convertIndicesToViewerSelection(std::set<int>(), aSelected);
602   return aSelected;
603 }
604
605 //********************************************************************
606 void ModuleBase_WidgetMultiSelector::updateSelectionList()
607 {
608   myListView->getControl()->clear();
609
610   DataPtr aData = myFeature->data();
611   AttributePtr anAttribute = aData->attribute(attributeID());
612   std::string aType = anAttribute->attributeType();
613   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
614     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
615     for (int i = 0; i < aSelectionListAttr->size(); i++) {
616       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
617       myListView->addItem(aAttr->namingName().c_str(), i);
618     }
619   }
620   else if (aType == ModelAPI_AttributeRefList::typeId()) {
621     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
622     for (int i = 0; i < aRefListAttr->size(); i++) {
623       ObjectPtr anObject = aRefListAttr->object(i);
624       if (anObject.get()) {
625         myListView->addItem(anObject->data()->name().c_str(), i);
626       }
627     }
628   }
629   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
630     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
631     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
632       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
633       QString aName;
634       if (anAttribute.get()) {
635         std::string anAttrName = generateName(anAttribute, myWorkshop);
636         aName = QString::fromStdString(anAttrName);
637       }
638       else {
639         ObjectPtr anObject = aRefAttrListAttr->object(i);
640         if (anObject.get()) {
641           aName = anObject->data()->name().c_str();
642         }
643       }
644       myListView->addItem(aName, i);
645     }
646   }
647
648   // We have to call repaint because sometimes the List control is not updated
649   myListView->getControl()->repaint();
650 }
651
652 //********************************************************************
653 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
654 {
655   std::string aType;
656
657   if (theType == "Vertices")
658     aType = "vertex";
659   else if (theType == "Edges")
660     aType = "edge";
661   else if (theType == "Faces")
662     aType = "face";
663   else if (theType == "Solids")
664     aType = "solid";
665
666   return aType;
667 }
668
669 //********************************************************************
670 void ModuleBase_WidgetMultiSelector::clearSelection()
671 {
672   bool isClearInNeutralPoint = myIsNeutralPointClear;
673   myIsNeutralPointClear = true;
674
675   QList<ModuleBase_ViewerPrsPtr> anEmptyList;
676   // This method will call Selection changed event which will call onSelectionChanged
677   // To clear mySelection, myListView and storeValue()
678   // So, we don't need to call it
679   myWorkshop->setSelected(anEmptyList);
680
681   myIsNeutralPointClear = isClearInNeutralPoint;
682 }
683
684 //********************************************************************
685 void ModuleBase_WidgetMultiSelector::onDeleteItem()
686 {
687   processDelete();
688 }
689
690 //********************************************************************
691 void ModuleBase_WidgetMultiSelector::onListSelection()
692 {
693   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
694                                         true);
695 }
696
697 //********************************************************************
698 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
699 {
700   myListView->getSelectedIndices(theAttributeIds);
701 }
702
703 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
704                                                    QList<ModuleBase_ViewerPrsPtr>& theValues) const
705 {
706   if(myFeature.get() == NULL)
707     return;
708
709   DataPtr aData = myFeature->data();
710   AttributePtr anAttribute = aData->attribute(attributeID());
711   std::string aType = anAttribute->attributeType();
712   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
713     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
714     for (int i = 0; i < aSelectionListAttr->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       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
719       ResultPtr anObject = anAttr->context();
720       if (anObject.get())
721         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
722                new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
723     }
724   }
725   else if (aType == ModelAPI_AttributeRefList::typeId()) {
726     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
727     for (int i = 0; i < aRefListAttr->size(); i++) {
728       // filter by attribute indices only if the container is not empty otherwise return all items
729       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
730         continue;
731       ObjectPtr anObject = aRefListAttr->object(i);
732       if (anObject.get()) {
733         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
734                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
735       }
736     }
737   }
738   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
739     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
740     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
741       // filter by attribute indices only if the container is not empty otherwise return all items
742       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
743         continue;
744       ObjectPtr anObject = aRefAttrListAttr->object(i);
745       if (!anObject.get())
746         continue;
747       TopoDS_Shape aShape;
748       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
749       if (anAttribute.get()) {
750         GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
751         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
752                new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
753       }
754     }
755   }
756 }
757
758 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
759                                                  (QList<ModuleBase_ViewerPrsPtr>& theValues)
760 {
761   bool isDone = false;
762
763   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
764   DataPtr aData = myFeature->data();
765   AttributePtr anAttribute = aData->attribute(attributeID());
766   std::string aType = anAttribute->attributeType();
767   std::set<GeomShapePtr> aShapes;
768   std::set<int> anIndicesToBeRemoved;
769   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
770     // iteration through data model to find not selected elements to remove them
771     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
772     for (int i = 0; i < aSelectionListAttr->size(); i++) {
773       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
774       bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection,
775                                     myWorkshop);
776       if (!aFound)
777         anIndicesToBeRemoved.insert(i);
778     }
779     isDone = anIndicesToBeRemoved.size() > 0;
780     aSelectionListAttr->remove(anIndicesToBeRemoved);
781   }
782   else if (aType == ModelAPI_AttributeRefList::typeId()) {
783     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
784     for (int i = 0; i < aRefListAttr->size(); i++) {
785       ObjectPtr anObject = aRefListAttr->object(i);
786       if (anObject.get()) {
787         bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection,
788                                       myWorkshop);
789         if (!aFound)
790           anIndicesToBeRemoved.insert(i);
791       }
792     }
793     isDone = anIndicesToBeRemoved.size() > 0;
794     aRefListAttr->remove(anIndicesToBeRemoved);
795   }
796   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
797     std::set<AttributePtr> anAttributes;
798     QList<ModuleBase_ViewerPrsPtr>::const_iterator
799       anIt = theValues.begin(), aLast = theValues.end();
800     ObjectPtr anObject;
801     GeomShapePtr aShape;
802     for (; anIt != aLast; anIt++) {
803       ModuleBase_ViewerPrsPtr aPrs = *anIt;
804       getGeomSelection(aPrs, anObject, aShape);
805       AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
806       if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
807         anAttributes.insert(anAttr);
808     }
809
810     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
811     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
812       bool aFound = false;
813       if (aRefAttrListAttr->isAttribute(i)) {
814         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
815         aFound = anAttributes.find(anAttribute) != anAttributes.end();
816       }
817       else {
818         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection,
819                                  myWorkshop);
820       }
821       if (!aFound)
822         anIndicesToBeRemoved.insert(i);
823     }
824     isDone = anIndicesToBeRemoved.size() > 0;
825     aRefAttrListAttr->remove(anIndicesToBeRemoved);
826   }
827
828   return isDone;
829 }
830
831 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
832                                                      (QList<ModuleBase_ViewerPrsPtr>& theValues)
833 {
834   // convert prs list to objects map
835   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
836   std::set<GeomShapePtr> aShapes;
837   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
838   ObjectPtr anObject;
839   GeomShapePtr aShape;
840   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
841   for (; anIt != aLast; anIt++) {
842     ModuleBase_ViewerPrsPtr aPrs = *anIt;
843     getGeomSelection(aPrs, anObject, aShape);
844     aShapes.clear();
845     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
846       aShapes = aGeomSelection[anObject];
847     // we need to know if there was an empty shape in selection for the object
848     if (!aShape.get())
849       aShape = anEmptyShape;
850     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
851       aShapes.insert(aShape);
852     aGeomSelection[anObject] = aShapes;
853   }
854   return aGeomSelection;
855 }
856
857 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
858                               GeomShapePtr theShape,
859                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection,
860                               ModuleBase_IWorkshop* theWorkshop)
861 {
862   // issue #2154: we should not remove from list objects hidden in the viewer if selection
863   // was done with SHIFT button
864   if (theWorkshop->hasSHIFTPressed() && !theObject->isDisplayed())
865     return true;
866
867   bool aFound = false;
868   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
869   if (theShape.get()) { // treat shape equal to context as null: 2219, keep order of shapes in list
870     const ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
871     if (aContext.get() && aContext->shape()->isEqual(theShape))
872       theShape.reset();
873   }
874   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
875   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
876     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
877     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
878     for (; anIt != aLast && !aFound; anIt++) {
879       GeomShapePtr aCShape = *anIt;
880       if (aCShape.get())
881         aFound = aCShape->isSame(aShape);
882     }
883   }
884   return aFound;
885 }
886
887 QList<ActionInfo> 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 }