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 "qquickplatformmessagedialog_p.h"
5
6#include <QtQuickTemplates2/private/qquickpopup_p_p.h>
7#include <QtQuickTemplates2/private/qquickpopupanchors_p.h>
8
9#include "qquickmessagedialogimpl_p.h"
10
11QT_BEGIN_NAMESPACE
12
13Q_LOGGING_CATEGORY(lcQuickPlatformMessageDialog, "qt.quick.dialogs.quickplatformmessagedialog")
14
15QQuickPlatformMessageDialog::QQuickPlatformMessageDialog(QObject *parent)
16{
17 qCDebug(lcQuickPlatformMessageDialog)
18 << "creating non-native Qt Quick MessageDialog with parent" << parent;
19
20 // Set a parent so that we get deleted if we can't be shown for whatever reason.
21 // Our eventual parent should be the window, though.
22 setParent(parent);
23
24 auto qmlContext = ::qmlContext(parent);
25 if (!qmlContext) {
26 qmlWarning(me: parent) << "No QQmlContext for QQuickPlatformMessageDialog; can't create "
27 "non-native MessageDialog implementation";
28 return;
29 }
30
31 const auto dialogQmlUrl = QUrl(QStringLiteral(
32 "qrc:/qt-project.org/imports/QtQuick/Dialogs/quickimpl/qml/MessageDialog.qml"));
33
34 QQmlComponent messageDialogComponent(qmlContext->engine(), dialogQmlUrl, parent);
35 if (!messageDialogComponent.isReady()) {
36 qmlWarning(me: parent) << "Failed to load non-native MessageBox implementation:\n"
37 << messageDialogComponent.errorString();
38 return;
39 }
40
41 m_dialog = qobject_cast<QQuickMessageDialogImpl *>(object: messageDialogComponent.create());
42
43 if (!m_dialog) {
44 qmlWarning(me: parent) << "Failed to create an instance of the non-native MessageBox:\n"
45 << messageDialogComponent.errorString();
46 return;
47 }
48
49 m_dialog->setParent(this);
50
51 connect(sender: m_dialog, signal: &QQuickDialog::accepted, context: this, slot: &QPlatformDialogHelper::accept);
52 connect(sender: m_dialog, signal: &QQuickDialog::rejected, context: this, slot: &QPlatformDialogHelper::reject);
53 connect(sender: m_dialog, signal: &QQuickMessageDialogImpl::buttonClicked, context: this,
54 slot: &QQuickPlatformMessageDialog::clicked);
55}
56
57void QQuickPlatformMessageDialog::exec()
58{
59 qCWarning(lcQuickPlatformMessageDialog)
60 << "exec() is not supported for the Qt Quick MessageDialog fallback";
61}
62
63bool QQuickPlatformMessageDialog::show(Qt::WindowFlags flags, Qt::WindowModality modality,
64 QWindow *parent)
65{
66 qCDebug(lcQuickPlatformMessageDialog)
67 << "show called with flags" << flags << "modality" << modality << "parent" << parent;
68 if (!m_dialog)
69 return false;
70
71 if (!parent)
72 return false;
73
74 auto quickWindow = qobject_cast<QQuickWindow *>(object: parent);
75 if (!quickWindow) {
76 qmlInfo(me: this->parent()) << "Parent window (" << parent
77 << ") of non-native dialog is not a QQuickWindow";
78 return false;
79 }
80 m_dialog->setParent(parent);
81 m_dialog->resetParentItem();
82
83 auto popupPrivate = QQuickPopupPrivate::get(popup: m_dialog);
84 popupPrivate->getAnchors()->setCenterIn(m_dialog->parentItem());
85
86 QSharedPointer<QMessageDialogOptions> options = QPlatformMessageDialogHelper::options();
87 m_dialog->setTitle(options->windowTitle());
88 m_dialog->setOptions(options);
89
90 m_dialog->open();
91 return true;
92}
93void QQuickPlatformMessageDialog::hide()
94{
95 if (isValid())
96 m_dialog->close();
97}
98
99bool QQuickPlatformMessageDialog::isValid() const
100{
101 return m_dialog;
102}
103
104QQuickMessageDialogImpl *QQuickPlatformMessageDialog::dialog() const
105{
106 return m_dialog;
107}
108
109QT_END_NAMESPACE
110
111#include "moc_qquickplatformmessagedialog_p.cpp"
112

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