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