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