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
qquickfolderdialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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#include <QtCore/qloggingcategory.h>
8#include <QtQml/qqmlfile.h>
9#if QT_CONFIG(quick_listview) && QT_CONFIG(quick_draganddrop)
10#include <QtQuickDialogs2QuickImpl/private/qquickplatformfolderdialog_p.h>
11#include <QtQuickDialogs2QuickImpl/private/qquickfolderdialogimpl_p.h>
12#endif
13
15
16/*!
17 \qmltype FolderDialog
18 \inherits Dialog
19//! \nativetype QQuickFolderDialog
20 \inqmlmodule QtQuick.Dialogs
21 \since 6.3
22 \brief A native folder dialog.
23
24 The FolderDialog type provides a QML API for native platform folder dialogs.
25
26 \image qtquickdialogs-folderdialog-gtk.png {The folder dialog type allow dialogs to copy the native look and feel of the platform}
27
28 To show a folder dialog, construct an instance of FolderDialog, set the
29 desired properties, and call \l {Dialog::}{open()}. The \l currentFolder
30 property can be used to determine the folder that is currently being
31 displayed in the dialog. The \l selectedFolder property can be used to
32 determine the last folder that was selected in the dialog.
33
34 \code
35 MenuItem {
36 text: qsTr("Open...")
37 onTriggered: folderDialog.open()
38 }
39
40 FolderDialog {
41 id: folderDialog
42 currentFolder: StandardPaths.standardLocations(StandardPaths.PicturesLocation)[0]
43 selectedFolder: viewer.folder
44 }
45
46 MyViewer {
47 id: viewer
48 folder: folderDialog.selectedFolder
49 }
50 \endcode
51
52 \section2 Availability
53
54 A native platform folder dialog is currently available on the following platforms:
55
56 \list
57 \li Android
58 \li iOS
59 \li Linux (when running with the GTK+ platform theme)
60 \li macOS
61 \li Windows
62 \endlist
63
64 \include includes/fallback.qdocinc
65
66 \sa FileDialog, {QtCore::}{StandardPaths}
67*/
68
69QQuickFolderDialog::QQuickFolderDialog(QObject *parent)
70 : QQuickAbstractDialog(QQuickDialogType::FolderDialog, parent),
71 m_options(QFileDialogOptions::create())
72{
73 m_options->setFileMode(QFileDialogOptions::Directory);
74 m_options->setAcceptMode(QFileDialogOptions::AcceptOpen);
75 m_options->setInitialDirectory(QUrl::fromLocalFile(QDir::currentPath()));
76}
77
78/*!
79 \qmlproperty url QtQuick.Dialogs::FolderDialog::currentFolder
80
81 This property holds the folder that is currently being displayed in the dialog.
82
83 \sa selectedFolder
84*/
85QUrl QQuickFolderDialog::currentFolder() const
86{
87 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle()))
88 return fileDialog->directory();
89 return m_options->initialDirectory();
90}
91
92void QQuickFolderDialog::setCurrentFolder(const QUrl &folder)
93{
94 if (folder == m_options->initialDirectory())
95 return;
96
97 m_options->setInitialDirectory(folder);
98 emit currentFolderChanged();
99}
100
101/*!
102 \qmlproperty url QtQuick.Dialogs::FolderDialog::selectedFolder
103
104 This property holds the last folder that was selected in the dialog.
105
106 The value of this property is updated each time the user selects a folder
107 in the dialog, and when the dialog is accepted. Alternatively, the
108 \l {Dialog::}{accepted()} signal can be handled to get the final selection.
109
110 \sa currentFolder, {Dialog::}{accepted()}
111*/
112QUrl QQuickFolderDialog::selectedFolder() const
113{
114 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) {
115 const QList<QUrl> selectedFiles = fileDialog->selectedFiles();
116 if (!selectedFiles.isEmpty())
117 return selectedFiles.first();
118 }
119 return QUrl();
120}
121
122void QQuickFolderDialog::setSelectedFolder(const QUrl &folder)
123{
124 if (folder == selectedFolder())
125 return;
126
127 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) {
128 fileDialog->selectFile(folder);
129 emit selectedFolderChanged();
130 }
131}
132
133/*!
134 \qmlproperty flags QtQuick.Dialogs::FolderDialog::options
135
136 This property holds the various options that affect the look and feel of the dialog.
137
138 By default, all options are disabled.
139
140 Options should be set before showing the dialog. Setting them while the dialog is
141 visible is not guaranteed to have an immediate effect on the dialog (depending on
142 the option and on the platform).
143
144 Available options:
145 \value FolderDialog.DontResolveSymlinks Don't resolve symlinks in the folder dialog. By default symlinks are resolved.
146 \value FolderDialog.ReadOnly Indicates that the dialog doesn't allow creating directories.
147 \value FolderDialog.DontUseNativeDialog Forces the dialog to use a non-native quick implementation.
148*/
149QFileDialogOptions::FileDialogOptions QQuickFolderDialog::options() const
150{
151 return m_options->options();
152}
153
154void QQuickFolderDialog::setOptions(QFileDialogOptions::FileDialogOptions options)
155{
156 if (options == m_options->options())
157 return;
158
159 m_options->setOptions(options);
160 emit optionsChanged();
161}
162
163void QQuickFolderDialog::resetOptions()
164{
165 setOptions({});
166}
167
168/*!
169 \qmlproperty string QtQuick.Dialogs::FolderDialog::acceptLabel
170
171 This property holds the label text shown on the button that accepts the dialog.
172
173 When set to an empty string, the default label of the underlying platform is used.
174 The default label is typically \uicontrol Open.
175
176 The default value is an empty string.
177
178 \sa rejectLabel
179*/
180QString QQuickFolderDialog::acceptLabel() const
181{
182 return m_options->labelText(QFileDialogOptions::Accept);
183}
184
185void QQuickFolderDialog::setAcceptLabel(const QString &label)
186{
187 if (label == m_options->labelText(QFileDialogOptions::Accept))
188 return;
189
190 m_options->setLabelText(QFileDialogOptions::Accept, label);
191 emit acceptLabelChanged();
192}
193
194void QQuickFolderDialog::resetAcceptLabel()
195{
196 setAcceptLabel(QString());
197}
198
199/*!
200 \qmlproperty string QtQuick.Dialogs::FolderDialog::rejectLabel
201
202 This property holds the label text shown on the button that rejects the dialog.
203
204 When set to an empty string, the default label of the underlying platform is used.
205 The default label is typically \uicontrol Cancel.
206
207 The default value is an empty string.
208
209 \sa acceptLabel
210*/
211QString QQuickFolderDialog::rejectLabel() const
212{
213 return m_options->labelText(QFileDialogOptions::Reject);
214}
215
216void QQuickFolderDialog::setRejectLabel(const QString &label)
217{
218 if (label == m_options->labelText(QFileDialogOptions::Reject))
219 return;
220
221 m_options->setLabelText(QFileDialogOptions::Reject, label);
222 emit rejectLabelChanged();
223}
224
225void QQuickFolderDialog::resetRejectLabel()
226{
227 setRejectLabel(QString());
228}
229
230bool QQuickFolderDialog::useNativeDialog() const
231{
232 if (!QQuickAbstractDialog::useNativeDialog())
233 return false;
234
235 if (m_options->testOption(QFileDialogOptions::DontUseNativeDialog)) {
236 qCDebug(lcDialogs) << " - the FolderDialog was told not to use a native dialog; not using native dialog";
237 return false;
238 }
239
240 return true;
241}
242
243void QQuickFolderDialog::onCreate(QPlatformDialogHelper *dialog)
244{
245 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(dialog)) {
246 connect(fileDialog, &QPlatformFileDialogHelper::directoryEntered, this, &QQuickFolderDialog::currentFolderChanged);
247 connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, this, &QQuickFolderDialog::selectedFolderChanged);
248 fileDialog->setOptions(m_options);
249 }
250}
251
252void QQuickFolderDialog::onShow(QPlatformDialogHelper *dialog)
253{
254 m_options->setWindowTitle(title());
255 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(dialog)) {
256 fileDialog->setOptions(m_options);
257
258 const QUrl initialDir = m_options->initialDirectory();
259 // If it's not valid, we shouldn't set it.
260 if (m_firstShow && initialDir.isValid() && QDir(QQmlFile::urlToLocalFileOrQrc(initialDir)).exists())
261 fileDialog->setDirectory(m_options->initialDirectory());
262 }
263#if QT_CONFIG(quick_listview) && QT_CONFIG(quick_draganddrop)
264 if (QQuickPlatformFolderDialog *folderDialog = qobject_cast<QQuickPlatformFolderDialog *>(dialog))
265 folderDialog->dialog()->setPopupType(m_popupType);
266#endif
267
268 QQuickAbstractDialog::onShow(dialog);
269}
270
271QT_END_NAMESPACE
272
273#include "moc_qquickfolderdialog_p.cpp"