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