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