From 1fb25f3dd6b9693b5c19935a3489c12845a751a9 Mon Sep 17 00:00:00 2001
From: Cyrille Valladeau <cyrille.valladeau@c-s.fr>
Date: Tue, 8 Nov 2011 16:55:53 +0100
Subject: [PATCH] ENH: wrong input behaviour control

---
 .../otbWrapperInputVectorDataParameter.cxx    | 101 ++++++++++++++++++
 ...WrapperQtWidgetInputImageListParameter.cxx |  24 +++--
 .../otbWrapperQtWidgetInputImageParameter.cxx |  11 +-
 ...erQtWidgetInputVectorDataListParameter.cxx |  67 ++++++++++--
 4 files changed, 180 insertions(+), 23 deletions(-)
 create mode 100644 Code/ApplicationEngine/otbWrapperInputVectorDataParameter.cxx

diff --git a/Code/ApplicationEngine/otbWrapperInputVectorDataParameter.cxx b/Code/ApplicationEngine/otbWrapperInputVectorDataParameter.cxx
new file mode 100644
index 0000000000..0aa0f7a53a
--- /dev/null
+++ b/Code/ApplicationEngine/otbWrapperInputVectorDataParameter.cxx
@@ -0,0 +1,101 @@
+/*=========================================================================
+
+  Program:   ORFEO Toolbox
+  Language:  C++
+  Date:      $Date$
+  Version:   $Revision$
+
+
+  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
+  See OTBCopyright.txt for details.
+
+
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+
+=========================================================================*/
+
+#include "otbWrapperInputVectorDataParameter.h"
+
+namespace otb
+{
+namespace Wrapper
+{
+
+InputVectorDataParameter::InputVectorDataParameter()
+{
+  this->SetName("Input VectorData");
+  this->SetKey("in");
+  this->ClearValue();
+}
+
+InputVectorDataParameter::~InputVectorDataParameter()
+{
+}
+
+bool
+InputVectorDataParameter::SetFromFileName(const std::string& filename)
+{
+  // First clear previous file choosen
+  this->ClearValue();
+
+  // TODO : when the logger will be available, redirect the exception
+  // in the logger (like what is done in MsgReporter)
+  if (!filename.empty()
+      && itksys::SystemTools::FileExists(filename.c_str()))
+    {
+    VectorDataFileReaderType::Pointer reader = VectorDataFileReaderType::New();
+    reader->SetFileName(filename);
+    try
+      {
+      reader->UpdateOutputInformation();
+      }
+    catch(itk::ExceptionObject & err)
+      {
+      return true;
+      }
+
+    // the specified filename is valid => store the value
+    m_FileName = filename;
+    SetActive(true);
+    }
+ return false;
+}
+
+
+VectorDataType*
+InputVectorDataParameter::GetVectorData() const
+{
+  return m_VectorData;
+}
+
+void
+InputVectorDataParameter::SetVectorData(VectorDataType* vectorData)
+{
+  m_VectorData = vectorData;
+}
+
+
+bool
+InputVectorDataParameter::HasValue() const
+{
+  if( m_FileName.empty() && m_VectorData.IsNull() )
+    return false;
+  else
+    return true;
+}
+
+void
+InputVectorDataParameter::ClearValue()
+{
+ m_VectorData  = NULL;
+ m_Reader = NULL;
+ m_FileName = "";
+}
+
+
+}
+}
+
+
diff --git a/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputImageListParameter.cxx b/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputImageListParameter.cxx
index 67fadae127..246f72a750 100644
--- a/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputImageListParameter.cxx
+++ b/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputImageListParameter.cxx
@@ -345,16 +345,26 @@ void QtWidgetInputImageListParameter::RecreateImageList()
 {
   // save value
   m_InputImageListParam->ClearValue();
+  const unsigned int initSize = m_FileSelectionList.size();
   for(unsigned int j=0; j<m_FileSelectionList.size(); j++ )
     {
-    m_InputImageListParam->AddFromFileName(m_FileSelectionList[j]->GetFilename());
-    connect( m_FileSelectionList[j]->GetInput(), SIGNAL(textChanged(const QString&)), this, SLOT(UpdateImageList()) );
+    if(m_InputImageListParam->AddFromFileName(m_FileSelectionList[j]->GetFilename()) == true )
+      connect( m_FileSelectionList[j]->GetInput(), SIGNAL(textChanged(const QString&)), this, SLOT(UpdateImageList()) );
+    else
+      {
+      m_FileSelectionList[j]->SetChecked(true);
+      }
     }
-
-  emit Change();
-  // notify of value change
-  QString key( QString::fromStdString(m_InputImageListParam->GetKey()) );
-  emit ParameterChanged(key);
+  
+  if( initSize == m_FileSelectionList.size() )
+    {
+    emit Change();
+      // notify of value change
+    QString key( QString::fromStdString(m_InputImageListParam->GetKey()) );
+    emit ParameterChanged(key);
+    }
+  else
+    this->SupressFile();
 }
 
 
diff --git a/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputImageParameter.cxx b/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputImageParameter.cxx
index 849688f9bc..c724beea65 100644
--- a/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputImageParameter.cxx
+++ b/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputImageParameter.cxx
@@ -78,11 +78,12 @@ void QtWidgetInputImageParameter::SelectFile()
 void QtWidgetInputImageParameter::SetFileName(const QString& value)
 {
   // save value
-  m_InputImageParam->SetFromFileName(value.toStdString());
-
-  // notify of value change
-  QString key( QString::fromStdString(m_InputImageParam->GetKey()) );
-  emit ParameterChanged(key);
+  if(m_InputImageParam->SetFromFileName(value.toStdString()) == false )
+    {
+    // notify of value change
+    QString key( QString::fromStdString(m_InputImageParam->GetKey()) );
+    emit ParameterChanged(key);
+    }
 }
 
 }
