X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FParametersPlugin%2FParametersPlugin_EvalListener.cpp;h=0d61bec9ff3aa2e3e0d1a7671bb9c6cbe69b5b89;hb=185923a92c76805bc1b410b0f655e214394a8573;hp=03b4b11996a3d4d1d8830da95dfb2015a8126d97;hpb=9518d50a9b5dd14c20ffa04ee8dbf9995f96ee2c;p=modules%2Fshaper.git diff --git a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp index 03b4b1199..0d61bec9f 100644 --- a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp +++ b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp @@ -1,3 +1,4 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> /* * ParametersPlugin_EvalListener.cpp * @@ -9,10 +10,11 @@ #include #include -#include -#include +#include +#include +#include #include #include #include @@ -21,24 +23,47 @@ #include #include -#include #include #include +#include + #include #include #include +//------------------------------------------------------------------------------ +// Tools + +std::string toStdString(double theValue) +{ + std::ostringstream sstream; + sstream << theValue; + size_t aPos = sstream.str().find("."); + std::string aPnt = ""; + if (aPos == std::string::npos) + aPnt = "."; + return sstream.str() + aPnt; +} + +std::set toSet(const std::list& theContainer) +{ + return std::set(theContainer.begin(), theContainer.end()); +} + +//------------------------------------------------------------------------------ + ParametersPlugin_EvalListener::ParametersPlugin_EvalListener() { Events_Loop* aLoop = Events_Loop::loop(); - const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId(); - aLoop->registerListener(this, kEvaluationEvent, NULL, true); - const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId(); - aLoop->registerListener(this, kObjectRenamedEvent, NULL, true); - myInterp = std::shared_ptr(new ParametersPlugin_PyInterp()); - myInterp->initialize(); + Events_ID anEvents_IDs[] = { + ModelAPI_ObjectRenamedMessage::eventId(), + ModelAPI_ReplaceParameterMessage::eventId() + }; + + for (int i = 0; i < sizeof(anEvents_IDs)/sizeof(anEvents_IDs[0]); ++i) + aLoop->registerListener(this, anEvents_IDs[i], NULL, true); } ParametersPlugin_EvalListener::~ParametersPlugin_EvalListener() @@ -51,112 +76,13 @@ void ParametersPlugin_EvalListener::processEvent( if (!theMessage.get()) return; - const Events_ID kEvaluationEvent = ModelAPI_AttributeEvalMessage::eventId(); const Events_ID kObjectRenamedEvent = ModelAPI_ObjectRenamedMessage::eventId(); - if (theMessage->eventID() == kEvaluationEvent) { - processEvaluationEvent(theMessage); - } else if (theMessage->eventID() == kObjectRenamedEvent) { - processObjectRenamedEvent(theMessage); - } else { - Events_Error::send(std::string("ParametersPlugin python interpreter, unhandled message caught: ") - + theMessage->eventID().eventText()); - } -} + const Events_ID kReplaceParameterEvent = ModelAPI_ReplaceParameterMessage::eventId(); -double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, std::string& theError, - const std::shared_ptr& theDocument) -{ - 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 (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes, theDocument)) continue; - - std::ostringstream sstream; - sstream << aValue; - std::string aParamValue = sstream.str(); - aContext.push_back(*it + "=" + aParamValue); - } - myInterp->extendLocalContext(aContext); - double result = myInterp->evaluate(theExpression, theError); - myInterp->clearLocalContext(); - return result; -} - -std::set toSet(const std::list& theContainer) -{ - return std::set(theContainer.begin(), theContainer.end()); -} - -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]); + if (theMessage->eventID() == kObjectRenamedEvent) { + processObjectRenamedEvent(theMessage); + } else if (theMessage->eventID() == kReplaceParameterEvent) { + processReplaceParameterEvent(theMessage); } } @@ -167,8 +93,10 @@ std::string ParametersPlugin_EvalListener::renameInPythonExpression( { 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; @@ -210,7 +138,7 @@ void ParametersPlugin_EvalListener::renameInParameter( 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); anExpressionAttribute->setValue(anExpressionString); @@ -222,6 +150,14 @@ void ParametersPlugin_EvalListener::renameInAttribute( const std::string& theOldName, const std::string& theNewName) { + if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) { + AttributeIntegerPtr anAttribute = + std::dynamic_pointer_cast(theAttribute); + std::string 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); @@ -260,9 +196,34 @@ void ParametersPlugin_EvalListener::renameInAttribute( } } +void ParametersPlugin_EvalListener::renameInDependents( + std::shared_ptr theResultParameter, + const std::string& theOldName, + const std::string& theNewName) +{ + std::set > anAttributes = + theResultParameter->data()->refsToMe(); + std::set >::const_iterator anAttributeIt = + 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()); + if (aParameter.get()) + // Rename + renameInParameter(aParameter, theOldName, theNewName); + } else + // 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); } @@ -270,9 +231,9 @@ void setParameterName(ResultParameterPtr theResultParameter, const std::string& { theResultParameter->data()->blockSendAttributeUpdated(true); theResultParameter->data()->setName(theName); - theResultParameter->data()->blockSendAttributeUpdated(false); + theResultParameter->data()->blockSendAttributeUpdated(false, false); - std::shared_ptr aParameter = + std::shared_ptr aParameter = std::dynamic_pointer_cast( ModelAPI_Feature::feature(theResultParameter)); @@ -296,7 +257,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 @@ -306,34 +267,55 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( if (!aParameter.get()) return; + std::string aNotActivatedNames; + if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) { + QMessageBox::StandardButton aRes = QMessageBox::warning(0, QObject::tr("Warning"), + QObject::tr("Selected objects can be used in Part documents which are not loaded: " + "%1. Whould you like to continue?").arg(aNotActivatedNames.c_str()), + QMessageBox::No | QMessageBox::Yes, QMessageBox::No); + if (aRes != QMessageBox::Yes) { + setParameterName(aResultParameter, 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 + // 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(aResultParameter, aMessage->oldName()); return; } - std::set > anAttributes = - aResultParameter->data()->refsToMe(); - std::set >::const_iterator anAttributeIt = - anAttributes.cbegin(); - for (; anAttributeIt != anAttributes.cend(); ++anAttributeIt) { - const AttributePtr& anAttribute = *anAttributeIt; - AttributeRefListPtr anAttributeRefList = - std::dynamic_pointer_cast(anAttribute); - if (anAttributeRefList.get()) { - std::shared_ptr aParameter = - std::dynamic_pointer_cast( - anAttributeRefList->owner()); - if (aParameter.get()) - // Rename - renameInParameter(aParameter, aMessage->oldName(), aMessage->newName()); - } else - // Rename - renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName()); - } + renameInDependents(aResultParameter, aMessage->oldName(), aMessage->newName()); } +void ParametersPlugin_EvalListener::processReplaceParameterEvent( + const std::shared_ptr& theMessage) +{ + std::shared_ptr aMessage = + std::dynamic_pointer_cast(theMessage); + + // get parameter feature for the object + std::shared_ptr aParameter = + std::dynamic_pointer_cast( + ModelAPI_Feature::feature(aMessage->object())); + if (!aParameter.get()) + return; + + ResultParameterPtr aResultParameter = + std::dynamic_pointer_cast( + aParameter->firstResult()); + if (!aResultParameter.get()) + return; + + double aRealValue = aResultParameter->data()->real(ModelAPI_ResultParameter::VALUE())->value(); + std::string aValue = toStdString(aRealValue); + + renameInDependents(aResultParameter, aResultParameter->data()->name(), aValue); +}