From 82d13a79d54e61845598ed1322a6350464f1b191 Mon Sep 17 00:00:00 2001
From: Guillaume Pasero <guillaume.pasero@c-s.fr>
Date: Fri, 9 Dec 2011 11:38:57 +0100
Subject: [PATCH] ENH: improved handling of parameter trees

---
 .../otbWrapperApplication.cxx                 | 36 ++++++++++++++++++-
 .../otbWrapperChoiceParameter.cxx             | 30 ++++++++++++++++
 2 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/Code/ApplicationEngine/otbWrapperApplication.cxx b/Code/ApplicationEngine/otbWrapperApplication.cxx
index 9cdb26cc5f..f7a6252a7e 100644
--- a/Code/ApplicationEngine/otbWrapperApplication.cxx
+++ b/Code/ApplicationEngine/otbWrapperApplication.cxx
@@ -1179,10 +1179,44 @@ Application::IsApplicationReady()
       //  - The param is not root and belonging to a Mandatory Group
       //    wich is activated
       if ( !this->HasValue(*it)  && IsMandatory(*it) )
-        if( GetParameterByKey(*it)->IsRoot() || GetParameterByKey(*it)->GetRoot()->GetActive() )
+        {
+        if( GetParameterByKey(*it)->IsRoot() )
           {
+          otbDebugMacro("MISSING : "<< (*it).c_str() << " ( Is Root)");
           return false;
           }
+        else
+          {
+          // check if the parameter is linked to a root parameter with a chain of active parameters
+          Parameter* currentParam = GetParameterByKey(*it)->GetRoot();
+          if (currentParam->IsRoot())
+            {
+            otbDebugMacro("MISSING : "<< (*it).c_str() << " ( Is Level 1)");
+            return false;
+            }
+          
+          int level = 1;
+          
+          while (!currentParam->IsRoot())
+            {
+            if (!currentParam->GetActive())
+              {
+              // the missing parameter is not on an active branch : we can ignore it
+              break;
+              }
+            currentParam = currentParam->GetRoot();
+            
+            level++;
+            
+            if (currentParam->IsRoot())
+              {
+              // the missing parameter is on an active branch : we need it
+              otbDebugMacro("MISSING : "<< (*it).c_str() << " ( Is Level "<< level<<")");
+              return false;
+              } 
+            }
+          }
+        }
       }
     }
 
diff --git a/Code/ApplicationEngine/otbWrapperChoiceParameter.cxx b/Code/ApplicationEngine/otbWrapperChoiceParameter.cxx
index bc4ebafd38..66d184a0d6 100644
--- a/Code/ApplicationEngine/otbWrapperChoiceParameter.cxx
+++ b/Code/ApplicationEngine/otbWrapperChoiceParameter.cxx
@@ -40,8 +40,16 @@ ChoiceParameter::AddChoice( std::string choicekey, std::string choiceName )
   choice.m_Name = choiceName;
   choice.m_AssociatedParameter = ParameterGroup::New();
   choice.m_AssociatedParameter->SetName(choiceName);
+  choice.m_AssociatedParameter->SetRoot(this);
+  
   m_ChoiceList.push_back(choice);
 
+  // check if the new choice matches the m_CurrentChoice : if so the group should be active.
+  if (m_CurrentChoice == (m_ChoiceList.size() - 1))
+    {
+    m_ChoiceList[m_CurrentChoice].m_AssociatedParameter->SetActive(true);
+    }
+
   // Add the associated parameter as a child
   // in order to not have a gap in the children hierarchy
   this->AddChild(choice.m_AssociatedParameter.GetPointer());
@@ -123,6 +131,28 @@ ChoiceParameter::SetValue(unsigned int v)
 {
   m_CurrentChoice = v;
   SetActive(true);
+  // update the active flag in sub parameters
+  for (unsigned int i=0; i< m_ChoiceList.size();i++)
+    {
+    if (m_ChoiceList[i].m_AssociatedParameter)
+      {
+      if (i==m_CurrentChoice)
+        {
+        if (!m_ChoiceList[i].m_AssociatedParameter->GetActive())
+          {
+          m_ChoiceList[i].m_AssociatedParameter->SetActive(true);
+          }
+        }
+      else
+        {      
+        if (m_ChoiceList[i].m_AssociatedParameter->GetActive())
+          {
+          m_ChoiceList[i].m_AssociatedParameter->SetActive(false);
+          }
+        }
+      }
+    }
+  
   // Call Modified();
   this->Modified();
 }
-- 
GitLab