Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickplatformfiledialog.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
5
6#include <QtCore/qloggingcategory.h>
7#include <QtGui/qwindow.h>
8#include <QtQml/qqmlcontext.h>
9#include <QtQml/qqmlinfo.h>
10#include <QtQuick/qquickwindow.h>
11#include <QtQuickDialogs2Utils/private/qquickfilenamefilter_p.h>
12#include <QtQuickTemplates2/private/qquickdialog_p.h>
13#include <QtQuickTemplates2/private/qquickpopup_p_p.h>
14#include <QtQuickTemplates2/private/qquickpopupanchors_p.h>
15
17
19
20Q_LOGGING_CATEGORY(lcQuickPlatformFileDialog, "qt.quick.dialogs.quickplatformfiledialog")
21
22
31{
32 qCDebug(lcQuickPlatformFileDialog) << "creating non-native Qt Quick FileDialog with parent" << parent;
33
34 // Set a parent so that we get deleted if we can't be shown for whatever reason.
35 // Our eventual parent should be the window, though.
36 setParent(parent);
37
38 auto qmlContext = ::qmlContext(parent);
39 if (!qmlContext) {
40 qmlWarning(parent) << "No QQmlContext for QQuickPlatformFileDialog; can't create non-native FileDialog implementation";
41 return;
42 }
43
44 const auto dialogQmlUrl = QUrl(QStringLiteral("qrc:/qt-project.org/imports/QtQuick/Dialogs/quickimpl/qml/FileDialog.qml"));
45 QQmlComponent fileDialogComponent(qmlContext->engine(), dialogQmlUrl, parent);
46 if (!fileDialogComponent.isReady()) {
47 qmlWarning(parent) << "Failed to load non-native FileDialog implementation:\n" << fileDialogComponent.errorString();
48 return;
49 }
50 m_dialog = qobject_cast<QQuickFileDialogImpl*>(fileDialogComponent.create());
51 if (!m_dialog) {
52 qmlWarning(parent) << "Failed to create an instance of the non-native FileDialog:\n" << fileDialogComponent.errorString();
53 return;
54 }
55 // Give it a parent until it's parented to the window in show().
56 m_dialog->setParent(this);
57
60
62 // TODO: add support for multiple file selection (QTBUG-92585)
63// connect(m_dialog, &QQuickFileDialogImpl::filesSelected, [this](const QList<QString> &files) {
64// QList<QUrl> urls;
65// urls.reserve(files.count());
66// for (const QString &file : files)
67// urls += QUrl::fromLocalFile(file);
68// emit filesSelected(urls);
69// });
73}
74
76{
77 return m_dialog;
78}
79
81{
82 return false;
83}
84
86{
87 if (!m_dialog)
88 return;
89
90 m_dialog->setCurrentFolder(directory);
91}
92
94{
95 if (!m_dialog)
96 return {};
97
98 return m_dialog->currentFolder();
99}
100
102{
103 if (!m_dialog)
104 return;
105
106 if (m_dialog->isVisible()) {
107 qWarning() << "Cannot set an initial selectedFile while FileDialog is open";
108 return;
109 }
110
111 // Since we're only called once each time the FileDialog is shown,
112 // we call setInitialCurrentFolderAndSelectedFile here, which will ensure that
113 // the first currentIndex change (to 0, as a result of the ListView's model changing
114 // as a result of the FolderListModel directory change) is effectively
115 // ignored and the correct index for the initial selectedFile is maintained.
117}
118
119// TODO: support for multiple selected files
121{
122 if (m_dialog->selectedFile().isEmpty())
123 return {};
124
125 return { m_dialog->selectedFile() };
126}
127
131
133{
134 // There is a bit of a problem with order here - QQuickFileDialog::onShow()
135 // is called before our show(), but it needs to set the selected filter
136 // (which we can't do in our show() because we don't know about QQuickFileDialog).
137 // So, delay setting the filter until we're shown. This shouldn't be an issue
138 // in practice, since it doesn't make sense for the filter to programmatically
139 // change while the dialog is visible.
140 m_pendingNameFilter = filter;
141}
142
147
149{
150 qCWarning(lcQuickPlatformFileDialog) << "exec() is not supported for the Qt Quick FileDialog fallback";
151}
152
159bool QQuickPlatformFileDialog::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
160{
161 qCDebug(lcQuickPlatformFileDialog) << "show called with flags" << flags <<
162 "modality" << modality << "parent" << parent;
163 if (!m_dialog)
164 return false;
165
166 if (!parent)
167 return false;
168
169 auto quickWindow = qobject_cast<QQuickWindow*>(parent);
170 if (!quickWindow) {
171 qmlInfo(this->parent()) << "Parent window (" << parent << ") of non-native dialog is not a QQuickWindow";
172 return false;
173 }
174 m_dialog->setParent(parent);
175 m_dialog->resetParentItem();
176
177 auto popupPrivate = QQuickPopupPrivate::get(m_dialog);
178 popupPrivate->getAnchors()->setCenterIn(m_dialog->parentItem());
179
180 QSharedPointer<QFileDialogOptions> options = QPlatformFileDialogHelper::options();
181 m_dialog->setTitle(options->windowTitle());
182 m_dialog->setOptions(options);
183 m_dialog->selectNameFilter(m_pendingNameFilter);
184 m_pendingNameFilter.clear();
189
191 if (m_dialog->currentFolder().isEmpty()) {
192 // The user didn't set an initial selectedFile nor currentFolder, so we'll set it to the working directory.
193 qCDebug(lcQuickPlatformFileDialog) << "- calling setCurrentFolder(QDir()) on quick dialog" << parent;
195 }
196 }
197
198 m_dialog->open();
199 return true;
200}
201
203{
204 if (!m_dialog)
205 return;
206
207 m_dialog->close();
208}
209
211{
212 return m_dialog;
213}
214
216
217#include "moc_qquickplatformfiledialog_p.cpp"
\inmodule QtCore
Definition qdir.h:20
QList< QUrl > initiallySelectedFiles() const
bool isLabelExplicitlySet(DialogLabel label)
QString labelText(DialogLabel label) const
bool isEmpty() const noexcept
Definition qlist.h:401
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
void setParent(QObject *parent)
Makes the object a child of parent.
Definition qobject.cpp:2195
void fileSelected(const QUrl &file)
void currentChanged(const QUrl &path)
void directoryEntered(const QUrl &directory)
void filterSelected(const QString &filter)
const QSharedPointer< QFileDialogOptions > & options() const
The QQmlComponent class encapsulates a QML component definition.
QQmlEngine * engine() const
Return the context's QQmlEngine, or \nullptr if the context has no QQmlEngine or the QQmlEngine was d...
void setTitle(const QString &title)
void accepted()
void rejected()
void setRejectLabel(const QString &label)
void setAcceptLabel(const QString &label)
void filterSelected(const QString &filter)
void fileSelected(const QUrl &fileUrl)
void setOptions(const QSharedPointer< QFileDialogOptions > &options)
void selectedFileChanged(const QUrl &selectedFileUrl)
void setCurrentFolder(const QUrl &currentFolder, SetReason setReason=SetReason::External)
void selectNameFilter(const QString &filter)
QQuickFileNameFilter * selectedNameFilter
void setInitialCurrentFolderAndSelectedFile(const QUrl &file)
void currentFolderChanged(const QUrl &folderUrl)
void selectFile(const QUrl &file) override
bool defaultNameFilterDisables() const override
bool show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) override
void setDirectory(const QUrl &directory) override
QString selectedNameFilter() const override
QQuickFileDialogImpl * dialog() const
void selectNameFilter(const QString &filter) override
QList< QUrl > selectedFiles() const override
static QQuickPopupPrivate * get(QQuickPopup *popup)
void close()
\qmlmethod void QtQuick.Controls::Popup::close()
void open()
\qmlmethod void QtQuick.Controls::Popup::open()
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
\inmodule QtCore
Definition qurl.h:94
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition qurl.cpp:3368
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition qurl.cpp:1896
\inmodule QtGui
Definition qwindow.h:63
Combined button and popup list for selecting options.
WindowModality
#define qWarning
Definition qlogging.h:166
#define Q_LOGGING_CATEGORY(name,...)
#define qCWarning(category,...)
#define qCDebug(category,...)
GLbitfield flags
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
Q_QML_EXPORT QQmlInfo qmlInfo(const QObject *me)
Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me)
static QString absolutePath(const QString &path)
#define QStringLiteral(str)
QFile file
[0]
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
file setParent(multiPart)