From: spo Date: Wed, 15 Jul 2015 13:19:02 +0000 (+0300) Subject: Issue #761 - Check and error for unique parameter name X-Git-Tag: V_1.4.0_beta4~458 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=eb0cd64411cc25e50efff3f695fca2ccf8be7a85;p=modules%2Fshaper.git Issue #761 - Check and error for unique parameter name --- diff --git a/src/ModelAPI/ModelAPI_Tools.cpp b/src/ModelAPI/ModelAPI_Tools.cpp index 3ffd15ec4..09fc2c0be 100644 --- a/src/ModelAPI/ModelAPI_Tools.cpp +++ b/src/ModelAPI/ModelAPI_Tools.cpp @@ -59,6 +59,18 @@ bool findVariable(const std::string& theName, double& outValue, ResultParameterP return false; } +bool findVariable(const DocumentPtr& theDocument, const std::string& theName, + double& outValue, ResultParameterPtr& theParam) +{ + ObjectPtr aParamObj = theDocument->objectByName(ModelAPI_ResultParameter::group(), theName); + theParam = std::dynamic_pointer_cast(aParamObj); + if (!theParam.get()) + return false; + AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE()); + outValue = aValueAttribute->value(); + return true; +} + static std::map > myColorMap; void appendValues(std::vector& theRGB, const int theRed, const int theGreen, const int theBlue) diff --git a/src/ModelAPI/ModelAPI_Tools.h b/src/ModelAPI/ModelAPI_Tools.h index 67b0427be..b99b9f87e 100644 --- a/src/ModelAPI/ModelAPI_Tools.h +++ b/src/ModelAPI/ModelAPI_Tools.h @@ -29,6 +29,13 @@ MODELAPI_EXPORT std::shared_ptr shape(const ResultPtr& theResult) MODELAPI_EXPORT bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam); +/*! + * Searches for variable with name \param theName in the document. + * If found, set it value in the \param outValue and returns true. + */ +MODELAPI_EXPORT bool findVariable(const DocumentPtr& theDocument, const std::string& theName, + double& outValue, ResultParameterPtr& theParam); + /*! * Returns the values of the next random color. The values are in range [0, 255] * \param theValues a container of component of RGB value: red, green, blue diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp index 9d64cef32..2ec43200f 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.cpp @@ -45,50 +45,58 @@ bool ParametersPlugin_Parameter::isInHistory() void ParametersPlugin_Parameter::attributeChanged(const std::string& theID) { - if (theID == EXPRESSION_ID()) { // recompute only on change of the expression - ResultParameterPtr aParam = document()->createParameter(data()); - - 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); - // Name - std::string aName = string(VARIABLE_ID())->value(); - std::ostringstream sstream; - sstream << aValue; - std::string aParamValue = sstream.str(); - data()->setName(aName); - aParam->data()->setName(aName); - // Error - if (!outErrorMessage.empty()) { - std::string aStateMsg("Error: " + outErrorMessage); - 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); - } - // Value - AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); - aValueAttribute->setValue(aValue); - setResult(aParam); + if (theID == EXPRESSION_ID()) + updateExpression(); +} + +void ParametersPlugin_Parameter::updateName() +{ + std::string aName = string(VARIABLE_ID())->value(); + data()->setName(aName); + + ResultParameterPtr aParam = document()->createParameter(data()); + aParam->data()->setName(aName); + setResult(aParam); +} + +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 + if (!outErrorMessage.empty()) { + std::string aStateMsg("Error: " + outErrorMessage); + 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); + } + + ResultParameterPtr aParam = document()->createParameter(data()); + AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE()); + aValueAttribute->setValue(aValue); + setResult(aParam); } void ParametersPlugin_Parameter::execute() { - // just call recompute - attributeChanged(EXPRESSION_ID()); + updateName(); + updateExpression(); } double ParametersPlugin_Parameter::evaluate(const std::string& theExpression, std::string& theError) { - std::list anExprParams = myInterp->compile(theExpression); // find expression's params in the model std::list aContext; diff --git a/src/ParametersPlugin/ParametersPlugin_Parameter.h b/src/ParametersPlugin/ParametersPlugin_Parameter.h index c348d0f73..c735c8ee7 100644 --- a/src/ParametersPlugin/ParametersPlugin_Parameter.h +++ b/src/ParametersPlugin/ParametersPlugin_Parameter.h @@ -72,6 +72,8 @@ class ParametersPlugin_Parameter : public ModelAPI_Feature protected: double evaluate(const std::string& theExpression, std::string& theError); + void updateName(); + void updateExpression(); private: std::shared_ptr myInterp; diff --git a/src/XGUI/XGUI_ObjectsBrowser.cpp b/src/XGUI/XGUI_ObjectsBrowser.cpp index 448f47dbd..417a95ec6 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.cpp +++ b/src/XGUI/XGUI_ObjectsBrowser.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -18,7 +19,7 @@ #include #include #include - +#include /// Width of second column (minimum acceptable = 27) #define SECOND_COL_WIDTH 30 @@ -86,17 +87,38 @@ void XGUI_DataTree::commitData(QWidget* theEditor) { QLineEdit* aEditor = dynamic_cast(theEditor); if (aEditor) { - QString aRes = aEditor->text(); + QString aName = aEditor->text(); QModelIndexList aIndexList = selectionModel()->selectedIndexes(); ModuleBase_IDocumentDataModel* aModel = dataModel(); ObjectPtr aObj = aModel->object(aIndexList.first()); SessionPtr aMgr = ModelAPI_Session::get(); aMgr->startOperation("Rename"); - aObj->data()->setName(qPrintable(aRes)); + + if (!canRename(aObj, aName)) { + aMgr->abortOperation(); + return; + } + + aObj->data()->setName(qPrintable(aName)); aMgr->finishOperation(); } } +bool XGUI_DataTree::canRename(const ObjectPtr& theObject, const QString& theName) +{ + double aValue; + ResultParameterPtr aParam; + + bool isVariableFound = ModelAPI_Tools::findVariable(theObject->document(), qPrintable(theName), aValue, aParam); + + if (isVariableFound) + QMessageBox::information(this, tr("Rename parameter"), + QString(tr("Selected parameter can not be renamed to: %1. \ +There is a parameter with the same name. Its value is: %2.")).arg(qPrintable(theName)).arg(aValue)); + + return !isVariableFound; +} + void XGUI_DataTree::clear() { ModuleBase_IDocumentDataModel* aModel = dataModel(); diff --git a/src/XGUI/XGUI_ObjectsBrowser.h b/src/XGUI/XGUI_ObjectsBrowser.h index ade333a35..09f9dc220 100644 --- a/src/XGUI/XGUI_ObjectsBrowser.h +++ b/src/XGUI/XGUI_ObjectsBrowser.h @@ -44,6 +44,9 @@ public slots: /// Commit modified data (used for renaming of objects) virtual void commitData(QWidget* theEditor); + /// Returns true if theObject can be renamed in theName + bool canRename(const ObjectPtr& theObject, const QString& theName); + protected: /// Redefinition of virtual method virtual void contextMenuEvent(QContextMenuEvent* theEvent);