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
qquickplatformfolderdialog.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 <QtQuickTemplates2/private/qquickdialog_p.h>
12#include <QtQuickTemplates2/private/qquickpopup_p_p.h>
13#include <QtQuickTemplates2/private/qquickpopupanchors_p.h>
14
16
18
19Q_LOGGING_CATEGORY(lcQuickPlatformFolderDialog, "qt.quick.dialogs.quickplatformfolderdialog")
20
21
30{
31 qCDebug(lcQuickPlatformFolderDialog) << "creating non-native Qt Quick FolderDialog with parent" << parent;
32
33 // Set a parent so that we get deleted if we can't be shown for whatever reason.
34 // Our eventual parent should be the window, though.
35 setParent(parent);
36
37 auto qmlContext = ::qmlContext(parent);
38 if (!qmlContext) {
39 qmlWarning(parent) << "No QQmlContext for QQuickPlatformFolderDialog; can't create non-native FolderDialog implementation";
40 return;
41 }
42
43 const auto dialogQmlUrl = QUrl(QStringLiteral("qrc:/qt-project.org/imports/QtQuick/Dialogs/quickimpl/qml/FolderDialog.qml"));
44 QQmlComponent folderDialogComponent(qmlContext->engine(), dialogQmlUrl, parent);
45 if (!folderDialogComponent.isReady()) {
46 qmlWarning(parent) << "Failed to load non-native FolderDialog implementation:\n" << folderDialogComponent.errorString();
47 return;
48 }
49 m_dialog = qobject_cast<QQuickFolderDialogImpl*>(folderDialogComponent.create());
50 if (!m_dialog) {
51 qmlWarning(parent) << "Failed to create an instance of the non-native FolderDialog:\n" << folderDialogComponent.errorString();
52 return;
53 }
54 // Give it a parent until it's parented to the window in show().
55 m_dialog->setParent(this);
56
59
63
64 // We would do this in QQuickFolderDialogImpl, but we need to ensure that folderChanged()
65 // is connected to directoryEntered() before setting it to ensure that the QQuickFolderDialog is notified.
66 if (m_dialog->currentFolder().isEmpty())
67 m_dialog->setCurrentFolder(QUrl::fromLocalFile(QDir().absolutePath()));
68}
69
71{
72 return m_dialog;
73}
74
76{
77 return false;
78}
79
81{
82 if (!m_dialog)
83 return;
84
85 m_dialog->setCurrentFolder(directory);
86}
87
89{
90 if (!m_dialog)
91 return {};
92
93 return m_dialog->currentFolder();
94}
95
97{
98 if (!m_dialog)
99 return;
100
101 m_dialog->setSelectedFolder(file);
102}
103
105{
106 // FolderDialog doesn't support multiple selected folders.
107 return { m_dialog->selectedFolder() };
108}
109
113
117
122
124{
125 qCWarning(lcQuickPlatformFolderDialog) << "exec() is not supported for the Qt Quick FolderDialog fallback";
126}
127
128bool QQuickPlatformFolderDialog::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
129{
130 qCDebug(lcQuickPlatformFolderDialog) << "show called with flags" << flags <<
131 "modality" << modality << "parent" << parent;
132 if (!m_dialog)
133 return false;
134
135 if (!parent)
136 return false;
137
138 auto quickWindow = qobject_cast<QQuickWindow*>(parent);
139 if (!quickWindow) {
140 qmlInfo(this->parent()) << "Parent window (" << parent << ") of non-native dialog is not a QQuickWindow";
141 return false;
142 }
143 m_dialog->setParent(parent);
144 m_dialog->resetParentItem();
145
146 auto popupPrivate = QQuickPopupPrivate::get(m_dialog);
147 popupPrivate->getAnchors()->setCenterIn(m_dialog->parentItem());
148
149 QSharedPointer<QFileDialogOptions> options = QPlatformFileDialogHelper::options();
150 m_dialog->setTitle(options->windowTitle());
151 m_dialog->setOptions(options);
156
157 m_dialog->open();
158 return true;
159}
160
162{
163 if (!m_dialog)
164 return;
165
166 m_dialog->close();
167}
168
170{
171 return m_dialog;
172}
173
175
176#include "moc_qquickplatformfolderdialog_p.cpp"
\inmodule QtCore
Definition qdir.h:20
bool isLabelExplicitlySet(DialogLabel label)
QString labelText(DialogLabel label) const
\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)
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 setAcceptLabel(const QString &label)
void setSelectedFolder(const QUrl &selectedFolder)
void setCurrentFolder(const QUrl &folder)
void setRejectLabel(const QString &label)
void selectedFolderChanged(const QUrl &folderUrl)
void setOptions(const QSharedPointer< QFileDialogOptions > &options)
void currentFolderChanged(const QUrl &folderUrl)
QList< QUrl > selectedFiles() const override
void selectFile(const QUrl &file) override
void setDirectory(const QUrl &directory) override
QString selectedNameFilter() const override
bool defaultNameFilterDisables() const override
void selectNameFilter(const QString &filter) override
QQuickFolderDialogImpl * dialog() const
bool show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) 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
\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
\inmodule QtGui
Definition qwindow.h:63
Combined button and popup list for selecting options.
WindowModality
#define Q_LOGGING_CATEGORY(name,...)
#define qCWarning(category,...)
#define qCDebug(category,...)
GLbitfield flags
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)