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