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 "qquickplatformfiledialog_p.h"
5
6#include <QtCore/qloggingcategory.h>
7#include <QtGui/qwindow.h>
8#include <QtQml/qqmlcontext.h>
9#include <QtQml/qqmlinfo.h>
10#include <QtQml/qqmlcomponent.h>
11#include <QtQuick/qquickwindow.h>
12#include <QtQuickDialogs2Utils/private/qquickfilenamefilter_p.h>
13#include <QtQuickTemplates2/private/qquickdialog_p.h>
14#include <QtQuickTemplates2/private/qquickdialog_p_p.h>
15#include <QtQuickTemplates2/private/qquickpopup_p_p.h>
16#include <QtQuickTemplates2/private/qquickpopupanchors_p.h>
17
18#include "qquickfiledialogimpl_p.h"
19
20QT_BEGIN_NAMESPACE
21
22Q_STATIC_LOGGING_CATEGORY(lcQuickPlatformFileDialog, "qt.quick.dialogs.quickplatformfiledialog")
23
24/*!
25 \class QQuickPlatformFileDialog
26 \internal
27
28 An interface that QQuickFileDialog can use to access the non-native Qt Quick FileDialog.
29
30 Both this and the native implementations are created in QQuickAbstractDialog::create().
31*/
32QQuickPlatformFileDialog::QQuickPlatformFileDialog(QObject *parent)
33{
34 qCDebug(lcQuickPlatformFileDialog) << "creating non-native Qt Quick FileDialog with parent" << parent;
35
36 // Set a parent so that we get deleted if we can't be shown for whatever reason.
37 // Our eventual parent should be the window, though.
38 setParent(parent);
39
40 auto qmlContext = ::qmlContext(parent);
41 if (!qmlContext) {
42 qmlWarning(me: parent) << "No QQmlContext for QQuickPlatformFileDialog; can't create non-native FileDialog implementation";
43 return;
44 }
45
46 const auto dialogQmlUrl = QUrl(QStringLiteral("qrc:/qt-project.org/imports/QtQuick/Dialogs/quickimpl/qml/FileDialog.qml"));
47 QQmlComponent fileDialogComponent(qmlContext->engine(), dialogQmlUrl, parent);
48 if (!fileDialogComponent.isReady()) {
49 qmlWarning(me: parent) << "Failed to load non-native FileDialog implementation:\n" << fileDialogComponent.errorString();
50 return;
51 }
52 m_dialog = qobject_cast<QQuickFileDialogImpl*>(object: fileDialogComponent.create());
53 if (!m_dialog) {
54 qmlWarning(me: parent) << "Failed to create an instance of the non-native FileDialog:\n" << fileDialogComponent.errorString();
55 return;
56 }
57 // Give it a parent until it's parented to the window in show().
58 m_dialog->setParent(this);
59
60 connect(sender: m_dialog, signal: &QQuickDialog::accepted, context: this, slot: &QPlatformDialogHelper::accept);
61 connect(sender: m_dialog, signal: &QQuickDialog::rejected, context: this, slot: &QPlatformDialogHelper::reject);
62
63 connect(sender: m_dialog, signal: &QQuickFileDialogImpl::fileSelected, context: this, slot: &QQuickPlatformFileDialog::fileSelected);
64 // TODO: add support for multiple file selection (QTBUG-92585)
65// connect(m_dialog, &QQuickFileDialogImpl::filesSelected, [this](const QList<QString> &files) {
66// QList<QUrl> urls;
67// urls.reserve(files.count());
68// for (const QString &file : files)
69// urls += QUrl::fromLocalFile(file);
70// emit filesSelected(urls);
71// });
72 connect(sender: m_dialog, signal: &QQuickFileDialogImpl::selectedFileChanged, context: this, slot: &QQuickPlatformFileDialog::currentChanged);
73 connect(sender: m_dialog, signal: &QQuickFileDialogImpl::currentFolderChanged, context: this, slot: &QQuickPlatformFileDialog::directoryEntered);
74 connect(sender: m_dialog, signal: &QQuickFileDialogImpl::filterSelected, context: this, slot: &QQuickPlatformFileDialog::filterSelected);
75}
76
77bool QQuickPlatformFileDialog::isValid() const
78{
79 return m_dialog;
80}
81
82bool QQuickPlatformFileDialog::defaultNameFilterDisables() const
83{
84 return false;
85}
86
87void QQuickPlatformFileDialog::setDirectory(const QUrl &directory)
88{
89 if (!m_dialog)
90 return;
91
92 m_dialog->setCurrentFolder(currentFolder: directory);
93}
94
95QUrl QQuickPlatformFileDialog::directory() const
96{
97 if (!m_dialog)
98 return {};
99
100 return m_dialog->currentFolder();
101}
102
103void QQuickPlatformFileDialog::selectFile(const QUrl &file)
104{
105 if (!m_dialog)
106 return;
107
108 if (m_dialog->isVisible()) {
109 qWarning() << "Cannot set an initial selectedFile while FileDialog is open";
110 return;
111 }
112
113 // Since we're only called once each time the FileDialog is shown,
114 // we call setInitialCurrentFolderAndSelectedFile here, which will ensure that
115 // the first currentIndex change (to 0, as a result of the ListView's model changing
116 // as a result of the FolderListModel directory change) is effectively
117 // ignored and the correct index for the initial selectedFile is maintained.
118 m_dialog->setInitialCurrentFolderAndSelectedFile(file);
119}
120
121// TODO: support for multiple selected files
122QList<QUrl> QQuickPlatformFileDialog::selectedFiles() const
123{
124 if (!m_dialog || m_dialog->selectedFile().isEmpty())
125 return {};
126
127 return { m_dialog->selectedFile() };
128}
129
130void QQuickPlatformFileDialog::setFilter()
131{
132}
133
134void QQuickPlatformFileDialog::selectNameFilter(const QString &filter)
135{
136 // There is a bit of a problem with order here - QQuickFileDialog::onShow()
137 // is called before our show(), but it needs to set the selected filter
138 // (which we can't do in our show() because we don't know about QQuickFileDialog).
139 // So, delay setting the filter until we're shown. This shouldn't be an issue
140 // in practice, since it doesn't make sense for the filter to programmatically
141 // change while the dialog is visible.
142 m_pendingNameFilter = filter;
143}
144
145QString QQuickPlatformFileDialog::selectedNameFilter() const
146{
147 return m_dialog->selectedNameFilter()->name();
148}
149
150void QQuickPlatformFileDialog::exec()
151{
152 qCWarning(lcQuickPlatformFileDialog) << "exec() is not supported for the Qt Quick FileDialog fallback";
153}
154
155/*!
156 \internal
157
158 This is called after QQuickFileDialog::onShow().
159 Both are called in QQuickAbstractDialog::open().
160*/
161bool QQuickPlatformFileDialog::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
162{
163 qCDebug(lcQuickPlatformFileDialog) << "show called with flags" << flags <<
164 "modality" << modality << "parent" << parent;
165 if (!m_dialog)
166 return false;
167
168 if (!parent)
169 return false;
170
171 auto quickWindow = qobject_cast<QQuickWindow*>(object: parent);
172 if (!quickWindow) {
173 qmlInfo(me: this->parent()) << "Parent window (" << parent << ") of non-native dialog is not a QQuickWindow";
174 return false;
175 }
176 m_dialog->setParent(parent);
177 m_dialog->resetParentItem();
178
179 auto popupPrivate = QQuickPopupPrivate::get(popup: m_dialog);
180 popupPrivate->getAnchors()->setCenterIn(m_dialog->parentItem());
181
182 QSharedPointer<QFileDialogOptions> options = QPlatformFileDialogHelper::options();
183 m_dialog->setTitle(options->windowTitle());
184 m_dialog->setOptions(options);
185 m_dialog->selectNameFilter(filter: m_pendingNameFilter);
186 m_pendingNameFilter.clear();
187 m_dialog->setAcceptLabel(options->isLabelExplicitlySet(label: QFileDialogOptions::Accept)
188 ? options->labelText(label: QFileDialogOptions::Accept) : QString());
189 m_dialog->setRejectLabel(options->isLabelExplicitlySet(label: QFileDialogOptions::Reject)
190 ? options->labelText(label: QFileDialogOptions::Reject) : QString());
191
192 if (options->initiallySelectedFiles().isEmpty()) {
193 if (m_dialog->currentFolder().isEmpty()) {
194 // The user didn't set an initial selectedFile nor currentFolder, so we'll set it to the working directory.
195 qCDebug(lcQuickPlatformFileDialog) << "- calling setCurrentFolder(QDir()) on quick dialog" << parent;
196 m_dialog->setCurrentFolder(currentFolder: QUrl::fromLocalFile(localfile: QDir().absolutePath()));
197 }
198 }
199 m_dialog->setWindowModality(modality);
200 m_dialog->open();
201 return true;
202}
203
204void QQuickPlatformFileDialog::hide()
205{
206 if (!m_dialog)
207 return;
208
209 m_dialog->close();
210}
211
212QQuickFileDialogImpl *QQuickPlatformFileDialog::dialog() const
213{
214 return m_dialog;
215}
216
217QT_END_NAMESPACE
218
219#include "moc_qquickplatformfiledialog_p.cpp"
220

source code of qtdeclarative/src/quickdialogs/quickdialogsquickimpl/qquickplatformfiledialog.cpp