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