]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp
Salome HOME
Do not clear in neutral point correction.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetMultiSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_WidgetMultiSelector.cpp
5  *
6  *  Created on: Aug 28, 2014
7  *      Author: sbh
8  */
9
10 #include <ModuleBase_WidgetMultiSelector.h>
11 #include <ModuleBase_WidgetShapeSelector.h>
12 #include <ModuleBase_ISelection.h>
13 #include <ModuleBase_IWorkshop.h>
14 #include <ModuleBase_IViewer.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_Definitions.h>
17 #include <ModuleBase_IModule.h>
18 #include <ModuleBase_ViewerPrs.h>
19 #include <ModuleBase_IconFactory.h>
20 #include <ModuleBase_Events.h>
21
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Object.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeRefList.h>
26 #include <ModelAPI_AttributeRefAttrList.h>
27 #include <ModelAPI_Tools.h>
28 #include <ModelAPI_Events.h>
29
30 #include <Config_WidgetAPI.h>
31
32 #include <QGridLayout>
33 #include <QLabel>
34 #include <QListWidget>
35 #include <QObject>
36 #include <QString>
37 #include <QComboBox>
38 #include <QEvent>
39 #include <QAction>
40 #include <QApplication>
41 #include <QClipboard>
42 #include <QTimer>
43 #include <QMainWindow>
44
45 #include <memory>
46 #include <string>
47
48 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
49
50 /**
51 * Customization of a List Widget to make it to be placed on full width of container
52 */
53 class CustomListWidget : public QListWidget
54 {
55 public:
56   /// Constructor
57   /// \param theParent a parent widget
58   CustomListWidget( QWidget* theParent )
59     : QListWidget( theParent )
60   {
61   }
62
63   /// Redefinition of virtual method
64   virtual QSize sizeHint() const
65   {
66     int aHeight = 2*QFontMetrics( font() ).height();
67     QSize aSize = QListWidget::sizeHint();
68     return QSize( aSize.width(), aHeight );
69   }
70
71   /// Redefinition of virtual method
72   virtual QSize minimumSizeHint() const
73   {
74     int aHeight = 4/*2*/*QFontMetrics( font() ).height();
75     QSize aSize = QListWidget::minimumSizeHint();
76     return QSize( aSize.width(), aHeight );
77   }
78
79 #ifndef WIN32
80 // The code is necessary only for Linux because
81 //it can not update viewport on widget resize
82 protected:
83   void resizeEvent(QResizeEvent* theEvent)
84   {
85     QListWidget::resizeEvent(theEvent);
86     QTimer::singleShot(5, viewport(), SLOT(repaint()));
87   }
88 #endif
89 };
90
91 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
92                                                                ModuleBase_IWorkshop* theWorkshop,
93                                                                const Config_WidgetAPI* theData)
94 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
95 {
96   QGridLayout* aMainLay = new QGridLayout(this);
97   ModuleBase_Tools::adjustMargins(aMainLay);
98
99   QLabel* aTypeLabel = new QLabel(tr("Type"), this);
100   aMainLay->addWidget(aTypeLabel, 0, 0);
101
102   myTypeCombo = new QComboBox(this);
103   // There is no sense to parameterize list of types while we can not parameterize selection mode
104
105   std::string aPropertyTypes = theData->getProperty("type_choice");
106   QString aTypesStr = aPropertyTypes.c_str();
107   QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
108
109   myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
110
111   if (!aShapeTypes.empty())
112     myTypeCombo->addItems(aShapeTypes);
113   aMainLay->addWidget(myTypeCombo, 0, 1);
114   // if the xml definition contains one type, the controls to select a type should not be shown
115   if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
116     aTypeLabel->setVisible(false);
117     myTypeCombo->setVisible(false);
118   }
119
120   std::string aLabelText = theData->getProperty("label");
121   QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
122                                                       : tr("Selected objects:"), this);
123   aMainLay->addWidget(aListLabel, 1, 0);
124   // if the xml definition contains one type, an information label should be shown near to the latest
125   if (aShapeTypes.size() <= 1) {
126     QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
127     if (!aLabelIcon.isEmpty()) {
128       QLabel* aSelectedLabel = new QLabel("", this);
129       aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
130       aMainLay->addWidget(aSelectedLabel, 1, 1);
131     }
132     aMainLay->setColumnStretch(2, 1);
133   }
134
135   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
136   myListControl = new CustomListWidget(this);
137   QString anObjName = QString::fromStdString(attributeID());
138   myListControl->setObjectName(anObjName);
139   myListControl->setToolTip(aToolTip);
140   myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
141
142   aMainLay->addWidget(myListControl, 2, 0, 1, -1);
143   aMainLay->setRowStretch(2, 1);
144   //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
145   //aMainLay->setRowMinimumHeight(3, 20);
146   //this->setLayout(aMainLay);
147   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
148
149   myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
150                           myWorkshop->desktop(), this, SLOT(onCopyItem()));
151   myCopyAction->setShortcut(QKeySequence::Copy);
152   myCopyAction->setEnabled(false);
153   myListControl->addAction(myCopyAction);
154
155   myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
156                           myWorkshop->desktop(), this, SLOT(onDeleteItem()));
157   myDeleteAction->setEnabled(false);
158   myListControl->addAction(myDeleteAction);
159
160   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
161   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
162
163   myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
164 }
165
166 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
167 {
168 }
169
170 //********************************************************************
171 void ModuleBase_WidgetMultiSelector::activateCustom()
172 {
173   ModuleBase_WidgetSelector::activateCustom();
174
175   myWorkshop->module()->activateCustomPrs(myFeature,
176                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
177 }
178
179 //********************************************************************
180 void ModuleBase_WidgetMultiSelector::deactivate()
181 {
182   ModuleBase_WidgetSelector::deactivate();
183
184   myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
185 }
186
187 //********************************************************************
188 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
189 {
190   // the value is stored on the selection changed signal processing 
191   // A rare case when plugin was not loaded. 
192   if (!myFeature)
193     return false;
194
195   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
196   std::string aType = anAttribute->attributeType();
197   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
198     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
199     aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
200   }
201   return true;
202 }
203
204 //********************************************************************
205 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
206 {
207   // A rare case when plugin was not loaded. 
208   if (!myFeature)
209     return false;
210
211   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
212   std::string aType = anAttribute->attributeType();
213   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
214     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
215     // Restore shape type
216     std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
217     if (!aSelectionType.empty())
218       setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
219   }
220   updateSelectionList();
221   return true;
222 }
223
224 //********************************************************************
225 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
226                                                   const bool theToValidate)
227 {
228   AttributeSelectionListPtr aSelectionListAttr;
229   if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
230     aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
231   if (aSelectionListAttr.get())
232     aSelectionListAttr->cashValues(true);
233
234   /// remove unused objects from the model attribute.
235   /// It should be performed before new attributes append.
236   bool isDone = removeUnusedAttributeObjects(theValues);
237
238   QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
239   QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
240   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
241   for (; anIt != aLast; anIt++) {
242     ModuleBase_ViewerPrsPtr aValue = *anIt;
243     // do not validate and append to attribute selection presentation if it exists in the attribute
244     ObjectPtr anObject;
245     GeomShapePtr aShape;
246     getGeomSelection(aValue, anObject, aShape);
247     if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
248       anAttributeValues.append(aValue);
249       continue;
250     }
251     if (theToValidate && !isValidInFilters(aValue))
252       anInvalidValues.append(aValue);
253   }
254   bool aHasInvalidValues = anInvalidValues.size() > 0;
255
256   for (anIt = theValues.begin(); anIt != aLast; anIt++) {
257     ModuleBase_ViewerPrsPtr aValue = *anIt;
258     bool aProcessed = false;
259     if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
260         anAttributeValues.contains(aValue))
261       continue;
262     aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
263     // if there is at least one set, the result is true
264     isDone = isDone || aProcessed;
265   }
266   // updateObject - to update/redisplay feature
267   // it is commented in order to perfom it outside the method
268   //if (isDone) {
269     //updateObject(myFeature);
270     // this emit is necessary to call store/restore method an restore type of selection
271     //emit valuesChanged();
272   //}
273
274   if (aSelectionListAttr.get())
275     aSelectionListAttr->cashValues(false);
276
277   theValues.clear();
278   if (!anInvalidValues.empty())
279     theValues.append(anInvalidValues);
280
281   if (isDone) // may be the feature's result is not displayed, but attributes should be
282     myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
283                                           true);/// hope that something is redisplayed by object updated
284
285   return isDone;
286 }
287
288 //********************************************************************
289 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
290 {
291   std::set<int> anAttributeIds;
292   getSelectedAttributeIndices(anAttributeIds);
293   if (!anAttributeIds.empty())
294     convertIndicesToViewerSelection(anAttributeIds, theValues);
295 }
296
297 //********************************************************************
298 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
299 {
300   bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
301   if (aValid) {
302     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
303     aValid = aResult.get() != NULL;
304     if (aValid) {
305       if (myFeature) {
306         // We can not select a result of our feature
307         std::list<ResultPtr> aResults;
308         ModelAPI_Tools::allResults(myFeature, aResults);
309         std::list<ResultPtr>::const_iterator aIt;
310         bool isSkipSelf = false;
311         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
312           if ((*aIt) == aResult) {
313             isSkipSelf = true;
314             break;
315           }
316         }
317         if (isSkipSelf)
318           aValid = false;
319       }
320     }
321   }
322   return aValid;
323 }
324
325 //********************************************************************
326 bool ModuleBase_WidgetMultiSelector::processDelete()
327 {
328   // find attribute indices to delete
329   std::set<int> anAttributeIds;
330   getSelectedAttributeIndices(anAttributeIds);
331
332   QModelIndexList aIndexes = myListControl->selectionModel()->selectedIndexes();
333
334   // refill attribute by the items which indices are not in the list of ids
335   bool aDone = false;
336   DataPtr aData = myFeature->data();
337   AttributePtr anAttribute = aData->attribute(attributeID());
338   std::string aType = anAttribute->attributeType();
339   aDone = !anAttributeIds.empty();
340   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
341     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
342     aSelectionListAttr->remove(anAttributeIds);
343
344   }
345   else if (aType == ModelAPI_AttributeRefList::typeId()) {
346     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
347     aRefListAttr->remove(anAttributeIds);
348   }
349   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
350     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
351     aRefAttrListAttr->remove(anAttributeIds);
352   }
353
354   if (aDone) {
355     // update object is necessary to flush update signal. It leads to objects references map update
356     // and the operation presentation will not contain deleted items visualized as parameters of
357     // the feature.
358     updateObject(myFeature);
359
360     restoreValue();
361     myWorkshop->setSelected(getAttributeSelection());
362
363     // may be the feature's result is not displayed, but attributes should be
364     myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
365                                           true); /// hope that something is redisplayed by object updated
366   }
367
368   // Restore selection
369   int aRows = myListControl->model()->rowCount();
370   if (aRows > 0) {
371     foreach(QModelIndex aIndex, aIndexes) {
372       if (aIndex.row() < aRows)
373         myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
374       else {
375         QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
376         myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
377       }
378     }
379   }
380   return aDone;
381 }
382
383 //********************************************************************
384 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
385 {
386   QList<QWidget*> result;
387   //result << myTypeCombo;
388   result << myListControl;
389   return result;
390 }
391
392 //********************************************************************
393 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
394 {
395   activateSelectionAndFilters(true);
396   QList<ModuleBase_ViewerPrsPtr> anEmptyList;
397   // This method will call Selection changed event which will call onSelectionChanged
398   // To clear mySelection, myListControl and storeValue()
399   // So, we don't need to call it
400   myWorkshop->setSelected(anEmptyList);
401 }
402
403 //********************************************************************
404 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
405 {
406   if (!myIsNeutralPointClear) {
407     QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
408     // do not clear selected object
409     if (aSelected.size() == 0) {
410       if (!getAttributeSelection().empty()) {
411         // Restore selection in the viewer by the attribute selection list
412         // it should be postponed to exit from the selectionChanged processing
413         static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
414         ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
415         Events_Loop::loop()->flush(anEvent);
416         return;
417       }
418     }
419   }
420   ModuleBase_WidgetSelector::onSelectionChanged();
421 }
422
423 void ModuleBase_WidgetMultiSelector::updateFocus()
424 {
425   // Set focus to List control in order to make possible 
426   // to use Tab key for transfer the focus to next widgets
427   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
428   ModuleBase_Tools::setFocus(myListControl,
429                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
430 }
431
432 //********************************************************************
433 void ModuleBase_WidgetMultiSelector::updateSelectionName()
434 {
435 }
436
437 //********************************************************************
438 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
439 {
440   QIntList aShapeTypes;
441
442   if (myTypeCombo->count() > 1 && myIsUseChoice) {
443     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
444   }
445   else {
446     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
447       aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
448   }
449   return aShapeTypes;
450 }
451
452 //********************************************************************
453 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
454 {
455   QString aShapeTypeName;
456   
457   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
458     aShapeTypeName = myTypeCombo->itemText(idx);
459     int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
460     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
461       bool aWasActivated = activateSelectionAndFilters(false);
462       bool isBlocked = myTypeCombo->blockSignals(true);
463       myTypeCombo->setCurrentIndex(idx);
464       myTypeCombo->blockSignals(isBlocked);
465       if (aWasActivated)
466         activateSelectionAndFilters(true);
467       break;
468     }
469   }
470 }
471
472 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
473 {
474   QList<ModuleBase_ViewerPrsPtr> aSelected;
475   convertIndicesToViewerSelection(std::set<int>(), aSelected);
476   return aSelected;
477 }
478
479 //********************************************************************
480 void ModuleBase_WidgetMultiSelector::updateSelectionList()
481 {
482   myListControl->clear();
483
484   DataPtr aData = myFeature->data();
485   AttributePtr anAttribute = aData->attribute(attributeID());
486   std::string aType = anAttribute->attributeType();
487   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
488     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
489     for (int i = 0; i < aSelectionListAttr->size(); i++) {
490       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
491       QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
492       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
493       myListControl->addItem(anItem);
494     }
495   }
496   else if (aType == ModelAPI_AttributeRefList::typeId()) {
497     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
498     for (int i = 0; i < aRefListAttr->size(); i++) {
499       ObjectPtr anObject = aRefListAttr->object(i);
500       if (anObject.get()) {
501         QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
502                                                       myListControl);
503         anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
504         myListControl->addItem(anItem);
505       }
506     }
507   }
508   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
509     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
510     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
511       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
512       QString aName;
513       if (anAttribute.get()) {
514         std::string anAttrName = generateName(anAttribute, myWorkshop);
515         aName = QString::fromStdString(anAttrName);
516       }
517       else {
518         ObjectPtr anObject = aRefAttrListAttr->object(i);
519         if (anObject.get()) {
520           aName = anObject->data()->name().c_str();
521         }
522       }
523       QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
524       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
525       myListControl->addItem(anItem);
526     }
527   }
528
529   // We have to call repaint because sometimes the List control is not updated
530   myListControl->repaint();
531 }
532
533 //********************************************************************
534 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
535 {
536   std::string aType;
537
538   if (theType == "Vertices")
539     aType = "vertex";
540   else if (theType == "Edges")
541     aType = "edge";
542   else if (theType == "Faces")
543     aType = "face";
544   else if (theType == "Solids")
545     aType = "solid";
546
547   return aType;
548 }
549
550 //********************************************************************
551 void ModuleBase_WidgetMultiSelector::onCopyItem()
552 {
553   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
554   QString aRes;
555   foreach(QListWidgetItem* aItem, aItems) {
556     if (!aRes.isEmpty())
557       aRes += "\n";
558     aRes += aItem->text();
559   }
560   if (!aRes.isEmpty()) {
561     QClipboard *clipboard = QApplication::clipboard();
562     clipboard->setText(aRes);
563   }
564 }
565
566 //********************************************************************
567 void ModuleBase_WidgetMultiSelector::onDeleteItem()
568 {
569   processDelete();
570 }
571
572 //********************************************************************
573 void ModuleBase_WidgetMultiSelector::onListSelection()
574 {
575   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
576   myCopyAction->setEnabled(!aItems.isEmpty());
577   myDeleteAction->setEnabled(!aItems.isEmpty());
578   
579   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
580                                         true);
581 }
582
583 //********************************************************************
584 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
585 {
586   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
587   foreach(QListWidgetItem* anItem, aItems) {
588     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
589     if (theAttributeIds.find(anIndex) == theAttributeIds.end())
590       theAttributeIds.insert(anIndex);
591   }
592 }
593
594 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
595                                                       QList<ModuleBase_ViewerPrsPtr>& theValues) const
596 {
597   if(myFeature.get() == NULL)
598     return;
599
600   DataPtr aData = myFeature->data();
601   AttributePtr anAttribute = aData->attribute(attributeID());
602   std::string aType = anAttribute->attributeType();
603   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
604     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
605     for (int i = 0; i < aSelectionListAttr->size(); i++) {
606       // filter by attribute indices only if the container is not empty otherwise return all items
607       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
608         continue;
609       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
610       ResultPtr anObject = anAttr->context();
611       if (anObject.get())
612         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
613                new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
614     }
615   }
616   else if (aType == ModelAPI_AttributeRefList::typeId()) {
617     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
618     for (int i = 0; i < aRefListAttr->size(); i++) {
619       // filter by attribute indices only if the container is not empty otherwise return all items
620       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
621         continue;
622       ObjectPtr anObject = aRefListAttr->object(i);
623       if (anObject.get()) {
624         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
625                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
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       // filter by attribute indices only if the container is not empty otherwise return all items
633       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
634         continue;
635       ObjectPtr anObject = aRefAttrListAttr->object(i);
636       if (!anObject.get())
637         continue;
638       TopoDS_Shape aShape;
639       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
640       if (anAttribute.get()) {
641         GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
642         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
643                new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
644       }
645     }
646   }
647 }
648
649 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
650                                                  (QList<ModuleBase_ViewerPrsPtr>& theValues)
651 {
652   bool isDone = false;
653
654   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
655   DataPtr aData = myFeature->data();
656   AttributePtr anAttribute = aData->attribute(attributeID());
657   std::string aType = anAttribute->attributeType();
658   std::set<GeomShapePtr> aShapes;
659   std::set<int> anIndicesToBeRemoved;
660   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
661     // iteration through data model to find not selected elements to remove them
662     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
663     for (int i = 0; i < aSelectionListAttr->size(); i++) {
664       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
665       bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
666       if (!aFound)
667         anIndicesToBeRemoved.insert(i);
668     }
669     isDone = anIndicesToBeRemoved.size() > 0;
670     aSelectionListAttr->remove(anIndicesToBeRemoved);
671   }
672   else if (aType == ModelAPI_AttributeRefList::typeId()) {
673     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
674     for (int i = 0; i < aRefListAttr->size(); i++) {
675       ObjectPtr anObject = aRefListAttr->object(i);
676       if (anObject.get()) {
677         bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
678         if (!aFound)
679           anIndicesToBeRemoved.insert(i);
680       }
681     }
682     isDone = anIndicesToBeRemoved.size() > 0;
683     aRefListAttr->remove(anIndicesToBeRemoved);
684   }
685   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
686     std::set<AttributePtr> anAttributes;
687     QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
688     ObjectPtr anObject;
689     GeomShapePtr aShape;
690     for (; anIt != aLast; anIt++) {
691       ModuleBase_ViewerPrsPtr aPrs = *anIt;
692       getGeomSelection(aPrs, anObject, aShape);
693       AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
694       if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
695         anAttributes.insert(anAttr);
696     }
697
698     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
699     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
700       bool aFound = false;
701       if (aRefAttrListAttr->isAttribute(i)) {
702         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
703         aFound = anAttributes.find(anAttribute) != anAttributes.end();
704       }
705       else {
706         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
707       }
708       if (!aFound)
709         anIndicesToBeRemoved.insert(i);
710     }
711     isDone = anIndicesToBeRemoved.size() > 0;
712     aRefAttrListAttr->remove(anIndicesToBeRemoved);
713   }
714
715   return isDone;
716 }
717
718 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
719                                                      (QList<ModuleBase_ViewerPrsPtr>& theValues)
720 {
721   // convert prs list to objects map
722   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
723   std::set<GeomShapePtr> aShapes;
724   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
725   ObjectPtr anObject;
726   GeomShapePtr aShape;
727   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
728   for (; anIt != aLast; anIt++) {
729     ModuleBase_ViewerPrsPtr aPrs = *anIt;
730     getGeomSelection(aPrs, anObject, aShape);
731     aShapes.clear();
732     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
733       aShapes = aGeomSelection[anObject];
734     // we need to know if there was an empty shape in selection for the object
735     if (!aShape.get())
736       aShape = anEmptyShape;
737     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
738       aShapes.insert(aShape);
739     aGeomSelection[anObject] = aShapes;
740   }
741   return aGeomSelection;
742 }
743
744 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
745                               const GeomShapePtr& theShape,
746                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
747 {
748   bool aFound = false;
749   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
750   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
751   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
752     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
753     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
754     for (; anIt != aLast && !aFound; anIt++) {
755       GeomShapePtr aCShape = *anIt;
756       if (aCShape.get())
757         aFound = aCShape->isEqual(aShape);
758     }
759   }
760   return aFound;
761 }