]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/QT/OpenFileDialog.cxx
yet another refactoring
[cpPlugins.git] / lib / cpPlugins / QT / OpenFileDialog.cxx
diff --git a/lib/cpPlugins/QT/OpenFileDialog.cxx b/lib/cpPlugins/QT/OpenFileDialog.cxx
new file mode 100644 (file)
index 0000000..784d111
--- /dev/null
@@ -0,0 +1,107 @@
+#include <cpPlugins/QT/OpenFileDialog.h>
+#include <cpPlugins/Pipeline/Parameters.h>
+
+// -------------------------------------------------------------------------
+cpPlugins::QT::OpenFileDialog::
+OpenFileDialog( QWidget* parent )
+  : QFileDialog( parent ),
+    m_Parameters( NULL ),
+    m_Name( "" )
+{
+  this->connect(
+    this, SIGNAL( accepted( ) ), this, SLOT( _dlg_Accepted( ) )
+    );
+  this->setWindowTitle( "Open an(some) file(s)" );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::QT::OpenFileDialog::
+~OpenFileDialog( )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::QT::OpenFileDialog::
+SetParameters(
+  cpPlugins::Pipeline::Parameters* params, const std::string& name
+  )
+{
+  if( params == NULL )
+  {
+    this->m_Parameters = NULL;
+    this->m_Name = "";
+    return;
+
+  } // fi
+  auto param_type = params->GetType( name );
+  if(
+    param_type != cpPlugins::Pipeline::Parameters::OpenFileName &&
+    param_type != cpPlugins::Pipeline::Parameters::OpenFileNameList
+    )
+  {
+    this->m_Parameters = NULL;
+    this->m_Name = "";
+    return;
+
+  } // fi
+  this->m_Parameters = params;
+  this->m_Name = name;
+
+  QStringList filters;
+  auto extensions = this->m_Parameters->GetAcceptedFileExtensions( name );
+  if( extensions != "" )
+    filters << extensions.c_str( );
+  filters << "Any file (*)";
+  if( param_type == cpPlugins::Pipeline::Parameters::OpenFileName )
+    this->setFileMode( QFileDialog::ExistingFile );
+  else
+    this->setFileMode( QFileDialog::ExistingFiles );
+  this->setNameFilters( filters );
+  this->setAcceptMode( QFileDialog::AcceptOpen );
+  if( param_type == cpPlugins::Pipeline::Parameters::OpenFileName )
+  {
+    auto file = this->m_Parameters->GetOpenFileName( this->m_Name );
+    if( file == "" )
+      file = ".";
+    QFileInfo info( file.c_str( ) );
+    this->setDirectory( info.canonicalPath( ) );
+  }
+  else
+  {
+    auto files = this->m_Parameters->GetOpenFileNameList( this->m_Name );
+    if( files.size( ) > 0 )
+    {
+      QFileInfo info( files[ 0 ].c_str( ) );
+      this->setDirectory( info.canonicalPath( ) );
+    }
+    else
+      this->setDirectory( "." );
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::QT::OpenFileDialog::
+_dlg_Accepted( )
+{
+  if( this->m_Parameters != NULL )
+  {
+    auto files = this->selectedFiles( );
+    auto param_type = this->m_Parameters->GetType( this->m_Name );
+    if( param_type == cpPlugins::Pipeline::Parameters::OpenFileNameList )
+    {
+      this->m_Parameters->ClearOpenFileNameList( this->m_Name );
+      for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
+        this->m_Parameters->AddToOpenFileNameList(
+          this->m_Name, fIt->toStdString( )
+          );
+    }
+    else
+      this->m_Parameters->SetOpenFileName(
+        this->m_Name, files[ 0 ].toStdString( )
+        );
+
+  } // fi
+}
+
+// eof - $RCSfile$