X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FParametersPlugin%2FParametersPlugin_EvalListener.cpp;h=d0e4ca3abf17c574bb917cd2fa47fd8a179885f2;hb=06e7f5859095193fc7f498bd89a7d28009794f53;hp=183a027f4962a42e35fc2031ed3da1e4079a978f;hpb=77b93142eb8ba33bfb9a42040c2fd8926ffea5c8;p=modules%2Fshaper.git diff --git a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp index 183a027f4..d0e4ca3ab 100644 --- a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp +++ b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp @@ -1,30 +1,49 @@ -/* - * ParametersPlugin_EvalListener.cpp - * - * Created on: Apr 28, 2015 - * Author: sbh - */ +// Copyright (C) 2014-2023 CEA, EDF +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include #include #include -#include -#include +#include +#include + +#include +#include #include #include #include #include #include +#include #include #include -#include #include #include +#include + +#include + #include #include #include @@ -32,16 +51,15 @@ //------------------------------------------------------------------------------ // Tools -std::string toStdString(double theValue) +static std::wstring toStdWString(double theValue) { - std::ostringstream sstream; + std::wostringstream sstream; sstream << theValue; - return sstream.str(); -} - -std::set toSet(const std::list& theContainer) -{ - return std::set(theContainer.begin(), theContainer.end()); + size_t aPos = sstream.str().find(L"."); + std::wstring aPnt = L""; + if (aPos == std::wstring::npos) + aPnt = L"."; + return sstream.str() + aPnt; } //------------------------------------------------------------------------------ @@ -51,16 +69,12 @@ ParametersPlugin_EvalListener::ParametersPlugin_EvalListener() Events_Loop* aLoop = Events_Loop::loop(); Events_ID anEvents_IDs[] = { - ModelAPI_AttributeEvalMessage::eventId(), ModelAPI_ObjectRenamedMessage::eventId(), ModelAPI_ReplaceParameterMessage::eventId() }; - for (int i = 0; i < sizeof(anEvents_IDs)/sizeof(anEvents_IDs[0]); ++i) + for (unsigned long long i = 0; i < sizeof(anEvents_IDs)/sizeof(anEvents_IDs[0]); ++i) aLoop->registerListener(this, anEvents_IDs[i], NULL, true); - - myInterp = std::shared_ptr(new ParametersPlugin_PyInterp()); - myInterp->initialize(); } ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener() @@ -73,121 +87,27 @@ void ParametersPlugin_EvalListener::processEvent( if (!theMessage.get()) return; - const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId(); const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId(); const Events_ID kReplaceParameterEvent = ModelAPI_ReplaceParameterMessage::eventId(); - if (theMessage->eventID() == kEvaluationEvent) { - processEvaluationEvent(theMessage); - } else if (theMessage->eventID() == kObjectRenamedEvent) { + if (theMessage->eventID() == kObjectRenamedEvent) { processObjectRenamedEvent(theMessage); } else if (theMessage->eventID() == kReplaceParameterEvent) { processReplaceParameterEvent(theMessage); - } else { - Events_Error::send(std::string("ParametersPlugin python interpreter, unhandled message caught: ") - + theMessage->eventID().eventText()); } } -double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, std::string& theError, - const std::shared_ptr& theDocument) +std::wstring ParametersPlugin_EvalListener::renameInPythonExpression( + const std::wstring& theExpression, + const std::wstring& theOldName, + const std::wstring& theNewName) { - std::list anExprParams = myInterp->compile(theExpression); - // find expression's params in the model - std::list aContext; - std::list::iterator it = anExprParams.begin(); - 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; - - aContext.push_back(*it + "=" + toStdString(aValue)); - } - myInterp->extendLocalContext(aContext); - double result = myInterp->evaluate(theExpression, theError); - myInterp->clearLocalContext(); - return result; -} + std::wstring anExpressionString = theExpression; -void ParametersPlugin_EvalListener::processEvaluationEvent( - const std::shared_ptr& theMessage) -{ - std::shared_ptr aMessage = - std::dynamic_pointer_cast(theMessage); - - if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) { - AttributeDoublePtr anAttribute = - std::dynamic_pointer_cast(aMessage->attribute()); - std::string anError; - double aValue = evaluate(anAttribute->text(), anError, anAttribute->owner()->document()); - bool isValid = anError.empty(); - if (isValid) - anAttribute->setCalculatedValue(aValue); - anAttribute->setUsedParameters(isValid ? toSet(myInterp->compile(anAttribute->text())) : std::set()); - anAttribute->setExpressionInvalid(!isValid); - anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError); - } else - if (aMessage->attribute()->attributeType() == GeomDataAPI_Point::typeId()) { - AttributePointPtr anAttribute = - std::dynamic_pointer_cast(aMessage->attribute()); - std::string aText[] = { - anAttribute->textX(), - anAttribute->textY(), - anAttribute->textZ() - }; - double aCalculatedValue[] = { - anAttribute->x(), - anAttribute->y(), - anAttribute->z() - }; - for (int i = 0; i < 3; ++i) { - std::string anError; - double aValue = evaluate(aText[i], anError, anAttribute->owner()->document()); - bool isValid = anError.empty(); - if (isValid) aCalculatedValue[i] = aValue; - anAttribute->setUsedParameters(i, isValid ? toSet(myInterp->compile(aText[i])) : std::set()); - anAttribute->setExpressionInvalid(i, !isValid); - anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError); - } - anAttribute->setCalculatedValue(aCalculatedValue[0], - aCalculatedValue[1], - aCalculatedValue[2]); - } else - if (aMessage->attribute()->attributeType() == GeomDataAPI_Point2D::typeId()) { - AttributePoint2DPtr anAttribute = - std::dynamic_pointer_cast(aMessage->attribute()); - std::string aText[] = { - anAttribute->textX(), - anAttribute->textY() - }; - double aCalculatedValue[] = { - anAttribute->x(), - anAttribute->y() - }; - for (int i = 0; i < 2; ++i) { - std::string anError; - double aValue = evaluate(aText[i], anError, anAttribute->owner()->document()); - bool isValid = anError.empty(); - if (isValid) aCalculatedValue[i] = aValue; - anAttribute->setUsedParameters(i, isValid ? toSet(myInterp->compile(aText[i])) : std::set()); - anAttribute->setExpressionInvalid(i, !isValid); - anAttribute->setExpressionError(i, aText[i].empty() ? "" : anError); - } - anAttribute->setCalculatedValue(aCalculatedValue[0], - aCalculatedValue[1]); - } -} - -std::string ParametersPlugin_EvalListener::renameInPythonExpression( - const std::string& theExpression, - const std::string& theOldName, - const std::string& theNewName) -{ - std::string anExpressionString = theExpression; - - std::list > aPositions = - myInterp->positions(anExpressionString, theOldName); + // ask interpreter to compute the positions in the expression + std::shared_ptr aMsg = + ModelAPI_ComputePositionsMessage::send(theExpression, theOldName, this); + const std::list >& aPositions = aMsg->positions(); if (aPositions.empty()) return anExpressionString; @@ -204,7 +124,7 @@ std::string ParametersPlugin_EvalListener::renameInPythonExpression( int aLineNo = ritLine->first - 1; size_t aLineStart = 0; for (int i = 0; i < aLineNo; ++i) - aLineStart = anExpressionString.find("\n", aLineStart) + 1; + aLineStart = anExpressionString.find(L"\n", aLineStart) + 1; const std::list& aColOffsets = ritLine->second; std::list::const_reverse_iterator ritOffset = aColOffsets.rbegin(); @@ -219,32 +139,42 @@ std::string ParametersPlugin_EvalListener::renameInPythonExpression( void ParametersPlugin_EvalListener::renameInParameter( std::shared_ptr theParameter, - const std::string& theOldName, - const std::string& theNewName) + const std::wstring& theOldName, + const std::wstring& theNewName) { std::shared_ptr anExpressionAttribute = theParameter->string(ParametersPlugin_Parameter::EXPRESSION_ID()); - std::string anExpressionString = anExpressionAttribute->value(); + std::wstring anExpressionString = anExpressionAttribute->isUValue() ? + Locale::Convert::toWString(anExpressionAttribute->valueU()) : + Locale::Convert::toWString(anExpressionAttribute->value()); anExpressionString = renameInPythonExpression(anExpressionString, theOldName, theNewName); - // Issue #588. No need for reevaluating expression. + // Issue #588. No need for reevaluating expression. // Moreover, current history may not contain necessary parameters. - anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(true); + bool aWasBlocked = anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(true); anExpressionAttribute->setValue(anExpressionString); - anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(false); + anExpressionAttribute->owner()->data()->blockSendAttributeUpdated(aWasBlocked); } void ParametersPlugin_EvalListener::renameInAttribute( std::shared_ptr theAttribute, - const std::string& theOldName, - const std::string& theNewName) + const std::wstring& theOldName, + const std::wstring& theNewName) { + if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) { + AttributeIntegerPtr anAttribute = + std::dynamic_pointer_cast(theAttribute); + std::wstring anExpressionString = anAttribute->text(); + anExpressionString = renameInPythonExpression(anExpressionString, + theOldName, theNewName); + anAttribute->setText(anExpressionString); + } else if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) { AttributeDoublePtr anAttribute = std::dynamic_pointer_cast(theAttribute); - std::string anExpressionString = anAttribute->text(); + std::wstring anExpressionString = anAttribute->text(); anExpressionString = renameInPythonExpression(anExpressionString, theOldName, theNewName); anAttribute->setText(anExpressionString); @@ -252,7 +182,7 @@ void ParametersPlugin_EvalListener::renameInAttribute( if (theAttribute->attributeType() == GeomDataAPI_Point::typeId()) { AttributePointPtr anAttribute = std::dynamic_pointer_cast(theAttribute); - std::string anExpressionString[3] = { + std::wstring anExpressionString[3] = { anAttribute->textX(), anAttribute->textY(), anAttribute->textZ() @@ -267,7 +197,7 @@ void ParametersPlugin_EvalListener::renameInAttribute( if (theAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) { AttributePoint2DPtr anAttribute = std::dynamic_pointer_cast(theAttribute); - std::string anExpressionString[2] = { + std::wstring anExpressionString[2] = { anAttribute->textX(), anAttribute->textY() }; @@ -279,49 +209,52 @@ void ParametersPlugin_EvalListener::renameInAttribute( } } -void ParametersPlugin_EvalListener::renameInDependants(std::shared_ptr theResultParameter, - const std::string& theOldName, - const std::string& theNewName) +void ParametersPlugin_EvalListener::renameInDependents( + std::shared_ptr theResultParameter, + const std::wstring& theOldName, + const std::wstring& theNewName) { std::set > anAttributes = - theResultParameter->data()->refsToMe(); + theResultParameter->data()->refsToMe(); std::set >::const_iterator anAttributeIt = - anAttributes.cbegin(); + anAttributes.cbegin(); for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) { const AttributePtr& anAttribute = *anAttributeIt; if (anAttribute->attributeType() == ModelAPI_AttributeRefList::typeId()) { std::shared_ptr aParameter = - std::dynamic_pointer_cast( - anAttribute->owner()); + std::dynamic_pointer_cast( + anAttribute->owner()); if (aParameter.get()) // Rename renameInParameter(aParameter, theOldName, theNewName); } else - // Rename - renameInAttribute(anAttribute, theOldName, theNewName); + // Rename + renameInAttribute(anAttribute, theOldName, theNewName); } } bool isValidAttribute(const AttributePtr& theAttribute) { - std::string aValidator, anError; + std::string aValidator; + Events_InfoMessage anError; return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError); } void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName) { - theResultParameter->data()->blockSendAttributeUpdated(true); - theResultParameter->data()->setName(theName); - theResultParameter->data()->blockSendAttributeUpdated(false); + bool aWasBlocked = theResultParameter->data()->blockSendAttributeUpdated(true); + theResultParameter->data()->setName(Locale::Convert::toWString(theName)); + theResultParameter->data()->blockSendAttributeUpdated(aWasBlocked, false); - std::shared_ptr aParameter = + std::shared_ptr aParameter = std::dynamic_pointer_cast( ModelAPI_Feature::feature(theResultParameter)); - aParameter->data()->blockSendAttributeUpdated(true); - aParameter->data()->setName(theName); + std::string anOldName = Locale::Convert::toString(aParameter->name()); + aWasBlocked = aParameter->data()->blockSendAttributeUpdated(true); + aParameter->data()->setName(Locale::Convert::toWString(theName)); aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(theName); - aParameter->data()->blockSendAttributeUpdated(false); + aParameter->data()->blockSendAttributeUpdated(aWasBlocked); } void ParametersPlugin_EvalListener::processObjectRenamedEvent( @@ -338,7 +271,7 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( // check if the renamed object is a result parameter ResultParameterPtr aResultParameter = std::dynamic_pointer_cast(aMessage->object()); - if (!aResultParameter.get()) + if (!aResultParameter.get()) return; // get parameter feature for the result @@ -348,17 +281,41 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( if (!aParameter.get()) return; + std::wstring aNotActivatedNames; + if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) { + static const std::string aMsgContext("ParametersPlugin"); + static const std::string aMsgText = + "Selected objects can be used in Part documents which are not loaded: " + + std::string("%1. Would you like to continue?"); + Events_InfoMessage aMsg(aMsgContext, aMsgText); + aMsg.arg(aNotActivatedNames.c_str()); + QMessageBox::StandardButton aRes = + QMessageBox::warning(0, ModuleBase_Tools::translate(aMsgContext, "Warning"), + ModuleBase_Tools::translate(aMsg), + QMessageBox::No | QMessageBox::Yes, QMessageBox::No); + if (aRes != QMessageBox::Yes) { + setParameterName(aResultParameter, Locale::Convert::toString(aMessage->oldName())); + return; + } + } + // 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 + setParameterName(aResultParameter, Locale::Convert::toString(aMessage->newName())); if (!isValidAttribute(aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID()))) { - setParameterName(aResultParameter, aMessage->oldName()); + //setParameterName(aResultParameter, aMessage->oldName()); + if (myOldNames.find(aParameter.get()) == myOldNames.end()) + myOldNames[aParameter.get()] = aMessage->oldName(); return; } - renameInDependants(aResultParameter, aMessage->oldName(), aMessage->newName()); + std::wstring anOldName = aMessage->oldName(); + if (myOldNames.find(aParameter.get()) != myOldNames.end()) { + anOldName = myOldNames[aParameter.get()]; + myOldNames.erase(aParameter.get()); + aParameter->execute(); // to enable result because of previously incorrect name + } + + renameInDependents(aResultParameter, anOldName, aMessage->newName()); } void ParametersPlugin_EvalListener::processReplaceParameterEvent( @@ -381,7 +338,7 @@ void ParametersPlugin_EvalListener::processReplaceParameterEvent( return; double aRealValue = aResultParameter->data()->real(ModelAPI_ResultParameter::VALUE())->value(); - std::string aValue = toStdString(aRealValue); + std::wstring aValue = toStdWString(aRealValue); - renameInDependants(aResultParameter, aResultParameter->data()->name(), aValue); + renameInDependents(aResultParameter, aResultParameter->data()->name(), aValue); }