1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_WidgetMultiSelector.cpp
6 * Created on: Aug 28, 2014
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>
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>
30 #include <Config_WidgetAPI.h>
32 #include <QGridLayout>
34 #include <QListWidget>
40 #include <QApplication>
43 #include <QMainWindow>
48 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
51 * Customization of a List Widget to make it to be placed on full width of container
53 class CustomListWidget : public QListWidget
57 /// \param theParent a parent widget
58 CustomListWidget( QWidget* theParent )
59 : QListWidget( theParent )
63 /// Redefinition of virtual method
64 virtual QSize sizeHint() const
66 int aHeight = 2*QFontMetrics( font() ).height();
67 QSize aSize = QListWidget::sizeHint();
68 return QSize( aSize.width(), aHeight );
71 /// Redefinition of virtual method
72 virtual QSize minimumSizeHint() const
74 int aHeight = 4/*2*/*QFontMetrics( font() ).height();
75 QSize aSize = QListWidget::minimumSizeHint();
76 return QSize( aSize.width(), aHeight );
80 // The code is necessary only for Linux because
81 //it can not update viewport on widget resize
83 void resizeEvent(QResizeEvent* theEvent)
85 QListWidget::resizeEvent(theEvent);
86 QTimer::singleShot(5, viewport(), SLOT(repaint()));
91 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
92 ModuleBase_IWorkshop* theWorkshop,
93 const Config_WidgetAPI* theData)
94 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData),
95 myIsSetSelectionBlocked(false)
97 QGridLayout* aMainLay = new QGridLayout(this);
98 ModuleBase_Tools::adjustMargins(aMainLay);
100 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
101 aMainLay->addWidget(aTypeLabel, 0, 0);
103 myTypeCombo = new QComboBox(this);
104 // There is no sense to parameterize list of types while we can not parameterize selection mode
106 std::string aPropertyTypes = theData->getProperty("type_choice");
107 QString aTypesStr = aPropertyTypes.c_str();
108 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
110 myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
112 if (!aShapeTypes.empty())
113 myTypeCombo->addItems(aShapeTypes);
114 aMainLay->addWidget(myTypeCombo, 0, 1);
115 // if the xml definition contains one type, the controls to select a type should not be shown
116 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
117 aTypeLabel->setVisible(false);
118 myTypeCombo->setVisible(false);
121 QString aLabelText = translate(theData->getProperty("label"));
122 QLabel* aListLabel = new QLabel(aLabelText, 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);
133 aMainLay->setColumnStretch(2, 1);
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);
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()));
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);
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);
161 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
162 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
164 myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
167 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
171 //********************************************************************
172 void ModuleBase_WidgetMultiSelector::activateCustom()
174 ModuleBase_WidgetSelector::activateCustom();
176 myWorkshop->module()->activateCustomPrs(myFeature,
177 ModuleBase_IModule::CustomizeHighlightedObjects, true);
180 //********************************************************************
181 void ModuleBase_WidgetMultiSelector::deactivate()
183 ModuleBase_WidgetSelector::deactivate();
185 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
188 //********************************************************************
189 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
191 // the value is stored on the selection changed signal processing
192 // A rare case when plugin was not loaded.
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());
205 //********************************************************************
206 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
208 // A rare case when plugin was not loaded.
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()));
221 updateSelectionList();
225 //********************************************************************
226 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
227 const bool theToValidate)
229 if (myIsSetSelectionBlocked)
232 AttributeSelectionListPtr aSelectionListAttr;
233 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
234 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
235 if (aSelectionListAttr.get())
236 aSelectionListAttr->cashValues(true);
238 /// remove unused objects from the model attribute.
239 /// It should be performed before new attributes append.
240 bool isDone = removeUnusedAttributeObjects(theValues);
242 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
243 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
244 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
245 for (; anIt != aLast; anIt++) {
246 ModuleBase_ViewerPrsPtr aValue = *anIt;
247 // do not validate and append to attribute selection presentation if it exists in the attribute
250 getGeomSelection(aValue, anObject, aShape);
251 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
252 anAttributeValues.append(aValue);
255 if (theToValidate && !isValidInFilters(aValue))
256 anInvalidValues.append(aValue);
258 bool aHasInvalidValues = anInvalidValues.size() > 0;
260 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
261 ModuleBase_ViewerPrsPtr aValue = *anIt;
262 bool aProcessed = false;
263 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
264 anAttributeValues.contains(aValue))
266 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
267 // if there is at least one set, the result is true
268 isDone = isDone || aProcessed;
270 // updateObject - to update/redisplay feature
271 // it is commented in order to perfom it outside the method
273 //updateObject(myFeature);
274 // this emit is necessary to call store/restore method an restore type of selection
275 //emit valuesChanged();
278 // Restore selection in the viewer by the attribute selection list
279 // it is possible that diring selection attribute filling, selection in Object Browser
280 // is changed(some items were removed/added) and as result, selection in the viewer
281 // differs from the selection come to this method. By next rows, we restore selection
282 // in the viewer according to content of selection attribute. Case is Edge selection in Group
283 myIsSetSelectionBlocked = true;
284 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
285 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
286 Events_Loop::loop()->flush(anEvent);
287 myIsSetSelectionBlocked = false;
289 if (aSelectionListAttr.get())
290 aSelectionListAttr->cashValues(false);
293 if (!anInvalidValues.empty())
294 theValues.append(anInvalidValues);
296 if (isDone) // may be the feature's result is not displayed, but attributes should be
297 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
298 true);/// hope that something is redisplayed by object updated
303 //********************************************************************
304 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
306 std::set<int> anAttributeIds;
307 getSelectedAttributeIndices(anAttributeIds);
308 if (!anAttributeIds.empty())
309 convertIndicesToViewerSelection(anAttributeIds, theValues);
312 //********************************************************************
313 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
315 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
317 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
318 aValid = aResult.get() != NULL;
321 // We can not select a result of our feature
322 std::list<ResultPtr> aResults;
323 ModelAPI_Tools::allResults(myFeature, aResults);
324 std::list<ResultPtr>::const_iterator aIt;
325 bool isSkipSelf = false;
326 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
327 if ((*aIt) == aResult) {
340 //********************************************************************
341 bool ModuleBase_WidgetMultiSelector::processDelete()
343 // find attribute indices to delete
344 std::set<int> anAttributeIds;
345 getSelectedAttributeIndices(anAttributeIds);
347 QModelIndexList aIndexes = myListControl->selectionModel()->selectedIndexes();
349 // refill attribute by the items which indices are not in the list of ids
351 DataPtr aData = myFeature->data();
352 AttributePtr anAttribute = aData->attribute(attributeID());
353 std::string aType = anAttribute->attributeType();
354 aDone = !anAttributeIds.empty();
355 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
356 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
357 aSelectionListAttr->remove(anAttributeIds);
360 else if (aType == ModelAPI_AttributeRefList::typeId()) {
361 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
362 aRefListAttr->remove(anAttributeIds);
364 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
365 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
366 aRefAttrListAttr->remove(anAttributeIds);
370 // update object is necessary to flush update signal. It leads to objects references map update
371 // and the operation presentation will not contain deleted items visualized as parameters of
373 updateObject(myFeature);
376 myWorkshop->setSelected(getAttributeSelection());
378 // may be the feature's result is not displayed, but attributes should be
379 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
380 true); /// hope that something is redisplayed by object updated
384 int aRows = myListControl->model()->rowCount();
386 foreach(QModelIndex aIndex, aIndexes) {
387 if (aIndex.row() < aRows)
388 myListControl->selectionModel()->select(aIndex, QItemSelectionModel::Select);
390 QModelIndex aIdx = myListControl->model()->index(aRows - 1, 0);
391 myListControl->selectionModel()->select(aIdx, QItemSelectionModel::Select);
398 //********************************************************************
399 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
401 QList<QWidget*> result;
402 //result << myTypeCombo;
403 result << myListControl;
407 //********************************************************************
408 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
410 clearValidatedCash();
411 activateSelectionAndFilters(true);
415 /// store the selected type
416 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
417 std::string aType = anAttribute->attributeType();
418 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
419 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
420 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
423 // clear attribute values
424 DataPtr aData = myFeature->data();
425 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
426 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
427 aSelectionListAttr->clear();
429 else if (aType == ModelAPI_AttributeRefList::typeId()) {
430 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
431 aRefListAttr->clear();
433 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
434 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
435 aRefAttrListAttr->clear();
438 // update object is necessary to flush update signal. It leads to objects references map update
439 // and the operation presentation will not contain deleted items visualized as parameters of
441 updateObject(myFeature);
443 myWorkshop->setSelected(getAttributeSelection());
444 // may be the feature's result is not displayed, but attributes should be
445 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
446 true); /// hope that something is redisplayed by object updated
449 //********************************************************************
450 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
452 if (!myIsNeutralPointClear) {
453 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
454 // do not clear selected object
455 if (aSelected.size() == 0) {
456 if (!getAttributeSelection().empty()) {
457 // Restore selection in the viewer by the attribute selection list
458 // it should be postponed to exit from the selectionChanged processing
459 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
460 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
461 Events_Loop::loop()->flush(anEvent);
466 ModuleBase_WidgetSelector::onSelectionChanged();
469 void ModuleBase_WidgetMultiSelector::updateFocus()
471 // Set focus to List control in order to make possible
472 // to use Tab key for transfer the focus to next widgets
473 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
474 ModuleBase_Tools::setFocus(myListControl,
475 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
478 //********************************************************************
479 void ModuleBase_WidgetMultiSelector::updateSelectionName()
483 //********************************************************************
484 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
486 QIntList aShapeTypes;
488 if (myTypeCombo->count() > 1 && myIsUseChoice) {
489 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
492 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
493 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
498 //********************************************************************
499 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
501 QString aShapeTypeName;
503 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
504 aShapeTypeName = myTypeCombo->itemText(idx);
505 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
506 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
507 bool aWasActivated = activateSelectionAndFilters(false);
508 bool isBlocked = myTypeCombo->blockSignals(true);
509 myTypeCombo->setCurrentIndex(idx);
510 myTypeCombo->blockSignals(isBlocked);
512 activateSelectionAndFilters(true);
518 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
520 QList<ModuleBase_ViewerPrsPtr> aSelected;
521 convertIndicesToViewerSelection(std::set<int>(), aSelected);
525 //********************************************************************
526 void ModuleBase_WidgetMultiSelector::updateSelectionList()
528 myListControl->clear();
530 DataPtr aData = myFeature->data();
531 AttributePtr anAttribute = aData->attribute(attributeID());
532 std::string aType = anAttribute->attributeType();
533 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
534 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
535 for (int i = 0; i < aSelectionListAttr->size(); i++) {
536 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
537 QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
538 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
539 myListControl->addItem(anItem);
542 else if (aType == ModelAPI_AttributeRefList::typeId()) {
543 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
544 for (int i = 0; i < aRefListAttr->size(); i++) {
545 ObjectPtr anObject = aRefListAttr->object(i);
546 if (anObject.get()) {
547 QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
549 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
550 myListControl->addItem(anItem);
554 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
555 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
556 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
557 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
559 if (anAttribute.get()) {
560 std::string anAttrName = generateName(anAttribute, myWorkshop);
561 aName = QString::fromStdString(anAttrName);
564 ObjectPtr anObject = aRefAttrListAttr->object(i);
565 if (anObject.get()) {
566 aName = anObject->data()->name().c_str();
569 QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
570 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
571 myListControl->addItem(anItem);
575 // We have to call repaint because sometimes the List control is not updated
576 myListControl->repaint();
579 //********************************************************************
580 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
584 if (theType == "Vertices")
586 else if (theType == "Edges")
588 else if (theType == "Faces")
590 else if (theType == "Solids")
596 //********************************************************************
597 void ModuleBase_WidgetMultiSelector::clearSelection()
599 bool isClearInNeutralPoint = myIsNeutralPointClear;
600 myIsNeutralPointClear = true;
602 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
603 // This method will call Selection changed event which will call onSelectionChanged
604 // To clear mySelection, myListControl and storeValue()
605 // So, we don't need to call it
606 myWorkshop->setSelected(anEmptyList);
608 myIsNeutralPointClear = isClearInNeutralPoint;
611 //********************************************************************
612 void ModuleBase_WidgetMultiSelector::onCopyItem()
614 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
616 foreach(QListWidgetItem* aItem, aItems) {
619 aRes += aItem->text();
621 if (!aRes.isEmpty()) {
622 QClipboard *clipboard = QApplication::clipboard();
623 clipboard->setText(aRes);
627 //********************************************************************
628 void ModuleBase_WidgetMultiSelector::onDeleteItem()
633 //********************************************************************
634 void ModuleBase_WidgetMultiSelector::onListSelection()
636 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
637 myCopyAction->setEnabled(!aItems.isEmpty());
638 myDeleteAction->setEnabled(!aItems.isEmpty());
640 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
644 //********************************************************************
645 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
647 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
648 foreach(QListWidgetItem* anItem, aItems) {
649 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
650 if (theAttributeIds.find(anIndex) == theAttributeIds.end())
651 theAttributeIds.insert(anIndex);
655 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
656 QList<ModuleBase_ViewerPrsPtr>& theValues) const
658 if(myFeature.get() == NULL)
661 DataPtr aData = myFeature->data();
662 AttributePtr anAttribute = aData->attribute(attributeID());
663 std::string aType = anAttribute->attributeType();
664 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
665 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
666 for (int i = 0; i < aSelectionListAttr->size(); i++) {
667 // filter by attribute indices only if the container is not empty otherwise return all items
668 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
670 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
671 ResultPtr anObject = anAttr->context();
673 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
674 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
677 else if (aType == ModelAPI_AttributeRefList::typeId()) {
678 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
679 for (int i = 0; i < aRefListAttr->size(); i++) {
680 // filter by attribute indices only if the container is not empty otherwise return all items
681 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
683 ObjectPtr anObject = aRefListAttr->object(i);
684 if (anObject.get()) {
685 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
686 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
690 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
691 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
692 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
693 // filter by attribute indices only if the container is not empty otherwise return all items
694 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
696 ObjectPtr anObject = aRefAttrListAttr->object(i);
700 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
701 if (anAttribute.get()) {
702 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
703 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
704 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
710 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
711 (QList<ModuleBase_ViewerPrsPtr>& theValues)
715 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
716 DataPtr aData = myFeature->data();
717 AttributePtr anAttribute = aData->attribute(attributeID());
718 std::string aType = anAttribute->attributeType();
719 std::set<GeomShapePtr> aShapes;
720 std::set<int> anIndicesToBeRemoved;
721 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
722 // iteration through data model to find not selected elements to remove them
723 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
724 for (int i = 0; i < aSelectionListAttr->size(); i++) {
725 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
726 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection);
728 anIndicesToBeRemoved.insert(i);
730 isDone = anIndicesToBeRemoved.size() > 0;
731 aSelectionListAttr->remove(anIndicesToBeRemoved);
733 else if (aType == ModelAPI_AttributeRefList::typeId()) {
734 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
735 for (int i = 0; i < aRefListAttr->size(); i++) {
736 ObjectPtr anObject = aRefListAttr->object(i);
737 if (anObject.get()) {
738 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection);
740 anIndicesToBeRemoved.insert(i);
743 isDone = anIndicesToBeRemoved.size() > 0;
744 aRefListAttr->remove(anIndicesToBeRemoved);
746 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
747 std::set<AttributePtr> anAttributes;
748 QList<ModuleBase_ViewerPrsPtr>::const_iterator
749 anIt = theValues.begin(), aLast = theValues.end();
752 for (; anIt != aLast; anIt++) {
753 ModuleBase_ViewerPrsPtr aPrs = *anIt;
754 getGeomSelection(aPrs, anObject, aShape);
755 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
756 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
757 anAttributes.insert(anAttr);
760 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
761 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
763 if (aRefAttrListAttr->isAttribute(i)) {
764 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
765 aFound = anAttributes.find(anAttribute) != anAttributes.end();
768 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
771 anIndicesToBeRemoved.insert(i);
773 isDone = anIndicesToBeRemoved.size() > 0;
774 aRefAttrListAttr->remove(anIndicesToBeRemoved);
780 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
781 (QList<ModuleBase_ViewerPrsPtr>& theValues)
783 // convert prs list to objects map
784 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
785 std::set<GeomShapePtr> aShapes;
786 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
789 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
790 for (; anIt != aLast; anIt++) {
791 ModuleBase_ViewerPrsPtr aPrs = *anIt;
792 getGeomSelection(aPrs, anObject, aShape);
794 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
795 aShapes = aGeomSelection[anObject];
796 // we need to know if there was an empty shape in selection for the object
798 aShape = anEmptyShape;
799 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
800 aShapes.insert(aShape);
801 aGeomSelection[anObject] = aShapes;
803 return aGeomSelection;
806 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
807 const GeomShapePtr& theShape,
808 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
811 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
812 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
813 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
814 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
815 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
816 for (; anIt != aLast && !aFound; anIt++) {
817 GeomShapePtr aCShape = *anIt;
819 aFound = aCShape->isSame(aShape);