File: pFileDialog.cpp

package info (click to toggle)
monkeystudio 1.9.0.4%2Bgit20161218-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,500 kB
  • ctags: 22,118
  • sloc: cpp: 144,671; ansic: 33,969; python: 2,922; makefile: 127; sh: 122; php: 73; cs: 69
file content (256 lines) | stat: -rw-r--r-- 8,590 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/****************************************************************************
    Copyright (C) 2005 - 2011  Filipe AZEVEDO & The Monkey Studio Team
    http://monkeystudio.org licensing under the GNU GPL.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
****************************************************************************/
#include "pFileDialog.h"

#include <QGridLayout>
#include <QTextCodec>
#include <QLabel>
#include <QComboBox>
#include <QCheckBox>

pFileDialog::pFileDialog( QWidget* parent, const QString& caption, const QString& directory, const QString& filter, bool textCodecEnabled, bool openReadOnlyEnabled )
    : QFileDialog( parent, caption, directory, filter )
{
    setFileMode( QFileDialog::AnyFile );

#if QT_VERSION >= 0x040500
    setOption( QFileDialog::DontUseNativeDialog, true );
#endif
    
    // get grid layout
    glDialog = qobject_cast<QGridLayout*>( layout() );
    
    // assert on gridlayout
    Q_ASSERT( glDialog );
    
    // text codec
    QStringList codecs;
    foreach ( const QByteArray& codec, QTextCodec::availableCodecs() )
    {
        codecs << codec;
    }
    codecs.sort();
    
    mTextCodecEnabled = true;
    
    lCodec = new QLabel( tr( "Codec:" ), this );
    cbCodec = new QComboBox( this );
    cbCodec->addItems( codecs );
    setTextCodec( QTextCodec::codecForLocale()->name() );
    
    glDialog->addWidget( lCodec, 4, 0 );
    glDialog->addWidget( cbCodec, 4, 1 );
    
    // read only
    mOpenReadOnlyEnabled = true;
    
    cbOpenReadOnly = new QCheckBox( tr( "Open in read only." ), this );
    
    glDialog->addWidget( cbOpenReadOnly, 5, 1 );
    
    // configuration
    
    setTextCodecEnabled( textCodecEnabled );
    setOpenReadOnlyEnabled( openReadOnlyEnabled );
}

QString pFileDialog::textCodec() const
{
    return cbCodec->currentText();
}

void pFileDialog::setTextCodec( const QString& codec )
{
    cbCodec->setCurrentIndex( cbCodec->findText( codec ) );
}

bool pFileDialog::textCodecEnabled() const
{
    return mTextCodecEnabled;
}

void pFileDialog::setTextCodecEnabled( bool enabled )
{
    mTextCodecEnabled = enabled;
    lCodec->setEnabled( enabled );
    cbCodec->setEnabled( enabled );
}

bool pFileDialog::openReadOnly() const
{
    return cbOpenReadOnly->isChecked();
}

void pFileDialog::setOpenReadOnly( bool readOnly )
{
    cbOpenReadOnly->setChecked( readOnly );
}

bool pFileDialog::openReadOnlyEnabled() const
{
    return mOpenReadOnlyEnabled;
}

void pFileDialog::setOpenReadOnlyEnabled( bool enabled )
{
    mOpenReadOnlyEnabled = enabled;
    cbOpenReadOnly->setEnabled( enabled );
    cbOpenReadOnly->setVisible( enabled );
}

QDir::Filters pFileDialog::filterForMode() const
{
    QDir::Filters filters = filter();
    
    if ( fileMode() == QFileDialog::DirectoryOnly )
    {
        filters |= QDir::Drives | QDir::AllDirs | QDir::Dirs;
        filters &= ~QDir::Files;
    }
    else
    {
        filters |= QDir::Drives | QDir::AllDirs | QDir::Files | QDir::Dirs;
    }
    
    return filters;
}

void pFileDialog::setDialog( pFileDialog* dlg, const QString& caption, const QString& dir, const QString& filter, bool enabledTextCodec, bool enabledOpenReadOnly, QString* selectedFilter, Options options )
{
#if defined( Q_OS_MAC ) && QT_VERSION < 0x040500
    if ( !( options & DontUseSheet ) )
    {
        // that's impossible to have a sheet in a sheet
        QWidget* parent = dlg->parentWidget();
        if ( parent && !parent->windowFlags().testFlag( Qt::Sheet ) )
        {
            dlg->setWindowFlags( dlg->windowFlags() | Qt::Sheet );
        }
    }
#endif
    
    // dialog settings
    dlg->setWindowTitle( caption );
    dlg->setDirectory( dir );
    dlg->setOption( QFileDialog::HideNameFilterDetails );
    dlg->setNameFilter( filter );
    dlg->setTextCodecEnabled( enabledTextCodec );
    dlg->setOpenReadOnlyEnabled( enabledOpenReadOnly );
    dlg->setConfirmOverwrite( !( options & DontConfirmOverwrite ) );
    dlg->setResolveSymlinks( !( options & DontResolveSymlinks ) );
    
    // select file if needed )
    if ( !( options & ShowDirsOnly ) && QFileInfo( dir ).isFile() )
    {
        dlg->selectFile( dir );
    }
    
    // select correct filter if needed
    if ( selectedFilter )
    {
        dlg->selectNameFilter( *selectedFilter );
    }
}

