X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FParametersPlugin%2FParametersPlugin_EvalListener.cpp;h=d63741c9fa606596f832c40bd95ddb6524d0b2ac;hb=471cc7b52168016a3b6fff7e64cdd800cd7d8f91;hp=3e626d3730f7d7b0f20ed96892fc660822975f8c;hpb=70a245439d9148ebde66aa98a00fcb7b01fb92bd;p=modules%2Fshaper.git diff --git a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp index 3e626d373..d63741c9f 100644 --- a/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp +++ b/src/ParametersPlugin/ParametersPlugin_EvalListener.cpp @@ -11,29 +11,60 @@ #include #include -#include +#include +#include +#include +#include #include +#include #include #include #include #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); + + 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) + aLoop->registerListener(this, anEvents_IDs[i], NULL, true); myInterp = std::shared_ptr(new ParametersPlugin_PyInterp()); myInterp->initialize(); @@ -51,18 +82,23 @@ void ParametersPlugin_EvalListener::processEvent( 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) { processObjectRenamedEvent(theMessage); + } else if (theMessage->eventID() == kReplaceParameterEvent) { + processReplaceParameterEvent(theMessage); } else { - Events_Error::send(std::string("ParametersPlugin python interpreter, unhandled message caught: ") - + theMessage->eventID().eventText()); + Events_InfoMessage("ParametersPlugin_EvalListener", + "ParametersPlugin python interpreter, unhandled message caught: ") + .arg(theMessage->eventID().eventText()).send(); } } -double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, - std::string& theError) +double ParametersPlugin_EvalListener::evaluate(FeaturePtr theParameter, + const std::string& theExpression, std::string& theError) { std::list anExprParams = myInterp->compile(theExpression); // find expression's params in the model @@ -71,12 +107,14 @@ double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression, for ( ; it != anExprParams.end(); it++) { double aValue; ResultParameterPtr aParamRes; - if (!ModelAPI_Tools::findVariable(*it, aValue, aParamRes)) continue; + // If variable does not exist python interpreter will generate an error. It is OK. + // But due to the issue 1479 it should not check the history position of parameters relatively + // to feature that contains expression + if (!ModelAPI_Tools::findVariable(/*theParameter*/ FeaturePtr(), + *it, aValue, aParamRes, theParameter->document())) + continue; - std::ostringstream sstream; - sstream << aValue; - std::string aParamValue = sstream.str(); - aContext.push_back(*it + "=" + aParamValue); + aContext.push_back(*it + "=" + toStdString(aValue)); } myInterp->extendLocalContext(aContext); double result = myInterp->evaluate(theExpression, theError); @@ -90,66 +128,80 @@ void ParametersPlugin_EvalListener::processEvaluationEvent( std::shared_ptr aMessage = std::dynamic_pointer_cast(theMessage); - // Double - AttributeDoublePtr aDoubleAttribute = - std::dynamic_pointer_cast(aMessage->attribute()); - if (aDoubleAttribute.get()) { + FeaturePtr aParamFeature = + std::dynamic_pointer_cast(aMessage->attribute()->owner()); + if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) { + AttributeIntegerPtr anAttribute = + std::dynamic_pointer_cast(aMessage->attribute()); std::string anError; - double aValue = evaluate(aDoubleAttribute->text(), anError); - if (anError.empty()) { - aDoubleAttribute->setCalculatedValue(aValue); - aDoubleAttribute->setExpressionInvalid(false); - } else { // set feature as invalid-parameter arguments - aDoubleAttribute->setExpressionInvalid(true); - } - } - - // Point - AttributePointPtr aPointAttribute = - std::dynamic_pointer_cast(aMessage->attribute()); - if (aPointAttribute.get()) { - std::string anError[3]; - double aValue[3] = { - evaluate(aPointAttribute->textX(), anError[0]), - evaluate(aPointAttribute->textY(), anError[1]), - evaluate(aPointAttribute->textZ(), anError[2]) + int aValue = (int)evaluate(aParamFeature, anAttribute->text(), anError); + 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() == ModelAPI_AttributeDouble::typeId()) { + AttributeDoublePtr anAttribute = + std::dynamic_pointer_cast(aMessage->attribute()); + std::string anError; + double aValue = evaluate(aParamFeature, anAttribute->text(), anError); + 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() }; - bool isValid[3] = { - anError[0].empty(), - anError[1].empty(), - anError[2].empty() + double aCalculatedValue[] = { + anAttribute->x(), + anAttribute->y(), + anAttribute->z() }; - aPointAttribute->setExpressionInvalid(0, !isValid[0]); - aPointAttribute->setExpressionInvalid(1, !isValid[1]); - aPointAttribute->setExpressionInvalid(2, !isValid[2]); - - aPointAttribute->setCalculatedValue( - isValid[0] ? aValue[0] : aPointAttribute->x(), - isValid[1] ? aValue[1] : aPointAttribute->y(), - isValid[2] ? aValue[2] : aPointAttribute->z() - ); - } - - // Point2D - AttributePoint2DPtr aPoint2DAttribute = - std::dynamic_pointer_cast(aMessage->attribute()); - if (aPoint2DAttribute.get()) { - std::string anError[2]; - double aValue[2] = { - evaluate(aPoint2DAttribute->textX(), anError[0]), - evaluate(aPoint2DAttribute->textY(), anError[1]) + for (int i = 0; i < 3; ++i) { + std::string anError; + double aValue = evaluate(aParamFeature, aText[i], anError); + 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() }; - bool isValid[2] = { - anError[0].empty(), - anError[1].empty() + double aCalculatedValue[] = { + anAttribute->x(), + anAttribute->y() }; - aPoint2DAttribute->setExpressionInvalid(0, !isValid[0]); - aPoint2DAttribute->setExpressionInvalid(1, !isValid[1]); - - aPoint2DAttribute->setCalculatedValue( - isValid[0] ? aValue[0] : aPoint2DAttribute->x(), - isValid[1] ? aValue[1] : aPoint2DAttribute->y() - ); + for (int i = 0; i < 2; ++i) { + std::string anError; + double aValue = evaluate(aParamFeature, aText[i], anError); + 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]); } } @@ -215,82 +267,87 @@ void ParametersPlugin_EvalListener::renameInAttribute( const std::string& theOldName, const std::string& theNewName) { - // Double - AttributeDoublePtr aDoubleAttribute = - std::dynamic_pointer_cast(theAttribute); - if (aDoubleAttribute.get()) { - std::string anExpressionString = aDoubleAttribute->text(); + if (theAttribute->attributeType() == ModelAPI_AttributeInteger::typeId()) { + AttributeIntegerPtr anAttribute = + std::dynamic_pointer_cast(theAttribute); + std::string anExpressionString = anAttribute->text(); anExpressionString = renameInPythonExpression(anExpressionString, - theOldName, - theNewName); - aDoubleAttribute->setText(anExpressionString); - } - - // Point - AttributePointPtr aPointAttribute = - std::dynamic_pointer_cast(theAttribute); - if (aPointAttribute.get()) { + theOldName, theNewName); + anAttribute->setText(anExpressionString); + } else + if (theAttribute->attributeType() == ModelAPI_AttributeDouble::typeId()) { + AttributeDoublePtr anAttribute = + std::dynamic_pointer_cast(theAttribute); + std::string anExpressionString = anAttribute->text(); + anExpressionString = renameInPythonExpression(anExpressionString, + theOldName, theNewName); + anAttribute->setText(anExpressionString); + } else + if (theAttribute->attributeType() == GeomDataAPI_Point::typeId()) { + AttributePointPtr anAttribute = + std::dynamic_pointer_cast(theAttribute); std::string anExpressionString[3] = { - aPointAttribute->textX(), - aPointAttribute->textY(), - aPointAttribute->textZ() + anAttribute->textX(), + anAttribute->textY(), + anAttribute->textZ() }; for (int i = 0; i < 3; ++i) anExpressionString[i] = renameInPythonExpression(anExpressionString[i], - theOldName, - theNewName); - aPointAttribute->setText(anExpressionString[0], - anExpressionString[1], - anExpressionString[2]); - } - - // Point2D - AttributePoint2DPtr aPoint2DAttribute = - std::dynamic_pointer_cast(theAttribute); - if (aPoint2DAttribute.get()) { + theOldName, theNewName); + anAttribute->setText(anExpressionString[0], + anExpressionString[1], + anExpressionString[2]); + } else + if (theAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) { + AttributePoint2DPtr anAttribute = + std::dynamic_pointer_cast(theAttribute); std::string anExpressionString[2] = { - aPoint2DAttribute->textX(), - aPoint2DAttribute->textY() + anAttribute->textX(), + anAttribute->textY() }; for (int i = 0; i < 2; ++i) anExpressionString[i] = renameInPythonExpression(anExpressionString[i], - theOldName, - theNewName); - aPoint2DAttribute->setText(anExpressionString[0], - anExpressionString[1]); + theOldName, theNewName); + anAttribute->setText(anExpressionString[0], + anExpressionString[1]); } } -bool isValidAttribute(const AttributePtr& theAttribute) +void ParametersPlugin_EvalListener::renameInDependents(std::shared_ptr theResultParameter, + const std::string& theOldName, + const std::string& theNewName) { - std::list aValidators; - std::list > anArguments; - - FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner()); - if (!aFeature.get()) - return false; - - ModelAPI_Session::get()->validators()->validators(aFeature->getKind(), - theAttribute->id(), - aValidators, anArguments); - std::list::const_iterator aValidatorIt = aValidators.begin(); - std::list >::const_iterator anArgumentIt = anArguments.begin(); - for (; aValidatorIt != aValidators.end() || anArgumentIt != anArguments.end(); ++aValidatorIt, ++anArgumentIt) { - const ModelAPI_AttributeValidator * anAttributeValidator = - dynamic_cast(*aValidatorIt); - if (!anAttributeValidator) - continue; - if (!anAttributeValidator->isValid(theAttribute, *anArgumentIt)) - return false; + 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); } - return true; +} + +bool isValidAttribute(const AttributePtr& theAttribute) +{ + 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); + theResultParameter->data()->blockSendAttributeUpdated(false, false); std::shared_ptr aParameter = std::dynamic_pointer_cast( @@ -308,10 +365,12 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( std::shared_ptr aMessage = std::dynamic_pointer_cast(theMessage); - if (!aMessage.get() || aMessage->oldName().empty() || aMessage->newName().empty()) + // Empty new name is not available too but it will be rejected by + // name validator in isValidAttribute. + if (!aMessage.get() || aMessage->oldName().empty()) return; - // check if the renamed object is a result perameter + // check if the renamed object is a result parameter ResultParameterPtr aResultParameter = std::dynamic_pointer_cast(aMessage->object()); if (!aResultParameter.get()) @@ -324,6 +383,18 @@ 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()) @@ -334,44 +405,30 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent( return; } - // List of documents to process - std::list aDocList; - SessionPtr aSession = ModelAPI_Session::get(); - DocumentPtr aDocument = aSession->activeDocument(); - DocumentPtr aRootDocument = aSession->moduleDocument(); - aDocList.push_back(aDocument); - if (aDocument != aRootDocument) { - aDocList.push_back(aRootDocument); - } + renameInDependents(aResultParameter, aMessage->oldName(), aMessage->newName()); +} - // Find all features - for (std::list::const_iterator aDicumentIt = aDocList.begin(); - aDicumentIt != aDocList.end(); ++aDicumentIt) { - const DocumentPtr& aDocument = *aDicumentIt; - std::list aFeatures = aDocument->allFeatures(); - std::list::iterator aFeatureIt = aFeatures.begin(); - for (; aFeatureIt != aFeatures.end(); ++aFeatureIt) { - const FeaturePtr& aFeature = *aFeatureIt; - - // If Parameter feature then rename its expression - std::shared_ptr aParameter = - std::dynamic_pointer_cast(aFeature); - if (aParameter.get()) { - // Rename - renameInParameter(aParameter, aMessage->oldName(), aMessage->newName()); - continue; - } +void ParametersPlugin_EvalListener::processReplaceParameterEvent( + const std::shared_ptr& theMessage) +{ + std::shared_ptr aMessage = + std::dynamic_pointer_cast(theMessage); - // Find all attributes - std::list anAttributes = aFeature->data()->attributes(std::string()); - std::list::const_iterator anAttributeIt = anAttributes.begin(); - for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) { - const AttributePtr& anAttribute = *anAttributeIt; + // get parameter feature for the object + std::shared_ptr aParameter = + std::dynamic_pointer_cast( + ModelAPI_Feature::feature(aMessage->object())); + if (!aParameter.get()) + return; - // Rename - renameInAttribute(anAttribute, aMessage->oldName(), aMessage->newName()); - } - } - } -} + 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); +}