From a067c3576c0f24c2ad7023f5ca7e06bad0fd2dd2 Mon Sep 17 00:00:00 2001
From: Otmane Lahlou <otmane.lahlou@c-s.fr>
Date: Wed, 26 Oct 2011 17:39:49 +0200
Subject: [PATCH] ENH: add Reset button for NumericalParameter      - Add new
 methods SetDefaultParameterInt and SetDefaultParameterFloat        useful for
 setting default values in the method DoCreateParameters()      - Add button
 Reset to each NumericalParameter, and connect this button        to a slot
 Reset() setting the value to the default one if any, making        the state
 HasUserValue and HasAutomaticValue to false

---
 .../Projections/otbOrthoRectification.cxx     | 10 +++---
 .../otbWrapperApplication.cxx                 | 36 +++++++++++++++++++
 .../ApplicationEngine/otbWrapperApplication.h | 22 ++++++++++++
 .../otbWrapperQtWidgetParameterBase.cxx       |  9 +++++
 .../otbWrapperQtWidgetParameterBase.h         |  1 +
 .../otbWrapperQtWidgetParameterGroup.cxx      | 22 ++++++++++--
 .../otbWrapperQtWidgetParameterGroup.h        |  2 ++
 7 files changed, 95 insertions(+), 7 deletions(-)

diff --git a/Applications/Projections/otbOrthoRectification.cxx b/Applications/Projections/otbOrthoRectification.cxx
index 60dccdfc0b..466b7a79e6 100644
--- a/Applications/Projections/otbOrthoRectification.cxx
+++ b/Applications/Projections/otbOrthoRectification.cxx
@@ -156,7 +156,7 @@ private:
     std::ostringstream oss;
     oss << "Activate RPC sensor model estimation"<<std::endl;
     oss << "Parameter is the number of control points per axis";
-    SetParameterDescription("rpc",oss.str().c_str());
+    //SetParameterDescription("rpc",oss.str().c_str());
     MandatoryOff("rpc");
 
     // Interpolators
@@ -165,7 +165,7 @@ private:
     AddChoice("interpolator.nn",     "Nearest Neighbor");
     AddChoice("interpolator.bco",    "BCO");
     AddParameter(ParameterType_Radius, "interpolator.bco.radius", "Radius");
-    SetParameterInt("interpolator.bco.radius", 2);
+    SetDefaultParameterInt("interpolator.bco.radius", 2);
 
     // Built the Output Map Projection
     AddParameter(ParameterType_Choice, "map", "Map Projection");
@@ -178,12 +178,12 @@ private:
 
     AddChoice("map.epsg","EPSG");
     AddParameter(ParameterType_Int, "map.epsg.code", "EPSG Code");    
-    SetParameterInt("map.epsg.code",32631);
+    SetDefaultParameterInt("map.epsg.code",32631);
     SetParameterString("map", "epsg");
 
     // Deformation Field Spacing
     AddParameter(ParameterType_Float, "gridspacing", "Deformation Field Spacing");
-    SetParameterInt("gridspacing", 4.);
+    SetDefaultParameterFloat("gridspacing", 4.);
     SetParameterDescription("gridspacing", "Generate a coarser deformation field with the given spacing");
     MandatoryOff("gridspacing");
   }
@@ -223,7 +223,9 @@ private:
       
       // Fill the Gui with the computed parameters      
       if (!HasUserValue("outputs.ulx"))
+        {
         SetParameterFloat("outputs.ulx", genericRSEstimator->GetOutputOrigin()[0]);
+        }
       
       if (!HasUserValue("outputs.uly"))
         SetParameterFloat("outputs.uly", genericRSEstimator->GetOutputOrigin()[1]);
diff --git a/Code/ApplicationEngine/otbWrapperApplication.cxx b/Code/ApplicationEngine/otbWrapperApplication.cxx
index 7da18b37fa..0b088ba619 100644
--- a/Code/ApplicationEngine/otbWrapperApplication.cxx
+++ b/Code/ApplicationEngine/otbWrapperApplication.cxx
@@ -419,6 +419,42 @@ void Application::SetParameterFloat(std::string parameter, float value)
     }
 }
 
