From dbf43e581cd94f420a395f7347926a26218451a3 Mon Sep 17 00:00:00 2001 From: nds Date: Mon, 23 Mar 2015 08:40:42 +0300 Subject: [PATCH] Union of validator and filter functionalities. Validators using for the multi-selection control. It uses the cashed objects mySeleciton for isValid functionality. The cash between onSelectionChanged and storeCustom is not used anumore. --- src/FeaturesPlugin/extrusion_widget.xml | 2 +- src/ModuleBase/ModuleBase_ISelection.h | 3 + .../ModuleBase_WidgetMultiSelector.cpp | 141 ++++++++++-------- .../ModuleBase_WidgetMultiSelector.h | 12 +- src/PartSet/PartSet_Module.cpp | 19 ++- src/PartSet/PartSet_Validators.cpp | 25 ++++ src/PartSet/PartSet_Validators.h | 14 ++ src/XGUI/XGUI_Selection.cpp | 14 ++ src/XGUI/XGUI_Selection.h | 9 ++ 9 files changed, 165 insertions(+), 74 deletions(-) diff --git a/src/FeaturesPlugin/extrusion_widget.xml b/src/FeaturesPlugin/extrusion_widget.xml index d723657a9..1109883fb 100644 --- a/src/FeaturesPlugin/extrusion_widget.xml +++ b/src/FeaturesPlugin/extrusion_widget.xml @@ -6,7 +6,7 @@ icon=":icons/sketch.png" tooltip="Select a sketch face" type_choice="Faces"> - + data(); - AttributeSelectionListPtr aSelectionListAttr = - std::dynamic_pointer_cast(aData->attribute(attributeID())); - - if (aSelectionListAttr) { - aSelectionListAttr->clear(); - // Store shapes type - TopAbs_ShapeEnum aCurrentType = - ModuleBase_WidgetShapeSelector::shapeType(myTypeCombo->currentText()); - aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString()); - // Store selection in the attribute - foreach (GeomSelection aSelec, mySelection) { - aSelectionListAttr->append(aSelec.first, aSelec.second); - } - //updateSelectionList(aSelectionListAttr); - updateObject(myFeature); - return true; - } - return false; + // the value is stored on the selection changed signal processing + return true; } //******************************************************************** @@ -156,15 +136,9 @@ bool ModuleBase_WidgetMultiSelector::restoreValue() std::dynamic_pointer_cast(aData->attribute(attributeID())); if (aSelectionListAttr) { - mySelection.clear(); // Restore shape type setCurrentShapeType( ModuleBase_WidgetShapeSelector::shapeType(aSelectionListAttr->selectionType().c_str())); - // Restore selection in the list - for (int i = 0; i < aSelectionListAttr->size(); i++) { - AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i); - mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value())); - } updateSelectionList(aSelectionListAttr); return true; } @@ -174,13 +148,73 @@ bool ModuleBase_WidgetMultiSelector::restoreValue() //******************************************************************** void ModuleBase_WidgetMultiSelector::backupAttributeValue(const bool isBackup) { + DataPtr aData = myFeature->data(); + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(aData->attribute(attributeID())); + if (aSelectionListAttr.get() == NULL) + return; + + if (isBackup) { + 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())); + } + } + else { + aSelectionListAttr->clear(); + // Store shapes type + aSelectionListAttr->setSelectionType(mySelectionType); + // Store selection in the attribute + foreach (GeomSelection aSelec, mySelection) { + aSelectionListAttr->append(aSelec.first, aSelec.second); + } + } } //******************************************************************** bool ModuleBase_WidgetMultiSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner) { - return false; + ModuleBase_ViewerPrs aPrs; + ModuleBase_ISelection* aSelection = myWorkshop->selection(); + aSelection->fillPresentation(aPrs, theOwner); + + 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; + } + } + if(isSkipSelf) + return false; + } + + // 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); + + return true; } //******************************************************************** @@ -212,42 +246,25 @@ void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged() //******************************************************************** void ModuleBase_WidgetMultiSelector::onSelectionChanged() { - ModuleBase_ISelection* aSelection = myWorkshop->selection(); - NCollection_List aSelectedShapes; - 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 = std::dynamic_pointer_cast(*aIt); - 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; - } + 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); } - if(isSkipSelf) - continue; } - aShape = std::shared_ptr(new GeomAPI_Shape()); - aShape->setImpl(new TopoDS_Shape(aShpIt.Value())); - - // if the result has the similar shap as the parameter shape, just the context is set to the - // selection list attribute. - if (aShape->isEqual(aResult->shape())) - mySelection.append(GeomSelection(aResult, NULL)); - else - mySelection.append(GeomSelection(aResult, aShape)); } - //updateSelectionList(); + // 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(); } diff --git a/src/ModuleBase/ModuleBase_WidgetMultiSelector.h b/src/ModuleBase/ModuleBase_WidgetMultiSelector.h index 385eabf3f..ce904a981 100644 --- a/src/ModuleBase/ModuleBase_WidgetMultiSelector.h +++ b/src/ModuleBase/ModuleBase_WidgetMultiSelector.h @@ -131,14 +131,20 @@ protected slots: /// Provides correspondance between Result object and its shape typedef QPair GeomSelection; - /// Variable of GeomSelection type - QList mySelection; - /// An action for pop-up menu in a list control QAction* myCopyAction; /// A filter for the Edges type, which avoid the generated edges selection Handle(ModuleBase_FilterNoDegeneratedEdge) myEdgesTypeFilter; + + /// backup parameters of the model attribute. The class processes three types of attribute: + /// Reference, RefAttr and Selection. Depending on the attribute type, only the attribute parameter + /// values are reserved in the backup + /// Variable of selection type + std::string mySelectionType; + + /// Variable of GeomSelection + QList mySelection; }; #endif /* MODULEBASE_WIDGETFILESELECTOR_H_ */ diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index 79d5b9664..6299ff04a 100644 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -139,6 +139,9 @@ void PartSet_Module::registerValidators() aFactory->registerValidator("ModuleBase_ValidatorNoConstructionSubShapes", new ModuleBase_ValidatorNoConstructionSubShapes); + + aFactory->registerValidator("PartSet_SketchEntityValidator", + new PartSet_SketchEntityValidator); } void PartSet_Module::registerFilters() @@ -147,14 +150,14 @@ void PartSet_Module::registerFilters() XGUI_ModuleConnector* aConnector = dynamic_cast(workshop()); ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters(); - aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge); - aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace); - aFactory->registerFilter("MultiFilter", new ModuleBase_FilterMulti); - Handle(SelectMgr_Filter) aSelectFilter = new ModuleBase_FilterNoConsructionSubShapes(workshop()); - aFactory->registerFilter("NoConstructionSubShapesFilter", - new ModuleBase_FilterCustom(aSelectFilter)); - aSelectFilter = new PartSet_FilterSketchEntity(workshop()); - aFactory->registerFilter("SketchEntityFilter", new ModuleBase_FilterCustom(aSelectFilter)); + //aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge); + //aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace); + //aFactory->registerFilter("MultiFilter", new ModuleBase_FilterMulti); + //Handle(SelectMgr_Filter) aSelectFilter = new ModuleBase_FilterNoConsructionSubShapes(workshop()); + //aFactory->registerFilter("NoConstructionSubShapesFilter", + // new ModuleBase_FilterCustom(aSelectFilter)); + //Handle(SelectMgr_Filter) aSelectFilter = new PartSet_FilterSketchEntity(workshop()); + //aFactory->registerFilter("SketchEntityFilter", new ModuleBase_FilterCustom(aSelectFilter)); } void PartSet_Module::registerProperties() diff --git a/src/PartSet/PartSet_Validators.cpp b/src/PartSet/PartSet_Validators.cpp index 7ac0693b5..305647650 100644 --- a/src/PartSet/PartSet_Validators.cpp +++ b/src/PartSet/PartSet_Validators.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -188,6 +189,30 @@ bool PartSet_DifferentObjectsValidator::featureHasReferences(const AttributePtr& return true; } +bool PartSet_SketchEntityValidator::isValid(const AttributePtr& theAttribute, + const std::list& theArguments) const +{ + AttributeSelectionListPtr aSelectionListAttr = + std::dynamic_pointer_cast(theAttribute); + + // it filters only selection list attributes + if (aSelectionListAttr.get() == NULL) + return true; + + std::string aType = aSelectionListAttr->selectionType().c_str(); + + // all context objects should be sketch entities + bool isSketchEntities = true; + int aSize = aSelectionListAttr->size(); + for (int i = 0; i < aSelectionListAttr->size(); i++) { + AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i); + ObjectPtr anObject = aSelectAttr->context(); + FeaturePtr aFeature = ModelAPI_Feature::feature(anObject); + isSketchEntities = aFeature->getKind() == SketchPlugin_Sketch::ID(); + } + return isSketchEntities; +} + bool PartSet_SketchValidator::isValid(const ObjectPtr theObject) const { FeaturePtr aFeature = ModelAPI_Feature::feature(theObject); diff --git a/src/PartSet/PartSet_Validators.h b/src/PartSet/PartSet_Validators.h index 0dc5f4619..556c2ce06 100644 --- a/src/PartSet/PartSet_Validators.h +++ b/src/PartSet/PartSet_Validators.h @@ -91,6 +91,20 @@ protected: bool featureHasReferences(const AttributePtr& theAttribute) const; }; +/** +* \ingroup Validators +* A validator which checks that objects selected for feature attributes are different (not the same) +*/ +class PartSet_SketchEntityValidator : public ModelAPI_RefAttrValidator +{ + public: + //! Returns true if the attribute is good for the feature attribute + //! \param theAttribute an attribute + //! \param theArguments a list of arguments (names of attributes to check) + virtual bool isValid(const AttributePtr& theAttribute, + const std::list& theArguments) const; +}; + /** * \ingroup Validators * A Validator which validates tha selected object is a Sketch diff --git a/src/XGUI/XGUI_Selection.cpp b/src/XGUI/XGUI_Selection.cpp index 0de6255da..56eea5db5 100644 --- a/src/XGUI/XGUI_Selection.cpp +++ b/src/XGUI/XGUI_Selection.cpp @@ -162,6 +162,20 @@ void XGUI_Selection::selectedAISObjects(AIS_ListOfInteractive& theList) const theList.Append(aContext->SelectedInteractive()); } +//************************************************************** +ObjectPtr XGUI_Selection::getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const +{ + ObjectPtr anObject; + + Handle(SelectMgr_EntityOwner) aEO = theOwner; + if (!aEO.IsNull()) { + Handle(AIS_InteractiveObject) anObj = + Handle(AIS_InteractiveObject)::DownCast(aEO->Selectable()); + anObject = myWorkshop->displayer()->getObject(anObj); + } + return anObject; +} + //************************************************************** void XGUI_Selection::selectedShapes(NCollection_List& theList, std::list& theOwners) const diff --git a/src/XGUI/XGUI_Selection.h b/src/XGUI/XGUI_Selection.h index a782444c8..ea5886a8a 100644 --- a/src/XGUI/XGUI_Selection.h +++ b/src/XGUI/XGUI_Selection.h @@ -61,9 +61,18 @@ class XGUI_EXPORT XGUI_Selection : public ModuleBase_ISelection //! Returns list of currently selected QModelIndexes virtual QModelIndexList selectedIndexes() const; + //! Returns list of currently selected QModelIndexes + ObjectPtr getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner); + //! Returns list of currently selected AIS objects virtual void selectedAISObjects(AIS_ListOfInteractive& theList) const; + //! Return a selectable object by the entity owner. It founds AIS object in the viewer + //! and returns the corresponded object + /// \param theOwner an entity owner + /// \return a found object or NULL + ObjectPtr getSelectableObject(const Handle(SelectMgr_EntityOwner)& theOwner) const; + //! Returns list of currently selected shapes virtual void selectedShapes(NCollection_List& theShapes, std::list& theOwners) const; -- 2.39.2