From 70a245439d9148ebde66aa98a00fcb7b01fb92bd Mon Sep 17 00:00:00 2001 From: spo Date: Fri, 10 Jul 2015 14:02:30 +0300 Subject: [PATCH] Issue #761 - Check and error for unique parameter name --- .../ParametersPlugin_EvalListener.cpp | 35 ++++++++++--------- .../ParametersPlugin_Validators.cpp | 23 +++++++++++- .../ParametersPlugin_Validators.h | 3 +- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp index 841322398..3e626d373 100644 --- a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp +++ b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp @@ -286,12 +286,20 @@ bool isValidAttribute(const AttributePtr& theAttribute) return true; } -void setParameterName(std::shared_ptr theParameter, const std::string& theName) +void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName) { - theParameter->data()->blockSendAttributeUpdated(true); - theParameter->data()->setName(theName); - theParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(theName); - theParameter->data()->blockSendAttributeUpdated(false); + theResultParameter->data()->blockSendAttributeUpdated(true); + theResultParameter->data()->setName(theName); + theResultParameter->data()->blockSendAttributeUpdated(false); + + std::shared_ptr aParameter = + std::dynamic_pointer_cast( + ModelAPI_Feature::feature(theResultParameter)); + + aParameter->data()->blockSendAttributeUpdated(true); + aParameter->data()->setName(theName); + aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(theName); + aParameter->data()->blockSendAttributeUpdated(false); } void ParametersPlugin_EvalListener::processObjectRenamedEvent( @@ -303,31 +311,26 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( if (!aMessage.get() || aMessage->oldName().empty() || aMessage->newName().empty()) return; - // check that the renamed object is a result + // check if the renamed object is a result perameter ResultParameterPtr aResultParameter = std::dynamic_pointer_cast(aMessage->object()); if (!aResultParameter.get()) return; // get parameter feature for the result - FeaturePtr aFeature = aResultParameter->document()->feature(aResultParameter); std::shared_ptr aParameter = - std::dynamic_pointer_cast(aFeature); + std::dynamic_pointer_cast( + ModelAPI_Feature::feature(aResultParameter)); if (!aParameter.get()) return; - // rename a parameter attributes - // short way: - //aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(aMessage->newName()); - //aParameter->execute(); - // manual way: - setParameterName(aParameter, aMessage->newName()); - + // try to update the parameter feature according the new name + setParameterName(aResultParameter, aMessage->newName()); // TODO(spo): replace with ModelAPI_Session::get()->validators()->validate(aParameter, ParametersPlugin_Parameter::VARIABLE_ID()) // when ModelAPI_ValidatorsFactory::validate(const std::shared_ptr& theFeature, const std::string& theAttribute) const // is ready if (!isValidAttribute(aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID()))) { - setParameterName(aParameter, aMessage->oldName()); + setParameterName(aResultParameter, aMessage->oldName()); return; } diff --git a/src/ParametersPlugin/ParametersPlugin_Validators.cpp b/src/ParametersPlugin/ParametersPlugin_Validators.cpp index d15102d64..90ded6031 100644 --- a/src/ParametersPlugin/ParametersPlugin_Validators.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Validators.cpp @@ -10,6 +10,7 @@ #include #include #include +#include ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator() { @@ -23,7 +24,7 @@ bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribut const std::list& theArguments) const { AttributeStringPtr aStrAttr = std::dynamic_pointer_cast(theAttribute); - bool result = isVariable(aStrAttr->value()); + bool result = isVariable(aStrAttr->value()) && isUnique(theAttribute, aStrAttr->value()); return result; } @@ -43,6 +44,26 @@ bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString return true; } +bool ParametersPlugin_VariableValidator::isUnique(const AttributePtr& theAttribute, + const std::string& theString) const +{ + DocumentPtr aDocument = theAttribute->owner()->document(); + for (int anIndex = 0, aSize = aDocument->size(ModelAPI_ResultParameter::group()); + anIndex < aSize; ++anIndex) { + ObjectPtr aParamObj = aDocument->object(ModelAPI_ResultParameter::group(), anIndex); + if (aParamObj->data()->name() != theString) + continue; + ResultParameterPtr aParam = std::dynamic_pointer_cast(aParamObj); + if (!aParam.get()) + continue; + FeaturePtr aFeature = ModelAPI_Feature::feature(aParam); + if (aFeature == theAttribute->owner()) + continue; + return false; + } + return true; +} + ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator() { diff --git a/src/ParametersPlugin/ParametersPlugin_Validators.h b/src/ParametersPlugin/ParametersPlugin_Validators.h index be8f285b5..8b4626ead 100644 --- a/src/ParametersPlugin/ParametersPlugin_Validators.h +++ b/src/ParametersPlugin/ParametersPlugin_Validators.h @@ -29,7 +29,8 @@ class ParametersPlugin_VariableValidator : public ModelAPI_AttributeValidator protected: PARAMETERSPLUGIN_EXPORT bool isVariable(const std::string& theString) const; - + PARAMETERSPLUGIN_EXPORT bool isUnique(const AttributePtr& theAttribute, + const std::string& theString) const; }; class ParametersPlugin_ExpressionValidator: public ModelAPI_AttributeValidator -- 2.39.2