From 1e8d15ec81b81263202acbc75b61ede88dbca4a6 Mon Sep 17 00:00:00 2001
From: gpernot <guillaume.pernot@c-s.fr>
Date: Thu, 9 Jan 2020 15:06:23 +0100
Subject: [PATCH] Allow UserValue modification from within application's
 DoExecute

---
 .../AppImageUtils/app/otbExtractROI.cxx             | 13 ++++++++-----
 .../include/otbWrapperApplication.h                 |  6 ++++++
 .../ApplicationEngine/src/otbWrapperApplication.cxx | 11 +++++++++++
 3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/Modules/Applications/AppImageUtils/app/otbExtractROI.cxx b/Modules/Applications/AppImageUtils/app/otbExtractROI.cxx
index 6a1a17c46b..707889a3c7 100644
--- a/Modules/Applications/AppImageUtils/app/otbExtractROI.cxx
+++ b/Modules/Applications/AppImageUtils/app/otbExtractROI.cxx
@@ -364,10 +364,13 @@ private:
     inImage->UpdateOutputInformation();
     if (region.Crop(inImage->GetLargestPossibleRegion()))
     {
-      SetParameterInt("sizex", region.GetSize(0));
-      SetParameterInt("sizey", region.GetSize(1));
-      SetParameterInt("startx", region.GetIndex(0));
-      SetParameterInt("starty", region.GetIndex(1));
+      /* SetParameterInt() resets UserValue flag when called from DoExecute(). Disable this behaviour.*/
+      DisableInPrivateDo();
+      SetParameterInt("sizex", region.GetSize(0), IsParameterEnabled("sizex") && HasUserValue("sizex"));
+      SetParameterInt("sizey", region.GetSize(1), IsParameterEnabled("sizey") && HasUserValue("sizey"));
+      SetParameterInt("startx", region.GetIndex(0), IsParameterEnabled("startx") && HasUserValue("startx"));
+      SetParameterInt("starty", region.GetIndex(1), IsParameterEnabled("starty") && HasUserValue("starty"));
+      EnableInPrivateDo(); // Restore default
       return true;
     }
     return false;
@@ -803,7 +806,7 @@ private:
 
     if (!CropRegionOfInterest())
       otbAppLogWARNING(<< "Could not extract the ROI as it is out of the "
-                          "input image.");
+                       "input image.");
 
     ExtractROIFilterType::Pointer extractROIFilter = ExtractROIFilterType::New();
     extractROIFilter->SetInput(inImage);
diff --git a/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h b/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h
index f0f41310cd..e145352b49 100644
--- a/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h
+++ b/Modules/Wrappers/ApplicationEngine/include/otbWrapperApplication.h
@@ -920,6 +920,12 @@ protected:
   /** Enable/Disable multiWriting */
   itkSetMacro(MultiWriting, bool);
 
+  /* Enable in-application prevention of modifications to m_UserValue (default behaviour) */
+  void EnableInPrivateDo();
+
+  /* Disable in-application prevention of modifications to m_UserValue */
+  void DisableInPrivateDo();
+
 private:
   /* Implement this method to add parameters */
   virtual void DoInit() = 0;
diff --git a/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx b/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx
index c0a8705478..02df87e476 100644
--- a/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx
+++ b/Modules/Wrappers/ApplicationEngine/src/otbWrapperApplication.cxx
@@ -1871,5 +1871,16 @@ bool Application::IsMultiWritingEnabled()
   return m_MultiWriting;
 }
 
+void Application::EnableInPrivateDo()
+{
+  m_IsInPrivateDo = true;
+
+}
+
+void Application::DisableInPrivateDo()
+{
+  m_IsInPrivateDo = false;
+}
+
 }
 }
-- 
GitLab