From 911427b07b7a2d242ada46ba790d86e0218ea50a Mon Sep 17 00:00:00 2001 From: vsv Date: Fri, 5 Dec 2014 18:03:10 +0300 Subject: [PATCH] Issue #308: Avoid selection of the same object for placement --- src/FeaturesPlugin/placement_widget.xml | 15 ++-- .../ModuleBase_WidgetShapeSelector.cpp | 73 ++++++++++++++++++- .../ModuleBase_WidgetShapeSelector.h | 6 ++ src/PartSet/PartSet_Module.cpp | 1 + src/PartSet/PartSet_Validators.cpp | 55 ++++++++++++++ src/PartSet/PartSet_Validators.h | 15 ++++ 6 files changed, 156 insertions(+), 9 deletions(-) diff --git a/src/FeaturesPlugin/placement_widget.xml b/src/FeaturesPlugin/placement_widget.xml index e5f67055c..0da033573 100644 --- a/src/FeaturesPlugin/placement_widget.xml +++ b/src/FeaturesPlugin/placement_widget.xml @@ -7,11 +7,12 @@ use_subshapes="true" /> + label="Select a face" + icon=":icons/cut_shape.png" + tooltip="Select a face of another object" + shape_types="face" + use_subshapes="true" + concealment="true" > + + diff --git a/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp b/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp index 74294d9a4..0f3c87e48 100644 --- a/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp @@ -29,6 +29,9 @@ #include #include #include +#include +#include +#include #include #include @@ -175,6 +178,26 @@ bool ModuleBase_WidgetShapeSelector::storeValue() const return false; } +//******************************************************************** +void ModuleBase_WidgetShapeSelector::clearAttribute() +{ + DataPtr aData = myFeature->data(); + AttributeSelectionPtr aSelect = aData->selection(attributeID()); + if (aSelect) { + aSelect->setValue(ResultPtr(), std::shared_ptr(new GeomAPI_Shape())); + return; + } + AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID()); + if (aRefAttr) { + aRefAttr->setObject(ObjectPtr()); + return; + } + AttributeReferencePtr aRef = aData->reference(attributeID()); + if (aRef) { + aRef->setObject(ObjectPtr()); + } +} + //******************************************************************** bool ModuleBase_WidgetShapeSelector::restoreValue() { @@ -218,6 +241,9 @@ QList ModuleBase_WidgetShapeSelector::getControls() const //******************************************************************** void ModuleBase_WidgetShapeSelector::onSelectionChanged() { + // In order to make reselection possible + // TODO: check with MPV clearAttribute(); + QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations(); if (aObjects.size() > 0) { ObjectPtr aObject = aObjects.first(); @@ -272,8 +298,10 @@ void ModuleBase_WidgetShapeSelector::onSelectionChanged() if (!acceptObjectShape(aObject)) return; } - setObject(aObject, aShape); - emit focusOutWidget(this); + if (isValid(aObject, aShape)) { + setObject(aObject, aShape); + emit focusOutWidget(this); + } } } @@ -442,3 +470,44 @@ void ModuleBase_WidgetShapeSelector::deactivate() { activateSelection(false); } + +//******************************************************************** +bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr theShape) +{ + SessionPtr aMgr = ModelAPI_Session::get(); + ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); + std::list aValidators; + std::list > anArguments; + aFactory->validators(parentID(), attributeID(), aValidators, anArguments); + + // Check the type of selected object + std::list::iterator aValidator = aValidators.begin(); + bool isValid = true; + for (; aValidator != aValidators.end(); aValidator++) { + const ModelAPI_ResultValidator* aResValidator = + dynamic_cast(*aValidator); + if (aResValidator) { + isValid = false; + if (aResValidator->isValid(theObj)) { + isValid = true; + break; + } + } + } + if (!isValid) + return false; + + // Check the acceptability of the object as attribute + aValidator = aValidators.begin(); + std::list >::iterator aArgs = anArguments.begin(); + for (; aValidator != aValidators.end(); aValidator++, aArgs++) { + const ModelAPI_RefAttrValidator* aAttrValidator = + dynamic_cast(*aValidator); + if (aAttrValidator) { + if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) { + return false; + } + } + } + return true; +} \ No newline at end of file diff --git a/src/ModuleBase/ModuleBase_WidgetShapeSelector.h b/src/ModuleBase/ModuleBase_WidgetShapeSelector.h index 4cf0a4d2e..0e479e178 100644 --- a/src/ModuleBase/ModuleBase_WidgetShapeSelector.h +++ b/src/ModuleBase/ModuleBase_WidgetShapeSelector.h @@ -96,6 +96,12 @@ Q_OBJECT // Set the given object as a value of the widget void setObject(ObjectPtr theObj, std::shared_ptr theShape = std::shared_ptr()); + /// Check the selected with validators if installed + virtual bool isValid(ObjectPtr theObj, std::shared_ptr theShape); + + /// Clear attribute + void clearAttribute(); + //----------- Class members ------------- protected: QWidget* myContainer; diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index 630644826..f4c782654 100644 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -134,6 +134,7 @@ void PartSet_Module::registerValidators() aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator); aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator); aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator); + aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator); } diff --git a/src/PartSet/PartSet_Validators.cpp b/src/PartSet/PartSet_Validators.cpp index 7a976bda3..f35b3ae05 100644 --- a/src/PartSet/PartSet_Validators.cpp +++ b/src/PartSet/PartSet_Validators.cpp @@ -11,6 +11,9 @@ #include #include +#include +#include + #include int shapesNbPoints(const ModuleBase_ISelection* theSelection) @@ -92,3 +95,55 @@ bool PartSet_RadiusValidator::isValid(const ModuleBase_ISelection* theSelection) return (aCount > 0) && (aCount < 2); } + + +bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature, + const std::list& theArguments, + const ObjectPtr& theObject) const +{ + // Check RefAttr attributes + std::list > anAttrs = + theFeature->data()->attributes(ModelAPI_AttributeRefAttr::type()); + if (anAttrs.size() > 0) { + std::list >::iterator anAttr = anAttrs.begin(); + for(; anAttr != anAttrs.end(); anAttr++) { + if (*anAttr) { + std::shared_ptr aRef = + std::dynamic_pointer_cast(*anAttr); + // check the object is already presented + if (aRef->isObject() && aRef->object() == theObject) + return false; + } + } + } + // Check selection attributes + anAttrs = theFeature->data()->attributes(ModelAPI_AttributeSelection::type()); + if (anAttrs.size() > 0) { + std::list >::iterator anAttr = anAttrs.begin(); + for(; anAttr != anAttrs.end(); anAttr++) { + if (*anAttr) { + std::shared_ptr aRef = + std::dynamic_pointer_cast(*anAttr); + // check the object is already presented + if (aRef->isInitialized() && aRef->context() == theObject) + return false; + } + } + } + return true; +} + +bool PartSet_DifferentObjectsValidator::isValid(const FeaturePtr& theFeature, + const std::list& theArguments, + const AttributePtr& theAttribute) const +{ + // not implemented + return true; +} + +bool PartSet_DifferentObjectsValidator::isValid(const AttributePtr& theAttribute, + const std::list& theArguments) const +{ + // not implemented + return true; +} \ No newline at end of file diff --git a/src/PartSet/PartSet_Validators.h b/src/PartSet/PartSet_Validators.h index 02f7d21e6..33deefd50 100644 --- a/src/PartSet/PartSet_Validators.h +++ b/src/PartSet/PartSet_Validators.h @@ -9,6 +9,7 @@ #include #include +#include /* * Selector validators @@ -49,4 +50,18 @@ class PartSet_RadiusValidator : public ModuleBase_SelectionValidator PARTSET_EXPORT virtual bool isValid(const ModuleBase_ISelection* theSelection) const; }; +class PartSet_DifferentObjectsValidator : public ModelAPI_RefAttrValidator +{ + public: + virtual bool isValid(const FeaturePtr& theFeature, const std::list& theArguments, + const ObjectPtr& theObject) const; + //! Returns true if the attribute is good for the feature attribute + virtual bool isValid(const FeaturePtr& theFeature, const std::list& theArguments, + const AttributePtr& theAttribute) const; + + virtual bool isValid(const AttributePtr& theAttribute, + const std::list& theArguments) const; +}; + + #endif -- 2.39.2