From c6ba8cc4d757304865c84d35abb1ad693fffa663 Mon Sep 17 00:00:00 2001 From: spo Date: Tue, 8 Sep 2015 14:03:01 +0300 Subject: [PATCH] Issue #903 - Parameter is not created, if expression contains not created parameter --- src/Model/Model_Validator.cpp | 2 +- .../ModuleBase_WidgetExprEditor.cpp | 4 +- .../ParametersPlugin_EvalListener.cpp | 1 + .../ParametersPlugin_Parameter.cpp | 32 +++++++------- .../ParametersPlugin_Parameter.h | 11 ++++- .../ParametersPlugin_Validators.cpp | 10 +++-- .../suite_ISSUES_SALOME/tst_903/test.py | 44 +++++++++++++++++++ 7 files changed, 79 insertions(+), 25 deletions(-) create mode 100644 test.squish/suite_ISSUES_SALOME/tst_903/test.py diff --git a/src/Model/Model_Validator.cpp b/src/Model/Model_Validator.cpp index 1fc4f7123..6795e43e1 100644 --- a/src/Model/Model_Validator.cpp +++ b/src/Model/Model_Validator.cpp @@ -206,7 +206,7 @@ bool Model_ValidatorsFactory::validate(const std::shared_ptr& // } // } //} - + // check all attributes for validity // Validity of data is checked by "Model_FeatureValidator" (kDefaultId) // if (!aData || !aData->isValid()) diff --git a/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp b/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp index b8e8ca60e..3b3d25c08 100644 --- a/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp +++ b/src/ModuleBase/ModuleBase_WidgetExprEditor.cpp @@ -227,7 +227,7 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const // Try to get the value QString aStateMsg; - std::string anErrorMessage = myFeature->error(); + std::string anErrorMessage = myFeature->string("ExpressionError")->value(); if (anErrorMessage.empty()) { ResultParameterPtr aParam = std::dynamic_pointer_cast(myFeature->firstResult()); @@ -240,7 +240,7 @@ bool ModuleBase_WidgetExprEditor::storeValueCustom() const } } } else { - aStateMsg = QString::fromStdString(anErrorMessage); + aStateMsg = "Error: " + QString::fromStdString(anErrorMessage); } myResultLabel->setText(aStateMsg); return true; diff --git a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp index 03b4b1199..029800afa 100644 --- a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp +++ b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp @@ -73,6 +73,7 @@ double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, for ( ; it != anExprParams.end(); it++) { double aValue; ResultParameterPtr aParamRes; + // If variable does not exist python interpreter will generate an error. It is OK. if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, theDocument)) continue; std::ostringstream sstream; diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp index 54dc39581..2b5ee41a5 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp @@ -34,6 +34,11 @@ void ParametersPlugin_Parameter::initAttributes() { data()->addAttribute(VARIABLE_ID(), ModelAPI_AttributeString::typeId()); data()->addAttribute(EXPRESSION_ID(), ModelAPI_AttributeString::typeId()); + + data()->addAttribute(EXPRESSION_ERROR_ID(), ModelAPI_AttributeString::typeId()); + data()->string(EXPRESSION_ERROR_ID())->setIsArgument(false); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXPRESSION_ERROR_ID()); + data()->addAttribute(ARGUMENTS_ID(), ModelAPI_AttributeRefList::typeId()); data()->reflist(ARGUMENTS_ID())->setIsArgument(false); ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ARGUMENTS_ID()); @@ -63,27 +68,20 @@ void ParametersPlugin_Parameter::updateName() void ParametersPlugin_Parameter::updateExpression() { std::string anExpression = string(EXPRESSION_ID())->value(); - if(anExpression.empty()) { - // clear error/result if the expression is empty - setError("", false); - return; - } + std::string outErrorMessage; double aValue = evaluate(anExpression, outErrorMessage); - std::ostringstream sstream; - sstream << aValue; - std::string aParamValue = sstream.str(); - // Error + + data()->string(EXPRESSION_ERROR_ID())->setValue(outErrorMessage); if (!outErrorMessage.empty()) { - std::string aStateMsg("Error: " + outErrorMessage); + setError("Expression error.", false); data()->execState(ModelAPI_StateExecFailed); - setError(aStateMsg, false); - } else { - static const std::string anEmptyMsg(""); // it is checked in the validator by the empty message - setError(anEmptyMsg, false); - data()->execState(ModelAPI_StateDone); + return; } + setError("", false); + data()->execState(ModelAPI_StateDone); + ResultParameterPtr aParam = document()->createParameter(data()); AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); aValueAttribute->setValue(aValue); @@ -104,9 +102,11 @@ double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, st std::list::iterator it = anExprParams.begin(); std::list aParamsList; for ( ; it != anExprParams.end(); it++) { + std::string& aVariableName = *it; + double aValue; ResultParameterPtr aParamRes; - if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, document())) continue; + if (!ModelAPI_Tools::findVariable(aVariableName, aValue, aParamRes, document())) continue; aParamsList.push_back(aParamRes); std::ostringstream sstream; diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.h b/src/ParametersPlugin/ParametersPlugin_Parameter.h index c735c8ee7..9b029a156 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.h +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.h @@ -40,11 +40,18 @@ class ParametersPlugin_Parameter : public ModelAPI_Feature return MY_EXPRESSION_ID; } + /// attribute name of extrusion size + inline static const std::string& EXPRESSION_ERROR_ID() + { + static const std::string MY_EXPRESSION_ERROR_ID("ExpressionError"); + return MY_EXPRESSION_ERROR_ID; + } + /// list of references to the arguments of this expression inline static const std::string& ARGUMENTS_ID() { - static const std::string MY_VARIABLE_ID("arguments"); - return MY_VARIABLE_ID; + static const std::string MY_ARGUMENTS_ID("arguments"); + return MY_ARGUMENTS_ID; } /// Returns the kind of a feature diff --git a/src/ParametersPlugin/ParametersPlugin_Validators.cpp b/src/ParametersPlugin/ParametersPlugin_Validators.cpp index d0b00ca8e..82bd9d8d4 100644 --- a/src/ParametersPlugin/ParametersPlugin_Validators.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Validators.cpp @@ -7,6 +7,8 @@ #include +#include + #include #include #include @@ -93,16 +95,16 @@ bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttrib AttributeStringPtr aStrAttr = std::dynamic_pointer_cast(theAttribute); bool isEmptyExpr = aStrAttr->value().empty(); - if(isEmptyExpr) { + if (isEmptyExpr) { theError = "Expression is empty."; return false; } - if(!aParam.get()) { + if (!aParam.get()) { theError = "Result is empty."; return false; } - theError = aFeature->error(); - return aFeature->error().empty(); + theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value(); + return theError.empty(); } diff --git a/test.squish/suite_ISSUES_SALOME/tst_903/test.py b/test.squish/suite_ISSUES_SALOME/tst_903/test.py new file mode 100644 index 000000000..40b767f6e --- /dev/null +++ b/test.squish/suite_ISSUES_SALOME/tst_903/test.py @@ -0,0 +1,44 @@ +def main(): + source(findFile("scripts", "common.py")) + + startApplication("salome_run.sh") + + activate_newgeom() + + #[step] Click menu Part->Parameter + activateItem(waitForObjectItem(":SALOME*_QMenuBar", "Part")) + activateItem(waitForObjectItem(":Part_QMenu", "Parameter")) + mouseClick(waitForObject(":Parameter_QLineEdit"), 79, 8, 0, Qt.LeftButton) + #[step] Check that feature ToolTip is: Model_FeatureValidator: Attribute "expression" is not initialized. + waitFor("object.exists(':Parameter_QFrame')", 20000) + test.compare(str(findObject(":Parameter_QFrame").toolTip), "Model_FeatureValidator: Attribute \"expression\" is not initialized.") + #[step] Check that name tooltip is: Errors:\nvariable - Parameters_VariableValidator: Incorrect variable name. + waitFor("object.exists(':Parameter_QLineEdit')", 20000) + test.compare(str(findObject(":Parameter_QLineEdit").toolTip), "Errors:\nvariable - Parameters_VariableValidator: Incorrect variable name.") + #[step] Check that expression tooltip is: Errors:\nexpression - Parameters_ExpressionValidator: Expression is empty. + waitFor("object.exists(':Parameter_ExpressionEditor')", 20000) + test.compare(str(findObject(":Parameter_ExpressionEditor").toolTip), "Errors:\nexpression - Parameters_ExpressionValidator: Expression is empty.") + + #[step] Enter variable name 'a' + type(waitForObject(":Parameter_QLineEdit"), "a") + mouseClick(waitForObject(":Parameter_ExpressionEditor"), 97, 31, 0, Qt.LeftButton) + + #[step] Enter variable expression '100+b' + type(waitForObject(":Parameter_ExpressionEditor"), "100+b") + + #[step] Check that expression tooltip is: Errors:\nexpression - Parameters_ExpressionValidator: name 'b' is not defined + waitFor("object.exists(':Parameter_ExpressionEditor')", 20000) + test.compare(str(findObject(":Parameter_ExpressionEditor").toolTip), "Errors:\nexpression - Parameters_ExpressionValidator: name 'b' is not defined") + #[step] Check that result message is: Error: unexpected EOF while parsing (, line 0) + waitFor("object.exists(':Parameter.Result_QLabel')", 20000) + test.compare(str(findObject(":Parameter.Result_QLabel").text), "Error: name 'b' is not defined") + + #[step] Check that feature ToolTip is: expression - Parameters_ExpressionValidator: name 'b' is not defined + waitFor("object.exists(':Parameter_QFrame')", 20000) + test.compare(str(findObject(":Parameter_QFrame").toolTip), "expression - Parameters_ExpressionValidator: name 'b' is not defined") + + #[step] Check that apply button is disabled + waitFor("object.exists(':Parameter.property_panel_ok_QToolButton')", 20000) + test.compare(findObject(":Parameter.property_panel_ok_QToolButton").enabled, False) + + close_application() -- 2.39.2