]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/QT/OpenFileDialog.cxx
yet another refactoring
[cpPlugins.git] / lib / cpPlugins / QT / OpenFileDialog.cxx
1 #include <cpPlugins/QT/OpenFileDialog.h>
2 #include <cpPlugins/Pipeline/Parameters.h>
3
4 // -------------------------------------------------------------------------
5 cpPlugins::QT::OpenFileDialog::
6 OpenFileDialog( QWidget* parent )
7   : QFileDialog( parent ),
8     m_Parameters( NULL ),
9     m_Name( "" )
10 {
11   this->connect(
12     this, SIGNAL( accepted( ) ), this, SLOT( _dlg_Accepted( ) )
13     );
14   this->setWindowTitle( "Open an(some) file(s)" );
15 }
16
17 // -------------------------------------------------------------------------
18 cpPlugins::QT::OpenFileDialog::
19 ~OpenFileDialog( )
20 {
21 }
22
23 // -------------------------------------------------------------------------
24 void cpPlugins::QT::OpenFileDialog::
25 SetParameters(
26   cpPlugins::Pipeline::Parameters* params, const std::string& name
27   )
28 {
29   if( params == NULL )
30   {
31     this->m_Parameters = NULL;
32     this->m_Name = "";
33     return;
34
35   } // fi
36   auto param_type = params->GetType( name );
37   if(
38     param_type != cpPlugins::Pipeline::Parameters::OpenFileName &&
39     param_type != cpPlugins::Pipeline::Parameters::OpenFileNameList
40     )
41   {
42     this->m_Parameters = NULL;
43     this->m_Name = "";
44     return;
45
46   } // fi
47   this->m_Parameters = params;
48   this->m_Name = name;
49
50   QStringList filters;
51   auto extensions = this->m_Parameters->GetAcceptedFileExtensions( name );
52   if( extensions != "" )
53     filters << extensions.c_str( );
54   filters << "Any file (*)";
55   if( param_type == cpPlugins::Pipeline::Parameters::OpenFileName )
56     this->setFileMode( QFileDialog::ExistingFile );
57   else
58     this->setFileMode( QFileDialog::ExistingFiles );
59   this->setNameFilters( filters );
60   this->setAcceptMode( QFileDialog::AcceptOpen );
61   if( param_type == cpPlugins::Pipeline::Parameters::OpenFileName )
62   {
63     auto file = this->m_Parameters->GetOpenFileName( this->m_Name );
64     if( file == "" )
65       file = ".";
66     QFileInfo info( file.c_str( ) );
67     this->setDirectory( info.canonicalPath( ) );
68   }
69   else
70   {
71     auto files = this->m_Parameters->GetOpenFileNameList( this->m_Name );
72     if( files.size( ) > 0 )
73     {
74       QFileInfo info( files[ 0 ].c_str( ) );
75       this->setDirectory( info.canonicalPath( ) );
76     }
77     else
78       this->setDirectory( "." );
79
80   } // fi
81 }
82
83 // -------------------------------------------------------------------------
84 void cpPlugins::QT::OpenFileDialog::
85 _dlg_Accepted( )
86 {
87   if( this->m_Parameters != NULL )
88   {
89     auto files = this->selectedFiles( );
90     auto param_type = this->m_Parameters->GetType( this->m_Name );
91     if( param_type == cpPlugins::Pipeline::Parameters::OpenFileNameList )
92     {
93       this->m_Parameters->ClearOpenFileNameList( this->m_Name );
94       for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
95         this->m_Parameters->AddToOpenFileNameList(
96           this->m_Name, fIt->toStdString( )
97           );
98     }
99     else
100       this->m_Parameters->SetOpenFileName(
101         this->m_Name, files[ 0 ].toStdString( )
102         );
103
104   } // fi
105 }
106
107 // eof - $RCSfile$