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