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