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/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 | |
15 | #include "qquickfolderdialogimpl_p.h" |
16 | |
17 | QT_BEGIN_NAMESPACE |
18 | |
19 | Q_LOGGING_CATEGORY(lcQuickPlatformFolderDialog, "qt.quick.dialogs.quickplatformfolderdialog" ) |
20 | |
21 | /*! |
22 | \class QQuickPlatformFolderDialog |
23 | \internal |
24 | |
25 | An interface that QQuickFolderDialog can use to access the non-native Qt Quick FolderDialog. |
26 | |
27 | Both this and the native implementations are created in QQuickAbstractDialog::create(). |
28 | */ |
29 | QQuickPlatformFolderDialog::QQuickPlatformFolderDialog(QObject *parent) |
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(me: 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(me: parent) << "Failed to load non-native FolderDialog implementation:\n" << folderDialogComponent.errorString(); |
47 | return; |
48 | } |
49 | m_dialog = qobject_cast<QQuickFolderDialogImpl*>(object: folderDialogComponent.create()); |
50 | if (!m_dialog) { |
51 | qmlWarning(me: 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 | |
57 | connect(sender: m_dialog, signal: &QQuickDialog::accepted, context: this, slot: &QPlatformDialogHelper::accept); |
58 | connect(sender: m_dialog, signal: &QQuickDialog::rejected, context: this, slot: &QPlatformDialogHelper::reject); |
59 | |
60 | connect(sender: m_dialog, signal: &QQuickFolderDialogImpl::selectedFolderChanged, context: this, slot: &QQuickPlatformFolderDialog::currentChanged); |
61 | connect(sender: m_dialog, signal: &QQuickFolderDialogImpl::currentFolderChanged, context: this, slot: &QQuickPlatformFolderDialog::directoryEntered); |
62 | connect(sender: m_dialog, signal: &QQuickFolderDialogImpl::selectedFolderChanged, context: this, slot: &QQuickPlatformFolderDialog::fileSelected); |
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(localfile: QDir().absolutePath())); |
68 | } |
69 | |
70 | bool QQuickPlatformFolderDialog::isValid() const |
71 | { |
72 | return m_dialog; |
73 | } |
74 | |
75 | bool QQuickPlatformFolderDialog::defaultNameFilterDisables() const |
76 | { |
77 | return false; |
78 | } |
79 | |
80 | void QQuickPlatformFolderDialog::setDirectory(const QUrl &directory) |
81 | { |
82 | if (!m_dialog) |
83 | return; |
84 | |
85 | m_dialog->setCurrentFolder(directory); |
86 | } |
87 | |
88 | QUrl QQuickPlatformFolderDialog::directory() const |
89 | { |
90 | if (!m_dialog) |
91 | return {}; |
92 | |
93 | return m_dialog->currentFolder(); |
94 | } |
95 | |
96 | void QQuickPlatformFolderDialog::selectFile(const QUrl &file) |
97 | { |
98 | if (!m_dialog) |
99 | return; |
100 | |
101 | m_dialog->setSelectedFolder(file); |
102 | } |
103 | |
104 | QList<QUrl> QQuickPlatformFolderDialog::selectedFiles() const |
105 | { |
106 | // FolderDialog doesn't support multiple selected folders. |
107 | return { m_dialog->selectedFolder() }; |
108 | } |
109 | |
110 | void QQuickPlatformFolderDialog::setFilter() |
111 | { |
112 | } |
113 | |
114 | void QQuickPlatformFolderDialog::selectNameFilter(const QString &/*filter*/) |
115 | { |
116 | } |
117 | |
118 | QString QQuickPlatformFolderDialog::selectedNameFilter() const |
119 | { |
120 | return QStringLiteral("*.*" ); |
121 | } |
122 | |
123 | void QQuickPlatformFolderDialog::exec() |
124 | { |
125 | qCWarning(lcQuickPlatformFolderDialog) << "exec() is not supported for the Qt Quick FolderDialog fallback" ; |
126 | } |
127 | |
128 | bool 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*>(object: parent); |
139 | if (!quickWindow) { |
140 | qmlInfo(me: 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 = QQuickPopupPrivate::get(popup: 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); |
152 | m_dialog->setAcceptLabel(options->isLabelExplicitlySet(label: QFileDialogOptions::Accept) |
153 | ? options->labelText(label: QFileDialogOptions::Accept) : QString()); |
154 | m_dialog->setRejectLabel(options->isLabelExplicitlySet(label: QFileDialogOptions::Reject) |
155 | ? options->labelText(label: QFileDialogOptions::Reject) : QString()); |
156 | |
157 | m_dialog->open(); |
158 | return true; |
159 | } |
160 | |
161 | void QQuickPlatformFolderDialog::hide() |
162 | { |
163 | if (!m_dialog) |
164 | return; |
165 | |
166 | m_dialog->close(); |
167 | } |
168 | |
169 | QQuickFolderDialogImpl *QQuickPlatformFolderDialog::dialog() const |
170 | { |
171 | return m_dialog; |
172 | } |
173 | |
174 | QT_END_NAMESPACE |
175 | |
176 | #include "moc_qquickplatformfolderdialog_p.cpp" |
177 | |