From c2aae933c090c6fd07fbabf256eca86656cf1666 Mon Sep 17 00:00:00 2001
From: Julien Malik <julien.malik@c-s.fr>
Date: Sun, 19 Jun 2011 16:24:02 +0200
Subject: [PATCH] BUG: fix choices

---
 Code/Core/otbWrapperApplication.cxx     | 22 +++++++++++++++++
 Code/Core/otbWrapperApplication.h       |  7 ++++++
 Code/Core/otbWrapperChoiceParameter.cxx | 32 +++++++++++++++++++++++++
 Code/Core/otbWrapperChoiceParameter.h   |  8 +++++++
 Code/Wrappers/PyQt/otbapp/widgets.py    | 31 +++++++++++++++++++-----
 Code/Wrappers/SWIG/otbApplication.i     |  3 +++
 Example/otbTestApplication.cxx          |  7 ++++++
 7 files changed, 104 insertions(+), 6 deletions(-)

diff --git a/Code/Core/otbWrapperApplication.cxx b/Code/Core/otbWrapperApplication.cxx
index a7f683dde6..96f45e1201 100644
--- a/Code/Core/otbWrapperApplication.cxx
+++ b/Code/Core/otbWrapperApplication.cxx
@@ -233,6 +233,28 @@ ParameterType Application::GetParameterType(std::string paramKey) const
   return type;
 }
 
