Salome HOME
[Code coverage ParametersPlugin]: Unit tests for parameter remove and for error treating
[modules/shaper.git] / src / ParametersPlugin / ParametersPlugin_EvalListener.cpp
index 0d61bec9ff3aa2e3e0d1a7671bb9c6cbe69b5b89..cc299083317be073bda56b3ab5da4bda2d24feca 100644 (file)
@@ -1,10 +1,22 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-/*
- * ParametersPlugin_EvalListener.cpp
- *
- *  Created on: Apr 28, 2015
- *      Author: sbh
- */
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// 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<mailto:webmaster.salome@opencascade.com>
+//
 
 #include <pyconfig.h>
 
@@ -20,6 +32,7 @@
 #include <ModelAPI_AttributeValidator.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Events.h>
+#include <ModelAPI_ResultParameter.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
 
@@ -140,9 +153,9 @@ void ParametersPlugin_EvalListener::renameInParameter(
                                                 theNewName);
   // 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(
@@ -202,21 +215,21 @@ void ParametersPlugin_EvalListener::renameInDependents(
               const std::string& theNewName)
 {
   std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
-      theResultParameter->data()->refsToMe();
+    theResultParameter->data()->refsToMe();
   std::set<std::shared_ptr<ModelAPI_Attribute> >::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<ParametersPlugin_Parameter> aParameter =
-          std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
-              anAttribute->owner());
+        std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
+        anAttribute->owner());
       if (aParameter.get())
         // Rename
         renameInParameter(aParameter, theOldName, theNewName);
     } else
-        // Rename
-        renameInAttribute(anAttribute, theOldName, theNewName);
+      // Rename
+      renameInAttribute(anAttribute, theOldName, theNewName);
   }
 }
 
@@ -229,18 +242,19 @@ bool isValidAttribute(const AttributePtr& theAttribute)
 
 void setParameterName(ResultParameterPtr theResultParameter, const std::string& theName)
 {
-  theResultParameter->data()->blockSendAttributeUpdated(true);
+  bool aWasBlocked = theResultParameter->data()->blockSendAttributeUpdated(true);
   theResultParameter->data()->setName(theName);
-  theResultParameter->data()->blockSendAttributeUpdated(false, false);
+  theResultParameter->data()->blockSendAttributeUpdated(aWasBlocked, false);
 
   std::shared_ptr<ParametersPlugin_Parameter> aParameter =
       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
           ModelAPI_Feature::feature(theResultParameter));
 
-  aParameter->data()->blockSendAttributeUpdated(true);
+  std::string anOldName = aParameter->name();
+  aWasBlocked = aParameter->data()->blockSendAttributeUpdated(true);
   aParameter->data()->setName(theName);
   aParameter->string(ParametersPlugin_Parameter::VARIABLE_ID())->setValue(theName);
-  aParameter->data()->blockSendAttributeUpdated(false);
+  aParameter->data()->blockSendAttributeUpdated(aWasBlocked);
 }
 
 void ParametersPlugin_EvalListener::processObjectRenamedEvent(
@@ -281,18 +295,21 @@ void ParametersPlugin_EvalListener::processObjectRenamedEvent(
 
   // 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<ModelAPI_Feature>& theFeature,
-  //                                           const std::string& theAttribute) const
-  // is ready
   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;
   }
 
-  renameInDependents(aResultParameter, aMessage->oldName(), aMessage->newName());
+  std::string 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(