| 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 | |
| 4 | #include "qquickfolderdialogimpl_p.h" |
| 5 | #include "qquickfolderdialogimpl_p_p.h" |
| 6 | |
| 7 | #include <QtCore/qloggingcategory.h> |
| 8 | #include <QtQuickTemplates2/private/qquickdialogbuttonbox_p_p.h> |
| 9 | |
| 10 | #include "qquickfiledialogdelegate_p.h" |
| 11 | #include "qquickfolderbreadcrumbbar_p.h" |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | Q_LOGGING_CATEGORY(lcFolderDialogCurrentFolder, "qt.quick.dialogs.quickfolderdialogimpl.currentFolder" ) |
| 16 | Q_LOGGING_CATEGORY(lcFolderDialogSelectedFolder, "qt.quick.dialogs.quickfolderdialogimpl.selectedFolder" ) |
| 17 | Q_LOGGING_CATEGORY(lcFolderDialogOptions, "qt.quick.dialogs.quickfolderdialogimpl.options" ) |
| 18 | |
| 19 | QQuickFolderDialogImplPrivate::QQuickFolderDialogImplPrivate() |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | void QQuickFolderDialogImplPrivate::updateEnabled() |
| 24 | { |
| 25 | Q_Q(QQuickFolderDialogImpl); |
| 26 | if (!buttonBox) |
| 27 | return; |
| 28 | |
| 29 | QQuickFolderDialogImplAttached *attached = attachedOrWarn(); |
| 30 | if (!attached) |
| 31 | return; |
| 32 | |
| 33 | auto openButton = buttonBox->standardButton(button: QPlatformDialogHelper::Open); |
| 34 | if (!openButton) { |
| 35 | qmlWarning(me: q).nospace() << "Can't update Open button's enabled state because it wasn't found" ; |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | openButton->setEnabled(!selectedFolder.isEmpty() && attached->breadcrumbBar() |
| 40 | && !attached->breadcrumbBar()->textField()->isVisible()); |
| 41 | } |
| 42 | |
| 43 | /*! |
| 44 | \internal |
| 45 | |
| 46 | Ensures that a folder is always selected after a change in \c currentFolder. |
| 47 | |
| 48 | \a oldFolderPath is the previous value of \c currentFolder. |
| 49 | */ |
| 50 | void QQuickFolderDialogImplPrivate::updateSelectedFolder(const QString &oldFolderPath) |
| 51 | { |
| 52 | Q_Q(QQuickFolderDialogImpl); |
| 53 | QQuickFolderDialogImplAttached *attached = attachedOrWarn(); |
| 54 | if (!attached || !attached->folderDialogListView()) |
| 55 | return; |
| 56 | |
| 57 | QString newSelectedFolderPath; |
| 58 | int newSelectedFolderIndex = 0; |
| 59 | const QString newFolderPath = QQmlFile::urlToLocalFileOrQrc(currentFolder); |
| 60 | if (!oldFolderPath.isEmpty() && !newFolderPath.isEmpty()) { |
| 61 | // If the user went up a directory (or several), we should set |
| 62 | // selectedFolder to be the directory that we were in (or |
| 63 | // its closest ancestor that is a child of the new directory). |
| 64 | // E.g. if oldFolderPath is /foo/bar/baz/abc/xyz, and newFolderPath is /foo/bar, |
| 65 | // then we want to set selectedFolder to be /foo/bar/baz. |
| 66 | const int indexOfFolder = oldFolderPath.indexOf(s: newFolderPath); |
| 67 | if (indexOfFolder != -1) { |
| 68 | // [folder] |
| 69 | // [ oldFolderPath ] |
| 70 | // /foo/bar/baz/abc/xyz |
| 71 | // [rel...Paths] |
| 72 | QStringList relativePaths = oldFolderPath.mid(position: indexOfFolder + newFolderPath.size()).split(sep: QLatin1Char('/'), behavior: Qt::SkipEmptyParts); |
| 73 | newSelectedFolderPath = newFolderPath + QLatin1Char('/') + relativePaths.first(); |
| 74 | |
| 75 | // Now find the index of that directory so that we can set the ListView's currentIndex to it. |
| 76 | const QDir newFolderDir(newFolderPath); |
| 77 | // Just to be safe... |
| 78 | if (!newFolderDir.exists()) { |
| 79 | qmlWarning(me: q) << "Directory" << newSelectedFolderPath << "doesn't exist; can't get a file entry list for it" ; |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | const QFileInfoList dirs = newFolderDir.entryInfoList(filters: QDir::Dirs | QDir::NoDotAndDotDot, sort: QDir::DirsFirst); |
| 84 | const QFileInfo newSelectedFileInfo(newSelectedFolderPath); |
| 85 | // The directory can contain files, but since we put dirs first, that should never affect the indices. |
| 86 | newSelectedFolderIndex = dirs.indexOf(t: newSelectedFileInfo); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (newSelectedFolderPath.isEmpty()) { |
| 91 | // When entering into a directory that isn't a parent of the old one, the first |
| 92 | // file delegate should be selected. |
| 93 | // TODO: is there a cheaper way to do this? QDirIterator doesn't support sorting, |
| 94 | // so we can't use that. QQuickFolderListModel uses threads to fetch its data, |
| 95 | // so should be considered asynchronous. We might be able to use it, but it would |
| 96 | // complicate the code even more... |
| 97 | QDir newFolderDir(newFolderPath); |
| 98 | if (newFolderDir.exists()) { |
| 99 | const QFileInfoList files = newFolderDir.entryInfoList(filters: QDir::Dirs | QDir::NoDotAndDotDot, sort: QDir::DirsFirst); |
| 100 | if (!files.isEmpty()) |
| 101 | newSelectedFolderPath = files.first().absoluteFilePath(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | const bool folderSelected = !newSelectedFolderPath.isEmpty(); |
| 106 | q->setSelectedFolder(folderSelected ? QUrl::fromLocalFile(localfile: newSelectedFolderPath) : QUrl()); |
| 107 | { |
| 108 | // Set the appropriate currentIndex for the selected folder. We block signals from ListView |
| 109 | // because we don't want folderDialogListViewCurrentIndexChanged to be called, as the file |
| 110 | // it gets from the delegate will not be up-to-date (but most importantly because we already |
| 111 | // just set the selected folder). |
| 112 | QSignalBlocker blocker(attached->folderDialogListView()); |
| 113 | attached->folderDialogListView()->setCurrentIndex(folderSelected ? newSelectedFolderIndex : -1); |
| 114 | } |
| 115 | if (folderSelected) { |
| 116 | if (QQuickItem *currentItem = attached->folderDialogListView()->currentItem()) |
| 117 | currentItem->forceActiveFocus(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void QQuickFolderDialogImplPrivate::handleAccept() |
| 122 | { |
| 123 | // Let handleClick take care of calling accept(). |
| 124 | } |
| 125 | |
| 126 | void QQuickFolderDialogImplPrivate::handleClick(QQuickAbstractButton *button) |
| 127 | { |
| 128 | Q_Q(QQuickFolderDialogImpl); |
| 129 | if (buttonRole(button) == QPlatformDialogHelper::AcceptRole && selectedFolder.isValid()) { |
| 130 | q->setSelectedFolder(selectedFolder); |
| 131 | q->accept(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /*! |
| 136 | \class QQuickFolderDialogImpl |
| 137 | \internal |
| 138 | |
| 139 | An interface that QQuickFolderDialog can use to access the non-native Qt Quick FolderDialog. |
| 140 | |
| 141 | Both this and the native implementations are created in QQuickAbstractDialog::create(). |
| 142 | */ |
| 143 | |
| 144 | QQuickFolderDialogImpl::QQuickFolderDialogImpl(QObject *parent) |
| 145 | : QQuickDialog(*(new QQuickFolderDialogImplPrivate), parent) |
| 146 | { |
| 147 | setPopupType(QQuickPopup::Window); |
| 148 | } |
| 149 | |
| 150 | QQuickFolderDialogImplAttached *QQuickFolderDialogImpl::qmlAttachedProperties(QObject *object) |
| 151 | { |
| 152 | return new QQuickFolderDialogImplAttached(object); |
| 153 | } |
| 154 | |
| 155 | QUrl QQuickFolderDialogImpl::currentFolder() const |
| 156 | { |
| 157 | Q_D(const QQuickFolderDialogImpl); |
| 158 | return d->currentFolder; |
| 159 | } |
| 160 | |
| 161 | void QQuickFolderDialogImpl::setCurrentFolder(const QUrl ¤tFolder) |
| 162 | { |
| 163 | qCDebug(lcFolderDialogCurrentFolder) << "setCurrentFolder called with" << currentFolder; |
| 164 | Q_D(QQuickFolderDialogImpl); |
| 165 | if (currentFolder == d->currentFolder) |
| 166 | return; |
| 167 | |
| 168 | const QString oldFolderPath = QQmlFile::urlToLocalFileOrQrc(d->currentFolder); |
| 169 | |
| 170 | d->currentFolder = currentFolder; |
| 171 | d->updateSelectedFolder(oldFolderPath); |
| 172 | emit currentFolderChanged(folderUrl: d->currentFolder); |
| 173 | } |
| 174 | |
| 175 | QUrl QQuickFolderDialogImpl::selectedFolder() const |
| 176 | { |
| 177 | Q_D(const QQuickFolderDialogImpl); |
| 178 | return d->selectedFolder; |
| 179 | } |
| 180 | |
| 181 | void QQuickFolderDialogImpl::setSelectedFolder(const QUrl &selectedFolder) |
| 182 | { |
| 183 | Q_D(QQuickFolderDialogImpl); |
| 184 | qCDebug(lcFolderDialogSelectedFolder).nospace() << "setSelectedFolder called with selectedFolder " |
| 185 | << selectedFolder << " (d->selectedFolder is " << d->selectedFolder << ")" ; |
| 186 | if (selectedFolder == d->selectedFolder) |
| 187 | return; |
| 188 | |
| 189 | d->selectedFolder = selectedFolder; |
| 190 | d->updateEnabled(); |
| 191 | emit selectedFolderChanged(folderUrl: selectedFolder); |
| 192 | } |
| 193 | |
| 194 | QSharedPointer<QFileDialogOptions> QQuickFolderDialogImpl::options() const |
| 195 | { |
| 196 | Q_D(const QQuickFolderDialogImpl); |
| 197 | return d->options; |
| 198 | } |
| 199 | |
| 200 | void QQuickFolderDialogImpl::setOptions(const QSharedPointer<QFileDialogOptions> &options) |
| 201 | { |
| 202 | qCDebug(lcFolderDialogOptions).nospace() << "setOptions called with:" |
| 203 | << " acceptMode=" << options->acceptMode() |
| 204 | << " fileMode=" << options->fileMode() |
| 205 | << " initialDirectory=" << options->initialDirectory(); |
| 206 | |
| 207 | Q_D(QQuickFolderDialogImpl); |
| 208 | d->options = options; |
| 209 | } |
| 210 | |
| 211 | /*! |
| 212 | \internal |
| 213 | |
| 214 | These allow QQuickPlatformFileDialog::show() to set custom labels on the |
| 215 | dialog buttons without having to know about/go through QQuickFolderDialogImplAttached |
| 216 | and QQuickDialogButtonBox. |
| 217 | */ |
| 218 | void QQuickFolderDialogImpl::setAcceptLabel(const QString &label) |
| 219 | { |
| 220 | Q_D(QQuickFolderDialogImpl); |
| 221 | d->acceptLabel = label; |
| 222 | QQuickFolderDialogImplAttached *attached = d->attachedOrWarn(); |
| 223 | if (!attached) |
| 224 | return; |
| 225 | |
| 226 | auto acceptButton = d->buttonBox->standardButton(button: QPlatformDialogHelper::Open); |
| 227 | if (!acceptButton) { |
| 228 | qmlWarning(me: this).nospace() << "Can't set accept label to " << label |
| 229 | << "; failed to find Open button in DialogButtonBox of " << this; |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | acceptButton->setText(!label.isEmpty() |
| 234 | ? label : QQuickDialogButtonBoxPrivate::buttonText(standardButton: QPlatformDialogHelper::Open)); |
| 235 | } |
| 236 | |
| 237 | void QQuickFolderDialogImpl::setRejectLabel(const QString &label) |
| 238 | { |
| 239 | Q_D(QQuickFolderDialogImpl); |
| 240 | d->rejectLabel = label; |
| 241 | if (!d->buttonBox) |
| 242 | return; |
| 243 | |
| 244 | auto rejectButton = d->buttonBox->standardButton(button: QPlatformDialogHelper::Cancel); |
| 245 | if (!rejectButton) { |
| 246 | qmlWarning(me: this).nospace() << "Can't set reject label to " << label |
| 247 | << "; failed to find Open button in DialogButtonBox of " << this; |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | rejectButton->setText(!label.isEmpty() |
| 252 | ? label : QQuickDialogButtonBoxPrivate::buttonText(standardButton: QPlatformDialogHelper::Cancel)); |
| 253 | } |
| 254 | |
| 255 | void QQuickFolderDialogImpl::componentComplete() |
| 256 | { |
| 257 | Q_D(QQuickFolderDialogImpl); |
| 258 | QQuickDialog::componentComplete(); |
| 259 | |
| 260 | // Find the right-most button and set its key navigation so that |
| 261 | // tab moves focus to the breadcrumb bar's up button. I tried |
| 262 | // doing this via KeyNavigation on the DialogButtonBox in QML, |
| 263 | // but it didn't work (probably because it's not the right item). |
| 264 | QQuickFolderDialogImplAttached *attached = d->attachedOrWarn(); |
| 265 | if (!attached) |
| 266 | return; |
| 267 | |
| 268 | Q_ASSERT(d->buttonBox); |
| 269 | const int buttonCount = d->buttonBox->count(); |
| 270 | if (buttonCount == 0) |
| 271 | return; |
| 272 | |
| 273 | QQuickAbstractButton *rightMostButton = qobject_cast<QQuickAbstractButton *>( |
| 274 | object: d->buttonBox->itemAt(index: buttonCount - 1)); |
| 275 | if (!rightMostButton) { |
| 276 | qmlWarning(me: this) << "Can't find right-most button in DialogButtonBox" ; |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | auto keyNavigationAttached = QQuickKeyNavigationAttached::qmlAttachedProperties(rightMostButton); |
| 281 | if (!keyNavigationAttached) { |
| 282 | qmlWarning(me: this) << "Can't create attached KeyNavigation object on" << QDebug::toString(object: rightMostButton); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | keyNavigationAttached->setTab(attached->breadcrumbBar()->upButton()); |
| 287 | } |
| 288 | |
| 289 | void QQuickFolderDialogImpl::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) |
| 290 | { |
| 291 | Q_D(QQuickFolderDialogImpl); |
| 292 | QQuickDialog::itemChange(change, data); |
| 293 | |
| 294 | if (change != QQuickItem::ItemVisibleHasChanged || !isComponentComplete() || !data.boolValue) |
| 295 | return; |
| 296 | |
| 297 | QQuickFolderDialogImplAttached *attached = d->attachedOrWarn(); |
| 298 | if (!attached) |
| 299 | return; |
| 300 | |
| 301 | attached->folderDialogListView()->forceActiveFocus(); |
| 302 | d->updateEnabled(); |
| 303 | } |
| 304 | |
| 305 | QQuickFolderDialogImplAttached *QQuickFolderDialogImplPrivate::attachedOrWarn() |
| 306 | { |
| 307 | Q_Q(QQuickFolderDialogImpl); |
| 308 | QQuickFolderDialogImplAttached *attached = static_cast<QQuickFolderDialogImplAttached*>( |
| 309 | qmlAttachedPropertiesObject<QQuickFolderDialogImpl>(obj: q)); |
| 310 | if (!attached) |
| 311 | qmlWarning(me: q) << "Expected FileDialogImpl attached object to be present on" << this; |
| 312 | return attached; |
| 313 | } |
| 314 | |
| 315 | void QQuickFolderDialogImplAttachedPrivate::folderDialogListViewCurrentIndexChanged() |
| 316 | { |
| 317 | auto folderDialogImpl = qobject_cast<QQuickFolderDialogImpl*>(object: parent); |
| 318 | if (!folderDialogImpl) |
| 319 | return; |
| 320 | |
| 321 | auto folderDialogDelegate = qobject_cast<QQuickFileDialogDelegate*>(object: folderDialogListView->currentItem()); |
| 322 | if (!folderDialogDelegate) |
| 323 | return; |
| 324 | |
| 325 | folderDialogImpl->setSelectedFolder(folderDialogDelegate->file()); |
| 326 | } |
| 327 | |
| 328 | QQuickFolderDialogImplAttached::QQuickFolderDialogImplAttached(QObject *parent) |
| 329 | : QObject(*(new QQuickFolderDialogImplAttachedPrivate), parent) |
| 330 | { |
| 331 | if (!qobject_cast<QQuickFolderDialogImpl*>(object: parent)) { |
| 332 | qmlWarning(me: this) << "FolderDialogImpl attached properties should only be " |
| 333 | << "accessed through the root FileDialogImpl instance" ; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | QQuickListView *QQuickFolderDialogImplAttached::folderDialogListView() const |
| 338 | { |
| 339 | Q_D(const QQuickFolderDialogImplAttached); |
| 340 | return d->folderDialogListView; |
| 341 | } |
| 342 | |
| 343 | void QQuickFolderDialogImplAttached::setFolderDialogListView(QQuickListView *folderDialogListView) |
| 344 | { |
| 345 | Q_D(QQuickFolderDialogImplAttached); |
| 346 | if (folderDialogListView == d->folderDialogListView) |
| 347 | return; |
| 348 | |
| 349 | d->folderDialogListView = folderDialogListView; |
| 350 | |
| 351 | QObjectPrivate::connect(sender: d->folderDialogListView, signal: &QQuickListView::currentIndexChanged, |
| 352 | receiverPrivate: d, slot: &QQuickFolderDialogImplAttachedPrivate::folderDialogListViewCurrentIndexChanged); |
| 353 | |
| 354 | emit folderDialogListViewChanged(); |
| 355 | } |
| 356 | |
| 357 | QQuickFolderBreadcrumbBar *QQuickFolderDialogImplAttached::breadcrumbBar() const |
| 358 | { |
| 359 | Q_D(const QQuickFolderDialogImplAttached); |
| 360 | return d->breadcrumbBar; |
| 361 | } |
| 362 | |
| 363 | void QQuickFolderDialogImplAttached::setBreadcrumbBar(QQuickFolderBreadcrumbBar *breadcrumbBar) |
| 364 | { |
| 365 | Q_D(QQuickFolderDialogImplAttached); |
| 366 | if (breadcrumbBar == d->breadcrumbBar) |
| 367 | return; |
| 368 | |
| 369 | d->breadcrumbBar = breadcrumbBar; |
| 370 | emit breadcrumbBarChanged(); |
| 371 | } |
| 372 | |
| 373 | QT_END_NAMESPACE |
| 374 | |
| 375 | #include "moc_qquickfolderdialogimpl_p.cpp" |
| 376 | |