X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetMultiSelector.cpp;h=00b13340261664da441ec695877efc0d5a66cfe2;hb=29dfb8a802f61cacf5f57fb79c62badee00ebcdf;hp=ff8bafdea6a8746a9ca85a9063af9e2cc62cbe73;hpb=7b9e257dacdb4564754c3bcd0c35817e73a3989c;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp old mode 100644 new mode 100755 index ff8bafdea..00b133402 --- a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D /* * ModuleBase_WidgetMultiSelector.cpp @@ -9,14 +9,17 @@ #include #include -#include #include #include #include #include +#include +#include #include #include +#include +#include #include @@ -30,16 +33,60 @@ #include #include #include +#include #include #include +const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1; + +/** +* Customization of a List Widget to make it to be placed on full width of container +*/ +class CustomListWidget : public QListWidget +{ +public: + /// Constructor + /// \param theParent a parent widget + CustomListWidget( QWidget* theParent ) + : QListWidget( theParent ) + { + } + + /// Redefinition of virtual method + virtual QSize sizeHint() const + { + int aHeight = 2*QFontMetrics( font() ).height(); + QSize aSize = QListWidget::sizeHint(); + return QSize( aSize.width(), aHeight ); + } + + /// Redefinition of virtual method + virtual QSize minimumSizeHint() const + { + int aHeight = 4/*2*/*QFontMetrics( font() ).height(); + QSize aSize = QListWidget::minimumSizeHint(); + return QSize( aSize.width(), aHeight ); + } + +#ifndef WIN32 +// The code is necessary only for Linux because +//it can not update viewport on widget resize +protected: + void resizeEvent(QResizeEvent* theEvent) + { + QListWidget::resizeEvent(theEvent); + QTimer::singleShot(5, viewport(), SLOT(repaint())); + } +#endif +}; + ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData, const std::string& theParentId) - : ModuleBase_WidgetValidated(theParent, theData, theParentId), - myWorkshop(theWorkshop), myIsActive(false) + : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId), + mySelectionCount(0) { QGridLayout* aMainLay = new QGridLayout(this); ModuleBase_Tools::adjustMargins(aMainLay); @@ -48,39 +95,50 @@ ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParen aMainLay->addWidget(aTypeLabel, 0, 0); myTypeCombo = new QComboBox(this); - // There is no sence to paramerize list of types while we can not parametrize selection mode + // There is no sense to parameterize list of types while we can not parameterize selection mode std::string aPropertyTypes = theData->getProperty("type_choice"); QString aTypesStr = aPropertyTypes.c_str(); - QStringList aShapeTypes = aTypesStr.split(' '); + QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts); + + myIsUseChoice = theData->getBooleanAttribute("use_choice", true); - myTypeCombo->addItems(aShapeTypes); + if (!aShapeTypes.empty()) + myTypeCombo->addItems(aShapeTypes); aMainLay->addWidget(myTypeCombo, 0, 1); // if the xml definition contains one type, the controls to select a type should not be shown - if (aShapeTypes.size() == 1) { + if (aShapeTypes.size() <= 1 || !myIsUseChoice) { aTypeLabel->setVisible(false); myTypeCombo->setVisible(false); } - QLabel* aListLabel = new QLabel(tr("Selected objects:"), this); + std::string aLabelText = theData->getProperty("label"); + QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str() + : tr("Selected objects:"), this); aMainLay->addWidget(aListLabel, 1, 0); // if the xml definition contains one type, an information label should be shown near to the latest - if (aShapeTypes.size() == 1) { - QString aLabelText = QString::fromStdString(theData->widgetLabel()); + if (aShapeTypes.size() <= 1) { QString aLabelIcon = QString::fromStdString(theData->widgetIcon()); - QLabel* aSelectedLabel = new QLabel(aLabelText, this); - if (!aLabelIcon.isEmpty()) + if (!aLabelIcon.isEmpty()) { + QLabel* aSelectedLabel = new QLabel("", this); aSelectedLabel->setPixmap(QPixmap(aLabelIcon)); - aMainLay->addWidget(aSelectedLabel, 1, 1); + aMainLay->addWidget(aSelectedLabel, 1, 1); + } aMainLay->setColumnStretch(2, 1); } - myListControl = new QListWidget(this); - aMainLay->addWidget(myListControl, 2, 0, 2, -1); + QString aToolTip = QString::fromStdString(theData->widgetTooltip()); + myListControl = new CustomListWidget(this); + QString anObjName = QString::fromStdString(attributeID()); + myListControl->setObjectName(anObjName); + myListControl->setToolTip(aToolTip); + myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection); + + aMainLay->addWidget(myListControl, 2, 0, 1, -1); aMainLay->setRowStretch(2, 1); - aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)??? - aMainLay->setRowMinimumHeight(3, 20); - this->setLayout(aMainLay); + //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)??? + //aMainLay->setRowMinimumHeight(3, 20); + //this->setLayout(aMainLay); connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged())); myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this); @@ -88,34 +146,35 @@ ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParen myCopyAction->setEnabled(false); connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem())); myListControl->addAction(myCopyAction); + + myDeleteAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this); + myDeleteAction->setEnabled(false); + connect(myDeleteAction, SIGNAL(triggered(bool)), SLOT(onDeleteItem())); + myListControl->addAction(myDeleteAction); + myListControl->setContextMenuPolicy(Qt::ActionsContextMenu); connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection())); } ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector() { - myIsActive = false; - activateShapeSelection(); } //******************************************************************** void ModuleBase_WidgetMultiSelector::activateCustom() { - ModuleBase_IViewer* aViewer = myWorkshop->viewer(); - connect(myWorkshop, SIGNAL(selectionChanged()), - this, SLOT(onSelectionChanged()), - Qt::UniqueConnection); + ModuleBase_WidgetSelector::activateCustom(); - myIsActive = true; - activateShapeSelection(); + myWorkshop->module()->activateCustomPrs(myFeature, + ModuleBase_IModule::CustomizeHighlightedObjects, true); } //******************************************************************** void ModuleBase_WidgetMultiSelector::deactivate() { - disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); - myIsActive = false; - activateShapeSelection(); + ModuleBase_WidgetSelector::deactivate(); + + myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true); } //******************************************************************** @@ -123,111 +182,224 @@ bool ModuleBase_WidgetMultiSelector::storeValueCustom() const { // the value is stored on the selection changed signal processing // A rare case when plugin was not loaded. - if(!myFeature) + if (!myFeature) return false; - DataPtr aData = myFeature->data(); - AttributeSelectionListPtr aSelectionListAttr = - std::dynamic_pointer_cast(aData->attribute(attributeID())); - - if (aSelectionListAttr) { - // Store shapes type - TopAbs_ShapeEnum aCurrentType = - ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText()); - aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString()); - } - return true; + + AttributePtr anAttribute = myFeature->data()->attribute(attributeID()); + std::string aType = anAttribute->attributeType(); + if (aType == ModelAPI_AttributeSelectionList::typeId()) { + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(anAttribute); + aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString()); + } + return true; } //******************************************************************** -bool ModuleBase_WidgetMultiSelector::restoreValue() +bool ModuleBase_WidgetMultiSelector::restoreValueCustom() { // A rare case when plugin was not loaded. - if(!myFeature) + if (!myFeature) return false; - DataPtr aData = myFeature->data(); - AttributeSelectionListPtr aSelectionListAttr = - std::dynamic_pointer_cast(aData->attribute(attributeID())); - if (aSelectionListAttr) { + AttributePtr anAttribute = myFeature->data()->attribute(attributeID()); + std::string aType = anAttribute->attributeType(); + if (aType == ModelAPI_AttributeSelectionList::typeId()) { + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(anAttribute); // Restore shape type - setCurrentShapeType( - ModuleBase_WidgetShapeSelector::shapeType(aSelectionListAttr->selectionType().c_str())); - updateSelectionList(aSelectionListAttr); - return true; + setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str())); } - return false; + updateSelectionList(); + return true; } //******************************************************************** -void ModuleBase_WidgetMultiSelector::backupAttributeValue(const bool isBackup) +void ModuleBase_WidgetMultiSelector::storeAttributeValue() { - DataPtr aData = myFeature->data(); - AttributeSelectionListPtr aSelectionListAttr = - std::dynamic_pointer_cast(aData->attribute(attributeID())); - if (aSelectionListAttr.get() == NULL) - return; + ModuleBase_WidgetValidated::storeAttributeValue(); - if (isBackup) { + AttributePtr anAttribute = myFeature->data()->attribute(attributeID()); + std::string aType = anAttribute->attributeType(); + if (aType == ModelAPI_AttributeSelectionList::typeId()) { + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(anAttribute); mySelectionType = aSelectionListAttr->selectionType(); - mySelection.clear(); - for (int i = 0; i < aSelectionListAttr->size(); i++) { - AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i); - mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value())); - } + mySelectionCount = aSelectionListAttr->size(); } - else { - aSelectionListAttr->clear(); - // Store shapes type + else if (aType == ModelAPI_AttributeRefList::typeId()) { + AttributeRefListPtr aRefListAttr = + std::dynamic_pointer_cast(anAttribute); + mySelectionCount = aRefListAttr->size(); + } +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool theValid) +{ + ModuleBase_WidgetValidated::restoreAttributeValue(theValid); + + AttributePtr anAttribute = myFeature->data()->attribute(attributeID()); + std::string aType = anAttribute->attributeType(); + if (aType == ModelAPI_AttributeSelectionList::typeId()) { + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(anAttribute); aSelectionListAttr->setSelectionType(mySelectionType); - // Store selection in the attribute - foreach (GeomSelection aSelec, mySelection) { - aSelectionListAttr->append(aSelec.first, aSelec.second); + // restore selection in the attribute. Indeed there is only one stored object + int aCountAppened = aSelectionListAttr->size() - mySelectionCount; + for (int i = 0; i < aCountAppened; i++) + aSelectionListAttr->removeLast(); + } + else if (aType == ModelAPI_AttributeRefList::typeId()) { + AttributeRefListPtr aRefListAttr = + std::dynamic_pointer_cast(anAttribute); + // restore objects in the attribute. Indeed there is only one stored object + int aCountAppened = aRefListAttr->size() - mySelectionCount; + for (int i = 0; i < aCountAppened; i++) + aRefListAttr->removeLast(); + } +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::clearAttribute() +{ + AttributePtr anAttribute = myFeature->data()->attribute(attributeID()); + std::string aType = anAttribute->attributeType(); + if (aType == ModelAPI_AttributeSelectionList::typeId()) { + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(anAttribute); + aSelectionListAttr->clear(); + } + else if (aType == ModelAPI_AttributeRefList::typeId()) { + AttributeRefListPtr aRefListAttr = + std::dynamic_pointer_cast(anAttribute); + aRefListAttr->clear(); + } +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject, + GeomShapePtr theShape) +{ + AttributePtr anAttribute = myFeature->data()->attribute(attributeID()); + std::string aType = anAttribute->attributeType(); + if (aType == ModelAPI_AttributeSelectionList::typeId()) { + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(anAttribute); + ResultPtr aResult = std::dynamic_pointer_cast(theSelectedObject); + aSelectionListAttr->append(aResult, theShape, myIsInValidate); + } + else if (aType == ModelAPI_AttributeRefList::typeId()) { + AttributeRefListPtr aRefListAttr = + std::dynamic_pointer_cast(anAttribute); + aRefListAttr->append(theSelectedObject); + } +} + +//******************************************************************** +bool ModuleBase_WidgetMultiSelector::setSelection(QList& theValues, + const bool theToValidate) +{ + QList aSkippedValues; + + QList::const_iterator anIt = theValues.begin(), aLast = theValues.end(); + bool isDone = false; + for (; anIt != aLast; anIt++) { + ModuleBase_ViewerPrs aValue = *anIt; + bool aProcessed = false; + if (!theToValidate || isValidInFilters(aValue)) { + aProcessed = setSelectionCustom(aValue); } + else + aSkippedValues.append(aValue); + // if there is at least one set, the result is true + isDone = isDone || aProcessed; } + // updateObject - to update/redisplay feature + // it is commented in order to perfom it outside the method + //if (isDone) { + //updateObject(myFeature); + // this emit is necessary to call store/restore method an restore type of selection + //emit valuesChanged(); + //} + theValues.clear(); + if (!aSkippedValues.empty()) + theValues.append(aSkippedValues); + + return isDone; } //******************************************************************** -bool ModuleBase_WidgetMultiSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner) +void ModuleBase_WidgetMultiSelector::getHighlighted(QList& theValues) { - ModuleBase_ViewerPrs aPrs; - ModuleBase_ISelection* aSelection = myWorkshop->selection(); - aSelection->fillPresentation(aPrs, theOwner); + std::set anAttributeIds; + getSelectedAttributeIndices(anAttributeIds); + if (!anAttributeIds.empty()) + convertIndicesToViewerSelection(anAttributeIds, theValues); +} - const TopoDS_Shape& aTDSShape = aPrs.shape(); - if (aTDSShape.IsNull()) - return false; - GeomShapePtr aShape = std::shared_ptr(new GeomAPI_Shape()); - aShape->setImpl(new TopoDS_Shape(aTDSShape)); - - ObjectPtr anObject = aSelection->getSelectableObject(theOwner); - ResultPtr aResult = std::dynamic_pointer_cast(anObject); - if (myFeature) { - // We can not select a result of our feature - const std::list& aResList = myFeature->results(); - std::list::const_iterator aIt; - bool isSkipSelf = false; - for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) { - if ((*aIt) == aResult) { - isSkipSelf = true; - break; +//******************************************************************** +bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs) +{ + bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs); + if (aValid) { + ResultPtr aResult = myWorkshop->selection()->getResult(thePrs); + aValid = aResult.get() != NULL; + if (aValid) { + if (myFeature) { + // We can not select a result of our feature + const std::list& aResList = myFeature->results(); + std::list::const_iterator aIt; + bool isSkipSelf = false; + for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) { + if ((*aIt) == aResult) { + isSkipSelf = true; + break; + } + } + if (isSkipSelf) + aValid = false; } } - if(isSkipSelf) - return false; } + return aValid; +} - // if the result has the similar shap as the parameter shape, just the context is set to the - // selection list attribute. - DataPtr aData = myFeature->data(); - AttributeSelectionListPtr aSelectionListAttr = - std::dynamic_pointer_cast(aData->attribute(attributeID())); - if (aShape->isEqual(aResult->shape())) - aSelectionListAttr->append(aResult, NULL); - else - aSelectionListAttr->append(aResult, aShape); +//******************************************************************** +bool ModuleBase_WidgetMultiSelector::processDelete() +{ + // find attribute indices to delete + std::set anAttributeIds; + getSelectedAttributeIndices(anAttributeIds); + + // refill attribute by the items which indices are not in the list of ids + bool aDone = false; + AttributePtr anAttribute = myFeature->data()->attribute(attributeID()); + std::string aType = anAttribute->attributeType(); + if (aType == ModelAPI_AttributeSelectionList::typeId()) { + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(anAttribute); + aDone = !anAttributeIds.empty(); + aSelectionListAttr->remove(anAttributeIds); - return true; + } + else if (aType == ModelAPI_AttributeRefList::typeId()) { + AttributeRefListPtr aRefListAttr = + std::dynamic_pointer_cast(anAttribute); + aDone = !anAttributeIds.empty(); + aRefListAttr->remove(anAttributeIds); + } + if (aDone) { + // update object is necessary to flush update signal. It leads to objects references map update + // and the operation presentation will not contain deleted items visualized as parameters of + // the feature. + updateObject(myFeature); + + restoreValue(); + myWorkshop->setSelected(getAttributeSelection()); + } + return aDone; } //******************************************************************** @@ -240,45 +412,47 @@ QList ModuleBase_WidgetMultiSelector::getControls() const } //******************************************************************** -bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent) +void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged() { - //TODO: Remove maybe? - return ModuleBase_ModelWidget::eventFilter(theObj, theEvent); + activateSelectionAndFilters(true); + QList anEmptyList; + // This method will call Selection changed event which will call onSelectionChanged + // To clear mySelection, myListControl and storeValue() + // So, we don't need to call it + myWorkshop->setSelected(anEmptyList); +} + +void ModuleBase_WidgetMultiSelector::updateFocus() +{ + // Set focus to List control in order to make possible + // to use Tab key for transfer the focus to next widgets + myListControl->setCurrentRow(myListControl->model()->rowCount() - 1); + ModuleBase_Tools::setFocus(myListControl, + "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()"); } //******************************************************************** -void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged() +void ModuleBase_WidgetMultiSelector::updateSelectionName() { - activateShapeSelection(); - QObjectPtrList anEmptyList; - myWorkshop->setSelected(anEmptyList); - // Clear mySelection, myListControl and storeValue() - onSelectionChanged(); } //******************************************************************** -void ModuleBase_WidgetMultiSelector::onSelectionChanged() +QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const { - QList aSelected = myWorkshop->selection()->getSelected(); - - DataPtr aData = myFeature->data(); - AttributeSelectionListPtr aSelectionListAttr = - std::dynamic_pointer_cast(aData->attribute(attributeID())); - - aSelectionListAttr->clear(); - if (aSelected.size() > 0) { - foreach (ModuleBase_ViewerPrs aPrs, aSelected) { - Handle(SelectMgr_EntityOwner) anOwner = aPrs.owner(); - if (isValid(anOwner)) { - setSelection(anOwner); - } + QIntList aShapeTypes; + + if (myTypeCombo->count() > 1 && myIsUseChoice) { + aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText())); + } + else { + for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) { + TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)); + aShapeTypes.append(aType); + if (aType == TopAbs_SOLID) + aShapeTypes.append(TopAbs_COMPSOLID); } } - // the updateObject method should be called to flush the updated sigal. The workshop listens it, - // calls validators for the feature and, as a result, updates the Apply button state. - updateObject(myFeature); - - emit valuesChanged(); + return aShapeTypes; } //******************************************************************** @@ -288,56 +462,76 @@ void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum for (int idx = 0; idx < myTypeCombo->count(); ++idx) { aShapeTypeName = myTypeCombo->itemText(idx); - TopAbs_ShapeEnum aRefType = ModuleBase_WidgetShapeSelector::shapeType(aShapeTypeName); + TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName); if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) { - myIsActive = false; - activateShapeSelection(); + activateSelectionAndFilters(false); bool isBlocked = myTypeCombo->blockSignals(true); myTypeCombo->setCurrentIndex(idx); - myIsActive = true; myTypeCombo->blockSignals(isBlocked); - activateShapeSelection(); + + activateSelectionAndFilters(true); break; } } } -void ModuleBase_WidgetMultiSelector::activateShapeSelection() +QList ModuleBase_WidgetMultiSelector::getAttributeSelection() const { - ModuleBase_IViewer* aViewer = myWorkshop->viewer(); - - if (myIsActive) { - QString aNewType = myTypeCombo->currentText(); - QIntList aList; - aList.append(ModuleBase_WidgetShapeSelector::shapeType(aNewType)); - myWorkshop->activateSubShapesSelection(aList); - - // it is necessary to filter the selected edges to be non-degenerated - // it is not possible to build naming name for such edges - if (aNewType == "Edges") { - myEdgesTypeFilter = new ModuleBase_FilterNoDegeneratedEdge(); - aViewer->addSelectionFilter(myEdgesTypeFilter); + QList aSelected; + convertIndicesToViewerSelection(std::set(), aSelected); + return aSelected; +} + +//******************************************************************** +void ModuleBase_WidgetMultiSelector::updateSelectionList() +{ + myListControl->clear(); + + AttributePtr anAttribute = myFeature->data()->attribute(attributeID()); + std::string aType = anAttribute->attributeType(); + if (aType == ModelAPI_AttributeSelectionList::typeId()) { + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(anAttribute); + for (int i = 0; i < aSelectionListAttr->size(); i++) { + AttributeSelectionPtr aAttr = aSelectionListAttr->value(i); + QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl); + anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i); + myListControl->addItem(anItem); } - else { - aViewer->removeSelectionFilter(myEdgesTypeFilter); + } + else if (aType == ModelAPI_AttributeRefList::typeId()) { + AttributeRefListPtr aRefListAttr = + std::dynamic_pointer_cast(anAttribute); + for (int i = 0; i < aRefListAttr->size(); i++) { + ObjectPtr anObject = aRefListAttr->object(i); + if (anObject.get()) { + QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(), + myListControl); + anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i); + myListControl->addItem(anItem); + } } - - } else { - myWorkshop->deactivateSubShapesSelection(); - aViewer->removeSelectionFilter(myEdgesTypeFilter); } - activateFilters(myWorkshop, myIsActive); + // We have to call repaint because sometimes the List control is not updated + myListControl->repaint(); } //******************************************************************** -void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList) +std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const { - myListControl->clear(); - for (int i = 0; i < theList->size(); i++) { - AttributeSelectionPtr aAttr = theList->value(i); - myListControl->addItem(aAttr->namingName().c_str()); - } + std::string aType; + + if (theType == "Vertices") + aType = "vertex"; + else if (theType == "Edges") + aType = "edge"; + else if (theType == "Faces") + aType = "face"; + else if (theType == "Solids") + aType = "solid"; + + return aType; } //******************************************************************** @@ -356,10 +550,72 @@ void ModuleBase_WidgetMultiSelector::onCopyItem() } } +//******************************************************************** +void ModuleBase_WidgetMultiSelector::onDeleteItem() +{ + processDelete(); +} + //******************************************************************** void ModuleBase_WidgetMultiSelector::onListSelection() { QList aItems = myListControl->selectedItems(); myCopyAction->setEnabled(!aItems.isEmpty()); + myDeleteAction->setEnabled(!aItems.isEmpty()); + + myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects, + true); } +//******************************************************************** +void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set& theAttributeIds) +{ + QList aItems = myListControl->selectedItems(); + foreach(QListWidgetItem* anItem, aItems) { + int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt(); + if (theAttributeIds.find(anIndex) == theAttributeIds.end()) + theAttributeIds.insert(anIndex); + } +} + +void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set theAttributeIds, + QList& theValues) const +{ + if(myFeature.get() == NULL) + return; + + AttributePtr anAttribute = myFeature->data()->attribute(attributeID()); + std::string aType = anAttribute->attributeType(); + if (aType == ModelAPI_AttributeSelectionList::typeId()) { + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(anAttribute); + for (int i = 0; i < aSelectionListAttr->size(); i++) { + // filter by attribute indices only if the container is not empty otherwise return all items + if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end()) + continue; + AttributeSelectionPtr anAttr = aSelectionListAttr->value(i); + ResultPtr anObject = anAttr->context(); + if (anObject.get()) { + TopoDS_Shape aShape; + std::shared_ptr aShapePtr = anAttr->value(); + if (aShapePtr.get()) { + aShape = aShapePtr->impl(); + } + theValues.append(ModuleBase_ViewerPrs(anObject, aShape, NULL)); + } + } + } + else if (aType == ModelAPI_AttributeRefList::typeId()) { + AttributeRefListPtr aRefListAttr = + std::dynamic_pointer_cast(anAttribute); + for (int i = 0; i < aRefListAttr->size(); i++) { + // filter by attribute indices only if the container is not empty otherwise return all items + if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end()) + continue; + ObjectPtr anObject = aRefListAttr->object(i); + if (anObject.get()) { + theValues.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL)); + } + } + } +}