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 "qquickplatformfolderdialog_p.h"
5
6#include <QtCore/qloggingcategory.h>
7#include <QtGui/qwindow.h>
8#include <QtQml/qqmlcontext.h>
9#include <QtQml/qqmlcomponent.h>
10#include <QtQml/qqmlinfo.h>
11#include <QtQuick/qquickwindow.h>
12#include <QtQuickTemplates2/private/qquickdialog_p.h>
13#include <QtQuickTemplates2/private/qquickdialog_p_p.h>
14#include <QtQuickTemplates2/private/qquickpopup_p_p.h>
15#include <QtQuickTemplates2/private/qquickpopupanchors_p.h>
16
17#include "qquickfolderdialogimpl_p.h"
18
19QT_BEGIN_NAMESPACE
20
21Q_STATIC_LOGGING_CATEGORY(lcQuickPlatformFolderDialog, "qt.quick.dialogs.quickplatformfolderdialog")
22
23/*!
24 \class QQuickPlatformFolderDialog
25 \internal
26
27 An interface that QQuickFolderDialog can use to access the non-native Qt Quick FolderDialog.
28
29 Both this and the native implementations are created in QQuickAbstractDialog::create().
30*/
31QQuickPlatformFolderDialog::QQuickPlatformFolderDialog(QObject *parent)
32{
33 qCDebug(lcQuickPlatformFolderDialog) << "creating non-native Qt Quick FolderDialog with parent" << parent;
34
35 // Set a parent so that we get deleted if we can't be shown for whatever reason.
36 // Our eventual parent should be the window, though.
37 setParent(parent);
38
39 auto qmlContext = ::qmlContext(parent);
40 if (!qmlContext) {
41 qmlWarning(me: parent) << "No QQmlContext for QQuickPlatformFolderDialog; can't create non-native FolderDialog implementation";
42 return;
43 }
44
45 const auto dialogQmlUrl = QUrl(QStringLiteral("qrc:/qt-project.org/imports/QtQuick/Dialogs/quickimpl/qml/FolderDialog.qml"));
46 QQmlComponent folderDialogComponent(qmlContext->engine(), dialogQmlUrl, parent);
47 if (!folderDialogComponent.isReady()) {
48 qmlWarning(me: parent) << "Failed to load non-native FolderDialog implementation:\n" << folderDialogComponent.errorString();
49 return;
50 }
51 m_dialog = qobject_cast<QQuickFolderDialogImpl*>(object: folderDialogComponent.create());
52 if (!m_dialog) {
53 qmlWarning(me: parent) << "Failed to create an instance of the non-native FolderDialog:\n" << folderDialogComponent.errorString();
54 return;
55 }
56 // Give it a parent until it's parented to the window in show().
57 m_dialog->setParent(this);
58
59 connect(sender: m_dialog, signal: &QQuickDialog::accepted, context: this, slot: &QPlatformDialogHelper::accept);
60 connect(sender: m_dialog, signal: &QQuickDialog::rejected, context: this, slot: &QPlatformDialogHelper::reject);
61
62 connect(sender: m_dialog, signal: &QQuickFolderDialogImpl::selectedFolderChanged, context: this, slot: &QQuickPlatformFolderDialog::currentChanged);
63 connect(sender: m_dialog, signal: &QQuickFolderDialogImpl::currentFolderChanged, context: this, slot: &QQuickPlatformFolderDialog::directoryEntered);
64 connect(sender: m_dialog, signal: &QQuickFolderDialogImpl::selectedFolderChanged, context: this, slot: &QQuickPlatformFolderDialog::fileSelected);
65
66 // We would do this in QQuickFolderDialogImpl, but we need to ensure that folderChanged()
67 // is connected to directoryEntered() before setting it to ensure that the QQuickFolderDialog is notified.
68 if (m_dialog->currentFolder().isEmpty())
69 m_dialog->setCurrentFolder(QUrl::fromLocalFile(localfile: QDir().absolutePath()));
70}
71
72bool QQuickPlatformFolderDialog::isValid() const
73{
74 return m_dialog;
75}
76
77bool QQuickPlatformFolderDialog::defaultNameFilterDisables() const
78{
79 return false;
80}
81
82void QQuickPlatformFolderDialog::setDirectory(const QUrl &directory)
83{
84 if (!m_dialog)
85 return;
86
87 m_dialog->setCurrentFolder(directory);
88}
89
90QUrl QQuickPlatformFolderDialog::directory() const
91{
92 if (!m_dialog)
93 return {};
94
95 return m_dialog->currentFolder();
96}
97
98void QQuickPlatformFolderDialog::selectFile(const QUrl &file)
99{
100 if (!m_dialog)
101 return;
102
103 m_dialog->setSelectedFolder(file);
104}
105
106QList<QUrl> QQuickPlatformFolderDialog::selectedFiles() const
107{
108 // FolderDialog doesn't support multiple selected folders.
109 return { m_dialog->selectedFolder() };
110}
111
112void QQuickPlatformFolderDialog::setFilter()
113{
114}
115
116void QQuickPlatformFolderDialog::selectNameFilter(const QString &/*filter*/)
117{
118}
119
120QString QQuickPlatformFolderDialog::selectedNameFilter() const
121{
122 return QStringLiteral("*.*");
123}
124
125void QQuickPlatformFolderDialog::exec()
126{
127 qCWarning(lcQuickPlatformFolderDialog) << "exec() is not supported for the Qt Quick FolderDialog fallback";
128}
129
130bool QQuickPlatformFolderDialog::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
131{
132 qCDebug(lcQuickPlatformFolderDialog) << "show called with flags" << flags <<
133 "modality" << modality << "parent" << parent;
134 if (!m_dialog)
135 return false;
136
137 if (!parent)
138 return false;
139
140 auto quickWindow = qobject_cast<QQuickWindow*>(object: parent);
141 if (!quickWindow) {
142 qmlInfo(me: this->parent()) << "Parent window (" << parent << ") of non-native dialog is not a QQuickWindow";
143 return false;
144 }
145 m_dialog->setParent(parent);
146 m_dialog->resetParentItem();
147
148 auto popupPrivate = QQuickPopupPrivate::get(popup: m_dialog);
149 popupPrivate->getAnchors()->setCenterIn(m_dialog->parentItem());
150
151 QSharedPointer<QFileDialogOptions> options = QPlatformFileDialogHelper::options();
152 m_dialog->setTitle(options->windowTitle());
153 m_dialog->setOptions(options);
154 m_dialog->setAcceptLabel(options->isLabelExplicitlySet(label: QFileDialogOptions::Accept)
155 ? options->labelText(label: QFileDialogOptions::Accept) : QString());
156 m_dialog->setRejectLabel(options->isLabelExplicitlySet(label: QFileDialogOptions::Reject)
157 ? options->labelText(label: QFileDialogOptions::Reject) : QString());
158 m_dialog->setWindowModality(modality);
159 m_dialog->open();
160 return true;
161}
162
163void QQuickPlatformFolderDialog::hide()
164{
165 if (!m_dialog)
166 return;
167
168 m_dialog->close();
169}
170
171QQuickFolderDialogImpl *QQuickPlatformFolderDialog::dialog() const
172{
173 return m_dialog;
174}
175
176QT_END_NAMESPACE
177
178#include "moc_qquickplatformfolderdialog_p.cpp"
179

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