diff --git a/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputVectorDataListParameter.cxx b/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputVectorDataListParameter.cxx
index 768baa36ef..c7bf9c314c 100644
--- a/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputVectorDataListParameter.cxx
+++ b/Code/Wrappers/QtWidget/otbWrapperQtWidgetInputVectorDataListParameter.cxx
@@ -137,13 +137,38 @@ void QtWidgetInputVectorDataListParameter::DoCreateWidget()
 void
 QtWidgetInputVectorDataListParameter::UpdateVectorDataList()
 {
+std::cout<<"UpdateVectorDataList"<<std::endl;
   // save value
+  bool update = false;
+
+ std::cout<<m_FileSelectionList.size()<<std::endl;
+ std::cout<<m_InputVectorDataListParam<<std::endl;
+ std::cout<<"m_InputVectorDataListParam->GetFileNameList().size()"<<std::endl;
+  std::cout<<m_InputVectorDataListParam->GetFileNameList().size()<<std::endl;
+  std::cout<<m_InputVectorDataListParam->GetVectorDataList()->Size()<<std::endl;
+
+
   for(unsigned int j=0; j<m_InputVectorDataListParam->GetVectorDataList()->Size(); j++ )
     {
-    m_InputVectorDataListParam->SetNthFileName(j, m_FileSelectionList[j]->GetFilename());
+    std::cout<<"UpdateVectorDataList,loop "<<j<<std::endl;
+    if( m_InputVectorDataListParam->SetNthFileName(j, m_FileSelectionList[j]->GetFilename()) == true )
+      {
+      std::cout<<"UpdateVectorDataList, can't create vd"<<std::endl;
+      m_FileSelectionList[j]->SetChecked(true);
+      m_InputVectorDataListParam->AddNullElement();
+      update = true;
+      }
     }
 
-  emit Change();
+ std::cout<<m_FileSelectionList.size()<<std::endl;
+  std::cout<<m_InputVectorDataListParam->GetFileNameList().size()<<std::endl;
+  std::cout<<m_InputVectorDataListParam->GetVectorDataList()->Size()<<std::endl;
+
+  if(update)
+    this->SupressFile();
+  else
+    emit Change();
+ 
 }
 
 
@@ -291,16 +316,22 @@ QtWidgetInputVectorDataListParameter::AddFile()
 void
 QtWidgetInputVectorDataListParameter::SupressFile()
 {
+  std::cout<<"QtWidgetInputVectorDataListParameter::SupressFile "<<m_FileSelectionList.size()<<std::endl;
   m_FileLayout = new QVBoxLayout();
-  m_FileLayout->setSpacing(0);
+  m_FileLayout->setSpacing(0 );
   std::vector<QtFileSelectionWidget *> tmpList;
   for (unsigned int i = 0; i < m_FileSelectionList.size(); i++)
     {
+    std::cout<<"QtWidgetInputVectorDataListParameter::SupressFile "<<i<<std::endl;
     if (!m_FileSelectionList[i]->IsChecked())
       {
       m_FileLayout->addWidget(m_FileSelectionList[i]);
       tmpList.push_back(m_FileSelectionList[i]);
       }
+    else
+      {
+      //m_InputVectorDataListParam->GetVectorDataList()->Erase(i-count);
+      }
     }
 
   m_FileSelectionList = tmpList;
@@ -339,19 +370,33 @@ QtWidgetInputVectorDataListParameter::EraseFile()
 
 void QtWidgetInputVectorDataListParameter::RecreateVectorDataList()
 {
+  std::cout<<"QtWidgetInputVectorDataListParameter::RecreateVectorDataList"<<std::endl;
   // save value
   m_InputVectorDataListParam->ClearValue();
-  for (unsigned int j = 0; j < m_FileSelectionList.size(); j++)
+
+  if( m_FileSelectionList.size() == 0)
     {
-    m_InputVectorDataListParam->AddFromFileName(m_FileSelectionList[j]->GetFilename());
-    connect(m_FileSelectionList[j]->GetInput(), SIGNAL(textChanged(const QString&)), this, SLOT(UpdateVectorDataList()));
+std::cout<<"QtWidgetInputVectorDataListParameter::RecreateVectorDataList:: Addfile"<<std::endl;
+    this->AddFile();
     }
+  else
+    {
+    std::cout<<"QtWidgetInputVectorDataListParameter::RecreateVectorDataList else "<<m_FileSelectionList.size()<<std::endl;
+    for (unsigned int j = 0; j < m_FileSelectionList.size(); j++)
+      {
+    std::cout<<j<<std::endl;
+      m_InputVectorDataListParam->AddFromFileName(m_FileSelectionList[j]->GetFilename());
+      connect(m_FileSelectionList[j]->GetInput(), SIGNAL(textChanged(const QString&)), this, SLOT(UpdateVectorDataList()));
+      }
+    
+    emit Change();
+    // notify of value change
+    QString key(QString::fromStdString(m_InputVectorDataListParam->GetKey()));
+    emit ParameterChanged(key);
+   
+    }
+ std::cout<<"QtWidgetInputVectorDataListParameter::RecreateVectorDataList done"<<std::endl;
 
-  emit
-  Change();
-  // notify of value change
-  QString key(QString::fromStdString(m_InputVectorDataListParam->GetKey()));
-  emit ParameterChanged(key);
 }
 
 
-- 
GitLab