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