| 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 "qquickplatformfontdialog_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 <QtQml/qqmlcomponent.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 "qquickfontdialogimpl_p.h" |
| 18 | |
| 19 | QT_BEGIN_NAMESPACE |
| 20 | |
| 21 | Q_STATIC_LOGGING_CATEGORY(lcQuickPlatformFontDialog, "qt.quick.dialogs.quickplatformfontdialog" ) |
| 22 | |
| 23 | /*! |
| 24 | \class QQuickPlatformFontDialog |
| 25 | \internal |
| 26 | |
| 27 | An interface that QQuickFontDialog can use to access the non-native Qt Quick FontDialog. |
| 28 | |
| 29 | Both this and the native implementations are created in QQuickAbstractDialog::create(). |
| 30 | |
| 31 | */ |
| 32 | QQuickPlatformFontDialog::QQuickPlatformFontDialog(QObject *parent) |
| 33 | { |
| 34 | qCDebug(lcQuickPlatformFontDialog) |
| 35 | << "creating non-native Qt Quick FontDialog with parent" << parent; |
| 36 | |
| 37 | // Set a parent so that we get deleted if we can't be shown for whatever reason. |
| 38 | // Our eventual parent should be the window, though. |
| 39 | setParent(parent); |
| 40 | |
| 41 | auto qmlContext = ::qmlContext(parent); |
| 42 | if (!qmlContext) { |
| 43 | qmlWarning(me: parent) << "No QQmlContext for QQuickPlatformFontDialog; can't create " |
| 44 | "non-native FontDialog implementation" ; |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | const auto dialogQmlUrl = QUrl(QStringLiteral( |
| 49 | "qrc:/qt-project.org/imports/QtQuick/Dialogs/quickimpl/qml/FontDialog.qml" )); |
| 50 | |
| 51 | QQmlComponent fontDialogComponent(qmlContext->engine(), dialogQmlUrl, parent); |
| 52 | if (!fontDialogComponent.isReady()) { |
| 53 | qmlWarning(me: parent) << "Failed to load non-native FontDialog implementation:\n" |
| 54 | << fontDialogComponent.errorString(); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | m_dialog = qobject_cast<QQuickFontDialogImpl *>(object: fontDialogComponent.create()); |
| 59 | |
| 60 | if (!m_dialog) { |
| 61 | qmlWarning(me: parent) << "Failed to create an instance of the non-native FontDialog:\n" |
| 62 | << fontDialogComponent.errorString(); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | m_dialog->setParent(this); |
| 67 | |
| 68 | connect(sender: m_dialog, signal: &QQuickDialog::accepted, context: this, slot: &QPlatformDialogHelper::accept); |
| 69 | connect(sender: m_dialog, signal: &QQuickDialog::rejected, context: this, slot: &QPlatformDialogHelper::reject); |
| 70 | |
| 71 | connect(sender: m_dialog, signal: &QQuickFontDialogImpl::currentFontChanged, |
| 72 | context: this, slot: &QQuickPlatformFontDialog::currentFontChanged); |
| 73 | } |
| 74 | |
| 75 | bool QQuickPlatformFontDialog::isValid() const |
| 76 | { |
| 77 | return m_dialog; |
| 78 | } |
| 79 | |
| 80 | void QQuickPlatformFontDialog::setCurrentFont(const QFont &font) |
| 81 | { |
| 82 | if (m_dialog) { |
| 83 | if (!m_dialog->options()) |
| 84 | m_dialog->setOptions(QPlatformFontDialogHelper::options()); |
| 85 | m_dialog->setCurrentFont(font, selectInListViews: true); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | QFont QQuickPlatformFontDialog::currentFont() const |
| 90 | { |
| 91 | return m_dialog ? m_dialog->currentFont() : QFont(); |
| 92 | } |
| 93 | |
| 94 | void QQuickPlatformFontDialog::exec() |
| 95 | { |
| 96 | qCWarning(lcQuickPlatformFontDialog) |
| 97 | << "exec() is not supported for the Qt Quick FontDialog fallback" ; |
| 98 | } |
| 99 | |
| 100 | bool QQuickPlatformFontDialog::show(Qt::WindowFlags flags, Qt::WindowModality modality, |
| 101 | QWindow *parent) |
| 102 | { |
| 103 | qCDebug(lcQuickPlatformFontDialog) |
| 104 | << "show called with flags" << flags << "modality" << modality << "parent" << parent; |
| 105 | |
| 106 | if (!isValid()) |
| 107 | return false; |
| 108 | |
| 109 | if (!parent) |
| 110 | return false; |
| 111 | |
| 112 | auto quickWindow = qobject_cast<QQuickWindow *>(object: parent); |
| 113 | if (!quickWindow) { |
| 114 | qmlInfo(me: this->parent()) << "Parent window (" << parent |
| 115 | << ") of non-native dialog is not a QQuickWindow" ; |
| 116 | return false; |
| 117 | } |
| 118 | m_dialog->setParent(parent); |
| 119 | m_dialog->resetParentItem(); |
| 120 | |
| 121 | auto = QQuickPopupPrivate::get(popup: m_dialog); |
| 122 | popupPrivate->getAnchors()->setCenterIn(m_dialog->parentItem()); |
| 123 | |
| 124 | QSharedPointer<QFontDialogOptions> options = QPlatformFontDialogHelper::options(); |
| 125 | m_dialog->setTitle(options->windowTitle()); |
| 126 | m_dialog->setOptions(options); |
| 127 | |
| 128 | m_dialog->init(); |
| 129 | m_dialog->setWindowModality(modality); |
| 130 | m_dialog->open(); |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | void QQuickPlatformFontDialog::hide() |
| 135 | { |
| 136 | if (isValid()) |
| 137 | m_dialog->close(); |
| 138 | } |
| 139 | |
| 140 | QQuickFontDialogImpl *QQuickPlatformFontDialog::dialog() const |
| 141 | { |
| 142 | return m_dialog; |
| 143 | } |
| 144 | |
| 145 | QT_END_NAMESPACE |
| 146 | |
| 147 | #include "moc_qquickplatformfontdialog_p.cpp" |
| 148 | |