1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Labs Platform module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickplatformfiledialog_p.h"
38
39#include <QtCore/qvector.h>
40
41QT_BEGIN_NAMESPACE
42
43/*!
44 \qmltype FileDialog
45 \inherits Dialog
46//! \instantiates QQuickPlatformFileDialog
47 \inqmlmodule Qt.labs.platform
48 \since 5.8
49 \brief A native file dialog.
50
51 The FileDialog type provides a QML API for native platform file dialogs.
52
53 \image qtlabsplatform-filedialog-gtk.png
54
55 To show a file dialog, construct an instance of FileDialog, set the
56 desired properties, and call \l {Dialog::}{open()}. The \l currentFile
57 or \l currentFiles properties can be used to determine the currently
58 selected file(s) in the dialog. The \l file and \l files properties
59 are updated only after the final selection has been made by accepting
60 the dialog.
61
62 \code
63 MenuItem {
64 text: "Open..."
65 onTriggered: fileDialog.open()
66 }
67
68 FileDialog {
69 id: fileDialog
70 currentFile: document.source
71 folder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation)
72 }
73
74 MyDocument {
75 id: document
76 source: fileDialog.file
77 }
78 \endcode
79
80 \section2 Availability
81
82 A native platform file dialog is currently available on the following platforms:
83
84 \list
85 \li iOS
86 \li Linux (when running with the GTK+ platform theme)
87 \li macOS
88 \li Windows
89 \li WinRT
90 \endlist
91
92 \input includes/widgets.qdocinc 1
93
94 \labs
95
96 \sa FolderDialog, StandardPaths
97*/
98
99QQuickPlatformFileDialog::QQuickPlatformFileDialog(QObject *parent)
100 : QQuickPlatformDialog(QPlatformTheme::FileDialog, parent),
101 m_fileMode(OpenFile),
102 m_options(QFileDialogOptions::create()),
103 m_selectedNameFilter(nullptr)
104{
105 m_options->setFileMode(QFileDialogOptions::ExistingFile);
106 m_options->setAcceptMode(QFileDialogOptions::AcceptOpen);
107}
108
109/*!
110 \qmlproperty enumeration Qt.labs.platform::FileDialog::fileMode
111
112 This property holds the mode of the dialog.
113
114 Available values:
115 \value FileDialog.OpenFile The dialog is used to select an existing file (default).
116 \value FileDialog.OpenFiles The dialog is used to select multiple existing files.
117 \value FileDialog.SaveFile The dialog is used to select any file. The file does not have to exist.
118*/
119QQuickPlatformFileDialog::FileMode QQuickPlatformFileDialog::fileMode() const
120{
121 return m_fileMode;
122}
123
124void QQuickPlatformFileDialog::setFileMode(FileMode mode)
125{
126 if (mode == m_fileMode)
127 return;
128
129 switch (mode) {
130 case OpenFile:
131 m_options->setFileMode(QFileDialogOptions::ExistingFile);
132 m_options->setAcceptMode(QFileDialogOptions::AcceptOpen);
133 break;
134 case OpenFiles:
135 m_options->setFileMode(QFileDialogOptions::ExistingFiles);
136 m_options->setAcceptMode(QFileDialogOptions::AcceptOpen);
137 break;
138 case SaveFile:
139 m_options->setFileMode(QFileDialogOptions::AnyFile);
140 m_options->setAcceptMode(QFileDialogOptions::AcceptSave);
141 break;
142 default:
143 break;
144 }
145
146 m_fileMode = mode;
147 emit fileModeChanged();
148}
149
150/*!
151 \qmlproperty url Qt.labs.platform::FileDialog::file
152
153 This property holds the final accepted file.
154
155 Unlike the \l currentFile property, the \c file property is not updated
156 while the user is selecting files in the dialog, but only after the final
157 selection has been made. That is, when the user has clicked \uicontrol OK
158 to accept a file. Alternatively, the \l {Dialog::}{accepted()} signal
159 can be handled to get the final selection.
160
161 \sa currentFile, {Dialog::}{accepted()}
162*/
163QUrl QQuickPlatformFileDialog::file() const
164{
165 return addDefaultSuffix(file: m_files.value(i: 0));
166}
167
168void QQuickPlatformFileDialog::setFile(const QUrl &file)
169{
170 setFiles(QList<QUrl>() << file);
171}
172
173/*!
174 \qmlproperty list<url> Qt.labs.platform::FileDialog::files
175
176 This property holds the final accepted files.
177
178 Unlike the \l currentFiles property, the \c files property is not updated
179 while the user is selecting files in the dialog, but only after the final
180 selection has been made. That is, when the user has clicked \uicontrol OK
181 to accept files. Alternatively, the \l {Dialog::}{accepted()} signal
182 can be handled to get the final selection.
183
184 \sa currentFiles, {Dialog::}{accepted()}
185*/
186QList<QUrl> QQuickPlatformFileDialog::files() const
187{
188 return addDefaultSuffixes(files: m_files);
189}
190
191void QQuickPlatformFileDialog::setFiles(const QList<QUrl> &files)
192{
193 if (m_files == files)
194 return;
195
196 bool firstChanged = m_files.value(i: 0) != files.value(i: 0);
197 m_files = files;
198 if (firstChanged)
199 emit fileChanged();
200 emit filesChanged();
201}
202
203/*!
204 \qmlproperty url Qt.labs.platform::FileDialog::currentFile
205
206 This property holds the currently selected file in the dialog.
207
208 Unlike the \l file property, the \c currentFile property is updated
209 while the user is selecting files in the dialog, even before the final
210 selection has been made.
211
212 \sa file, currentFiles
213*/
214QUrl QQuickPlatformFileDialog::currentFile() const
215{
216 return currentFiles().value(i: 0);
217}
218
219void QQuickPlatformFileDialog::setCurrentFile(const QUrl &file)
220{
221 setCurrentFiles(QList<QUrl>() << file);
222}
223
224/*!
225 \qmlproperty list<url> Qt.labs.platform::FileDialog::currentFiles
226
227 This property holds the currently selected files in the dialog.
228
229 Unlike the \l files property, the \c currentFiles property is updated
230 while the user is selecting files in the dialog, even before the final
231 selection has been made.
232
233 \sa files, currentFile
234*/
235QList<QUrl> QQuickPlatformFileDialog::currentFiles() const
236{
237 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(object: handle()))
238 return fileDialog->selectedFiles();
239 return m_options->initiallySelectedFiles();
240}
241
242void QQuickPlatformFileDialog::setCurrentFiles(const QList<QUrl> &files)
243{
244 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(object: handle())) {
245 for (const QUrl &file : files)
246 fileDialog->selectFile(filename: file);
247 }
248 m_options->setInitiallySelectedFiles(files);
249}
250
251/*!
252 \qmlproperty url Qt.labs.platform::FileDialog::folder
253
254 This property holds the folder where files are selected.
255 For selecting a folder, use FolderDialog instead.
256
257 \sa FolderDialog
258*/
259QUrl QQuickPlatformFileDialog::folder() const
260{
261 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(object: handle()))
262 return fileDialog->directory();
263 return m_options->initialDirectory();
264}
265
266void QQuickPlatformFileDialog::setFolder(const QUrl &folder)
267{
268 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(object: handle()))
269 fileDialog->setDirectory(folder);
270 m_options->setInitialDirectory(folder);
271}
272
273/*!
274 \qmlproperty flags Qt.labs.platform::FileDialog::options
275
276 This property holds the various options that affect the look and feel of the dialog.
277
278 By default, all options are disabled.
279
280 Options should be set before showing the dialog. Setting them while the dialog is
281 visible is not guaranteed to have an immediate effect on the dialog (depending on
282 the option and on the platform).
283
284 Available options:
285 \value FileDialog.DontResolveSymlinks Don't resolve symlinks in the file dialog. By default symlinks are resolved.
286 \value FileDialog.DontConfirmOverwrite Don't ask for confirmation if an existing file is selected. By default confirmation is requested.
287 \value FileDialog.ReadOnly Indicates that the dialog doesn't allow creating directories.
288 \value FileDialog.HideNameFilterDetails Indicates if the file name filter details are hidden or not.
289*/
290QFileDialogOptions::FileDialogOptions QQuickPlatformFileDialog::options() const
291{
292 return m_options->options();
293}
294
295void QQuickPlatformFileDialog::setOptions(QFileDialogOptions::FileDialogOptions options)
296{
297 if (options == m_options->options())
298 return;
299
300 m_options->setOptions(options);
301 emit optionsChanged();
302}
303
304void QQuickPlatformFileDialog::resetOptions()
305{
306 setOptions({});
307}
308
309/*!
310 \qmlproperty list<string> Qt.labs.platform::FileDialog::nameFilters
311
312 This property holds the filters that restrict the types of files that
313 can be selected.
314
315 \code
316 FileDialog {
317 nameFilters: ["Text files (*.txt)", "HTML files (*.html *.htm)"]
318 }
319 \endcode
320
321 \note \b{*.*} is not a portable filter, because the historical assumption
322 that the file extension determines the file type is not consistent on every
323 operating system. It is possible to have a file with no dot in its name (for
324 example, \c Makefile). In a native Windows file dialog, \b{*.*} will match
325 such files, while in other types of file dialogs it may not. So it is better
326 to use \b{*} if you mean to select any file.
327
328 \sa selectedNameFilter
329*/
330QStringList QQuickPlatformFileDialog::nameFilters() const
331{
332 return m_options->nameFilters();
333}
334
335void QQuickPlatformFileDialog::setNameFilters(const QStringList &filters)
336{
337 if (filters == m_options->nameFilters())
338 return;
339
340 m_options->setNameFilters(filters);
341 if (m_selectedNameFilter) {
342 int index = m_selectedNameFilter->index();
343 if (index < 0 || index >= filters.count())
344 index = 0;
345 m_selectedNameFilter->update(filter: filters.value(i: index));
346 }
347 emit nameFiltersChanged();
348}
349
350void QQuickPlatformFileDialog::resetNameFilters()
351{
352 setNameFilters(QStringList());
353}
354
355/*!
356 \qmlproperty int Qt.labs.platform::FileDialog::selectedNameFilter.index
357 \qmlproperty string Qt.labs.platform::FileDialog::selectedNameFilter.name
358 \qmlproperty list<string> Qt.labs.platform::FileDialog::selectedNameFilter.extensions
359
360 These properties hold the currently selected name filter.
361
362 \table
363 \header
364 \li Name
365 \li Description
366 \row
367 \li \b index : int
368 \li This property determines which \l {nameFilters}{name filter} is selected.
369 The specified filter is selected when the dialog is opened. The value is
370 updated when the user selects another filter.
371 \row
372 \li [read-only] \b name : string
373 \li This property holds the name of the selected filter. In the
374 example below, the name of the first filter is \c {"Text files"}
375 and the second is \c {"HTML files"}.
376 \row
377 \li [read-only] \b extensions : list<string>
378 \li This property holds the list of extensions of the selected filter.
379 In the example below, the list of extensions of the first filter is
380 \c {["txt"]} and the second is \c {["html", "htm"]}.
381 \endtable
382
383 \code
384 FileDialog {
385 id: fileDialog
386 selectedNameFilter.index: 1
387 nameFilters: ["Text files (*.txt)", "HTML files (*.html *.htm)"]
388 }
389
390 MyDocument {
391 id: document
392 fileType: fileDialog.selectedNameFilter.extensions[0]
393 }
394 \endcode
395
396 \sa nameFilters
397*/
398QQuickPlatformFileNameFilter *QQuickPlatformFileDialog::selectedNameFilter() const
399{
400 if (!m_selectedNameFilter) {
401 QQuickPlatformFileDialog *that = const_cast<QQuickPlatformFileDialog *>(this);
402 m_selectedNameFilter = new QQuickPlatformFileNameFilter(that);
403 m_selectedNameFilter->setOptions(m_options);
404 }
405 return m_selectedNameFilter;
406}
407
408/*!
409 \qmlproperty string Qt.labs.platform::FileDialog::defaultSuffix
410
411 This property holds a suffix that is added to selected files that have
412 no suffix specified. The suffix is typically used to indicate the file
413 type (e.g. "txt" indicates a text file).
414
415 If the first character is a dot ('.'), it is removed.
416*/
417QString QQuickPlatformFileDialog::defaultSuffix() const
418{
419 return m_options->defaultSuffix();
420}
421
422void QQuickPlatformFileDialog::setDefaultSuffix(const QString &suffix)
423{
424 if (suffix == m_options->defaultSuffix())
425 return;
426
427 m_options->setDefaultSuffix(suffix);
428 emit defaultSuffixChanged();
429}
430
431void QQuickPlatformFileDialog::resetDefaultSuffix()
432{
433 setDefaultSuffix(QString());
434}
435
436/*!
437 \qmlproperty string Qt.labs.platform::FileDialog::acceptLabel
438
439 This property holds the label text shown on the button that accepts the dialog.
440
441 When set to an empty string, the default label of the underlying platform is used.
442 The default label is typically \uicontrol Open or \uicontrol Save depending on which
443 \l fileMode the dialog is used in.
444
445 The default value is an empty string.
446
447 \sa rejectLabel
448*/
449QString QQuickPlatformFileDialog::acceptLabel() const
450{
451 return m_options->labelText(label: QFileDialogOptions::Accept);
452}
453
454void QQuickPlatformFileDialog::setAcceptLabel(const QString &label)
455{
456 if (label == m_options->labelText(label: QFileDialogOptions::Accept))
457 return;
458
459 m_options->setLabelText(label: QFileDialogOptions::Accept, text: label);
460 emit acceptLabelChanged();
461}
462
463void QQuickPlatformFileDialog::resetAcceptLabel()
464{
465 setAcceptLabel(QString());
466}
467
468/*!
469 \qmlproperty string Qt.labs.platform::FileDialog::rejectLabel
470
471 This property holds the label text shown on the button that rejects the dialog.
472
473 When set to an empty string, the default label of the underlying platform is used.
474 The default label is typically \uicontrol Cancel.
475
476 The default value is an empty string.
477
478 \sa acceptLabel
479*/
480QString QQuickPlatformFileDialog::rejectLabel() const
481{
482 return m_options->labelText(label: QFileDialogOptions::Reject);
483}
484
485void QQuickPlatformFileDialog::setRejectLabel(const QString &label)
486{
487 if (label == m_options->labelText(label: QFileDialogOptions::Reject))
488 return;
489
490 m_options->setLabelText(label: QFileDialogOptions::Reject, text: label);
491 emit rejectLabelChanged();
492}
493
494void QQuickPlatformFileDialog::resetRejectLabel()
495{
496 setRejectLabel(QString());
497}
498
499bool QQuickPlatformFileDialog::useNativeDialog() const
500{
501 return QQuickPlatformDialog::useNativeDialog()
502 && !m_options->testOption(option: QFileDialogOptions::DontUseNativeDialog);
503}
504
505void QQuickPlatformFileDialog::onCreate(QPlatformDialogHelper *dialog)
506{
507 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(object: dialog)) {
508 // TODO: emit currentFileChanged only when the first entry in currentFiles changes
509 connect(sender: fileDialog, signal: &QPlatformFileDialogHelper::currentChanged, receiver: this, slot: &QQuickPlatformFileDialog::currentFileChanged);
510 connect(sender: fileDialog, signal: &QPlatformFileDialogHelper::currentChanged, receiver: this, slot: &QQuickPlatformFileDialog::currentFilesChanged);
511 connect(sender: fileDialog, signal: &QPlatformFileDialogHelper::directoryEntered, receiver: this, slot: &QQuickPlatformFileDialog::folderChanged);
512 fileDialog->setOptions(m_options);
513 }
514}
515
516void QQuickPlatformFileDialog::onShow(QPlatformDialogHelper *dialog)
517{
518 m_options->setWindowTitle(title());
519 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(object: dialog)) {
520 fileDialog->setOptions(m_options); // setOptions only assigns a member and isn't virtual
521 if (m_firstShow && m_options->initialDirectory().isValid())
522 fileDialog->setDirectory(m_options->initialDirectory());
523 if (m_selectedNameFilter) {
524 const int index = m_selectedNameFilter->index();
525 const QString filter = m_options->nameFilters().value(i: index);
526 m_options->setInitiallySelectedNameFilter(filter);
527 fileDialog->selectNameFilter(filter);
528 connect(sender: fileDialog, signal: &QPlatformFileDialogHelper::filterSelected, receiver: m_selectedNameFilter, slot: &QQuickPlatformFileNameFilter::update);
529 }
530 }
531 if (m_firstShow)
532 m_firstShow = false;
533}
534
535void QQuickPlatformFileDialog::onHide(QPlatformDialogHelper *dialog)
536{
537 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(object: dialog)) {
538 if (m_selectedNameFilter)
539 disconnect(sender: fileDialog, signal: &QPlatformFileDialogHelper::filterSelected, receiver: m_selectedNameFilter, slot: &QQuickPlatformFileNameFilter::update);
540 }
541}
542
543void QQuickPlatformFileDialog::accept()
544{
545 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(object: handle()))
546 setFiles(fileDialog->selectedFiles());
547 QQuickPlatformDialog::accept();
548}
549
550QUrl QQuickPlatformFileDialog::addDefaultSuffix(const QUrl &file) const
551{
552 QUrl url = file;
553 const QString path = url.path();
554 const QString suffix = m_options->defaultSuffix();
555 if (!suffix.isEmpty() && !path.endsWith(c: QLatin1Char('/')) && path.lastIndexOf(c: QLatin1Char('.')) == -1)
556 url.setPath(path: path + QLatin1Char('.') + suffix);
557 return url;
558}
559
560QList<QUrl> QQuickPlatformFileDialog::addDefaultSuffixes(const QList<QUrl> &files) const
561{
562 QList<QUrl> urls;
563 urls.reserve(alloc: files.size());
564 for (const QUrl &file : files)
565 urls += addDefaultSuffix(file);
566 return urls;
567}
568
569QQuickPlatformFileNameFilter::QQuickPlatformFileNameFilter(QObject *parent)
570 : QObject(parent), m_index(-1)
571{
572}
573
574int QQuickPlatformFileNameFilter::index() const
575{
576 return m_index;
577}
578
579void QQuickPlatformFileNameFilter::setIndex(int index)
580{
581 if (m_index == index)
582 return;
583
584 m_index = index;
585 emit indexChanged(index);
586}
587
588QString QQuickPlatformFileNameFilter::name() const
589{
590 return m_name;
591}
592
593QStringList QQuickPlatformFileNameFilter::extensions() const
594{
595 return m_extensions;
596}
597
598QSharedPointer<QFileDialogOptions> QQuickPlatformFileNameFilter::options() const
599{
600 return m_options;
601}
602
603void QQuickPlatformFileNameFilter::setOptions(const QSharedPointer<QFileDialogOptions> &options)
604{
605 m_options = options;
606}
607
608static QString extractName(const QString &filter)
609{
610 return filter.left(n: filter.indexOf(c: QLatin1Char('(')) - 1);
611}
612
613static QString extractExtension(const QString &filter)
614{
615 return filter.mid(position: filter.indexOf(c: QLatin1Char('.')) + 1);
616}
617
618static QStringList extractExtensions(const QString &filter)
619{
620 QStringList extensions;
621 const int from = filter.indexOf(c: QLatin1Char('('));
622 const int to = filter.lastIndexOf(c: QLatin1Char(')')) - 1;
623 if (from >= 0 && from < to) {
624 const QStringRef ref = filter.midRef(position: from + 1, n: to - from);
625 const QVector<QStringRef> exts = ref.split(sep: QLatin1Char(' '), behavior: Qt::SkipEmptyParts);
626 for (const QStringRef &ref : exts)
627 extensions += extractExtension(filter: ref.toString());
628 }
629
630 return extensions;
631}
632
633void QQuickPlatformFileNameFilter::update(const QString &filter)
634{
635 const QStringList filters = nameFilters();
636
637 const int oldIndex = m_index;
638 const QString oldName = m_name;
639 const QStringList oldExtensions = m_extensions;
640
641 m_index = filters.indexOf(t: filter);
642 m_name = extractName(filter);
643 m_extensions = extractExtensions(filter);
644
645 if (oldIndex != m_index)
646 emit indexChanged(index: m_index);
647 if (oldName != m_name)
648 emit nameChanged(name: m_name);
649 if (oldExtensions != m_extensions)
650 emit extensionsChanged(extensions: m_extensions);
651}
652
653QStringList QQuickPlatformFileNameFilter::nameFilters() const
654{
655 return m_options ? m_options->nameFilters() : QStringList();
656}
657
658QString QQuickPlatformFileNameFilter::nameFilter(int index) const
659{
660 return m_options ? m_options->nameFilters().value(i: index) : QString();
661}
662
663QT_END_NAMESPACE
664

source code of qtquickcontrols2/src/imports/platform/qquickplatformfiledialog.cpp