void pFileDialog::setOpenFileNameDialog( pFileDialog* dlg, const QString& caption, const QString& dir, const QString& filter, bool enabledTextCodec, bool enabledOpenReadOnly, QString* selectedFilter, Options options )
{
    setDialog( dlg, caption, dir, filter, enabledTextCodec, enabledOpenReadOnly, selectedFilter, options );
    dlg->setFileMode( QFileDialog::ExistingFile );
    dlg->setFilter( dlg->filterForMode() );
    dlg->setAcceptMode( AcceptOpen );
}

void pFileDialog::setOpenFileNamesDialog( pFileDialog* dlg, const QString& caption, const QString& dir, const QString& filter, bool enabledTextCodec, bool enabledOpenReadOnly, QString* selectedFilter, Options options )
{
    setDialog( dlg, caption, dir, filter, enabledTextCodec, enabledOpenReadOnly, selectedFilter, options );
    dlg->setFileMode( QFileDialog::ExistingFiles );
    dlg->setFilter( dlg->filterForMode() );
    dlg->setAcceptMode( AcceptOpen );
}

void pFileDialog::setSaveFileNameDialog( pFileDialog* dlg, const QString& caption, const QString& dir, const QString& filter, bool enabledTextCodec, QString* selectedFilter, Options options )
{
    setDialog( dlg, caption, dir, filter, enabledTextCodec, false, selectedFilter, options );
    dlg->setFileMode( QFileDialog::AnyFile );
    dlg->setFilter( dlg->filterForMode() );
    dlg->setAcceptMode( AcceptSave );
}

pFileDialogResult pFileDialog::getOpenFileName( QWidget* parent, const QString& caption, const QString& dir, const QString& filter, bool enabledTextCodec, bool enabledOpenReadOnly, QString* selectedFilter, Options options )
{
    pFileDialogResult result;
    pFileDialog fd( parent );
    setOpenFileNameDialog( &fd, caption, dir, filter, enabledTextCodec, enabledOpenReadOnly, selectedFilter, options );
    
    if ( fd.exec() == QDialog::Accepted )
    {
        if ( selectedFilter )
        {
            *selectedFilter = fd.selectedNameFilter();
        }
        
        result[ "filename" ] = fd.selectedFiles().value( 0 );
        result[ "codec" ] = fd.textCodec();
        result[ "openreadonly" ] = fd.openReadOnly();
    }
    
    return result;
}

pFileDialogResult pFileDialog::getOpenFileNames( QWidget* parent, const QString& caption, const QString& dir, const QString& filter, bool enabledTextCodec, bool enabledOpenReadOnly, QString* selectedFilter, Options options )
{
    pFileDialogResult result;
    pFileDialog fd( parent );
    setOpenFileNamesDialog( &fd, caption, dir, filter, enabledTextCodec, enabledOpenReadOnly, selectedFilter, options );
    
    if ( fd.exec() == QDialog::Accepted )
    {
        if ( selectedFilter )
        {
            *selectedFilter = fd.selectedNameFilter();
        }
        
        result[ "filenames" ] = fd.selectedFiles().value( 0 );
        result[ "codec" ] = fd.textCodec();
        result[ "openreadonly" ] = fd.openReadOnly();
    }
    
    return result;
}

pFileDialogResult pFileDialog::getSaveFileName( QWidget* parent, const QString& caption, const QString& dir, const QString& filter, bool enabledTextCodec, QString* selectedFilter, Options options )
{
    pFileDialogResult result;
    pFileDialog fd( parent );
    setSaveFileNameDialog( &fd, caption, dir, filter, enabledTextCodec, selectedFilter, options );
    
    if ( fd.exec() == QDialog::Accepted )
    {
        if ( selectedFilter )
        {
            *selectedFilter = fd.selectedNameFilter();
        }
        
        result[ "filename" ] = fd.selectedFiles().value( 0 );
        result[ "codec" ] = fd.textCodec();
        result[ "openreadonly" ] = fd.openReadOnly();
    }
    
    return result;
}