From 6e732a3d58c7b753b6ad2a4d6d75de4af9fa9cd3 Mon Sep 17 00:00:00 2001
From: Guillaume Pasero <guillaume.pasero@c-s.fr>
Date: Thu, 7 Sep 2017 13:03:26 +0200
Subject: [PATCH] BUG: fix update after a drag & drop in InputImage and lists

---
 .../mvdQtWidgetParameterInitializers.h        | 30 ++++++++++++++-----
 .../include/otbQtFileSelectionWidget.h        |  1 +
 .../otbWrapperQtWidgetInputImageParameter.h   |  5 +++-
 .../QtWidget/src/otbQtFileSelectionWidget.cxx | 11 +++++--
 .../otbWrapperQtWidgetInputImageParameter.cxx | 16 ++++++----
 5 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/Modules/Visualization/MonteverdiGui/include/mvdQtWidgetParameterInitializers.h b/Modules/Visualization/MonteverdiGui/include/mvdQtWidgetParameterInitializers.h
index 023029fc20..61d425e75d 100644
--- a/Modules/Visualization/MonteverdiGui/include/mvdQtWidgetParameterInitializers.h
+++ b/Modules/Visualization/MonteverdiGui/include/mvdQtWidgetParameterInitializers.h
@@ -763,13 +763,29 @@ SetupForFilenameDrop( W* widget, const char* text )
 
   lineEdit->installEventFilter( eventFilter );
 
-  QObject::connect(
-    eventFilter,
-    SIGNAL( FilenameDropped( const QString& ) ),
-    // to:
-    lineEdit,
-    SLOT( setText( const QString& ) )
-  );
+  // BUG : temporary fix for drag & drop in InputImageParameter
+  // in the future, all "filename" parameters should have the same behaviour
+  if (dynamic_cast<otb::Wrapper::QtWidgetInputImageParameter*>(widget) ||
+      dynamic_cast<otb::Wrapper::QtFileSelectionWidget*>(widget))
+    {
+    QObject::connect(
+      eventFilter,
+      SIGNAL( FilenameDropped( const QString& ) ),
+      // to:
+      widget,
+      SLOT( SetFileName( const QString& ) )
+    );
+    }
+  else
+    {
+    QObject::connect(
+      eventFilter,
+      SIGNAL( FilenameDropped( const QString& ) ),
+      // to:
+      lineEdit,
+      SLOT( setText( const QString& ) )
+    );
+    }
 }
 
 /*****************************************************************************/
diff --git a/Modules/Wrappers/QtWidget/include/otbQtFileSelectionWidget.h b/Modules/Wrappers/QtWidget/include/otbQtFileSelectionWidget.h
index d7b42e3083..ec1977b33a 100644
--- a/Modules/Wrappers/QtWidget/include/otbQtFileSelectionWidget.h
+++ b/Modules/Wrappers/QtWidget/include/otbQtFileSelectionWidget.h
@@ -87,6 +87,7 @@ signals:
 protected slots:
   void SelectFile();
   void CallFilenameChanged();
+  void SetFileName(const QString &);
 
 private:
   QtFileSelectionWidget(const QtFileSelectionWidget&); //purposely not implemented
diff --git a/Modules/Wrappers/QtWidget/include/otbWrapperQtWidgetInputImageParameter.h b/Modules/Wrappers/QtWidget/include/otbWrapperQtWidgetInputImageParameter.h
index 01684af932..5c970fc5c7 100644
--- a/Modules/Wrappers/QtWidget/include/otbWrapperQtWidgetInputImageParameter.h
+++ b/Modules/Wrappers/QtWidget/include/otbWrapperQtWidgetInputImageParameter.h
@@ -52,9 +52,12 @@ signals:
   void FileNameIsSet();
 
 protected slots:
-  bool SetFileName();
+  bool SetFileName(const QString& value);
   void SelectFile();
 
+private slots:
+  void OnEditingFinished();
+
 private:
   QtWidgetInputImageParameter(const QtWidgetInputImageParameter&); //purposely not implemented
   void operator=(const QtWidgetInputImageParameter&); //purposely not implemented
diff --git a/Modules/Wrappers/QtWidget/src/otbQtFileSelectionWidget.cxx b/Modules/Wrappers/QtWidget/src/otbQtFileSelectionWidget.cxx
index 37e4f3eed3..44eb911061 100644
--- a/Modules/Wrappers/QtWidget/src/otbQtFileSelectionWidget.cxx
+++ b/Modules/Wrappers/QtWidget/src/otbQtFileSelectionWidget.cxx
@@ -116,15 +116,22 @@ QtFileSelectionWidget
   if( filename.isEmpty() )
     return;
 
-  m_Input->setText( filename  );
+  SetFileName(filename);
+}
 
+void
+QtFileSelectionWidget
+::CallFilenameChanged()
+{
   emit FilenameChanged();
 }
 
 void
 QtFileSelectionWidget
-::CallFilenameChanged()
+::SetFileName(const QString & filename)
 {
+  m_Input->setText( filename  );
+
   emit FilenameChanged();
 }
 
diff --git a/Modules/Wrappers/QtWidget/src/otbWrapperQtWidgetInputImageParameter.cxx b/Modules/Wrappers/QtWidget/src/otbWrapperQtWidgetInputImageParameter.cxx
index 2929d528a5..e41c35dc01 100644
--- a/Modules/Wrappers/QtWidget/src/otbWrapperQtWidgetInputImageParameter.cxx
+++ b/Modules/Wrappers/QtWidget/src/otbWrapperQtWidgetInputImageParameter.cxx
@@ -69,7 +69,7 @@ void QtWidgetInputImageParameter::DoCreateWidget()
   m_HLayout->setContentsMargins(0, 0, 0, 0);
   m_Input = new QLineEdit;
   m_Input->setToolTip( m_InputImageParam->GetDescription() );
-  connect( m_Input, SIGNAL(editingFinished()), this, SLOT(SetFileName()) );
+  connect( m_Input, SIGNAL(editingFinished()), this, SLOT(OnEditingFinished()) );
   connect( this, SIGNAL(FileNameIsSet()), GetModel(), SLOT(NotifyUpdate()) );
 
   m_HLayout->addWidget(m_Input);
@@ -105,9 +105,7 @@ QtWidgetInputImageParameter
   if( filename.isEmpty() )
     return;
 
-  m_Input->setText( filename  );
-
-  if( !SetFileName() )
+  if( !SetFileName(filename) )
     {
     std::ostringstream oss;
 
@@ -123,13 +121,14 @@ QtWidgetInputImageParameter
     }
 }
 
-bool QtWidgetInputImageParameter::SetFileName()
+bool QtWidgetInputImageParameter::SetFileName(const QString& value)
 {
   bool res = true;
   // save value
   if( m_InputImageParam->SetFromFileName(
-	QFile::encodeName( m_Input->text() ).constData() ) == true )
+	QFile::encodeName( value ).constData() ) == true )
     {
+    m_Input->setText( value  );
     // notify of value change
     QString key( m_InputImageParam->GetKey() );
 
@@ -142,5 +141,10 @@ bool QtWidgetInputImageParameter::SetFileName()
   return res;
 }
 
+void QtWidgetInputImageParameter::OnEditingFinished()
+{
+  SetFileName( m_Input->text() );
+}
+
 }
 }
-- 
GitLab