From c506c6e7f301201b56d0c3db2b255a629f923cb3 Mon Sep 17 00:00:00 2001 From: vsv Date: Thu, 23 Oct 2014 15:47:52 +0400 Subject: [PATCH] Using AttributeSelection for Groups. --- src/FeaturesPlugin/FeaturesPlugin_Group.cpp | 4 +- src/GeomAPI/GeomAPI_Shape.h | 3 + src/ModelAPI/ModelAPI_AttributeSelection.h | 3 + .../ModelAPI_AttributeSelectionList.h | 3 + src/ModuleBase/ModuleBase_ISelection.h | 3 +- .../ModuleBase_WidgetMultiSelector.cpp | 83 ++++++++++++++----- .../ModuleBase_WidgetMultiSelector.h | 13 ++- .../ModuleBase_WidgetShapeSelector.cpp | 3 +- src/XGUI/XGUI_Selection.cpp | 11 ++- src/XGUI/XGUI_Selection.h | 3 +- 10 files changed, 99 insertions(+), 30 deletions(-) diff --git a/src/FeaturesPlugin/FeaturesPlugin_Group.cpp b/src/FeaturesPlugin/FeaturesPlugin_Group.cpp index 4a5a34bc2..ae5d82771 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Group.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Group.cpp @@ -8,6 +8,8 @@ #include #include #include +#include + using namespace std; @@ -18,7 +20,7 @@ FeaturesPlugin_Group::FeaturesPlugin_Group() void FeaturesPlugin_Group::initAttributes() { data()->addAttribute(FeaturesPlugin_Group::NAME_ID(), ModelAPI_AttributeString::type()); - data()->addAttribute(FeaturesPlugin_Group::LIST_ID(), ModelAPI_AttributeString::type()); + data()->addAttribute(FeaturesPlugin_Group::LIST_ID(), ModelAPI_AttributeSelectionList::type()); } void FeaturesPlugin_Group::execute() diff --git a/src/GeomAPI/GeomAPI_Shape.h b/src/GeomAPI/GeomAPI_Shape.h index 3aa0987f7..96db71456 100644 --- a/src/GeomAPI/GeomAPI_Shape.h +++ b/src/GeomAPI/GeomAPI_Shape.h @@ -31,4 +31,7 @@ class GEOMAPI_EXPORT GeomAPI_Shape : public GeomAPI_Interface }; +//! Pointer on attribute object +typedef boost::shared_ptr GeomShapePtr; + #endif diff --git a/src/ModelAPI/ModelAPI_AttributeSelection.h b/src/ModelAPI/ModelAPI_AttributeSelection.h index 98d4af7d6..08f5f6e5d 100644 --- a/src/ModelAPI/ModelAPI_AttributeSelection.h +++ b/src/ModelAPI/ModelAPI_AttributeSelection.h @@ -50,4 +50,7 @@ class ModelAPI_AttributeSelection : public ModelAPI_Attribute } }; +//! Pointer on double attribute +typedef boost::shared_ptr AttributeSelectionPtr; + #endif diff --git a/src/ModelAPI/ModelAPI_AttributeSelectionList.h b/src/ModelAPI/ModelAPI_AttributeSelectionList.h index 4305471d3..6dc13dd3a 100644 --- a/src/ModelAPI/ModelAPI_AttributeSelectionList.h +++ b/src/ModelAPI/ModelAPI_AttributeSelectionList.h @@ -54,4 +54,7 @@ class ModelAPI_AttributeSelectionList : public ModelAPI_Attribute } }; +//! Pointer on double attribute +typedef boost::shared_ptr AttributeSelectionListPtr; + #endif diff --git a/src/ModuleBase/ModuleBase_ISelection.h b/src/ModuleBase/ModuleBase_ISelection.h index 43bd0ea86..72845616b 100644 --- a/src/ModuleBase/ModuleBase_ISelection.h +++ b/src/ModuleBase/ModuleBase_ISelection.h @@ -47,7 +47,8 @@ class ModuleBase_ISelection virtual void selectedAISObjects(AIS_ListOfInteractive& theList) const = 0; //! Returns list of currently selected shapes - virtual void selectedShapes(NCollection_List& theList) const = 0; + virtual void selectedShapes(NCollection_List& theList, + std::list& theOwners) const = 0; }; diff --git a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp index 189b659c4..563cc1878 100644 --- a/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetMultiSelector.cpp @@ -15,12 +15,13 @@ #include #include #include +#include #include #include #include -#include +#include #include #include #include @@ -39,17 +40,20 @@ ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParen myMainWidget = new QWidget(theParent); QGridLayout* aMainLay = new QGridLayout(myMainWidget); ModuleBase_Tools::adjustMargins(aMainLay); + QLabel* aTypeLabel = new QLabel(tr("Type"), myMainWidget); aMainLay->addWidget(aTypeLabel, 0, 0); + myTypeCombo = new QComboBox(myMainWidget); std::string aTypes = theData->getProperty("type_choice"); myShapeTypes = QString::fromStdString(aTypes).split(' '); myTypeCombo->addItems(myShapeTypes); aMainLay->addWidget(myTypeCombo, 0, 1); + QLabel* aListLabel = new QLabel(tr("Selected objects:"), myMainWidget); aMainLay->addWidget(aListLabel, 1, 0, 1, -1); - myListControl = new QTextEdit(myMainWidget); - myListControl->setReadOnly(true); + + myListControl = new QListWidget(myMainWidget); aMainLay->addWidget(myListControl, 2, 0, 2, -1); aMainLay->setColumnStretch(1, 1); myMainWidget->setLayout(aMainLay); @@ -72,11 +76,18 @@ bool ModuleBase_WidgetMultiSelector::storeValue() const if(!myFeature) return false; DataPtr aData = myFeature->data(); - AttributeStringPtr aStringAttr = aData->string(attributeID()); - QString aWidgetValue = myListControl->toPlainText(); - aStringAttr->setValue(aWidgetValue.toStdString()); - updateObject(myFeature); - return true; + AttributeSelectionListPtr aSelectionListAttr = + boost::dynamic_pointer_cast(aData->attribute(attributeID())); + + if (aSelectionListAttr && (mySelection.size() > 0)) { + aSelectionListAttr->clear(); + foreach (GeomSelection aSelec, mySelection) { + aSelectionListAttr->append(aSelec.first, aSelec.second); + } + updateObject(myFeature); + return true; + } + return false; } bool ModuleBase_WidgetMultiSelector::restoreValue() @@ -86,13 +97,19 @@ bool ModuleBase_WidgetMultiSelector::restoreValue() if(!myFeature) return false; DataPtr aData = myFeature->data(); - AttributeStringPtr aStringAttr = aData->string(attributeID()); - - bool isBlocked = myListControl->blockSignals(true); - myListControl->setText(QString::fromStdString(aStringAttr->value())); - myListControl->blockSignals(isBlocked); - - return true; + AttributeSelectionListPtr aSelectionListAttr = + boost::dynamic_pointer_cast(aData->attribute(attributeID())); + + if (aSelectionListAttr) { + mySelection.clear(); + for (int i = 0; i < aSelectionListAttr->size(); i++) { + AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i); + mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value())); + } + updateSelectionList(); + return true; + } + return false; } QWidget* ModuleBase_WidgetMultiSelector::getControl() const @@ -121,16 +138,38 @@ bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEve void ModuleBase_WidgetMultiSelector::onSelectionChanged() { ModuleBase_ISelection* aSelection = myWorkshop->selection(); - NCollection_List aSelectedShapes, aFilteredShapes; - aSelection->selectedShapes(aSelectedShapes); - QString aText; - if (!aSelectedShapes.IsEmpty()) { - filterShapes(aSelectedShapes, aFilteredShapes); - aText = QString("Items selected: %1").arg(aFilteredShapes.Size()); + NCollection_List aSelectedShapes; //, aFilteredShapes; + std::list aOwnersList; + aSelection->selectedShapes(aSelectedShapes, aOwnersList); + + mySelection.clear(); + std::list::const_iterator aIt; + NCollection_List::Iterator aShpIt(aSelectedShapes); + GeomShapePtr aShape; + for (aIt = aOwnersList.cbegin(); aIt != aOwnersList.cend(); aShpIt.Next(), aIt++) { + ResultPtr aResult = boost::dynamic_pointer_cast(*aIt); + aShape = boost::shared_ptr(new GeomAPI_Shape()); + aShape->setImpl(new TopoDS_Shape(aShpIt.Value())); + mySelection.append(GeomSelection(aResult, aShape)); + } + updateSelectionList(); + emit valuesChanged(); +} + + +void ModuleBase_WidgetMultiSelector::updateSelectionList() +{ + myListControl->clear(); + int i = 1; + foreach (GeomSelection aSel, mySelection) { + QString aName(aSel.first->data()->name().c_str()); + aName += ":" + myTypeCombo->currentText() + QString::number(i); + myListControl->addItem(aName); + i++; } - myListControl->setText(aText); } + void ModuleBase_WidgetMultiSelector::filterShapes(const NCollection_List& theShapesToFilter, NCollection_List& theResult) { diff --git a/src/ModuleBase/ModuleBase_WidgetMultiSelector.h b/src/ModuleBase/ModuleBase_WidgetMultiSelector.h index bc184d5e5..c141c87e9 100644 --- a/src/ModuleBase/ModuleBase_WidgetMultiSelector.h +++ b/src/ModuleBase/ModuleBase_WidgetMultiSelector.h @@ -11,15 +11,19 @@ #include #include +#include +#include + #include #include #include #include #include +#include class QWidget; -class QTextEdit; +class QListWidget; class QComboBox; class ModuleBase_IWorkshop; @@ -59,7 +63,9 @@ class MODULEBASE_EXPORT ModuleBase_WidgetMultiSelector : public ModuleBase_Model NCollection_List& theResult); private: - QTextEdit* myListControl; + void updateSelectionList(); + + QListWidget* myListControl; QComboBox* myTypeCombo; QWidget* myMainWidget; @@ -70,6 +76,9 @@ class MODULEBASE_EXPORT ModuleBase_WidgetMultiSelector : public ModuleBase_Model QStringList myShapeTypes; bool myUseSubShapes; bool myIsActive; + + typedef QPair GeomSelection; + QList mySelection; }; #endif /* MODULEBASE_WIDGETFILESELECTOR_H_ */ diff --git a/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp b/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp index d68309a4f..470450070 100644 --- a/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp @@ -194,7 +194,8 @@ void ModuleBase_WidgetShapeSelector::onSelectionChanged() boost::shared_ptr aShape; if (myUseSubShapes) { NCollection_List aShapeList; - myWorkshop->selection()->selectedShapes(aShapeList); + std::list aOwners; + myWorkshop->selection()->selectedShapes(aShapeList, aOwners); if (aShapeList.Extent() > 0) { aShape = boost::shared_ptr(new GeomAPI_Shape()); aShape->setImpl(new TopoDS_Shape(aShapeList.First())); diff --git a/src/XGUI/XGUI_Selection.cpp b/src/XGUI/XGUI_Selection.cpp index d39e31359..1b91cb3dc 100644 --- a/src/XGUI/XGUI_Selection.cpp +++ b/src/XGUI/XGUI_Selection.cpp @@ -111,13 +111,20 @@ void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const } //************************************************************** -void XGUI_Selection::selectedShapes(NCollection_List& theList) const +void XGUI_Selection::selectedShapes(NCollection_List& theList, + std::list& theOwners) const { theList.Clear(); Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext(); for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) { TopoDS_Shape aShape = aContext->SelectedShape(); - if (!aShape.IsNull()) + if (!aShape.IsNull()) { theList.Append(aShape); + Handle(SelectMgr_EntityOwner) aEO = aContext->SelectedOwner(); + Handle(AIS_InteractiveObject) anObj = + Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable()); + ObjectPtr anObject = myWorkshop->displayer()->getObject(anObj); + theOwners.push_back(anObject); + } } } diff --git a/src/XGUI/XGUI_Selection.h b/src/XGUI/XGUI_Selection.h index b67adbc52..5f399a7ee 100644 --- a/src/XGUI/XGUI_Selection.h +++ b/src/XGUI/XGUI_Selection.h @@ -52,7 +52,8 @@ class XGUI_EXPORT XGUI_Selection : public ModuleBase_ISelection virtual void selectedAISObjects(AIS_ListOfInteractive& theList) const; //! Returns list of currently selected shapes - virtual void selectedShapes(NCollection_List& theList) const; + virtual void selectedShapes(NCollection_List& theShapes, + std::list& theOwners) const; private: XGUI_Workshop* myWorkshop; -- 2.39.2