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
qquicklabsplatformfolderdialog.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
9QT_BEGIN_NAMESPACE
10
11/*!
12 \qmltype FolderDialog
13 \inherits Dialog
14//! \nativetype QQuickLabsPlatformFolderDialog
15 \inqmlmodule Qt.labs.platform
16 \since 5.8
17 \deprecated [6.9] Use QtQuick.Dialogs::FolderDialog instead.
18 \brief A native folder dialog.
19
20 The FolderDialog type provides a QML API for native platform folder dialogs.
21
22 \image {qtlabsplatform-folderdialog-gtk.png} {A native folder dialog}
23
24 To show a folder dialog, construct an instance of FolderDialog, set the
25 desired properties, and call \l {Dialog::}{open()}. The \l currentFolder
26 property can be used to determine the currently selected folder in the
27 dialog. The \l folder property is updated only after the final selection
28 has been made by accepting the dialog.
29
30 \code
31 MenuItem {
32 text: "Open..."
33 onTriggered: folderDialog.open()
34 }
35
36 FolderDialog {
37 id: folderDialog
38 currentFolder: viewer.folder
39 folder: StandardPaths.standardLocations(StandardPaths.PicturesLocation)[0]
40 }
41
42 MyViewer {
43 id: viewer
44 folder: folderDialog.folder
45 }
46 \endcode
47
48 \section2 Availability
49
50 A native platform folder dialog is currently available on the following platforms:
51
52 \list
53 \li Android
54 \li iOS
55 \li Linux (when running with the GTK+ platform theme)
56 \li macOS
57 \li Windows
58 \endlist
59
60 \input includes/widgets.qdocinc 1
61
62 \labs
63
64 \sa QtQuick.Dialogs::FolderDialog, FileDialog, StandardPaths
65*/
66
67QQuickLabsPlatformFolderDialog::QQuickLabsPlatformFolderDialog(QObject *parent)
68 : QQuickLabsPlatformDialog(QPlatformTheme::FileDialog, parent),
69 m_options(QFileDialogOptions::create())
70{
71 m_options->setFileMode(QFileDialogOptions::Directory);
72 m_options->setAcceptMode(QFileDialogOptions::AcceptOpen);
73}
74
75/*!
76 \qmlproperty url Qt.labs.platform::FolderDialog::folder
77
78 This property holds the final accepted folder.
79
80 Unlike the \l currentFolder property, the \c folder property is not updated
81 while the user is selecting folders in the dialog, but only after the final
82 selection has been made. That is, when the user has clicked \uicontrol OK
83 to accept a folder. Alternatively, the \l {Dialog::}{accepted()} signal
84 can be handled to get the final selection.
85
86 \sa currentFolder, {Dialog::}{accepted()}
87*/
88QUrl QQuickLabsPlatformFolderDialog::folder() const
89{
90 return m_folder;
91}
92
93void QQuickLabsPlatformFolderDialog::setFolder(const QUrl &folder)
94{
95 if (m_folder == folder)
96 return;
97
98 m_folder = folder;
99 emit folderChanged();
100}
101
102/*!
103 \qmlproperty url Qt.labs.platform::FolderDialog::currentFolder
104
105 This property holds the currently selected folder in the dialog.
106
107 Unlike the \l folder property, the \c currentFolder property is updated
108 while the user is selecting folders in the dialog, even before the final
109 selection has been made.
110
111 \sa folder
112*/
113QUrl QQuickLabsPlatformFolderDialog::currentFolder() const
114{
115 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle())) {
116 const QList<QUrl> selectedFiles = fileDialog->selectedFiles();
117 if (!selectedFiles.isEmpty())
118 return selectedFiles.first();
119 return fileDialog->directory();
120 }
121 return m_options->initialDirectory();
122}
123
124void QQuickLabsPlatformFolderDialog::setCurrentFolder(const QUrl &folder)
125{
126 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(handle()))
127 fileDialog->setDirectory(folder);
128 m_options->setInitialDirectory(folder);
129}
130
131/*!
132 \qmlproperty flags Qt.labs.platform::FolderDialog::options
133
134 This property holds the various options that affect the look and feel of the dialog.
135
136 By default, all options are disabled.
137
138 Options should be set before showing the dialog. Setting them while the dialog is
139 visible is not guaranteed to have an immediate effect on the dialog (depending on
140 the option and on the platform).
141
142 Available options:
143 \value FolderDialog.ShowDirsOnly Only show directories in the folder dialog. By default both folders and directories are shown.
144 \value FolderDialog.DontResolveSymlinks Don't resolve symlinks in the folder dialog. By default symlinks are resolved.
145 \value FolderDialog.ReadOnly Indicates that the dialog doesn't allow creating directories.
146*/
147QFileDialogOptions::FileDialogOptions QQuickLabsPlatformFolderDialog::options() const
148{
149 return m_options->options();
150}
151
152void QQuickLabsPlatformFolderDialog::setOptions(QFileDialogOptions::FileDialogOptions options)
153{
154 if (options == m_options->options())
155 return;
156
157 m_options->setOptions(options);
158 emit optionsChanged();
159}
160
161void QQuickLabsPlatformFolderDialog::resetOptions()
162{
163 setOptions({});
164}
165
166/*!
167 \qmlproperty string Qt.labs.platform::FolderDialog::acceptLabel
168
169 This property holds the label text shown on the button that accepts the dialog.
170
171 When set to an empty string, the default label of the underlying platform is used.
172 The default label is typically \uicontrol Open.
173
174 The default value is an empty string.
175
176 \sa rejectLabel
177*/
178QString QQuickLabsPlatformFolderDialog::acceptLabel() const
179{
180 return m_options->labelText(QFileDialogOptions::Accept);
181}
182
183void QQuickLabsPlatformFolderDialog::setAcceptLabel(const QString &label)
184{
185 if (label == m_options->labelText(QFileDialogOptions::Accept))
186 return;
187
188 m_options->setLabelText(QFileDialogOptions::Accept, label);
189 emit acceptLabelChanged();
190}
191
192void QQuickLabsPlatformFolderDialog::resetAcceptLabel()
193{
194 setAcceptLabel(QString());
195}
196
197/*!
198 \qmlproperty string Qt.labs.platform::FolderDialog::rejectLabel
199
200 This property holds the label text shown on the button that rejects the dialog.
201
202 When set to an empty string, the default label of the underlying platform is used.
203 The default label is typically \uicontrol Cancel.
204
205 The default value is an empty string.
206
207 \sa acceptLabel
208*/
209QString QQuickLabsPlatformFolderDialog::rejectLabel() const
210{
211 return m_options->labelText(QFileDialogOptions::Reject);
212}
213
214void QQuickLabsPlatformFolderDialog::setRejectLabel(const QString &label)
215{
216 if (label == m_options->labelText(QFileDialogOptions::Reject))
217 return;
218
219 m_options->setLabelText(QFileDialogOptions::Reject, label);
220 emit rejectLabelChanged();
221}
222
223void QQuickLabsPlatformFolderDialog::resetRejectLabel()
224{
225 setRejectLabel(QString());
226}
227
228bool QQuickLabsPlatformFolderDialog::useNativeDialog() const
229{
230 return QQuickLabsPlatformDialog::useNativeDialog()
231 && !m_options->testOption(QFileDialogOptions::DontUseNativeDialog);
232}
233
234void QQuickLabsPlatformFolderDialog::onCreate(QPlatformDialogHelper *dialog)
235{
236 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(dialog)) {
237 connect(fileDialog, &QPlatformFileDialogHelper::currentChanged, this, &QQuickLabsPlatformFolderDialog::currentFolderChanged);
238 fileDialog->setOptions(m_options);
239 }
240}
241
242void QQuickLabsPlatformFolderDialog::onShow(QPlatformDialogHelper *dialog)
243{
244 m_options->setWindowTitle(title());
245 if (QPlatformFileDialogHelper *fileDialog = qobject_cast<QPlatformFileDialogHelper *>(dialog))
246 fileDialog->setOptions(m_options);
247}
248
249void QQuickLabsPlatformFolderDialog::accept()
250{
251 setFolder(currentFolder());
252 QQuickLabsPlatformDialog::accept();
253}
254
255QT_END_NAMESPACE
256
257#include "moc_qquicklabsplatformfolderdialog_p.cpp"
258
259#endif // QT_DEPRECATED_SINCE(6, 9)