| 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 "qquickfiledialogimpl_p.h" |
| 5 | #include "qquickfiledialogimpl_p_p.h" |
| 6 | |
| 7 | #include <QtCore/qloggingcategory.h> |
| 8 | #include <QtGui/private/qguiapplication_p.h> |
| 9 | #include <QtGui/qpa/qplatformtheme.h> |
| 10 | #include <QtQml/qqmlinfo.h> |
| 11 | #include <QtQml/qqmlfile.h> |
| 12 | #include <QtQuick/private/qquickitemview_p_p.h> |
| 13 | #include <QtQuickTemplates2/private/qquickdialogbuttonbox_p_p.h> |
| 14 | #include <QtQuickTemplates2/private/qquickpopupitem_p_p.h> |
| 15 | #include <QtQuickControls2Impl/private/qquickplatformtheme_p.h> |
| 16 | #include <QtQuickDialogs2Utils/private/qquickfilenamefilter_p.h> |
| 17 | |
| 18 | #include "qquickfiledialogdelegate_p.h" |
| 19 | #include "qquickfolderbreadcrumbbar_p.h" |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | Q_LOGGING_CATEGORY(lcCurrentFolder, "qt.quick.dialogs.quickfiledialogimpl.currentFolder" ) |
| 24 | Q_LOGGING_CATEGORY(lcSelectedFile, "qt.quick.dialogs.quickfiledialogimpl.selectedFile" ) |
| 25 | Q_LOGGING_CATEGORY(lcUpdateSelectedFile, "qt.quick.dialogs.quickfiledialogimpl.updateSelectedFile" ) |
| 26 | Q_LOGGING_CATEGORY(lcOptions, "qt.quick.dialogs.quickfiledialogimpl.options" ) |
| 27 | Q_LOGGING_CATEGORY(lcNameFilters, "qt.quick.dialogs.quickfiledialogimpl.namefilters" ) |
| 28 | Q_LOGGING_CATEGORY(lcAttachedNameFilters, "qt.quick.dialogs.quickfiledialogimplattached.namefilters" ) |
| 29 | Q_LOGGING_CATEGORY(lcAttachedCurrentIndex, "qt.quick.dialogs.quickfiledialogimplattached.currentIndex" ) |
| 30 | |
| 31 | QQuickFileDialogImplPrivate::QQuickFileDialogImplPrivate() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | void QQuickFileDialogImplPrivate::setNameFilters(const QStringList &filters) |
| 36 | { |
| 37 | Q_Q(QQuickFileDialogImpl); |
| 38 | if (filters == nameFilters) |
| 39 | return; |
| 40 | |
| 41 | nameFilters = filters; |
| 42 | emit q->nameFiltersChanged(); |
| 43 | } |
| 44 | |
| 45 | void QQuickFileDialogImplPrivate::updateEnabled() |
| 46 | { |
| 47 | Q_Q(QQuickFileDialogImpl); |
| 48 | QQuickFileDialogImplAttached *attached = attachedOrWarn(); |
| 49 | if (!attached) |
| 50 | return; |
| 51 | |
| 52 | auto openButton = attached->buttonBox()->standardButton(button: QPlatformDialogHelper::Open); |
| 53 | if (!openButton) { |
| 54 | qmlWarning(me: q).nospace() << "Can't update Open button's enabled state because it wasn't found" ; |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | openButton->setEnabled(!selectedFile.isEmpty() && attached->breadcrumbBar() |
| 59 | && !attached->breadcrumbBar()->textField()->isVisible()); |
| 60 | } |
| 61 | |
| 62 | /*! |
| 63 | \internal |
| 64 | |
| 65 | Ensures that a file is always selected after a change in \c folder. |
| 66 | |
| 67 | \a oldFolderPath is the previous value of \c folder. |
| 68 | */ |
| 69 | void QQuickFileDialogImplPrivate::updateSelectedFile(const QString &oldFolderPath) |
| 70 | { |
| 71 | Q_Q(QQuickFileDialogImpl); |
| 72 | QQuickFileDialogImplAttached *attached = attachedOrWarn(); |
| 73 | if (!attached || !attached->fileDialogListView()) |
| 74 | return; |
| 75 | |
| 76 | qCDebug(lcUpdateSelectedFile) << "updateSelectedFile called with oldFolderPath" << oldFolderPath; |
| 77 | |
| 78 | QString newSelectedFilePath; |
| 79 | int newSelectedFileIndex = -1; |
| 80 | const QString newFolderPath = QQmlFile::urlToLocalFileOrQrc(currentFolder); |
| 81 | if (!oldFolderPath.isEmpty() && !newFolderPath.isEmpty()) { |
| 82 | // TODO: Add another platform theme hint for this behavior too, as e.g. macOS |
| 83 | // doesn't do it this way. |
| 84 | // If the user went up a directory (or several), we should set |
| 85 | // selectedFile to be the directory that we were in (or |
| 86 | // its closest ancestor that is a child of the new directory). |
| 87 | // E.g. if oldFolderPath is /foo/bar/baz/abc/xyz, and newFolderPath is /foo/bar, |
| 88 | // then we want to set selectedFile to be /foo/bar/baz. |
| 89 | const int indexOfFolder = oldFolderPath.indexOf(s: newFolderPath); |
| 90 | if (indexOfFolder != -1) { |
| 91 | // [folder] |
| 92 | // [ oldFolderPath ] |
| 93 | // /foo/bar/baz/abc/xyz |
| 94 | // [rel...Paths] |
| 95 | QStringList relativePaths = oldFolderPath.mid(position: indexOfFolder + newFolderPath.size()).split(sep: QLatin1Char('/'), behavior: Qt::SkipEmptyParts); |
| 96 | newSelectedFilePath = newFolderPath + QLatin1Char('/') + relativePaths.first(); |
| 97 | |
| 98 | // Now find the index of that directory so that we can set the ListView's currentIndex to it. |
| 99 | const QDir newFolderDir(newFolderPath); |
| 100 | // Just to be safe... |
| 101 | if (!newFolderDir.exists()) { |
| 102 | qmlWarning(me: q) << "Directory" << newSelectedFilePath << "doesn't exist; can't get a file entry list for it" ; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | const QFileInfoList filesInNewDir = fileList(dir: newFolderDir); |
| 107 | const QFileInfo newSelectedFileInfo(newSelectedFilePath); |
| 108 | newSelectedFileIndex = filesInNewDir.indexOf(t: newSelectedFileInfo); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static const bool preselectFirstFile = []() { |
| 113 | const QVariant envVar = qEnvironmentVariable(varName: "QT_QUICK_DIALOGS_PRESELECT_FIRST_FILE" ); |
| 114 | if (envVar.isValid() && envVar.canConvert<bool>()) |
| 115 | return envVar.toBool(); |
| 116 | return QGuiApplicationPrivate::platformTheme()->themeHint( |
| 117 | hint: QPlatformTheme::PreselectFirstFileInDirectory).toBool(); |
| 118 | }(); |
| 119 | |
| 120 | if (preselectFirstFile && newSelectedFilePath.isEmpty()) { |
| 121 | // When entering into a directory that isn't a parent of the old one, the first |
| 122 | // file delegate should be selected. |
| 123 | // TODO: is there a cheaper way to do this? QDirIterator doesn't support sorting, |
| 124 | // so we can't use that. QQuickFolderListModel uses threads to fetch its data, |
| 125 | // so should be considered asynchronous. We might be able to use it, but it would |
| 126 | // complicate the code even more... |
| 127 | const QDir newFolderDir(newFolderPath); |
| 128 | if (newFolderDir.exists()) { |
| 129 | if (!cachedFileList.isEmpty()) { |
| 130 | newSelectedFilePath = cachedFileList.first().absoluteFilePath(); |
| 131 | newSelectedFileIndex = 0; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | const QUrl newSelectedFileUrl = QUrl::fromLocalFile(localfile: newSelectedFilePath); |
| 137 | qCDebug(lcUpdateSelectedFile).nospace() << "updateSelectedFile is setting selectedFile to " << newSelectedFileUrl |
| 138 | << ", newSelectedFileIndex is " << newSelectedFileIndex; |
| 139 | q->setSelectedFile(newSelectedFileUrl); |
| 140 | updateFileNameTextEdit(); |
| 141 | // If the index is -1, there are no files in the directory, and so fileDialogListView's |
| 142 | // currentIndex will already be -1. |
| 143 | if (newSelectedFileIndex != -1) |
| 144 | tryUpdateFileDialogListViewCurrentIndex(newCurrentIndex: newSelectedFileIndex); |
| 145 | } |
| 146 | |
| 147 | void QQuickFileDialogImplPrivate::updateFileNameTextEdit() |
| 148 | { |
| 149 | QQuickFileDialogImplAttached *attached = attachedOrWarn(); |
| 150 | if (Q_UNLIKELY(!attached)) |
| 151 | return; |
| 152 | |
| 153 | const QFileInfo fileInfo(selectedFile.toLocalFile()); |
| 154 | if (fileInfo.isFile()) |
| 155 | attached->fileNameTextField()->setText(fileInfo.fileName()); |
| 156 | } |
| 157 | |
| 158 | QDir::SortFlags QQuickFileDialogImplPrivate::fileListSortFlags() |
| 159 | { |
| 160 | QDir::SortFlags sortFlags = QDir::IgnoreCase; |
| 161 | if (QQuickPlatformTheme::getThemeHint(themeHint: QPlatformTheme::ShowDirectoriesFirst).toBool()) |
| 162 | sortFlags.setFlag(flag: QDir::DirsFirst); |
| 163 | return sortFlags; |
| 164 | } |
| 165 | |
| 166 | QFileInfoList QQuickFileDialogImplPrivate::fileList(const QDir &dir) |
| 167 | { |
| 168 | return dir.entryInfoList(filters: QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, sort: fileListSortFlags()); |
| 169 | } |
| 170 | |
| 171 | void QQuickFileDialogImplPrivate::setFileDialogListViewCurrentIndex(int newCurrentIndex) |
| 172 | { |
| 173 | qCDebug(lcSelectedFile) << "setting fileDialogListView's currentIndex to" << newCurrentIndex; |
| 174 | |
| 175 | // We block signals from ListView because we don't want fileDialogListViewCurrentIndexChanged |
| 176 | // to be called, as the file it gets from the delegate will not be up-to-date (but most |
| 177 | // importantly because we already just set the selected file). |
| 178 | QQuickFileDialogImplAttached *attached = attachedOrWarn(); |
| 179 | const QSignalBlocker blocker(attached->fileDialogListView()); |
| 180 | attached->fileDialogListView()->setCurrentIndex(newCurrentIndex); |
| 181 | attached->fileDialogListView()->positionViewAtIndex(index: newCurrentIndex, mode: QQuickListView::Center); |
| 182 | if (QQuickItem *currentItem = attached->fileDialogListView()->currentItem()) |
| 183 | currentItem->forceActiveFocus(); |
| 184 | } |
| 185 | |
| 186 | /*! |
| 187 | \internal |
| 188 | |
| 189 | Tries to set the currentIndex of fileDialogListView to \a newCurrentIndex and gives |
| 190 | focus to the current item. |
| 191 | */ |
| 192 | void QQuickFileDialogImplPrivate::tryUpdateFileDialogListViewCurrentIndex(int newCurrentIndex) |
| 193 | { |
| 194 | qCDebug(lcSelectedFile) << "tryUpdateFileDialogListViewCurrentIndex called with newCurrentIndex" << newCurrentIndex; |
| 195 | QQuickFileDialogImplAttached *attached = attachedOrWarn(); |
| 196 | Q_ASSERT(attached); |
| 197 | Q_ASSERT(attached->fileDialogListView()); |
| 198 | |
| 199 | // We were likely trying to set an index for a file that the ListView hadn't loaded yet. |
| 200 | // We need to wait until the ListView has loaded all expected items, but since we have no |
| 201 | // efficient way of verifying that, we just check that the count is as expected. |
| 202 | if (newCurrentIndex != -1 && newCurrentIndex >= attached->fileDialogListView()->count()) { |
| 203 | qCDebug(lcSelectedFile) << "- trying to set currentIndex to" << newCurrentIndex |
| 204 | << "but fileDialogListView only has" << attached->fileDialogListView()->count() |
| 205 | << "items; setting pendingCurrentIndexToSet to" << newCurrentIndex; |
| 206 | pendingCurrentIndexToSet = newCurrentIndex; |
| 207 | QObjectPrivate::connect(sender: attached->fileDialogListView(), signal: &QQuickItemView::countChanged, |
| 208 | receiverPrivate: this, slot: &QQuickFileDialogImplPrivate::fileDialogListViewCountChanged, type: Qt::ConnectionType(Qt::DirectConnection | Qt::UniqueConnection)); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | setFileDialogListViewCurrentIndex(newCurrentIndex); |
| 213 | } |
| 214 | |
| 215 | void QQuickFileDialogImplPrivate::fileDialogListViewCountChanged() |
| 216 | { |
| 217 | QQuickFileDialogImplAttached *attached = attachedOrWarn(); |
| 218 | qCDebug(lcSelectedFile) << "fileDialogListView count changed to" << attached->fileDialogListView()->count(); |
| 219 | |
| 220 | if (pendingCurrentIndexToSet != -1 && pendingCurrentIndexToSet < attached->fileDialogListView()->count()) { |
| 221 | // The view now has all of the items we expect it to, so we can set |
| 222 | // its currentIndex back to the selected file. |
| 223 | qCDebug(lcSelectedFile) << "- ListView has expected count;" |
| 224 | << "applying pending fileDialogListView currentIndex" << pendingCurrentIndexToSet; |
| 225 | |
| 226 | QObjectPrivate::disconnect(sender: attached->fileDialogListView(), signal: &QQuickItemView::countChanged, |
| 227 | receiverPrivate: this, slot: &QQuickFileDialogImplPrivate::fileDialogListViewCountChanged); |
| 228 | setFileDialogListViewCurrentIndex(pendingCurrentIndexToSet); |
| 229 | pendingCurrentIndexToSet = -1; |
| 230 | qCDebug(lcSelectedFile) << "- reset pendingCurrentIndexToSet to -1" ; |
| 231 | } else { |
| 232 | qCDebug(lcSelectedFile) << "- ListView doesn't yet have expected count of" << cachedFileList.size(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void QQuickFileDialogImplPrivate::handleAccept() |
| 237 | { |
| 238 | // Let handleClick take care of calling accept(). |
| 239 | } |
| 240 | |
| 241 | void QQuickFileDialogImplPrivate::handleClick(QQuickAbstractButton *button) |
| 242 | { |
| 243 | Q_Q(QQuickFileDialogImpl); |
| 244 | if (buttonRole(button) == QPlatformDialogHelper::AcceptRole && selectedFile.isValid()) { |
| 245 | // The "Open" button was clicked, so we need to set the file to the current file, if any. |
| 246 | const QFileInfo fileInfo(selectedFile.toLocalFile()); |
| 247 | if (fileInfo.isDir()) { |
| 248 | // If it's a directory, navigate to it. |
| 249 | q->setCurrentFolder(currentFolder: selectedFile); |
| 250 | // Don't call accept(), because selecting a folder != accepting the dialog. |
| 251 | } else { |
| 252 | // Otherwise it's a file, so select it and close the dialog. |
| 253 | |
| 254 | lastButtonClicked = button; |
| 255 | |
| 256 | // Unless it already exists... |
| 257 | const bool dontConfirmOverride = q->options()->testOption(option: QFileDialogOptions::DontConfirmOverwrite); |
| 258 | const bool isSaveMode = q->options()->fileMode() == QFileDialogOptions::AnyFile; |
| 259 | if (QQuickFileDialogImplAttached *attached = attachedOrWarn(); |
| 260 | attached && fileInfo.exists() && isSaveMode && !dontConfirmOverride) { |
| 261 | QQuickDialog *confirmationDialog = attached->overwriteConfirmationDialog(); |
| 262 | confirmationDialog->open(); |
| 263 | static_cast<QQuickDialogButtonBox *>(confirmationDialog->footer())->standardButton(button: QPlatformDialogHelper::Yes) |
| 264 | ->forceActiveFocus(reason: Qt::PopupFocusReason); |
| 265 | } else { |
| 266 | selectFile(); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void QQuickFileDialogImplPrivate::selectFile() |
| 273 | { |
| 274 | Q_Q(QQuickFileDialogImpl); |
| 275 | Q_ASSERT(lastButtonClicked); |
| 276 | q->setSelectedFile(selectedFile); |
| 277 | q->accept(); |
| 278 | QQuickDialogPrivate::handleClick(button: lastButtonClicked); |
| 279 | emit q->fileSelected(fileUrl: selectedFile); |
| 280 | } |
| 281 | |
| 282 | QQuickFileDialogImpl::QQuickFileDialogImpl(QObject *parent) |
| 283 | : QQuickDialog(*(new QQuickFileDialogImplPrivate), parent) |
| 284 | { |
| 285 | setPopupType(QQuickPopup::Window); |
| 286 | } |
| 287 | |
| 288 | QQuickFileDialogImplAttached *QQuickFileDialogImpl::qmlAttachedProperties(QObject *object) |
| 289 | { |
| 290 | return new QQuickFileDialogImplAttached(object); |
| 291 | } |
| 292 | |
| 293 | QUrl QQuickFileDialogImpl::currentFolder() const |
| 294 | { |
| 295 | Q_D(const QQuickFileDialogImpl); |
| 296 | return d->currentFolder; |
| 297 | } |
| 298 | |
| 299 | void QQuickFileDialogImpl::setCurrentFolder(const QUrl ¤tFolder, SetReason setReason) |
| 300 | { |
| 301 | Q_D(QQuickFileDialogImpl); |
| 302 | qCDebug(lcCurrentFolder).nospace() << "setCurrentFolder called with " << currentFolder |
| 303 | << " (old currentFolder is " << d->currentFolder << ")" ; |
| 304 | |
| 305 | // As we would otherwise get the file list from scratch in a couple of places, |
| 306 | // just get it once and cache it. |
| 307 | // We need to cache it before the equality check, otherwise opening the dialog |
| 308 | // several times in the same directory wouldn't update the cache. |
| 309 | if (!currentFolder.isEmpty()) |
| 310 | d->cachedFileList = d->fileList(dir: QQmlFile::urlToLocalFileOrQrc(currentFolder)); |
| 311 | else |
| 312 | d->cachedFileList.clear(); |
| 313 | qCDebug(lcCurrentFolder) << "- cachedFileList size is now " << d->cachedFileList.size(); |
| 314 | |
| 315 | if (currentFolder == d->currentFolder) |
| 316 | return; |
| 317 | |
| 318 | const QString oldFolderPath = QQmlFile::urlToLocalFileOrQrc(d->currentFolder); |
| 319 | |
| 320 | d->currentFolder = currentFolder; |
| 321 | // Don't update the selectedFile if it's an Internal set, as that |
| 322 | // means that the user just set selectedFile, and we're being called as a result of that. |
| 323 | if (setReason == SetReason::External) { |
| 324 | // Since the directory changed, the old file can no longer be selected. |
| 325 | d->updateSelectedFile(oldFolderPath); |
| 326 | } |
| 327 | emit currentFolderChanged(folderUrl: d->currentFolder); |
| 328 | } |
| 329 | |
| 330 | QUrl QQuickFileDialogImpl::selectedFile() const |
| 331 | { |
| 332 | Q_D(const QQuickFileDialogImpl); |
| 333 | return d->selectedFile; |
| 334 | } |
| 335 | |
| 336 | /*! |
| 337 | \internal |
| 338 | |
| 339 | This is mostly called as a result of user interaction, but is also |
| 340 | called (indirectly) by QQuickFileDialog::onShow when the user set an initial |
| 341 | selectedFile. |
| 342 | */ |
| 343 | void QQuickFileDialogImpl::setSelectedFile(const QUrl &selectedFile) |
| 344 | { |
| 345 | qCDebug(lcSelectedFile) << "setSelectedFile called with" << selectedFile; |
| 346 | Q_D(QQuickFileDialogImpl); |
| 347 | if (selectedFile == d->selectedFile) |
| 348 | return; |
| 349 | |
| 350 | d->selectedFile = selectedFile; |
| 351 | d->updateEnabled(); |
| 352 | emit selectedFileChanged(selectedFileUrl: d->selectedFile); |
| 353 | } |
| 354 | |
| 355 | /*! |
| 356 | \internal |
| 357 | |
| 358 | Called when showing the FileDialog each time, so long as |
| 359 | QFileDialogOptions::initiallySelectedFiles is not empty. |
| 360 | */ |
| 361 | void QQuickFileDialogImpl::setInitialCurrentFolderAndSelectedFile(const QUrl &file) |
| 362 | { |
| 363 | Q_D(QQuickFileDialogImpl); |
| 364 | const QUrl fileDirUrl = QUrl::fromLocalFile(localfile: QFileInfo(file.toLocalFile()).dir().absolutePath()); |
| 365 | const bool currentFolderChanged = d->currentFolder != fileDirUrl; |
| 366 | qCDebug(lcSelectedFile) << "setting initial currentFolder to" << fileDirUrl << "and selectedFile to" << file; |
| 367 | setCurrentFolder(currentFolder: fileDirUrl, setReason: QQuickFileDialogImpl::SetReason::Internal); |
| 368 | setSelectedFile(file); |
| 369 | d->updateFileNameTextEdit(); |
| 370 | d->setCurrentIndexToInitiallySelectedFile = true; |
| 371 | |
| 372 | // If the currentFolder didn't change, the FolderListModel won't change and |
| 373 | // neither will the ListView. This means that setFileDialogListViewCurrentIndex |
| 374 | // will never get called and the currentIndex will not reflect selectedFile. |
| 375 | // We need to account for that here. |
| 376 | if (!currentFolderChanged) { |
| 377 | const QFileInfo newSelectedFileInfo(d->selectedFile.toLocalFile()); |
| 378 | const int indexOfSelectedFileInFileDialogListView = d->cachedFileList.indexOf(t: newSelectedFileInfo); |
| 379 | d->tryUpdateFileDialogListViewCurrentIndex(newCurrentIndex: indexOfSelectedFileInFileDialogListView); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | QSharedPointer<QFileDialogOptions> QQuickFileDialogImpl::options() const |
| 384 | { |
| 385 | Q_D(const QQuickFileDialogImpl); |
| 386 | return d->options; |
| 387 | } |
| 388 | |
| 389 | void QQuickFileDialogImpl::setOptions(const QSharedPointer<QFileDialogOptions> &options) |
| 390 | { |
| 391 | qCDebug(lcOptions).nospace() << "setOptions called with:" |
| 392 | << " acceptMode=" << options->acceptMode() |
| 393 | << " fileMode=" << options->fileMode() |
| 394 | << " initialDirectory=" << options->initialDirectory() |
| 395 | << " nameFilters=" << options->nameFilters() |
| 396 | << " initiallySelectedNameFilter=" << options->initiallySelectedNameFilter(); |
| 397 | |
| 398 | Q_D(QQuickFileDialogImpl); |
| 399 | d->options = options; |
| 400 | |
| 401 | if (d->options) { |
| 402 | d->selectedNameFilter->setOptions(options); |
| 403 | d->setNameFilters(options->nameFilters()); |
| 404 | |
| 405 | if (auto attached = d->attachedOrWarn()) { |
| 406 | const bool isSaveMode = d->options->fileMode() == QFileDialogOptions::AnyFile; |
| 407 | attached->fileNameLabel()->setVisible(isSaveMode); |
| 408 | attached->fileNameTextField()->setVisible(isSaveMode); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /*! |
| 414 | \internal |
| 415 | |
| 416 | The list of user-facing strings describing the available file filters. |
| 417 | */ |
| 418 | QStringList QQuickFileDialogImpl::nameFilters() const |
| 419 | { |
| 420 | Q_D(const QQuickFileDialogImpl); |
| 421 | return d->options ? d->options->nameFilters() : QStringList(); |
| 422 | } |
| 423 | |
| 424 | void QQuickFileDialogImpl::resetNameFilters() |
| 425 | { |
| 426 | Q_D(QQuickFileDialogImpl); |
| 427 | d->setNameFilters(QStringList()); |
| 428 | } |
| 429 | |
| 430 | QQuickFileNameFilter *QQuickFileDialogImpl::selectedNameFilter() const |
| 431 | { |
| 432 | Q_D(const QQuickFileDialogImpl); |
| 433 | if (!d->selectedNameFilter) { |
| 434 | QQuickFileDialogImpl *that = const_cast<QQuickFileDialogImpl *>(this); |
| 435 | d->selectedNameFilter = new QQuickFileNameFilter(that); |
| 436 | if (d->options) |
| 437 | d->selectedNameFilter->setOptions(d->options); |
| 438 | } |
| 439 | return d->selectedNameFilter; |
| 440 | } |
| 441 | |
| 442 | /*! |
| 443 | \internal |
| 444 | |
| 445 | These allow QQuickPlatformFileDialog::show() to set custom labels on the |
| 446 | dialog buttons without having to know about/go through QQuickFileDialogImplAttached |
| 447 | and QQuickDialogButtonBox. |
| 448 | */ |
| 449 | void QQuickFileDialogImpl::setAcceptLabel(const QString &label) |
| 450 | { |
| 451 | Q_D(QQuickFileDialogImpl); |
| 452 | d->acceptLabel = label; |
| 453 | QQuickFileDialogImplAttached *attached = d->attachedOrWarn(); |
| 454 | if (!attached) |
| 455 | return; |
| 456 | |
| 457 | auto acceptButton = attached->buttonBox()->standardButton(button: QPlatformDialogHelper::Open); |
| 458 | if (!acceptButton) { |
| 459 | qmlWarning(me: this).nospace() << "Can't set accept label to " << label |
| 460 | << "; failed to find Open button in DialogButtonBox of " << this; |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | auto buttonType = (d->options && d->options->acceptMode() == QFileDialogOptions::AcceptSave) |
| 465 | ? QPlatformDialogHelper::Save |
| 466 | : QPlatformDialogHelper::Open; |
| 467 | acceptButton->setText(!label.isEmpty() |
| 468 | ? label : QQuickDialogButtonBoxPrivate::buttonText(standardButton: buttonType)); |
| 469 | } |
| 470 | |
| 471 | void QQuickFileDialogImpl::setRejectLabel(const QString &label) |
| 472 | { |
| 473 | Q_D(QQuickFileDialogImpl); |
| 474 | d->rejectLabel = label; |
| 475 | QQuickFileDialogImplAttached *attached = d->attachedOrWarn(); |
| 476 | if (!attached) |
| 477 | return; |
| 478 | |
| 479 | auto rejectButton = attached->buttonBox()->standardButton(button: QPlatformDialogHelper::Cancel); |
| 480 | if (!rejectButton) { |
| 481 | qmlWarning(me: this).nospace() << "Can't set reject label to " << label |
| 482 | << "; failed to find Open button in DialogButtonBox of " << this; |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | rejectButton->setText(!label.isEmpty() |
| 487 | ? label : QQuickDialogButtonBoxPrivate::buttonText(standardButton: QPlatformDialogHelper::Cancel)); |
| 488 | } |
| 489 | |
| 490 | void QQuickFileDialogImpl::selectNameFilter(const QString &filter) |
| 491 | { |
| 492 | qCDebug(lcNameFilters) << "selectNameFilter called with" << filter; |
| 493 | Q_D(QQuickFileDialogImpl); |
| 494 | d->selectedNameFilter->update(filter); |
| 495 | emit filterSelected(filter); |
| 496 | } |
| 497 | |
| 498 | QString QQuickFileDialogImpl::fileName() const |
| 499 | { |
| 500 | return selectedFile().fileName(); |
| 501 | } |
| 502 | void QQuickFileDialogImpl::setFileName(const QString &fileName) |
| 503 | { |
| 504 | const QString previous = selectedFile().fileName(); |
| 505 | if (previous == fileName) |
| 506 | return; |
| 507 | |
| 508 | QUrl newSelectedFile; |
| 509 | newSelectedFile.setScheme(currentFolder().scheme()); |
| 510 | newSelectedFile.setPath(path: currentFolder().path() + u'/' + fileName); |
| 511 | setSelectedFile(newSelectedFile); |
| 512 | } |
| 513 | |
| 514 | QString QQuickFileDialogImpl::currentFolderName() const |
| 515 | { |
| 516 | return QDir(currentFolder().toLocalFile()).dirName(); |
| 517 | } |
| 518 | |
| 519 | void QQuickFileDialogImpl::componentComplete() |
| 520 | { |
| 521 | Q_D(QQuickFileDialogImpl); |
| 522 | QQuickDialog::componentComplete(); |
| 523 | |
| 524 | // Find the right-most button and set its key navigation so that |
| 525 | // tab moves focus to the breadcrumb bar's up button. I tried |
| 526 | // doing this via KeyNavigation on the DialogButtonBox in QML, |
| 527 | // but it didn't work (probably because it's not the right item). |
| 528 | QQuickFileDialogImplAttached *attached = d->attachedOrWarn(); |
| 529 | if (!attached) |
| 530 | return; |
| 531 | |
| 532 | const int buttonCount = attached->buttonBox()->count(); |
| 533 | if (buttonCount == 0) |
| 534 | return; |
| 535 | |
| 536 | QQuickAbstractButton *rightMostButton = qobject_cast<QQuickAbstractButton *>( |
| 537 | object: attached->buttonBox()->itemAt(index: buttonCount - 1)); |
| 538 | if (!rightMostButton) { |
| 539 | qmlWarning(me: this) << "Can't find right-most button in DialogButtonBox" ; |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | auto keyNavigationAttached = QQuickKeyNavigationAttached::qmlAttachedProperties(rightMostButton); |
| 544 | if (!keyNavigationAttached) { |
| 545 | qmlWarning(me: this) << "Can't create attached KeyNavigation object on" << QDebug::toString(object: rightMostButton); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | keyNavigationAttached->setTab(attached->breadcrumbBar()->upButton()); |
| 550 | } |
| 551 | |
| 552 | void QQuickFileDialogImpl::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) |
| 553 | { |
| 554 | Q_D(QQuickFileDialogImpl); |
| 555 | QQuickDialog::itemChange(change, data); |
| 556 | |
| 557 | if (change != QQuickItem::ItemVisibleHasChanged || !isComponentComplete() || !data.boolValue) |
| 558 | return; |
| 559 | |
| 560 | QQuickFileDialogImplAttached *attached = d->attachedOrWarn(); |
| 561 | if (!attached) |
| 562 | return; |
| 563 | |
| 564 | attached->fileDialogListView()->forceActiveFocus(); |
| 565 | d->updateEnabled(); |
| 566 | } |
| 567 | |
| 568 | QQuickFileDialogImplAttached *QQuickFileDialogImplPrivate::attachedOrWarn() |
| 569 | { |
| 570 | Q_Q(QQuickFileDialogImpl); |
| 571 | QQuickFileDialogImplAttached *attached = static_cast<QQuickFileDialogImplAttached*>( |
| 572 | qmlAttachedPropertiesObject<QQuickFileDialogImpl>(obj: q, create: false)); |
| 573 | if (!attached) |
| 574 | qmlWarning(me: q) << "Expected FileDialogImpl attached object to be present on" << this; |
| 575 | return attached; |
| 576 | } |
| 577 | |
| 578 | void QQuickFileDialogImplAttachedPrivate::nameFiltersComboBoxItemActivated(int index) |
| 579 | { |
| 580 | qCDebug(lcAttachedNameFilters) << "nameFiltersComboBoxItemActivated called with" << index; |
| 581 | auto fileDialogImpl = qobject_cast<QQuickFileDialogImpl*>(object: parent); |
| 582 | if (!fileDialogImpl) |
| 583 | return; |
| 584 | |
| 585 | fileDialogImpl->selectNameFilter(filter: nameFiltersComboBox->textAt(index)); |
| 586 | } |
| 587 | |
| 588 | void QQuickFileDialogImplAttachedPrivate::fileDialogListViewCurrentIndexChanged() |
| 589 | { |
| 590 | auto fileDialogImpl = qobject_cast<QQuickFileDialogImpl*>(object: parent); |
| 591 | if (!fileDialogImpl) |
| 592 | return; |
| 593 | |
| 594 | auto fileDialogDelegate = qobject_cast<QQuickFileDialogDelegate*>(object: fileDialogListView->currentItem()); |
| 595 | if (!fileDialogDelegate) |
| 596 | return; |
| 597 | |
| 598 | const QQuickItemViewPrivate::MovementReason moveReason = QQuickItemViewPrivate::get(o: fileDialogListView)->moveReason; |
| 599 | qCDebug(lcAttachedCurrentIndex).nospace() << "fileDialogListView currentIndex changed to " << fileDialogListView->currentIndex() |
| 600 | << " with moveReason " << moveReason |
| 601 | << "; the file at that index is " << fileDialogDelegate->file(); |
| 602 | |
| 603 | // Only update selectedFile if the currentIndex changed as a result of user interaction; |
| 604 | // things like model changes (i.e. QQuickItemViewPrivate::applyModelChanges() calling |
| 605 | // QQuickItemViewPrivate::updateCurrent as a result of us changing the directory on the FolderListModel) |
| 606 | // shouldn't cause the selectedFile to change. |
| 607 | auto fileDialogImplPrivate = QQuickFileDialogImplPrivate::get(dialog: fileDialogImpl); |
| 608 | if (moveReason != QQuickItemViewPrivate::Other) { |
| 609 | fileDialogImpl->setSelectedFile(fileDialogDelegate->file()); |
| 610 | fileDialogImplPrivate->updateFileNameTextEdit(); |
| 611 | } else if (fileDialogImplPrivate->setCurrentIndexToInitiallySelectedFile) { |
| 612 | // When setting selectedFile before opening the FileDialog, |
| 613 | // we need to ensure that the currentIndex is correct, because the initial change |
| 614 | // in directory will cause the underyling FolderListModel to change its folder property, |
| 615 | // which in turn resets the fileDialogListView's currentIndex to 0. |
| 616 | const QFileInfo newSelectedFileInfo(fileDialogImplPrivate->selectedFile.toLocalFile()); |
| 617 | const int indexOfSelectedFileInFileDialogListView = fileDialogImplPrivate->cachedFileList.indexOf(t: newSelectedFileInfo); |
| 618 | fileDialogImplPrivate->tryUpdateFileDialogListViewCurrentIndex(newCurrentIndex: indexOfSelectedFileInFileDialogListView); |
| 619 | fileDialogImplPrivate->setCurrentIndexToInitiallySelectedFile = false; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | void QQuickFileDialogImplAttachedPrivate::fileNameEditedByUser() |
| 624 | { |
| 625 | if (!buttonBox) |
| 626 | return; |
| 627 | auto openButton = buttonBox->standardButton(button: QPlatformDialogHelper::Open); |
| 628 | if (!openButton || !fileNameTextField) |
| 629 | return; |
| 630 | openButton->setEnabled(!fileNameTextField->text().isEmpty()); |
| 631 | } |
| 632 | |
| 633 | void QQuickFileDialogImplAttachedPrivate::fileNameEditingByUserFinished() |
| 634 | { |
| 635 | auto fileDialogImpl = qobject_cast<QQuickFileDialogImpl *>(object: parent); |
| 636 | if (!fileDialogImpl) |
| 637 | return; |
| 638 | |
| 639 | fileDialogImpl->setFileName(fileNameTextField->text()); |
| 640 | } |
| 641 | |
| 642 | QQuickFileDialogImplAttached::QQuickFileDialogImplAttached(QObject *parent) |
| 643 | : QObject(*(new QQuickFileDialogImplAttachedPrivate), parent) |
| 644 | { |
| 645 | if (!qobject_cast<QQuickFileDialogImpl*>(object: parent)) { |
| 646 | qmlWarning(me: this) << "FileDialogImpl attached properties should only be " |
| 647 | << "accessed through the root FileDialogImpl instance" ; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | QQuickDialogButtonBox *QQuickFileDialogImplAttached::buttonBox() const |
| 652 | { |
| 653 | Q_D(const QQuickFileDialogImplAttached); |
| 654 | return d->buttonBox; |
| 655 | } |
| 656 | |
| 657 | void QQuickFileDialogImplAttached::setButtonBox(QQuickDialogButtonBox *buttonBox) |
| 658 | { |
| 659 | Q_D(QQuickFileDialogImplAttached); |
| 660 | if (buttonBox == d->buttonBox) |
| 661 | return; |
| 662 | |
| 663 | if (d->buttonBox) { |
| 664 | QQuickFileDialogImpl *fileDialogImpl = qobject_cast<QQuickFileDialogImpl*>(object: parent()); |
| 665 | if (fileDialogImpl) { |
| 666 | auto dialogPrivate = QQuickDialogPrivate::get(dialog: fileDialogImpl); |
| 667 | QObjectPrivate::disconnect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::accepted, |
| 668 | receiverPrivate: dialogPrivate, slot: &QQuickDialogPrivate::handleAccept); |
| 669 | QObjectPrivate::disconnect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::rejected, |
| 670 | receiverPrivate: dialogPrivate, slot: &QQuickDialogPrivate::handleReject); |
| 671 | QObjectPrivate::disconnect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::clicked, |
| 672 | receiverPrivate: dialogPrivate, slot: &QQuickDialogPrivate::handleClick); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | d->buttonBox = buttonBox; |
| 677 | |
| 678 | if (buttonBox) { |
| 679 | QQuickFileDialogImpl *fileDialogImpl = qobject_cast<QQuickFileDialogImpl*>(object: parent()); |
| 680 | if (fileDialogImpl) { |
| 681 | auto dialogPrivate = QQuickDialogPrivate::get(dialog: fileDialogImpl); |
| 682 | QObjectPrivate::connect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::accepted, |
| 683 | receiverPrivate: dialogPrivate, slot: &QQuickDialogPrivate::handleAccept); |
| 684 | QObjectPrivate::connect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::rejected, |
| 685 | receiverPrivate: dialogPrivate, slot: &QQuickDialogPrivate::handleReject); |
| 686 | QObjectPrivate::connect(sender: d->buttonBox, signal: &QQuickDialogButtonBox::clicked, |
| 687 | receiverPrivate: dialogPrivate, slot: &QQuickDialogPrivate::handleClick); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | emit buttonBoxChanged(); |
| 692 | } |
| 693 | |
| 694 | QQuickComboBox *QQuickFileDialogImplAttached::nameFiltersComboBox() const |
| 695 | { |
| 696 | Q_D(const QQuickFileDialogImplAttached); |
| 697 | return d->nameFiltersComboBox; |
| 698 | } |
| 699 | |
| 700 | void QQuickFileDialogImplAttached::setNameFiltersComboBox(QQuickComboBox *nameFiltersComboBox) |
| 701 | { |
| 702 | Q_D(QQuickFileDialogImplAttached); |
| 703 | if (nameFiltersComboBox == d->nameFiltersComboBox) |
| 704 | return; |
| 705 | |
| 706 | d->nameFiltersComboBox = nameFiltersComboBox; |
| 707 | |
| 708 | QObjectPrivate::connect(sender: d->nameFiltersComboBox, signal: &QQuickComboBox::activated, |
| 709 | receiverPrivate: d, slot: &QQuickFileDialogImplAttachedPrivate::nameFiltersComboBoxItemActivated); |
| 710 | |
| 711 | emit nameFiltersComboBoxChanged(); |
| 712 | } |
| 713 | |
| 714 | QString QQuickFileDialogImplAttached::selectedNameFilter() const |
| 715 | { |
| 716 | Q_D(const QQuickFileDialogImplAttached); |
| 717 | return d->nameFiltersComboBox ? d->nameFiltersComboBox->currentText() : QString(); |
| 718 | } |
| 719 | |
| 720 | void QQuickFileDialogImplAttached::selectNameFilter(const QString &filter) |
| 721 | { |
| 722 | Q_D(QQuickFileDialogImplAttached); |
| 723 | qCDebug(lcAttachedNameFilters) << "selectNameFilter called with" << filter; |
| 724 | if (!d->nameFiltersComboBox) |
| 725 | return; |
| 726 | |
| 727 | const int indexInComboBox = d->nameFiltersComboBox->find(text: filter); |
| 728 | if (indexInComboBox == -1) |
| 729 | return; |
| 730 | |
| 731 | qCDebug(lcAttachedNameFilters) << "setting ComboBox's currentIndex to" << indexInComboBox; |
| 732 | d->nameFiltersComboBox->setCurrentIndex(indexInComboBox); |
| 733 | } |
| 734 | |
| 735 | QQuickListView *QQuickFileDialogImplAttached::fileDialogListView() const |
| 736 | { |
| 737 | Q_D(const QQuickFileDialogImplAttached); |
| 738 | return d->fileDialogListView; |
| 739 | } |
| 740 | |
| 741 | void QQuickFileDialogImplAttached::setFileDialogListView(QQuickListView *fileDialogListView) |
| 742 | { |
| 743 | Q_D(QQuickFileDialogImplAttached); |
| 744 | if (fileDialogListView == d->fileDialogListView) |
| 745 | return; |
| 746 | |
| 747 | d->fileDialogListView = fileDialogListView; |
| 748 | |
| 749 | QObjectPrivate::connect(sender: d->fileDialogListView, signal: &QQuickListView::currentIndexChanged, |
| 750 | receiverPrivate: d, slot: &QQuickFileDialogImplAttachedPrivate::fileDialogListViewCurrentIndexChanged); |
| 751 | |
| 752 | emit fileDialogListViewChanged(); |
| 753 | } |
| 754 | |
| 755 | QQuickFolderBreadcrumbBar *QQuickFileDialogImplAttached::breadcrumbBar() const |
| 756 | { |
| 757 | Q_D(const QQuickFileDialogImplAttached); |
| 758 | return d->breadcrumbBar; |
| 759 | } |
| 760 | |
| 761 | void QQuickFileDialogImplAttached::setBreadcrumbBar(QQuickFolderBreadcrumbBar *breadcrumbBar) |
| 762 | { |
| 763 | Q_D(QQuickFileDialogImplAttached); |
| 764 | if (breadcrumbBar == d->breadcrumbBar) |
| 765 | return; |
| 766 | |
| 767 | d->breadcrumbBar = breadcrumbBar; |
| 768 | emit breadcrumbBarChanged(); |
| 769 | } |
| 770 | |
| 771 | QQuickLabel *QQuickFileDialogImplAttached::fileNameLabel() const |
| 772 | { |
| 773 | Q_D(const QQuickFileDialogImplAttached); |
| 774 | return d->fileNameLabel; |
| 775 | } |
| 776 | |
| 777 | void QQuickFileDialogImplAttached::setFileNameLabel(QQuickLabel *fileNameLabel) |
| 778 | { |
| 779 | Q_D(QQuickFileDialogImplAttached); |
| 780 | if (fileNameLabel == d->fileNameLabel) |
| 781 | return; |
| 782 | |
| 783 | d->fileNameLabel = fileNameLabel; |
| 784 | |
| 785 | emit fileNameLabelChanged(); |
| 786 | } |
| 787 | |
| 788 | QQuickTextField *QQuickFileDialogImplAttached::fileNameTextField() const |
| 789 | { |
| 790 | Q_D(const QQuickFileDialogImplAttached); |
| 791 | return d->fileNameTextField; |
| 792 | } |
| 793 | |
| 794 | void QQuickFileDialogImplAttached::setFileNameTextField(QQuickTextField *fileNameTextField) |
| 795 | { |
| 796 | Q_D(QQuickFileDialogImplAttached); |
| 797 | if (fileNameTextField == d->fileNameTextField) |
| 798 | return; |
| 799 | |
| 800 | if (d->fileNameTextField) { |
| 801 | QObjectPrivate::disconnect(sender: d->fileNameTextField, signal: &QQuickTextField::editingFinished, |
| 802 | receiverPrivate: d, slot: &QQuickFileDialogImplAttachedPrivate::fileNameEditingByUserFinished); |
| 803 | QObjectPrivate::disconnect(sender: d->fileNameTextField, signal: &QQuickTextField::textEdited, |
| 804 | receiverPrivate: d, slot: &QQuickFileDialogImplAttachedPrivate::fileNameEditedByUser); |
| 805 | } |
| 806 | |
| 807 | d->fileNameTextField = fileNameTextField; |
| 808 | |
| 809 | if (d->fileNameTextField) { |
| 810 | QObjectPrivate::connect(sender: d->fileNameTextField, signal: &QQuickTextField::editingFinished, |
| 811 | receiverPrivate: d, slot: &QQuickFileDialogImplAttachedPrivate::fileNameEditingByUserFinished); |
| 812 | QObjectPrivate::connect(sender: d->fileNameTextField, signal: &QQuickTextField::textEdited, |
| 813 | receiverPrivate: d, slot: &QQuickFileDialogImplAttachedPrivate::fileNameEditedByUser); |
| 814 | } |
| 815 | emit fileNameTextFieldChanged(); |
| 816 | } |
| 817 | |
| 818 | QQuickDialog *QQuickFileDialogImplAttached::overwriteConfirmationDialog() const |
| 819 | { |
| 820 | Q_D(const QQuickFileDialogImplAttached); |
| 821 | return d->overwriteConfirmationDialog; |
| 822 | } |
| 823 | |
| 824 | void QQuickFileDialogImplAttached::setOverwriteConfirmationDialog(QQuickDialog *dialog) |
| 825 | { |
| 826 | Q_D(QQuickFileDialogImplAttached); |
| 827 | if (dialog == d->overwriteConfirmationDialog) |
| 828 | return; |
| 829 | |
| 830 | QQuickFileDialogImpl *fileDialogImpl = qobject_cast<QQuickFileDialogImpl*>(object: parent()); |
| 831 | if (d->overwriteConfirmationDialog && fileDialogImpl) |
| 832 | QObjectPrivate::disconnect(sender: d->overwriteConfirmationDialog, signal: &QQuickDialog::accepted, |
| 833 | receiverPrivate: QQuickFileDialogImplPrivate::get(dialog: fileDialogImpl), slot: &QQuickFileDialogImplPrivate::selectFile); |
| 834 | |
| 835 | d->overwriteConfirmationDialog = dialog; |
| 836 | |
| 837 | if (d->overwriteConfirmationDialog && fileDialogImpl) |
| 838 | QObjectPrivate::connect(sender: d->overwriteConfirmationDialog, signal: &QQuickDialog::accepted, |
| 839 | receiverPrivate: QQuickFileDialogImplPrivate::get(dialog: fileDialogImpl), slot: &QQuickFileDialogImplPrivate::selectFile, type: Qt::QueuedConnection); |
| 840 | |
| 841 | emit overwriteConfirmationDialogChanged(); |
| 842 | } |
| 843 | |
| 844 | QT_END_NAMESPACE |
| 845 | |
| 846 | #include "moc_qquickfiledialogimpl_p.cpp" |
| 847 | |