]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Issue #761 - Check and error for unique parameter name
authorspo <sergey.pokhodenko@opencascade.com>
Fri, 10 Jul 2015 11:02:30 +0000 (14:02 +0300)
committerspo <sergey.pokhodenko@opencascade.com>
Tue, 14 Jul 2015 09:44:25 +0000 (12:44 +0300)
src/ParametersPlugin/ParametersPlugin_EvalListener.cpp
src/ParametersPlugin/ParametersPlugin_Validators.cpp
src/ParametersPlugin/ParametersPlugin_Validators.h

index 8413223988f1287357fef28536b88717673ad3b5..3e626d3730f7d7b0f20ed96892fc660822975f8c 100644 (file)
@@ -286,12 +286,20 @@ bool isValidAttribute(const AttributePtr& theAttribute)
   return true;
 }
 
-void setParameterName(std::shared_ptr<ParametersPlugin_Parameter> 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<ParametersPlugin_Parameter> aParameter = 
+      std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
+          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<ModelAPI_ResultParameter>(aMessage->object());
   if (!aResultParameter.get()) 
     return;
 
   // get parameter feature for the result
-  FeaturePtr aFeature = aResultParameter->document()->feature(aResultParameter);
   std::shared_ptr<ParametersPlugin_Parameter> aParameter =
-      std::dynamic_pointer_cast<ParametersPlugin_Parameter>(aFeature);
+      std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
+          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<ModelAPI_Feature>& 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;
   }
 
index d15102d64105f8bfdbce812d92a822182e355003..90ded60313edf285607d299f0d0908c846d4bbd9 100644 (file)
@@ -10,6 +10,7 @@
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_ResultParameter.h>
+#include <ModelAPI_Tools.h>
 
 ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator()
 {
@@ -23,7 +24,7 @@ bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribut
                                                  const std::list<std::string>& theArguments) const
 {
   AttributeStringPtr aStrAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(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<ModelAPI_ResultParameter>(aParamObj);
+    if (!aParam.get())
+      continue;
+    FeaturePtr aFeature = ModelAPI_Feature::feature(aParam);
+    if (aFeature == theAttribute->owner())
+      continue;
+    return false;
+  }
+  return true;
+}
+
 ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator()
 {
 
index be8f285b5c1684edfd38cc0bccafa3e344cadbce..8b4626eadb823064aea0eacdf5ced93595ecc753 100644 (file)
@@ -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