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

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