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