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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "filedialogpanel.h"
#include "utils.h"
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QFormLayout>
#include <QSpacerItem>
#include <QGroupBox>
#include <QPushButton>
#include <QCheckBox>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QLabel>
#include <QMessageBox>
#include <QApplication>
#include <QUrl>
#include <QTimer>
#include <QDebug>
const FlagData acceptModeComboData[] =
{
{"AcceptOpen", QFileDialog::AcceptOpen },
{"AcceptSave", QFileDialog::AcceptSave }
};
const FlagData viewModeComboData[] =
{
{"Detail", QFileDialog::Detail},
{"List", QFileDialog::List}
};
const FlagData fileModeComboData[] =
{
{"AnyFile", QFileDialog::AnyFile},
{"ExistingFile", QFileDialog::ExistingFile},
{"ExistingFiles", QFileDialog::ExistingFiles},
{"Directory", QFileDialog::Directory}
};
static inline QPushButton *addButton(const QString &description, QGridLayout *layout,
int &row, int column, QObject *receiver, const char *slotFunc)
{
QPushButton *button = new QPushButton(description);
QObject::connect(button, SIGNAL(clicked()), receiver, slotFunc);
layout->addWidget(button, row++, column);
return button;
}
// A line edit for editing the label fields of the dialog, keeping track of whether it has
// been modified by the user to avoid applying Qt's default texts to native dialogs.
class LabelLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit LabelLineEdit(QFileDialog::DialogLabel label, QWidget *parent = nullptr) : QLineEdit(parent), m_label(label), m_dirty(false)
{
connect(this, SIGNAL(textEdited(QString)), this, SLOT(setDirty()));
}
void restoreDefault(const QFileDialog *d)
{
setText(d->labelText(m_label));
m_dirty = false;
}
void apply(QFileDialog *d) const
{
if (m_dirty)
d->setLabelText(m_label, text());
}
private slots:
void setDirty() { m_dirty = true; }
private:
const QFileDialog::DialogLabel m_label;
bool m_dirty;
};
FileDialogPanel::FileDialogPanel(QWidget *parent)
: QWidget(parent)
, m_showDirsOnly(new QCheckBox(tr("Show dirs only")))
, m_readOnly(new QCheckBox(tr("Read only")))
, m_confirmOverWrite(new QCheckBox(tr("Confirm overwrite")))
, m_nameFilterDetailsVisible(new QCheckBox(tr("Name filter details visible")))
, m_resolveSymLinks(new QCheckBox(tr("Resolve symlinks")))
, m_native(new QCheckBox(tr("Use native dialog")))
, m_customDirIcons(new QCheckBox(tr("Don't use custom directory icons")))
, m_acceptMode(createCombo(this, acceptModeComboData, sizeof(acceptModeComboData)/sizeof(FlagData)))
, m_fileMode(createCombo(this, fileModeComboData, sizeof(fileModeComboData)/sizeof(FlagData)))
, m_viewMode(createCombo(this, viewModeComboData, sizeof(viewModeComboData)/sizeof(FlagData)))
, m_allowedSchemes(new QLineEdit(this))
, m_defaultSuffix(new QLineEdit(this))
, m_directory(new QLineEdit(this))
, m_selectedFileName(new QLineEdit(this))
, m_useMimeTypeFilters(new QCheckBox(this))
, m_nameFilters(new QPlainTextEdit)
, m_selectedNameFilter(new QLineEdit(this))
, m_deleteNonModalDialogButton(0)
, m_deleteModalDialogButton(0)
{
// Options
QGroupBox *optionsGroupBox = new QGroupBox(tr("Options"));
QFormLayout *optionsLayout = new QFormLayout(optionsGroupBox);
optionsLayout->addRow(tr("AcceptMode:"), m_acceptMode);
optionsLayout->addRow(tr("FileMode:"), m_fileMode);
optionsLayout->addRow(tr("ViewMode:"), m_viewMode);
optionsLayout->addRow(tr("Allowed Schemes:"), m_allowedSchemes);
optionsLayout->addRow(m_showDirsOnly);
optionsLayout->addRow(m_native);
optionsLayout->addRow(m_confirmOverWrite);
optionsLayout->addRow(m_nameFilterDetailsVisible);
optionsLayout->addRow(m_resolveSymLinks);
optionsLayout->addRow(m_readOnly);
optionsLayout->addRow(m_customDirIcons);
// Files
QGroupBox *filesGroupBox = new QGroupBox(tr("Files / Filters"));
filesLayout = new QFormLayout(filesGroupBox);
filesLayout->addRow(tr("Default Suffix:"), m_defaultSuffix);
filesLayout->addRow(tr("Directory:"), m_directory);
filesLayout->addRow(tr("Selected file:"), m_selectedFileName);
filesLayout->addRow(tr("Use mime type filters:"), m_useMimeTypeFilters);
m_nameFilters->setMaximumHeight(80);
filesLayout->addRow(tr("Name filters:"), m_nameFilters);
filesLayout->addRow(tr("Selected name filter:"), m_selectedNameFilter);
// Optional labels
QGroupBox *labelsGroupBox = new QGroupBox(tr("Labels"));
QFormLayout *labelsLayout = new QFormLayout(labelsGroupBox);
m_labelLineEdits.push_back(new LabelLineEdit(QFileDialog::LookIn, this));
labelsLayout->addRow(tr("Look in label:"), m_labelLineEdits.back());
m_labelLineEdits.push_back(new LabelLineEdit(QFileDialog::FileName, this));
labelsLayout->addRow(tr("File name label:"), m_labelLineEdits.back());
m_labelLineEdits.push_back(new LabelLineEdit(QFileDialog::FileType, this));
labelsLayout->addRow(tr("File type label:"), m_labelLineEdits.back());
m_labelLineEdits.push_back(new LabelLineEdit(QFileDialog::Accept, this));
labelsLayout->addRow(tr("Accept label:"), m_labelLineEdits.back());
m_labelLineEdits.push_back(new LabelLineEdit(QFileDialog::Reject, this));
labelsLayout->addRow(tr("Reject label:"), m_labelLineEdits.back());
// Buttons
QGroupBox *buttonsGroupBox = new QGroupBox(tr("Show"));
QGridLayout *buttonLayout = new QGridLayout(buttonsGroupBox);
int row = 0;
int column = 0;
addButton(tr("Exec modal"), buttonLayout, row, column, this, SLOT(execModal()));
addButton(tr("Show modal"), buttonLayout, row, column, this, SLOT(showModal()));
m_deleteModalDialogButton =
addButton(tr("Delete modal"), buttonLayout, row, column, this, SLOT(deleteModalDialog()));
addButton(tr("Show non-modal"), buttonLayout, row, column, this, SLOT(showNonModal()));
m_deleteNonModalDialogButton =
addButton(tr("Delete non-modal"), buttonLayout, row, column, this, SLOT(deleteNonModalDialog()));
row = 0;
column++;
addButton(tr("getOpenFileName"), buttonLayout, row, column, this, SLOT(getOpenFileName()));
addButton(tr("getOpenFileUrl"), buttonLayout, row, column, this, SLOT(getOpenFileUrl()));
addButton(tr("getOpenFileNames"), buttonLayout, row, column, this, SLOT(getOpenFileNames()));
addButton(tr("getOpenFileUrls"), buttonLayout, row, column, this, SLOT(getOpenFileUrls()));
addButton(tr("getSaveFileName"), buttonLayout, row, column, this, SLOT(getSaveFileName()));
addButton(tr("getSaveFileUrl"), buttonLayout, row, column, this, SLOT(getSaveFileUrl()));
addButton(tr("getExistingDirectory"), buttonLayout, row, column, this, SLOT(getExistingDirectory()));
addButton(tr("getExistingDirectoryUrl"), buttonLayout, row, column, this, SLOT(getExistingDirectoryUrl()));
addButton(tr("Restore defaults"), buttonLayout, row, column, this, SLOT(restoreDefaults()));
// Main layout
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->addWidget(optionsGroupBox, 0, 0);
gridLayout->addWidget(filesGroupBox, 0, 1);
gridLayout->addWidget(labelsGroupBox, 1, 0);
gridLayout->addWidget(buttonsGroupBox, 1, 1);
connect(m_useMimeTypeFilters, SIGNAL(toggled(bool)), this, SLOT(useMimeTypeFilters(bool)));
enableDeleteModalDialogButton();
enableDeleteNonModalDialogButton();
restoreDefaults();
}
void FileDialogPanel::execModal()
{
QFileDialog dialog(this);
applySettings(&dialog);
connect(&dialog, SIGNAL(accepted()), this, SLOT(accepted()));
dialog.setWindowTitle(tr("Modal File Dialog Qt %1").arg(QLatin1String(QT_VERSION_STR)));
dialog.exec();
}
void FileDialogPanel::showModal()
{
if (m_modalDialog.isNull()) {
static int n = 0;
m_modalDialog = new QFileDialog(this);
m_modalDialog->setModal(true);
connect(m_modalDialog.data(), SIGNAL(accepted()), this, SLOT(accepted()));
m_modalDialog->setWindowTitle(tr("Modal File Dialog #%1 Qt %2")
.arg(++n)
.arg(QLatin1String(QT_VERSION_STR)));
enableDeleteModalDialogButton();
}
applySettings(m_modalDialog);
m_modalDialog->show();
}
void FileDialogPanel::showNonModal()
{
if (m_nonModalDialog.isNull()) {
static int n = 0;
m_nonModalDialog = new QFileDialog(this);
connect(m_nonModalDialog.data(), SIGNAL(accepted()), this, SLOT(accepted()));
m_nonModalDialog->setWindowTitle(tr("Non-Modal File Dialog #%1 Qt %2")
.arg(++n)
.arg(QLatin1String(QT_VERSION_STR)));
enableDeleteNonModalDialogButton();
}
applySettings(m_nonModalDialog);
m_nonModalDialog->show();
}
void FileDialogPanel::deleteNonModalDialog()
{
if (!m_nonModalDialog.isNull())
delete m_nonModalDialog;
enableDeleteNonModalDialogButton();
}
void FileDialogPanel::deleteModalDialog()
{
if (!m_modalDialog.isNull())
delete m_modalDialog;
enableDeleteModalDialogButton();
}
void FileDialogPanel::enableDeleteNonModalDialogButton()
{
m_deleteNonModalDialogButton->setEnabled(!m_nonModalDialog.isNull());
}
void FileDialogPanel::enableDeleteModalDialogButton()
{
m_deleteModalDialogButton->setEnabled(!m_modalDialog.isNull());
}
QString FileDialogPanel::filterString() const
{
return m_nameFilters->toPlainText().trimmed().replace(QLatin1String("\n"), QLatin1String(";;"));
}
QUrl FileDialogPanel::currentDirectoryUrl() const
{
return QUrl::fromUserInput(m_directory->text().trimmed());
}
QFileDialog::Options FileDialogPanel::options() const
{
QFileDialog::Options result;
if (m_showDirsOnly->isChecked())
result |= QFileDialog::ShowDirsOnly;
if (!m_nameFilterDetailsVisible->isChecked())
result |= QFileDialog::HideNameFilterDetails;
if (!m_resolveSymLinks->isChecked())
result |= QFileDialog::DontResolveSymlinks;
if (m_readOnly->isChecked())
result |= QFileDialog::ReadOnly;
if (!m_confirmOverWrite->isChecked())
result |= QFileDialog::DontConfirmOverwrite;
if (!m_native->isChecked())
result |= QFileDialog::DontUseNativeDialog;
if (m_customDirIcons->isChecked())
result |= QFileDialog::DontUseCustomDirectoryIcons;
return result;
}
QStringList FileDialogPanel::allowedSchemes() const
{
return m_allowedSchemes->text().simplified().split(' ', Qt::SkipEmptyParts);
}
void FileDialogPanel::getOpenFileNames()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QStringList files =
QFileDialog::getOpenFileNames(this, tr("getOpenFileNames Qt %1").arg(QLatin1String(QT_VERSION_STR)),
m_directory->text(), filterString(), &selectedFilter, options());
if (!files.isEmpty()) {
QString result;
QDebug(&result).nospace()
<< "Files: " << files
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getOpenFileNames"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getOpenFileUrls()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QList<QUrl> files =
QFileDialog::getOpenFileUrls(this, tr("getOpenFileNames Qt %1").arg(QLatin1String(QT_VERSION_STR)),
currentDirectoryUrl(), filterString(), &selectedFilter, options(),
allowedSchemes());
if (!files.isEmpty()) {
QString result;
QDebug(&result).nospace()
<< "Files: " << QUrl::toStringList(files)
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getOpenFileNames"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getOpenFileName()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QString file =
QFileDialog::getOpenFileName(this, tr("getOpenFileName Qt %1").arg(QLatin1String(QT_VERSION_STR)),
m_directory->text(), filterString(), &selectedFilter, options());
if (!file.isEmpty()) {
QString result;
QDebug(&result).nospace()
<< "File: " << file
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getOpenFileName"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getOpenFileUrl()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QUrl file =
QFileDialog::getOpenFileUrl(this, tr("getOpenFileUrl Qt %1").arg(QLatin1String(QT_VERSION_STR)),
currentDirectoryUrl(), filterString(), &selectedFilter, options(),
allowedSchemes());
if (file.isValid()) {
QString result;
QDebug(&result).nospace()
<< "File: " << file.toString()
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getOpenFileName"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getSaveFileName()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QString file =
QFileDialog::getSaveFileName(this, tr("getSaveFileName Qt %1").arg(QLatin1String(QT_VERSION_STR)),
m_directory->text(), filterString(), &selectedFilter, options());
if (!file.isEmpty()) {
QString result;
QDebug(&result).nospace()
<< "File: " << file
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getSaveFileNames"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getSaveFileUrl()
{
QString selectedFilter = m_selectedNameFilter->text().trimmed();
const QUrl file =
QFileDialog::getSaveFileUrl(this, tr("getSaveFileName Qt %1").arg(QLatin1String(QT_VERSION_STR)),
currentDirectoryUrl(), filterString(), &selectedFilter, options(),
allowedSchemes());
if (file.isValid()) {
QString result;
QDebug(&result).nospace()
<< "File: " << file.toString()
<< "\nName filter: " << selectedFilter;
QMessageBox::information(this, tr("getSaveFileNames"), result, QMessageBox::Ok);
}
}
void FileDialogPanel::getExistingDirectory()
{
const QString dir =
QFileDialog::getExistingDirectory(this, tr("getExistingDirectory Qt %1").arg(QLatin1String(QT_VERSION_STR)),
m_directory->text(), options() | QFileDialog::ShowDirsOnly);
if (!dir.isEmpty())
QMessageBox::information(this, tr("getExistingDirectory"), QLatin1String("Directory: ") + dir, QMessageBox::Ok);
}
void FileDialogPanel::getExistingDirectoryUrl()
{
const QUrl dir =
QFileDialog::getExistingDirectoryUrl(this, tr("getExistingDirectory Qt %1").arg(QLatin1String(QT_VERSION_STR)),
currentDirectoryUrl(), options() | QFileDialog::ShowDirsOnly,
allowedSchemes());
if (!dir.isEmpty())
QMessageBox::information(this, tr("getExistingDirectory"), QLatin1String("Directory: ") + dir.toString(), QMessageBox::Ok);
}
void FileDialogPanel::restoreDefaults()
{
QFileDialog d;
setComboBoxValue(m_acceptMode, d.acceptMode());
setComboBoxValue(m_fileMode, d.fileMode());
setComboBoxValue(m_viewMode, d.viewMode());
m_showDirsOnly->setChecked(d.testOption(QFileDialog::ShowDirsOnly));
m_allowedSchemes->setText(QString());
m_confirmOverWrite->setChecked(!d.testOption(QFileDialog::DontConfirmOverwrite));
m_nameFilterDetailsVisible->setChecked(!d.testOption(QFileDialog::HideNameFilterDetails));
m_resolveSymLinks->setChecked(!d.testOption(QFileDialog::DontResolveSymlinks));
m_readOnly->setChecked(d.testOption(QFileDialog::ReadOnly));
m_native->setChecked(true);
m_customDirIcons->setChecked(d.testOption(QFileDialog::DontUseCustomDirectoryIcons));
m_directory->setText(QDir::homePath());
m_defaultSuffix->setText(QLatin1String("txt"));
m_useMimeTypeFilters->setChecked(false);
useMimeTypeFilters(false);
m_selectedFileName->setText(QString());
m_selectedNameFilter->setText(QString());
foreach (LabelLineEdit *l, m_labelLineEdits)
l->restoreDefault(&d);
}
void FileDialogPanel::applySettings(QFileDialog *d) const
{
d->setAcceptMode(comboBoxValue<QFileDialog::AcceptMode>(m_acceptMode));
d->setViewMode(comboBoxValue<QFileDialog::ViewMode>(m_viewMode));
d->setFileMode(comboBoxValue<QFileDialog::FileMode>(m_fileMode));
d->setOptions(options());
d->setDefaultSuffix(m_defaultSuffix->text().trimmed());
const QString directory = m_directory->text().trimmed();
if (!directory.isEmpty())
d->setDirectory(directory);
const QString file = m_selectedFileName->text().trimmed();
if (!file.isEmpty())
d->selectFile(file);
const QString filter = m_selectedNameFilter->text().trimmed();
const QStringList filters = m_nameFilters->toPlainText().trimmed().split(QLatin1Char('\n'), Qt::SkipEmptyParts);
if (!m_useMimeTypeFilters->isChecked()) {
d->setNameFilters(filters);
if (!filter.isEmpty())
d->selectNameFilter(filter);
} else {
d->setMimeTypeFilters(filters);
if (!filter.isEmpty())
d->selectMimeTypeFilter(filter);
}
foreach (LabelLineEdit *l, m_labelLineEdits)
l->apply(d);
}
void FileDialogPanel::useMimeTypeFilters(bool b)
{
QWidget *textEdit = filesLayout->labelForField(m_nameFilters);
if (QLabel *label = qobject_cast<QLabel *>(textEdit))
label->setText(b ? tr("Mime type filters:") : tr("Name filters:"));
QWidget *w = filesLayout->labelForField(m_selectedNameFilter);
if (QLabel *label = qobject_cast<QLabel *>(w))
label->setText(b ? tr("Selected mime type filter:") : tr("Selected name filter:"));
if (b)
m_nameFilters->setPlainText(QLatin1String("image/jpeg\nimage/png\ntext/plain\napplication/octet-stream"));
else
m_nameFilters->setPlainText(QLatin1String("Any files (*)\nImage files (*.png *.xpm *.jpg)\nText files (*.txt)"));
}
void FileDialogPanel::accepted()
{
const QFileDialog *d = qobject_cast<const QFileDialog *>(sender());
Q_ASSERT(d);
m_result.clear();
QDebug(&m_result).nospace()
<< "URLs: " << d->selectedUrls() << '\n'
<< "Files: " << d->selectedFiles()
<< "\nDirectory: "
<< d->directoryUrl() << ", "
<< d->directory().absolutePath()
<< "\nName filter: " << d->selectedNameFilter();
QTimer::singleShot(0, this, SLOT(showAcceptedResult())); // Avoid problems with the closing (modal) dialog as parent.
}
void FileDialogPanel::showAcceptedResult()
{
QMessageBox::information(this, tr("File Dialog Accepted"), m_result, QMessageBox::Ok);
}
#include "filedialogpanel.moc"
|