+void Application::SetDefaultParameterInt(std::string parameter, int value)
+{
+  Parameter* param = GetParameterByKey(parameter);
+
+  if (dynamic_cast<IntParameter*>(param))
+    {
+    IntParameter* paramInt = dynamic_cast<IntParameter*>(param);
+    paramInt->SetDefaultValue(value);
+    paramInt->SetValue(value);
+    }
+  else if (dynamic_cast<FloatParameter*>(param))
+    {
+    FloatParameter* paramFloat = dynamic_cast<FloatParameter*>(param);
+    paramFloat->SetDefaultValue(static_cast<float>(value));
+    paramFloat->SetValue(static_cast<float>(value));
+    }
+  else if (dynamic_cast<RadiusParameter*>(param))
+    {
+    RadiusParameter* paramRadius = dynamic_cast<RadiusParameter*>(param);
+    paramRadius->SetDefaultValue(value);
+    paramRadius->SetValue(value);
+    }
+}
+
+void Application::SetDefaultParameterFloat(std::string parameter, float value)
+{
+  Parameter* param = GetParameterByKey(parameter);
+
+  if (dynamic_cast<FloatParameter*>(param))
+    {
+    FloatParameter* paramFloat = dynamic_cast<FloatParameter*>(param);
+    paramFloat->SetDefaultValue(value);
+    paramFloat->SetValue(value);
+    }
+}
+
 void Application::SetParameterString(std::string parameter, std::string value)
 {
   Parameter* param = GetParameterByKey(parameter);
diff --git a/Code/ApplicationEngine/otbWrapperApplication.h b/Code/ApplicationEngine/otbWrapperApplication.h
index 37f56e4077..d5d6d73669 100644
--- a/Code/ApplicationEngine/otbWrapperApplication.h
+++ b/Code/ApplicationEngine/otbWrapperApplication.h
@@ -183,6 +183,28 @@ public:
    */
   void SetParameterFloat(std::string parameter, float value);
 
+  /* Set an default integer value,must used in the 
+   * DoCreateParameters when setting a value by default
+   * for the parameter  
+   *
+   * Can be called for types :
+   * \li ParameterType_Int
+   * \li ParameterType_Float
+   * \li ParameterType_Radius
+   * \li ParameterType_Choice
+   */
+  void SetDefaultParameterInt(std::string parameter, int value);
+
+  /* Set a default floating value, must used in the 
+   * DoCreateParameters when setting a value by default
+   * for the parameter 
+   *
+   * Can be called for types :
+   * \li ParameterType_Float
+   */
+  void SetDefaultParameterFloat(std::string parameter, float value);
+
+
   /* Set a string value
    *
    * Can be called for types :
diff --git a/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterBase.cxx b/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterBase.cxx
index 4a6ce5d4d8..47376344c2 100644
--- a/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterBase.cxx
+++ b/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterBase.cxx
@@ -76,5 +76,14 @@ void QtWidgetParameterBase::SetActivationState( bool value )
   m_Param->SetChecked(value);
   m_Param->SetActive(value);
 }
+
+// Slot connected to the signal emitted by the Reset Button
+void QtWidgetParameterBase::Reset(  )
+{
+  m_Param->Reset();
+  m_Param->SetUserValue(false);
+  m_Param->SetAutomaticValue(false);
+  this->UpdateGUI();
+}
 }
 }
diff --git a/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterBase.h b/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterBase.h
index d77e24c58c..e8d2cffcc8 100644
--- a/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterBase.h
+++ b/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterBase.h
@@ -42,6 +42,7 @@ public:
 public slots:
   void UpdateGUI();
   virtual void SetActivationState( bool value );
+  void Reset();
 
 protected slots:
   void ParameterChanged(const QString& key);
diff --git a/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterGroup.cxx b/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterGroup.cxx
index 5d1a213d99..b6449db830 100644
--- a/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterGroup.cxx
+++ b/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterGroup.cxx
@@ -63,7 +63,7 @@ void QtWidgetParameterGroup::DoCreateWidget()
 
       if (paramAsGroup == 0 && paramAsChoice == 0)
         {
-        // Label (col 0)
+        // Label (col 1)
         QWidget* label = new QtWidgetParameterLabel( param );
         gridLayout->addWidget(label, i, 1);
 
@@ -71,7 +71,7 @@ void QtWidgetParameterGroup::DoCreateWidget()
         QtWidgetParameterBase* specificWidget = QtWidgetParameterFactory::CreateQtWidget( param, GetModel() );
         gridLayout->addWidget(specificWidget, i, 2 );
 
-        // CheckBox (col 1)
+        // CheckBox (col 0)
         QCheckBox * checkBox = new QCheckBox;
         connect( checkBox, SIGNAL(clicked(bool)), specificWidget, SLOT(SetActivationState(bool)));
         connect( checkBox, SIGNAL(clicked(bool)), GetModel(), SLOT(NotifyUpdate()) );
@@ -91,8 +91,24 @@ void QtWidgetParameterGroup::DoCreateWidget()
           checkBox->setEnabled(true);
           specificWidget->setEnabled(false);
           }
-
         gridLayout->addWidget(checkBox, i, 0);
+
+        // Reset Button 
+        // Make sense only for NumericalParameter
+        if (dynamic_cast<IntParameter*>(param)
+            || dynamic_cast<FloatParameter*>(param) 
+            || dynamic_cast<RadiusParameter*>(param) )
+          {
+          QPushButton* resetButton = new QPushButton;
+          resetButton->setText("Reset");
+          resetButton->setToolTip("Reset the value of this parameter");
+          gridLayout->addWidget(resetButton, i, 3);
+          
+          // Slots to connect to the reset button
+          connect( resetButton, SIGNAL(clicked()), specificWidget, SLOT(Reset()) );
+          connect( resetButton, SIGNAL(clicked()), GetModel(), SLOT(NotifyUpdate()) );
+          }
+
         m_WidgetList.push_back(specificWidget);
         }
       else
diff --git a/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterGroup.h b/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterGroup.h
index fb4679ce41..3ac3f35dac 100644
--- a/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterGroup.h
+++ b/Code/Wrappers/QtWidget/otbWrapperQtWidgetParameterGroup.h
@@ -20,6 +20,8 @@
 
 #include <QtGui>
 #include "otbWrapperParameterGroup.h"
+#include "otbWrapperNumericalParameter.h"
+#include "otbWrapperRadiusParameter.h"
 #include "otbWrapperQtWidgetParameterBase.h"
 
 namespace otb
-- 
GitLab