1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModuleBase_WidgetMultiSelector.h>
22 #include <ModuleBase_WidgetShapeSelector.h>
23 #include <ModuleBase_ISelection.h>
24 #include <ModuleBase_IWorkshop.h>
25 #include <ModuleBase_IViewer.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_Definitions.h>
28 #include <ModuleBase_IModule.h>
29 #include <ModuleBase_ViewerPrs.h>
30 #include <ModuleBase_IconFactory.h>
31 #include <ModuleBase_Events.h>
33 #include <ModelAPI_Data.h>
34 #include <ModelAPI_Object.h>
35 #include <ModelAPI_AttributeSelectionList.h>
36 #include <ModelAPI_AttributeRefList.h>
37 #include <ModelAPI_AttributeRefAttrList.h>
38 #include <ModelAPI_Tools.h>
39 #include <ModelAPI_Events.h>
41 #include <Config_WidgetAPI.h>
43 #include <QGridLayout>
45 #include <QListWidget>
51 #include <QApplication>
54 #include <QMainWindow>
59 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
62 * Customization of a List Widget to make it to be placed on full width of container
64 class CustomListWidget : public QListWidget
68 /// \param theParent a parent widget
69 CustomListWidget( QWidget* theParent )
70 : QListWidget( theParent )
74 /// Redefinition of virtual method
75 virtual QSize sizeHint() const
77 int aHeight = 2*QFontMetrics( font() ).height();
78 QSize aSize = QListWidget::sizeHint();
79 return QSize( aSize.width(), aHeight );
82 /// Redefinition of virtual method
83 virtual QSize minimumSizeHint() const
85 int aHeight = 4/*2*/*QFontMetrics( font() ).height();
86 QSize aSize = QListWidget::minimumSizeHint();
87 return QSize( aSize.width(), aHeight );
91 // The code is necessary only for Linux because
92 //it can not update viewport on widget resize
94 void resizeEvent(QResizeEvent* theEvent)
96 QListWidget::resizeEvent(theEvent);
97 QTimer::singleShot(5, viewport(), SLOT(repaint()));
102 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
103 ModuleBase_IWorkshop* theWorkshop,
104 const Config_WidgetAPI* theData)
105 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData),
106 myIsSetSelectionBlocked(false)
108 QGridLayout* aMainLay = new QGridLayout(this);
109 ModuleBase_Tools::adjustMargins(aMainLay);
111 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
112 aMainLay->addWidget(aTypeLabel, 0, 0);
114 myTypeCombo = new QComboBox(this);
115 // There is no sense to parameterize list of types while we can not parameterize selection mode
117 std::string aPropertyTypes = theData->getProperty("type_choice");
118 QString aTypesStr = aPropertyTypes.c_str();
119 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
121 myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
123 if (!aShapeTypes.empty())
124 myTypeCombo->addItems(aShapeTypes);
125 aMainLay->addWidget(myTypeCombo, 0, 1);
126 // if the xml definition contains one type, the controls to select a type should not be shown
127 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
128 aTypeLabel->setVisible(false);
129 myTypeCombo->setVisible(false);
132 QString aLabelText = translate(theData->getProperty("label"));
133 QLabel* aListLabel = new QLabel(aLabelText, this);
134 aMainLay->addWidget(aListLabel, 1, 0);
135 // if the xml definition contains one type, an information label
136 // should be shown near to the latest
137 if (aShapeTypes.size() <= 1) {
138 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
139 if (!aLabelIcon.isEmpty()) {
140 QLabel* aSelectedLabel = new QLabel("", this);
141 aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
142 aMainLay->addWidget(aSelectedLabel, 1, 1);
144 aMainLay->setColumnStretch(2, 1);
147 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
148 myListControl = new CustomListWidget(this);
149 QString anObjName = QString::fromStdString(attributeID());
150 myListControl->setObjectName(anObjName);
151 myListControl->setToolTip(aToolTip);
152 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
154 aMainLay->addWidget(myListControl, 2, 0, 1, -1);
155 aMainLay->setRowStretch(2, 1);
156 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
157 //aMainLay->setRowMinimumHeight(3, 20);
158 //this->setLayout(aMainLay);
159 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
161 myCopyAction = ModuleBase_Tools::createAction(QIcon(":pictures/copy.png"), tr("Copy"),
162 myWorkshop->desktop(), this, SLOT(onCopyItem()));
163 myCopyAction->setShortcut(QKeySequence::Copy);
164 myCopyAction->setEnabled(false);
165 myListControl->addAction(myCopyAction);
167 myDeleteAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
168 myWorkshop->desktop(), this, SLOT(onDeleteItem()));
169 myDeleteAction->setEnabled(false);
170 myListControl->addAction(myDeleteAction);
172 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
173 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
175 myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
178 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
182 //********************************************************************
183 void ModuleBase_WidgetMultiSelector::activateCustom()
185 ModuleBase_WidgetSelector::activateCustom();
187 myWorkshop->module()->activateCustomPrs(myFeature,
188 ModuleBase_IModule::CustomizeHighlightedObjects, true);
191 //********************************************************************
192 void ModuleBase_WidgetMultiSelector::deactivate()
194 ModuleBase_WidgetSelector::deactivate();
196 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
199 //********************************************************************
200 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
202 // the value is stored on the selection changed signal processing
203 // A rare case when plugin was not loaded.
207 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
208 std::string aType = anAttribute->attributeType();
209 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
210 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
211 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
216 //********************************************************************
217 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
219 // A rare case when plugin was not loaded.
223 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
224 std::string aType = anAttribute->attributeType();
225 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
226 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
227 // Restore shape type
228 std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
229 if (!aSelectionType.empty())
230 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
232 updateSelectionList();
236 //********************************************************************
237 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
238 const bool theToValidate)
240 if (myIsSetSelectionBlocked)
243 AttributeSelectionListPtr aSelectionListAttr;
244 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
245 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
246 if (aSelectionListAttr.get())
247 aSelectionListAttr->cashValues(true);
249 /// remove unused objects from the model attribute.
250 /// It should be performed before new attributes append.
251 bool isDone = removeUnusedAttributeObjects(theValues);
253 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
254 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
255 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
256 for (; anIt != aLast; anIt++) {
257 ModuleBase_ViewerPrsPtr aValue = *anIt;
258 // do not validate and append to attribute selection presentation if it exists in the attribute
261 getGeomSelection(aValue, anObject, aShape);
262 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
263 anAttributeValues.append(aValue);
266 if (theToValidate && !isValidInFilters(aValue))
267 anInvalidValues.append(aValue);
269 bool aHasInvalidValues = anInvalidValues.size() > 0;
271 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
272 ModuleBase_ViewerPrsPtr aValue = *anIt;
273 bool aProcessed = false;
274 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
275 anAttributeValues.contains(aValue))
277 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
278 // if there is at least one set, the result is true
279 isDone = isDone || aProcessed;
281 // updateObject - to update/redisplay feature
282 // it is commented in order to perfom it outside the method
284 //updateObject(myFeature);
285 // this emit is necessary to call store/restore method an restore type of selection
286 //emit valuesChanged();
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 activateSelectionAndFilters(true);
414 /// store the selected type
415 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
416 std::string aType = anAttribute->attributeType();
417 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
418 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
419 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
422 // clear attribute values
423 DataPtr aData = myFeature->data();
424 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
425 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
426 aSelectionListAttr->clear();
428 else if (aType == ModelAPI_AttributeRefList::typeId()) {
429 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
430 aRefListAttr->clear();
432 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
433 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
434 aRefAttrListAttr->clear();
437 // update object is necessary to flush update signal. It leads to objects references map update
438 // and the operation presentation will not contain deleted items visualized as parameters of
440 updateObject(myFeature);
442 myWorkshop->setSelected(getAttributeSelection());
443 // may be the feature's result is not displayed, but attributes should be
444 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
445 true); /// hope that something is redisplayed by object updated
448 //********************************************************************
449 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
451 if (!myIsNeutralPointClear) {
452 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
453 // do not clear selected object
454 if (aSelected.size() == 0) {
455 if (!getAttributeSelection().empty()) {
456 // Restore selection in the viewer by the attribute selection list
457 // it should be postponed to exit from the selectionChanged processing
458 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
459 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
460 Events_Loop::loop()->flush(anEvent);
465 ModuleBase_WidgetSelector::onSelectionChanged();
468 void ModuleBase_WidgetMultiSelector::updateFocus()
470 // Set focus to List control in order to make possible
471 // to use Tab key for transfer the focus to next widgets
472 ModuleBase_Tools::setFocus(myListControl,
473 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
476 //********************************************************************
477 void ModuleBase_WidgetMultiSelector::updateSelectionName()
481 //********************************************************************
482 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
484 if (myIsSetSelectionBlocked)
486 ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
488 // according to #2154 we need to update OB selection when selection in the viewer happens
489 // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
490 // because we need that Object Browser has been already updated when synchronize happens.
492 // Restore selection in the viewer by the attribute selection list
493 // it is possible that diring selection attribute filling, selection in Object Browser
494 // is changed(some items were removed/added) and as result, selection in the viewer
495 // differs from the selection come to this method. By next rows, we restore selection
496 // in the viewer according to content of selection attribute. Case is Edge selection in Group
497 myIsSetSelectionBlocked = true;
498 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
499 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
500 Events_Loop::loop()->flush(anEvent);
501 myIsSetSelectionBlocked = false;
504 //********************************************************************
505 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
507 QIntList aShapeTypes;
509 if (myTypeCombo->count() > 1 && myIsUseChoice) {
510 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
513 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
514 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
519 //********************************************************************
520 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
522 QString aShapeTypeName;
524 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
525 aShapeTypeName = myTypeCombo->itemText(idx);
526 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
527 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
528 bool aWasActivated = activateSelectionAndFilters(false);
529 bool isBlocked = myTypeCombo->blockSignals(true);
530 myTypeCombo->setCurrentIndex(idx);
531 myTypeCombo->blockSignals(isBlocked);
533 activateSelectionAndFilters(true);
539 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
541 QList<ModuleBase_ViewerPrsPtr> aSelected;
542 convertIndicesToViewerSelection(std::set<int>(), aSelected);
546 //********************************************************************
547 void ModuleBase_WidgetMultiSelector::updateSelectionList()
549 myListControl->clear();
551 DataPtr aData = myFeature->data();
552 AttributePtr anAttribute = aData->attribute(attributeID());
553 std::string aType = anAttribute->attributeType();
554 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
555 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
556 for (int i = 0; i < aSelectionListAttr->size(); i++) {
557 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
558 QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
559 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
560 myListControl->addItem(anItem);
563 else if (aType == ModelAPI_AttributeRefList::typeId()) {
564 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
565 for (int i = 0; i < aRefListAttr->size(); i++) {
566 ObjectPtr anObject = aRefListAttr->object(i);
567 if (anObject.get()) {
568 QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
570 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
571 myListControl->addItem(anItem);
575 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
576 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
577 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
578 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
580 if (anAttribute.get()) {
581 std::string anAttrName = generateName(anAttribute, myWorkshop);
582 aName = QString::fromStdString(anAttrName);
585 ObjectPtr anObject = aRefAttrListAttr->object(i);
586 if (anObject.get()) {
587 aName = anObject->data()->name().c_str();
590 QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
591 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
592 myListControl->addItem(anItem);
596 // We have to call repaint because sometimes the List control is not updated
597 myListControl->repaint();
600 //********************************************************************
601 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
605 if (theType == "Vertices")
607 else if (theType == "Edges")
609 else if (theType == "Faces")
611 else if (theType == "Solids")
617 //********************************************************************
618 void ModuleBase_WidgetMultiSelector::clearSelection()
620 bool isClearInNeutralPoint = myIsNeutralPointClear;
621 myIsNeutralPointClear = true;
623 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
624 // This method will call Selection changed event which will call onSelectionChanged
625 // To clear mySelection, myListControl and storeValue()
626 // So, we don't need to call it
627 myWorkshop->setSelected(anEmptyList);
629 myIsNeutralPointClear = isClearInNeutralPoint;
632 //********************************************************************
633 void ModuleBase_WidgetMultiSelector::onCopyItem()
635 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
637 foreach(QListWidgetItem* aItem, aItems) {
640 aRes += aItem->text();
642 if (!aRes.isEmpty()) {
643 QClipboard *clipboard = QApplication::clipboard();
644 clipboard->setText(aRes);
648 //********************************************************************
649 void ModuleBase_WidgetMultiSelector::onDeleteItem()
654 //********************************************************************
655 void ModuleBase_WidgetMultiSelector::onListSelection()
657 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
658 myCopyAction->setEnabled(!aItems.isEmpty());
659 myDeleteAction->setEnabled(!aItems.isEmpty());
661 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
665 //********************************************************************
666 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
668 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
669 foreach(QListWidgetItem* anItem, aItems) {
670 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
671 if (theAttributeIds.find(anIndex) == theAttributeIds.end())
672 theAttributeIds.insert(anIndex);
676 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
677 QList<ModuleBase_ViewerPrsPtr>& theValues) const
679 if(myFeature.get() == NULL)
682 DataPtr aData = myFeature->data();
683 AttributePtr anAttribute = aData->attribute(attributeID());
684 std::string aType = anAttribute->attributeType();
685 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
686 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
687 for (int i = 0; i < aSelectionListAttr->size(); i++) {
688 // filter by attribute indices only if the container is not empty otherwise return all items
689 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
691 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
692 ResultPtr anObject = anAttr->context();
694 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
695 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
698 else if (aType == ModelAPI_AttributeRefList::typeId()) {
699 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
700 for (int i = 0; i < aRefListAttr->size(); i++) {
701 // filter by attribute indices only if the container is not empty otherwise return all items
702 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
704 ObjectPtr anObject = aRefListAttr->object(i);
705 if (anObject.get()) {
706 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
707 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
711 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
712 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
713 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
714 // filter by attribute indices only if the container is not empty otherwise return all items
715 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
717 ObjectPtr anObject = aRefAttrListAttr->object(i);
721 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
722 if (anAttribute.get()) {
723 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
724 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
725 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
731 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
732 (QList<ModuleBase_ViewerPrsPtr>& theValues)
736 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
737 DataPtr aData = myFeature->data();
738 AttributePtr anAttribute = aData->attribute(attributeID());
739 std::string aType = anAttribute->attributeType();
740 std::set<GeomShapePtr> aShapes;
741 std::set<int> anIndicesToBeRemoved;
742 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
743 // iteration through data model to find not selected elements to remove them
744 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
745 for (int i = 0; i < aSelectionListAttr->size(); i++) {
746 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
747 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection,
750 anIndicesToBeRemoved.insert(i);
752 isDone = anIndicesToBeRemoved.size() > 0;
753 aSelectionListAttr->remove(anIndicesToBeRemoved);
755 else if (aType == ModelAPI_AttributeRefList::typeId()) {
756 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
757 for (int i = 0; i < aRefListAttr->size(); i++) {
758 ObjectPtr anObject = aRefListAttr->object(i);
759 if (anObject.get()) {
760 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection,
763 anIndicesToBeRemoved.insert(i);
766 isDone = anIndicesToBeRemoved.size() > 0;
767 aRefListAttr->remove(anIndicesToBeRemoved);
769 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
770 std::set<AttributePtr> anAttributes;
771 QList<ModuleBase_ViewerPrsPtr>::const_iterator
772 anIt = theValues.begin(), aLast = theValues.end();
775 for (; anIt != aLast; anIt++) {
776 ModuleBase_ViewerPrsPtr aPrs = *anIt;
777 getGeomSelection(aPrs, anObject, aShape);
778 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
779 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
780 anAttributes.insert(anAttr);
783 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
784 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
786 if (aRefAttrListAttr->isAttribute(i)) {
787 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
788 aFound = anAttributes.find(anAttribute) != anAttributes.end();
791 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection,
795 anIndicesToBeRemoved.insert(i);
797 isDone = anIndicesToBeRemoved.size() > 0;
798 aRefAttrListAttr->remove(anIndicesToBeRemoved);
804 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
805 (QList<ModuleBase_ViewerPrsPtr>& theValues)
807 // convert prs list to objects map
808 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
809 std::set<GeomShapePtr> aShapes;
810 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
813 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
814 for (; anIt != aLast; anIt++) {
815 ModuleBase_ViewerPrsPtr aPrs = *anIt;
816 getGeomSelection(aPrs, anObject, aShape);
818 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
819 aShapes = aGeomSelection[anObject];
820 // we need to know if there was an empty shape in selection for the object
822 aShape = anEmptyShape;
823 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
824 aShapes.insert(aShape);
825 aGeomSelection[anObject] = aShapes;
827 return aGeomSelection;
830 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
831 GeomShapePtr theShape,
832 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection,
833 ModuleBase_IWorkshop* theWorkshop)
835 // issue #2154: we should not remove from list objects hidden in the viewer if selection
836 // was done with SHIFT button
837 if (theWorkshop->hasSHIFTPressed() && !theObject->isDisplayed())
841 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
842 if (theShape.get()) { // treat shape equal to context as null: 2219, keep order of shapes in list
843 const ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
844 if (aContext.get() && aContext->shape()->isEqual(theShape))
847 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
848 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
849 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
850 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
851 for (; anIt != aLast && !aFound; anIt++) {
852 GeomShapePtr aCShape = *anIt;
854 aFound = aCShape->isSame(aShape);