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 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
473 ModuleBase_Tools::setFocus(myListControl,
474 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
477 //********************************************************************
478 void ModuleBase_WidgetMultiSelector::updateSelectionName()
482 //********************************************************************
483 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
485 if (myIsSetSelectionBlocked)
487 ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
489 // according to #2154 we need to update OB selection when selection in the viewer happens
490 // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
491 // because we need that Object Browser has been already updated when synchronize happens.
493 // Restore selection in the viewer by the attribute selection list
494 // it is possible that diring selection attribute filling, selection in Object Browser
495 // is changed(some items were removed/added) and as result, selection in the viewer
496 // differs from the selection come to this method. By next rows, we restore selection
497 // in the viewer according to content of selection attribute. Case is Edge selection in Group
498 myIsSetSelectionBlocked = true;
499 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
500 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
501 Events_Loop::loop()->flush(anEvent);
502 myIsSetSelectionBlocked = false;
505 //********************************************************************
506 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
508 QIntList aShapeTypes;
510 if (myTypeCombo->count() > 1 && myIsUseChoice) {
511 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
514 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
515 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
520 //********************************************************************
521 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
523 QString aShapeTypeName;
525 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
526 aShapeTypeName = myTypeCombo->itemText(idx);
527 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
528 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
529 bool aWasActivated = activateSelectionAndFilters(false);
530 bool isBlocked = myTypeCombo->blockSignals(true);
531 myTypeCombo->setCurrentIndex(idx);
532 myTypeCombo->blockSignals(isBlocked);
534 activateSelectionAndFilters(true);
540 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
542 QList<ModuleBase_ViewerPrsPtr> aSelected;
543 convertIndicesToViewerSelection(std::set<int>(), aSelected);
547 //********************************************************************
548 void ModuleBase_WidgetMultiSelector::updateSelectionList()
550 myListControl->clear();
552 DataPtr aData = myFeature->data();
553 AttributePtr anAttribute = aData->attribute(attributeID());
554 std::string aType = anAttribute->attributeType();
555 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
556 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
557 for (int i = 0; i < aSelectionListAttr->size(); i++) {
558 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
559 QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
560 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
561 myListControl->addItem(anItem);
564 else if (aType == ModelAPI_AttributeRefList::typeId()) {
565 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
566 for (int i = 0; i < aRefListAttr->size(); i++) {
567 ObjectPtr anObject = aRefListAttr->object(i);
568 if (anObject.get()) {
569 QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
571 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
572 myListControl->addItem(anItem);
576 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
577 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
578 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
579 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
581 if (anAttribute.get()) {
582 std::string anAttrName = generateName(anAttribute, myWorkshop);
583 aName = QString::fromStdString(anAttrName);
586 ObjectPtr anObject = aRefAttrListAttr->object(i);
587 if (anObject.get()) {
588 aName = anObject->data()->name().c_str();
591 QListWidgetItem* anItem = new QListWidgetItem(aName, myListControl);
592 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
593 myListControl->addItem(anItem);
597 // We have to call repaint because sometimes the List control is not updated
598 myListControl->repaint();
601 //********************************************************************
602 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
606 if (theType == "Vertices")
608 else if (theType == "Edges")
610 else if (theType == "Faces")
612 else if (theType == "Solids")
618 //********************************************************************
619 void ModuleBase_WidgetMultiSelector::clearSelection()
621 bool isClearInNeutralPoint = myIsNeutralPointClear;
622 myIsNeutralPointClear = true;
624 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
625 // This method will call Selection changed event which will call onSelectionChanged
626 // To clear mySelection, myListControl and storeValue()
627 // So, we don't need to call it
628 myWorkshop->setSelected(anEmptyList);
630 myIsNeutralPointClear = isClearInNeutralPoint;
633 //********************************************************************
634 void ModuleBase_WidgetMultiSelector::onCopyItem()
636 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
638 foreach(QListWidgetItem* aItem, aItems) {
641 aRes += aItem->text();
643 if (!aRes.isEmpty()) {
644 QClipboard *clipboard = QApplication::clipboard();
645 clipboard->setText(aRes);
649 //********************************************************************
650 void ModuleBase_WidgetMultiSelector::onDeleteItem()
655 //********************************************************************
656 void ModuleBase_WidgetMultiSelector::onListSelection()
658 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
659 myCopyAction->setEnabled(!aItems.isEmpty());
660 myDeleteAction->setEnabled(!aItems.isEmpty());
662 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
666 //********************************************************************
667 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
669 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
670 foreach(QListWidgetItem* anItem, aItems) {
671 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
672 if (theAttributeIds.find(anIndex) == theAttributeIds.end())
673 theAttributeIds.insert(anIndex);
677 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
678 QList<ModuleBase_ViewerPrsPtr>& theValues) const
680 if(myFeature.get() == NULL)
683 DataPtr aData = myFeature->data();
684 AttributePtr anAttribute = aData->attribute(attributeID());
685 std::string aType = anAttribute->attributeType();
686 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
687 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
688 for (int i = 0; i < aSelectionListAttr->size(); i++) {
689 // filter by attribute indices only if the container is not empty otherwise return all items
690 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
692 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
693 ResultPtr anObject = anAttr->context();
695 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
696 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
699 else if (aType == ModelAPI_AttributeRefList::typeId()) {
700 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
701 for (int i = 0; i < aRefListAttr->size(); i++) {
702 // filter by attribute indices only if the container is not empty otherwise return all items
703 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
705 ObjectPtr anObject = aRefListAttr->object(i);
706 if (anObject.get()) {
707 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
708 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
712 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
713 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
714 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
715 // filter by attribute indices only if the container is not empty otherwise return all items
716 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
718 ObjectPtr anObject = aRefAttrListAttr->object(i);
722 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
723 if (anAttribute.get()) {
724 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
725 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
726 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
732 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
733 (QList<ModuleBase_ViewerPrsPtr>& theValues)
737 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
738 DataPtr aData = myFeature->data();
739 AttributePtr anAttribute = aData->attribute(attributeID());
740 std::string aType = anAttribute->attributeType();
741 std::set<GeomShapePtr> aShapes;
742 std::set<int> anIndicesToBeRemoved;
743 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
744 // iteration through data model to find not selected elements to remove them
745 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
746 for (int i = 0; i < aSelectionListAttr->size(); i++) {
747 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
748 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);
762 anIndicesToBeRemoved.insert(i);
765 isDone = anIndicesToBeRemoved.size() > 0;
766 aRefListAttr->remove(anIndicesToBeRemoved);
768 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
769 std::set<AttributePtr> anAttributes;
770 QList<ModuleBase_ViewerPrsPtr>::const_iterator
771 anIt = theValues.begin(), aLast = theValues.end();
774 for (; anIt != aLast; anIt++) {
775 ModuleBase_ViewerPrsPtr aPrs = *anIt;
776 getGeomSelection(aPrs, anObject, aShape);
777 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
778 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
779 anAttributes.insert(anAttr);
782 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
783 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
785 if (aRefAttrListAttr->isAttribute(i)) {
786 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
787 aFound = anAttributes.find(anAttribute) != anAttributes.end();
790 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection);
793 anIndicesToBeRemoved.insert(i);
795 isDone = anIndicesToBeRemoved.size() > 0;
796 aRefAttrListAttr->remove(anIndicesToBeRemoved);
802 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
803 (QList<ModuleBase_ViewerPrsPtr>& theValues)
805 // convert prs list to objects map
806 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
807 std::set<GeomShapePtr> aShapes;
808 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
811 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
812 for (; anIt != aLast; anIt++) {
813 ModuleBase_ViewerPrsPtr aPrs = *anIt;
814 getGeomSelection(aPrs, anObject, aShape);
816 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
817 aShapes = aGeomSelection[anObject];
818 // we need to know if there was an empty shape in selection for the object
820 aShape = anEmptyShape;
821 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
822 aShapes.insert(aShape);
823 aGeomSelection[anObject] = aShapes;
825 return aGeomSelection;
828 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
829 const GeomShapePtr& theShape,
830 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection)
833 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
834 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
835 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
836 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
837 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
838 for (; anIt != aLast && !aFound; anIt++) {
839 GeomShapePtr aCShape = *anIt;
841 aFound = aCShape->isSame(aShape);