+std::vector<std::string> Application::GetChoiceKeys(std::string name)
+{
+  Parameter* param = GetParameterByKey(name);
+  if (dynamic_cast<ChoiceParameter*>(param))
+    {
+    ChoiceParameter* paramChoice = dynamic_cast<ChoiceParameter*>(param);
+    return paramChoice->GetChoiceKeys();
+    }
+  itkExceptionMacro(<< name << " is not a choice parameter");
+}
+
+std::vector<std::string> Application::GetChoiceNames(std::string name)
+{
+  Parameter* param = GetParameterByKey(name);
+  if (dynamic_cast<ChoiceParameter*>(param))
+    {
+    ChoiceParameter* paramChoice = dynamic_cast<ChoiceParameter*>(param);
+    return paramChoice->GetChoiceNames();
+    }
+  itkExceptionMacro(<< name << " is not a choice parameter");
+}
+
 void Application::SetParameterInt(std::string parameter, int value)
 {
   Parameter* param = GetParameterByKey(parameter);
diff --git a/Code/Core/otbWrapperApplication.h b/Code/Core/otbWrapperApplication.h
index 1ccc04bd84..68bfa7210f 100644
--- a/Code/Core/otbWrapperApplication.h
+++ b/Code/Core/otbWrapperApplication.h
@@ -137,6 +137,13 @@ public:
   /* Get the parameter type from its name */
   ParameterType GetParameterType(std::string paramKey) const;
 
+  /* Returns the description of a parameter */
+  std::vector<std::string> GetChoiceKeys(std::string paramKey);
+
+  /* Returns the description of a parameter */
+  std::vector<std::string> GetChoiceNames(std::string paramKey);
+
+
   /* Set an integer value
    *
    * Can be called for types :
diff --git a/Code/Core/otbWrapperChoiceParameter.cxx b/Code/Core/otbWrapperChoiceParameter.cxx
index 2f0245cc0e..fd883db885 100644
--- a/Code/Core/otbWrapperChoiceParameter.cxx
+++ b/Code/Core/otbWrapperChoiceParameter.cxx
@@ -48,12 +48,44 @@ ChoiceParameter::GetChoiceKey( int i )
   return m_ChoiceList[i].m_Key;
 }
 
+
+std::vector<std::string>
+ChoiceParameter::GetChoiceKeys( )
+{
+  std::vector<std::string> ret;
+  ChoiceList::iterator it = m_ChoiceList.begin();
+
+  for (it = m_ChoiceList.begin(); it != m_ChoiceList.end(); ++it)
+    {
+    ret.push_back(it->m_Key);
+    }
+
+  return ret;
+}
+
+
 std::string
 ChoiceParameter::GetChoiceName( int i )
 {
   return m_ChoiceList[i].m_Name;
 }
 
+
+std::vector<std::string>
+ChoiceParameter::GetChoiceNames( )
+{
+  std::vector<std::string> ret;
+  ChoiceList::iterator it = m_ChoiceList.begin();
+
+  for (it = m_ChoiceList.begin(); it != m_ChoiceList.end(); ++it)
+    {
+    ret.push_back(it->m_Name);
+    }
+  return ret;
+}
+
+
+
 ParameterGroup::Pointer
 ChoiceParameter::GetChoiceParameterGroupByIndex( int i )
 {
diff --git a/Code/Core/otbWrapperChoiceParameter.h b/Code/Core/otbWrapperChoiceParameter.h
index b35af25587..1df1c302f1 100644
--- a/Code/Core/otbWrapperChoiceParameter.h
+++ b/Code/Core/otbWrapperChoiceParameter.h
@@ -54,17 +54,25 @@ public:
   /** Get the key of a specific choice value */
   std::string GetChoiceKey( int i );
 
+  /** Get the list of the different choice keys */
+  std::vector<std::string> GetChoiceKeys();
+
   /** Get the long name of a specific choice value */
   std::string GetChoiceName( int i );
 
+  /** Get the list of the different choice keys */
+  std::vector<std::string> GetChoiceNames();
+
   /** Get the ParameterGroup associated to a choice value */
   ParameterGroup::Pointer GetChoiceParameterGroupByIndex( int i );
 
   /** Get the ParameterGroup associated to a choice value */
   ParameterGroup::Pointer GetChoiceParameterGroupByKey( std::string choiceKey );
 
+  /** Get all parameters that are child of this choice parameter */
   std::vector<std::string> GetParametersKeys();
 
+
   /** Get the number of available choice */
   unsigned int GetNbChoices( void );
 
diff --git a/Code/Wrappers/PyQt/otbapp/widgets.py b/Code/Wrappers/PyQt/otbapp/widgets.py
index be70170f8a..2d58ceac70 100644
--- a/Code/Wrappers/PyQt/otbapp/widgets.py
+++ b/Code/Wrappers/PyQt/otbapp/widgets.py
@@ -103,24 +103,43 @@ class QParameterChoice(QParameterBase):
         
         stack = QtGui.QStackedWidget()
         
-        allparams = app.GetParametersKeys()
-        subgroups = set()
+        allparams = app.GetParametersKeys(True)
+        print str(allparams)
+        
+        subgroups = []
+        choicelist = []
         for paramKey in allparams:
+            print 'paramKey ' + paramKey
             if paramKey.startswith(self._paramKey):
+                choicelist.append(paramKey)
                 choiceSubParam = paramKey.partition(self._paramKey + '.')[2].partition('.')[0]
                 if choiceSubParam:
-                    subgroups.add(choiceSubParam.partition('.')[0])
-        
+                    print "choiceSubParam "+ choiceSubParam
+                    subgroups.append(choiceSubParam.partition('.')[0])
+                else:
+                    print "choiceSubParam None"
+                    subgroups.append(None)
+        
+        print 'choicelist ' + str(choicelist)
+        for choice in zip(app.GetChoiceKeys(self._paramKey), app.GetChoiceNames(self._paramKey)):
+            combo.addItem(choice[1], choice[0])
+            
         for subgroup in subgroups:
-            combo.addItem(subgroup, subgroup)
+            if not subgroup:
+                continue
             widget = QParameterGroup(self.GetModel(), self._paramKey + '.' + subgroup)
             if widget:
                 widget.CreateWidget()
                 stack.addWidget(widget)
-            
+            else:
+                stack.addWidget(QtGui.QWidget())
+
+        self.connect(combo, QtCore.SIGNAL("currentIndexChanged(int)"), self.SetValue)
+        self.connect(combo, QtCore.SIGNAL("currentIndexChanged(int)"), stack.setCurrentIndex)
 
         layout.addWidget(combo)
         layout.addWidget(stack)
+        layout.addStretch()
         self.setLayout(layout)
         
     def SetValue(self, val):
diff --git a/Code/Wrappers/SWIG/otbApplication.i b/Code/Wrappers/SWIG/otbApplication.i
index f6e39db73a..fb20a947ef 100644
--- a/Code/Wrappers/SWIG/otbApplication.i
+++ b/Code/Wrappers/SWIG/otbApplication.i
@@ -106,6 +106,9 @@ public:
   int GetParameterInt(std::string parameter);
   float GetParameterFloat(std::string parameter);
   std::string GetParameterString(std::string parameter);
+
+  std::vector<std::string> GetChoiceKeys(std::string choiceKey);
+  std::vector<std::string> GetChoiceNames(std::string choiceKey);
   
 protected:
   Application();
diff --git a/Example/otbTestApplication.cxx b/Example/otbTestApplication.cxx
index c27ddcf51c..3427ac8a40 100644
--- a/Example/otbTestApplication.cxx
+++ b/Example/otbTestApplication.cxx
@@ -68,6 +68,13 @@ private:
     AddChoice("choice.choice1", "Choice 1");
     AddChoice("choice.choice2", "Choice 2");
     AddChoice("choice.choice3", "Choice 3");
+    AddParameter(ParameterType_Float,  "choice.choice1.floatchoice1", "Float of choice1");
+    SetParameterFloat("choice.choice1.floatchoice1",   0.125);
+//    AddParameter(ParameterType_Float,  "choice.choice2.floatchoice2", "Float of choice2");
+//    SetParameterFloat("choice.choice2.floatchoice2",   1.0);
+    AddParameter(ParameterType_Float,  "choice.choice3.floatchoice3", "Float of choice3");
+    SetParameterFloat("choice.choice3.floatchoice3",   5.0);
+
     //AddParameter(ParameterType_Group, "group", "Group");
   }
 
-- 
GitLab