From f153f87345bd8427b6ad98b38073962ecb973d74 Mon Sep 17 00:00:00 2001 From: spo Date: Fri, 10 Jul 2015 13:46:18 +0300 Subject: [PATCH] Issue #749 - Crash when input parameter name with white spaces --- .../ParametersPlugin_EvalListener.cpp | 51 +++++++++++++++++-- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp index e381b6f00..841322398 100644 --- a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp +++ b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -260,6 +261,39 @@ void ParametersPlugin_EvalListener::renameInAttribute( } } +bool isValidAttribute(const AttributePtr& theAttribute) +{ + std::list aValidators; + std::list > anArguments; + + FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner()); + if (!aFeature.get()) + return false; + + ModelAPI_Session::get()->validators()->validators(aFeature->getKind(), + theAttribute->id(), + aValidators, anArguments); + std::list::const_iterator aValidatorIt = aValidators.begin(); + std::list >::const_iterator anArgumentIt = anArguments.begin(); + for (; aValidatorIt != aValidators.end() || anArgumentIt != anArguments.end(); ++aValidatorIt, ++anArgumentIt) { + const ModelAPI_AttributeValidator * anAttributeValidator = + dynamic_cast(*aValidatorIt); + if (!anAttributeValidator) + continue; + if (!anAttributeValidator->isValid(theAttribute, *anArgumentIt)) + return false; + } + return true; +} + +void setParameterName(std::shared_ptr theParameter, const std::string& theName) +{ + theParameter->data()->blockSendAttributeUpdated(true); + theParameter->data()->setName(theName); + theParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(theName); + theParameter->data()->blockSendAttributeUpdated(false); +} + void ParametersPlugin_EvalListener::processObjectRenamedEvent( const std::shared_ptr& theMessage) { @@ -270,13 +304,13 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( return; // check that the renamed object is a result - std::shared_ptr aResultParameter = + ResultParameterPtr aResultParameter = std::dynamic_pointer_cast(aMessage->object()); if (!aResultParameter.get()) return; - + // get parameter feature for the result - std::shared_ptr aFeature = aResultParameter->document()->feature(aResultParameter); + FeaturePtr aFeature = aResultParameter->document()->feature(aResultParameter); std::shared_ptr aParameter = std::dynamic_pointer_cast(aFeature); if (!aParameter.get()) @@ -287,8 +321,15 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( //aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(aMessage->newName()); //aParameter->execute(); // manual way: - aParameter->data()->setName(aMessage->newName()); - aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(aMessage->newName()); + setParameterName(aParameter, 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()); + return; + } // List of documents to process std::list aDocList; -- 2.39.2