Salome HOME
Fix for the issue #1605 : optimization of undo/redo and abort processing
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_EvalListener.cpp
index 183a027f4962a42e35fc2031ed3da1e4079a978f..d63741c9fa606596f832c40bd95ddb6524d0b2ac 100644 (file)
 #include <ParametersPlugin_Parameter.h>
 #include <ParametersPlugin_PyInterp.h>
 
-#include <Events_Error.h>
+#include <Events_InfoMessage.h>
 
+#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_AttributeValidator.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
 
-#include <ModelAPI_AttributeDouble.h>
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Point2D.h>
 
+#include <QMessageBox>
+
 #include <string>
 #include <set>
 #include <sstream>
@@ -36,7 +39,11 @@ std::string toStdString(double theValue)
 {
   std::ostringstream sstream;
   sstream << theValue;
-  return sstream.str();
+  size_t aPos = sstream.str().find(".");
+  std::string aPnt = "";
+  if (aPos == std::string::npos)
+    aPnt = ".";
+  return sstream.str() + aPnt;
 }
 
 std::set<std::string> toSet(const std::list<std::string>& theContainer)
@@ -84,13 +91,14 @@ void ParametersPlugin_EvalListener::processEvent(
   } 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, 
-                                               const std::shared_ptr<ModelAPI_Document>& theDocument)
+double ParametersPlugin_EvalListener::evaluate(FeaturePtr theParameter,
+  const std::string& theExpression, std::string& theError)
 {
   std::list<std::string> anExprParams = myInterp->compile(theExpression);
   // find expression's params in the model
@@ -100,7 +108,11 @@ double ParametersPlugin_EvalListener::evaluate(const std::string& theExpression,
     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;
+    // 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;
 
     aContext.push_back(*it + "=" + toStdString(aValue));
   }
@@ -116,11 +128,25 @@ void ParametersPlugin_EvalListener::processEvaluationEvent(
   std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
       std::dynamic_pointer_cast<ModelAPI_AttributeEvalMessage>(theMessage);
 
+  FeaturePtr aParamFeature = 
+    std::dynamic_pointer_cast<ModelAPI_Feature>(aMessage->attribute()->owner());
+  if (aMessage->attribute()->attributeType() == ModelAPI_AttributeInteger::typeId()) {
+    AttributeIntegerPtr anAttribute =
+        std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(aMessage->attribute());
+    std::string anError;
+    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<std::string>());
+    anAttribute->setExpressionInvalid(!isValid);
+    anAttribute->setExpressionError(anAttribute->text().empty() ? "" : anError);
+  } else
   if (aMessage->attribute()->attributeType() == ModelAPI_AttributeDouble::typeId()) {
     AttributeDoublePtr anAttribute =
         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aMessage->attribute());
     std::string anError;
-    double aValue = evaluate(anAttribute->text(), anError, anAttribute->owner()->document());
+    double aValue = evaluate(aParamFeature, anAttribute->text(), anError);
     bool isValid = anError.empty();
     if (isValid)
       anAttribute->setCalculatedValue(aValue);
@@ -143,7 +169,7 @@ void ParametersPlugin_EvalListener::processEvaluationEvent(
     };
     for (int i = 0; i < 3; ++i) {
       std::string anError;
-      double aValue = evaluate(aText[i], anError, anAttribute->owner()->document());
+      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<std::string>());
@@ -167,7 +193,7 @@ void ParametersPlugin_EvalListener::processEvaluationEvent(
     };
     for (int i = 0; i < 2; ++i) {
       std::string anError;
-      double aValue = evaluate(aText[i], anError, anAttribute->owner()->document());
+      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<std::string>());
@@ -241,6 +267,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<ModelAPI_AttributeInteger>(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<ModelAPI_AttributeDouble>(theAttribute);
@@ -279,7 +313,7 @@ void ParametersPlugin_EvalListener::renameInAttribute(
   }
 }
 
-void ParametersPlugin_EvalListener::renameInDependants(std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
+void ParametersPlugin_EvalListener::renameInDependents(std::shared_ptr<ModelAPI_ResultParameter> theResultParameter,
                                                        const std::string& theOldName,
                                                        const std::string& theNewName)
 {
@@ -304,7 +338,8 @@ void ParametersPlugin_EvalListener::renameInDependants(std::shared_ptr<ModelAPI_
 
 bool isValidAttribute(const AttributePtr& theAttribute)
 {
-  std::string aValidator, anError;
+  std::string aValidator;
+  Events_InfoMessage anError;
   return ModelAPI_Session::get()->validators()->validate(theAttribute, aValidator, anError);
 }
 
@@ -312,7 +347,7 @@ 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<ParametersPlugin_Parameter> aParameter = 
       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
@@ -348,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())
@@ -358,7 +405,7 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent(
     return;
   }
 
-  renameInDependants(aResultParameter, aMessage->oldName(), aMessage->newName());
+  renameInDependents(aResultParameter, aMessage->oldName(), aMessage->newName());
 }
 
 void ParametersPlugin_EvalListener::processReplaceParameterEvent(
@@ -383,5 +430,5 @@ void ParametersPlugin_EvalListener::processReplaceParameterEvent(
   double aRealValue = aResultParameter->data()->real(ModelAPI_ResultParameter::VALUE())->value();
   std::string aValue = toStdString(aRealValue);
 
-  renameInDependants(aResultParameter, aResultParameter->data()->name(), aValue);
+  renameInDependents(aResultParameter, aResultParameter->data()->name(), aValue);
 }