Salome HOME
2e8e7a6cb36e6eda6433a2413fdb0210e70e5a63
[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
125   // should be shown near to the latest
126   if (aShapeTypes.size() <= 1) {
127     QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
128     if (!aLabelIcon.isEmpty()) {
129       QLabel* aSelectedLabel = new QLabel("", this);
130       aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
131       aMainLay->addWidget(aSelectedLabel, 1, 1);
132     }
133     aMainLay->setColumnStretch(2, 1);
134   }
135
136   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
137   myListControl = new CustomListWidget(this);
138   QString anObjName = QString::fromStdString(attributeID());
139   myListControl->setObjectName(anObjName);
140   myListControl->setToolTip(aToolTip);
141   myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
142
143   aMainLay->addWidget(myListControl, 2, 0, 1, -1);
144   aMainLay->setRowStretch(2, 1);
145   //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
146   //aMainLay->setRowMinimumHeight(3, 20);
147   //this->setLayout(aMainLay);
148   connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
149
150   myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
151                           myWorkshop->desktop(), this, SLOT(onCopyItem()));
152   myCopyAction->setShortcut(QKeySequence::Copy);
153   myCopyAction->setEnabled(false);
154   myListControl->addAction(myCopyAction);
155
156   myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
157                           myWorkshop->desktop(), this, SLOT(onDeleteItem()));
158   myDeleteAction->setEnabled(false);
159   myListControl->addAction(myDeleteAction);
160
161   myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
162   connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
163
164   myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
165 }
166
167 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
168 {
169 }
170
171 //********************************************************************
172 void ModuleBase_WidgetMultiSelector::activateCustom()
173 {
174   ModuleBase_WidgetSelector::activateCustom();
175
176   myWorkshop->module()->activateCustomPrs(myFeature,
177                             ModuleBase_IModule::CustomizeHighlightedObjects, true);
178 }
179
180 //********************************************************************
181 void ModuleBase_WidgetMultiSelector::deactivate()
182 {
183   ModuleBase_WidgetSelector::deactivate();
184
185   myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
186 }
187
188 //********************************************************************
189 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
190 {
191   // the value is stored on the selection changed signal processing
192   // A rare case when plugin was not loaded.
193   if (!myFeature)
194     return false;
195
196   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
197   std::string aType = anAttribute->attributeType();
198   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
199     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
200     aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
201   }
202   return true;
203 }
204
205 //********************************************************************
206 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
207 {
208   // A rare case when plugin was not loaded.
209   if (!myFeature)
210     return false;
211
212   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
213   std::string aType = anAttribute->attributeType();
214   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
215     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
216     // Restore shape type
217     std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
218     if (!aSelectionType.empty())
219       setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
220   }
221   updateSelectionList();
222   return true;
223 }
224
225 //********************************************************************
226 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
227                                                   const bool theToValidate)
228 {
229   AttributeSelectionListPtr aSelectionListAttr;
230   if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
231     aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
232   if (aSelectionListAttr.get())
233     aSelectionListAttr->cashValues(true);
234
235   /// remove unused objects from the model attribute.
236   /// It should be performed before new attributes append.
237   bool isDone = removeUnusedAttributeObjects(theValues);
238
239   QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
240   QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
241   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
242   for (; anIt != aLast; anIt++) {
243     ModuleBase_ViewerPrsPtr aValue = *anIt;
244     // do not validate and append to attribute selection presentation if it exists in the attribute
245     ObjectPtr anObject;
246     GeomShapePtr aShape;
247     getGeomSelection(aValue, anObject, aShape);
248     if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
249       anAttributeValues.append(aValue);
250       continue;
251     }
252     if (theToValidate && !isValidInFilters(aValue))
253       anInvalidValues.append(aValue);
254   }
255   bool aHasInvalidValues = anInvalidValues.size() > 0;
256
257   for (anIt = theValues.begin(); anIt != aLast; anIt++) {
258     ModuleBase_ViewerPrsPtr aValue = *anIt;
259     bool aProcessed = false;
260     if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
261         anAttributeValues.contains(aValue))
262       continue;
263     aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
264     // if there is at least one set, the result is true
265     isDone = isDone || aProcessed;
266   }
267   // updateObject - to update/redisplay feature
268   // it is commented in order to perfom it outside the method
269   //if (isDone) {
270     //updateObject(myFeature);
271     // this emit is necessary to call store/restore method an restore type of selection
272     //emit valuesChanged();
273   //}
274
275   if (aSelectionListAttr.get())
276     aSelectionListAttr->cashValues(false);
277
278   theValues.clear();
279   if (!anInvalidValues.empty())
280     theValues.append(anInvalidValues);
281
282   if (isDone) // may be the feature's result is not displayed, but attributes should be
283     myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
284                              true);/// hope that something is redisplayed by object updated
285
286   return isDone;
287 }
288
289 //********************************************************************
290 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
291 {
292   std::set<int> anAttributeIds;
293   getSelectedAttributeIndices(anAttributeIds);
294   if (!anAttributeIds.empty())
295     convertIndicesToViewerSelection(anAttributeIds, theValues);
296 }
297
298 //********************************************************************
299 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
300 {
301   bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
302   if (aValid) {
303     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
304     aValid = aResult.get() != NULL;
305     if (aValid) {
306       if (myFeature) {
307         // We can not select a result of our feature
308         std::list<ResultPtr> aResults;
309         ModelAPI_Tools::allResults(myFeature, aResults);
310         std::list<ResultPtr>::const_iterator aIt;
311         bool isSkipSelf = false;
312         for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
313           if ((*aIt) == aResult) {
314             isSkipSelf = true;
315             break;
316           }
317         }
318         if (isSkipSelf)
319           aValid = false;
320       }
321     }
322   }
323   return aValid;
324 }
325
326 //********************************************************************
327 bool ModuleBase_WidgetMultiSelector::processDelete()
328 {
329   // find attribute indices to delete
330   std::set<int> anAttributeIds;
331   getSelectedAttributeIndices(anAttributeIds);
332
333   QModelIndexList aIndexes = myListControl->selectionModel()->selectedIndexes();
334
335   // refill attribute by the items which indices are not in the list of ids
336   bool aDone = false;
337   DataPtr aData = myFeature->data();
338   AttributePtr anAttribute = aData->attribute(attributeID());
339   std::string aType = anAttribute->attributeType();
340   aDone = !anAttributeIds.empty();
341   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
342     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
343     aSelectionListAttr->remove(anAttributeIds);
344
345   }
346   else if (aType == ModelAPI_AttributeRefList::typeId()) {
347     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
348     aRefListAttr->remove(anAttributeIds);
349   }
350   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
351     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
352     aRefAttrListAttr->remove(anAttributeIds);
353   }
354
355   if (aDone) {
356     // update object is necessary to flush update signal. It leads to objects references map update
357     // and the operation presentation will not contain deleted items visualized as parameters of
358     // the feature.
359     updateObject(myFeature);
360
361     restoreValue();
362     myWorkshop->setSelected(getAttributeSelection());
363
364     // may be the feature's result is not displayed, but attributes should be
365     myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
366                               true); /// hope that something is redisplayed by object updated
367   }
368
369   // Restore selection
370   int aRows = myListControl->model()->rowCount();
371   if (aRows > 0) {
372     foreach(QModelIndex aIndex, aIndexes) {
373       if (aIndex.row() < aRows)
374         myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
375       else {
376         QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
377         myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
378       }
379     }
380   }
381   return aDone;
382 }
383
384 //********************************************************************
385 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
386 {
387   QList<QWidget*> result;
388   //result << myTypeCombo;
389   result << myListControl;
390   return result;
391 }
392
393 //********************************************************************
394 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
395 {
396   activateSelectionAndFilters(true);
397
398   if (!myFeature)
399     return;
400   /// store the selected type
401   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
402   std::string aType = anAttribute->attributeType();
403   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
404     AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
405     aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
406   }
407
408   // clear attribute values
409   DataPtr aData = myFeature->data();
410   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
411     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
412     aSelectionListAttr->clear();
413   }
414   else if (aType == ModelAPI_AttributeRefList::typeId()) {
415     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
416     aRefListAttr->clear();
417   }
418   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
419     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
420     aRefAttrListAttr->clear();
421   }
422
423   // update object is necessary to flush update signal. It leads to objects references map update
424   // and the operation presentation will not contain deleted items visualized as parameters of
425   // the feature.
426   updateObject(myFeature);
427   restoreValue();
428   myWorkshop->setSelected(getAttributeSelection());
429   // may be the feature's result is not displayed, but attributes should be
430   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
431                             true); /// hope that something is redisplayed by object updated
432 }
433
434 //********************************************************************
435 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
436 {
437   if (!myIsNeutralPointClear) {
438     QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
439     // do not clear selected object
440     if (aSelected.size() == 0) {
441       if (!getAttributeSelection().empty()) {
442         // Restore selection in the viewer by the attribute selection list
443         // it should be postponed to exit from the selectionChanged processing
444         static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
445         ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
446         Events_Loop::loop()->flush(anEvent);
447         return;
448       }
449     }
450   }
451   ModuleBase_WidgetSelector::onSelectionChanged();
452 }
453
454 void ModuleBase_WidgetMultiSelector::updateFocus()
455 {
456   // Set focus to List control in order to make possible
457   // to use Tab key for transfer the focus to next widgets
458   myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
459   ModuleBase_Tools::setFocus(myListControl,
460                              "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
461 }
462
463 //********************************************************************
464 void ModuleBase_WidgetMultiSelector::updateSelectionName()
465 {
466 }
467
468 //********************************************************************
469 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
470 {
471   QIntList aShapeTypes;
472
473   if (myTypeCombo->count() > 1 && myIsUseChoice) {
474     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
475   }
476   else {
477     for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
478       aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
479   }
480   return aShapeTypes;
481 }
482
483 //********************************************************************
484 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
485 {
486   QString aShapeTypeName;
487
488   for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
489     aShapeTypeName = myTypeCombo->itemText(idx);
490     int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
491     if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
492       bool aWasActivated = activateSelectionAndFilters(false);
493       bool isBlocked = myTypeCombo->blockSignals(true);
494       myTypeCombo->setCurrentIndex(idx);
495       myTypeCombo->blockSignals(isBlocked);
496       if (aWasActivated)
497         activateSelectionAndFilters(true);
498       break;
499     }
500   }
501 }
502
503 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
504 {
505   QList<ModuleBase_ViewerPrsPtr> aSelected;
506   convertIndicesToViewerSelection(std::set<int>(), aSelected);
507   return aSelected;
508 }
509
510 //********************************************************************
511 void ModuleBase_WidgetMultiSelector::updateSelectionList()
512 {
513   myListControl->clear();
514
515   DataPtr aData = myFeature->data();
516   AttributePtr anAttribute = aData->attribute(attributeID());
517   std::string aType = anAttribute->attributeType();
518   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
519     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
520     for (int i = 0; i < aSelectionListAttr->size(); i++) {
521       AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
522       QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
523       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
524       myListControl->addItem(anItem);
525     }
526   }
527   else if (aType == ModelAPI_AttributeRefList::typeId()) {
528     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
529     for (int i = 0; i < aRefListAttr->size(); i++) {
530       ObjectPtr anObject = aRefListAttr->object(i);
531       if (anObject.get()) {
532         QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
533                                                       myListControl);
534         anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
535         myListControl->addItem(anItem);
536       }
537     }
538   }
539   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
540     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
541     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
542       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
543       QString aName;
544       if (anAttribute.get()) {
545         std::string anAttrName = generateName(anAttribute, myWorkshop);
546         aName = QString::fromStdString(anAttrName);
547       }
548       else {
549         ObjectPtr anObject = aRefAttrListAttr->object(i);
550         if (anObject.get()) {
551           aName = anObject->data()->name().c_str();
552         }
553       }
554       QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
555       anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
556       myListControl->addItem(anItem);
557     }
558   }
559
560   // We have to call repaint because sometimes the List control is not updated
561   myListControl->repaint();
562 }
563
564 //********************************************************************
565 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
566 {
567   std::string aType;
568
569   if (theType == "Vertices")
570     aType = "vertex";
571   else if (theType == "Edges")
572     aType = "edge";
573   else if (theType == "Faces")
574     aType = "face";
575   else if (theType == "Solids")
576     aType = "solid";
577
578   return aType;
579 }
580
581 //********************************************************************
582 void ModuleBase_WidgetMultiSelector::clearSelection()
583 {
584   bool isClearInNeutralPoint = myIsNeutralPointClear;
585   myIsNeutralPointClear = true;
586
587   QList<ModuleBase_ViewerPrsPtr> anEmptyList;
588   // This method will call Selection changed event which will call onSelectionChanged
589   // To clear mySelection, myListControl and storeValue()
590   // So, we don't need to call it
591   myWorkshop->setSelected(anEmptyList);
592
593   myIsNeutralPointClear = isClearInNeutralPoint;
594 }
595
596 //********************************************************************
597 void ModuleBase_WidgetMultiSelector::onCopyItem()
598 {
599   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
600   QString aRes;
601   foreach(QListWidgetItem* aItem, aItems) {
602     if (!aRes.isEmpty())
603       aRes += "\n";
604     aRes += aItem->text();
605   }
606   if (!aRes.isEmpty()) {
607     QClipboard *clipboard = QApplication::clipboard();
608     clipboard->setText(aRes);
609   }
610 }
611
612 //********************************************************************
613 void ModuleBase_WidgetMultiSelector::onDeleteItem()
614 {
615   processDelete();
616 }
617
618 //********************************************************************
619 void ModuleBase_WidgetMultiSelector::onListSelection()
620 {
621   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
622   myCopyAction->setEnabled(!aItems.isEmpty());
623   myDeleteAction->setEnabled(!aItems.isEmpty());
624
625   myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
626                                         true);
627 }
628
629 //********************************************************************
630 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
631 {
632   QList<QListWidgetItem*> aItems = myListControl->selectedItems();
633   foreach(QListWidgetItem* anItem, aItems) {
634     int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
635     if (theAttributeIds.find(anIndex) == theAttributeIds.end())
636       theAttributeIds.insert(anIndex);
637   }
638 }
639
640 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
641                                                    QList<ModuleBase_ViewerPrsPtr>& theValues) const
642 {
643   if(myFeature.get() == NULL)
644     return;
645
646   DataPtr aData = myFeature->data();
647   AttributePtr anAttribute = aData->attribute(attributeID());
648   std::string aType = anAttribute->attributeType();
649   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
650     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
651     for (int i = 0; i < aSelectionListAttr->size(); i++) {
652       // filter by attribute indices only if the container is not empty otherwise return all items
653       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
654         continue;
655       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
656       ResultPtr anObject = anAttr->context();
657       if (anObject.get())
658         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
659                new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
660     }
661   }
662   else if (aType == ModelAPI_AttributeRefList::typeId()) {
663     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
664     for (int i = 0; i < aRefListAttr->size(); i++) {
665       // filter by attribute indices only if the container is not empty otherwise return all items
666       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
667         continue;
668       ObjectPtr anObject = aRefListAttr->object(i);
669       if (anObject.get()) {
670         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
671                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
672       }
673     }
674   }
675   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
676     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
677     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
678       // filter by attribute indices only if the container is not empty otherwise return all items
679       if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
680         continue;
681       ObjectPtr anObject = aRefAttrListAttr->object(i);
682       if (!anObject.get())
683         continue;
684       TopoDS_Shape aShape;
685       AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
686       if (anAttribute.get()) {
687         GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
688         theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
689                new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
690       }
691     }
692   }
693 }
694
695 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
696                                                  (QList<ModuleBase_ViewerPrsPtr>& theValues)
697 {
698   bool isDone = false;
699
700   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
701   DataPtr aData = myFeature->data();
702   AttributePtr anAttribute = aData->attribute(attributeID());
703   std::string aType = anAttribute->attributeType();
704   std::set<GeomShapePtr> aShapes;
705   std::set<int> anIndicesToBeRemoved;
706   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
707     // iteration through data model to find not selected elements to remove them
708     AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
709     for (int i = 0; i < aSelectionListAttr->size(); i++) {
710       AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
711       bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
712       if (!aFound)
713         anIndicesToBeRemoved.insert(i);
714     }
715     isDone = anIndicesToBeRemoved.size() > 0;
716     aSelectionListAttr->remove(anIndicesToBeRemoved);
717   }
718   else if (aType == ModelAPI_AttributeRefList::typeId()) {
719     AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
720     for (int i = 0; i < aRefListAttr->size(); i++) {
721       ObjectPtr anObject = aRefListAttr->object(i);
722       if (anObject.get()) {
723         bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
724         if (!aFound)
725           anIndicesToBeRemoved.insert(i);
726       }
727     }
728     isDone = anIndicesToBeRemoved.size() > 0;
729     aRefListAttr->remove(anIndicesToBeRemoved);
730   }
731   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
732     std::set<AttributePtr> anAttributes;
733     QList<ModuleBase_ViewerPrsPtr>::const_iterator
734       anIt = theValues.begin(), aLast = theValues.end();
735     ObjectPtr anObject;
736     GeomShapePtr aShape;
737     for (; anIt != aLast; anIt++) {
738       ModuleBase_ViewerPrsPtr aPrs = *anIt;
739       getGeomSelection(aPrs, anObject, aShape);
740       AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
741       if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
742         anAttributes.insert(anAttr);
743     }
744
745     AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
746     for (int i = 0; i < aRefAttrListAttr->size(); i++) {
747       bool aFound = false;
748       if (aRefAttrListAttr->isAttribute(i)) {
749         AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
750         aFound = anAttributes.find(anAttribute) != anAttributes.end();
751       }
752       else {
753         aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
754       }
755       if (!aFound)
756         anIndicesToBeRemoved.insert(i);
757     }
758     isDone = anIndicesToBeRemoved.size() > 0;
759     aRefAttrListAttr->remove(anIndicesToBeRemoved);
760   }
761
762   return isDone;
763 }
764
765 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
766                                                      (QList<ModuleBase_ViewerPrsPtr>& theValues)
767 {
768   // convert prs list to objects map
769   std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
770   std::set<GeomShapePtr> aShapes;
771   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
772   ObjectPtr anObject;
773   GeomShapePtr aShape;
774   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
775   for (; anIt != aLast; anIt++) {
776     ModuleBase_ViewerPrsPtr aPrs = *anIt;
777     getGeomSelection(aPrs, anObject, aShape);
778     aShapes.clear();
779     if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
780       aShapes = aGeomSelection[anObject];
781     // we need to know if there was an empty shape in selection for the object
782     if (!aShape.get())
783       aShape = anEmptyShape;
784     if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
785       aShapes.insert(aShape);
786     aGeomSelection[anObject] = aShapes;
787   }
788   return aGeomSelection;
789 }
790
791 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
792                               const GeomShapePtr& theShape,
793                               const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
794 {
795   bool aFound = false;
796   GeomShapePtr anEmptyShape(new GeomAPI_Shape());
797   GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
798   if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
799     const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
800     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
801     for (; anIt != aLast && !aFound; anIt++) {
802       GeomShapePtr aCShape = *anIt;
803       if (aCShape.get())
804         aFound = aCShape->isEqual(aShape);
805     }
806   }
807   return aFound;
808 }