1// Copyright (C) 2017 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 "qquicklabsplatformfontdialog_p.h"
5
6#if QT_DEPRECATED_SINCE(6, 9)
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 \qmltype FontDialog
12 \inherits Dialog
13//! \nativetype QQuickLabsPlatformFontDialog
14 \inqmlmodule Qt.labs.platform
15 \since 5.8
16 \deprecated [6.9] Use QtQuick.Dialogs::FontDialog instead.
17 \brief A native font dialog.
18
19 The FontDialog type provides a QML API for native platform font dialogs.
20
21 \image {qtlabsplatform-fontdialog-gtk.png} {A native font dialog}
22
23 To show a font dialog, construct an instance of FontDialog, set the
24 desired properties, and call \l {Dialog::}{open()}. The \l currentFont
25 property can be used to determine the currently selected font in the
26 dialog. The \l font property is updated only after the final selection
27 has been made by accepting the dialog.
28
29 \code
30 MenuItem {
31 text: "Font"
32 onTriggered: fontDialog.open()
33 }
34
35 FontDialog {
36 id: fontDialog
37 currentFont.family: document.font
38 }
39
40 MyDocument {
41 id: document
42 font: fontDialog.font
43 }
44 \endcode
45
46 \section2 Availability
47
48 A native platform font dialog is currently available on the following platforms:
49
50 \list
51 \li iOS
52 \li Linux (when running with the GTK+ platform theme)
53 \li macOS
54 \endlist
55
56 \input includes/widgets.qdocinc 1
57
58 \labs
59
60 \sa QtQuick.Dialogs::FontDialog
61*/
62
63QQuickLabsPlatformFontDialog::QQuickLabsPlatformFontDialog(QObject *parent)
64 : QQuickLabsPlatformDialog(QPlatformTheme::FontDialog, parent),
65 m_options(QFontDialogOptions::create())
66{
67}
68
69/*!
70 \qmlproperty font Qt.labs.platform::FontDialog::font
71
72 This property holds the final accepted font.
73
74 Unlike the \l currentFont property, the \c font property is not updated
75 while the user is selecting fonts in the dialog, but only after the final
76 selection has been made. That is, when the user has clicked \uicontrol OK
77 to accept a font. Alternatively, the \l {Dialog::}{accepted()} signal
78 can be handled to get the final selection.
79
80 \sa currentFont, {Dialog::}{accepted()}
81*/
82QFont QQuickLabsPlatformFontDialog::font() const
83{
84 return m_font;
85}
86
87void QQuickLabsPlatformFontDialog::setFont(const QFont &font)
88{
89 if (m_font == font)
90 return;
91
92 m_font = font;
93 setCurrentFont(font);
94 emit fontChanged();
95}
96
97/*!
98 \qmlproperty font Qt.labs.platform::FontDialog::currentFont
99
100 This property holds the currently selected font in the dialog.
101
102 Unlike the \l font property, the \c currentFont property is updated
103 while the user is selecting fonts in the dialog, even before the final
104 selection has been made.
105
106 \sa font
107*/
108QFont QQuickLabsPlatformFontDialog::currentFont() const
109{
110 if (QPlatformFontDialogHelper *fontDialog = qobject_cast<QPlatformFontDialogHelper *>(object: handle()))
111 return fontDialog->currentFont();
112 return m_currentFont;
113}
114
115void QQuickLabsPlatformFontDialog::setCurrentFont(const QFont &font)
116{
117 if (QPlatformFontDialogHelper *fontDialog = qobject_cast<QPlatformFontDialogHelper *>(object: handle()))
118 fontDialog->setCurrentFont(font);
119 m_currentFont = font;
120}
121
122/*!
123 \qmlproperty flags Qt.labs.platform::FontDialog::options
124
125 This property holds the various options that affect the look and feel of the dialog.
126
127 By default, all options are disabled.
128
129 Options should be set before showing the dialog. Setting them while the dialog is
130 visible is not guaranteed to have an immediate effect on the dialog (depending on
131 the option and on the platform).
132
133 Available options:
134 \value FontDialog.ScalableFonts Show scalable fonts.
135 \value FontDialog.NonScalableFonts Show non-scalable fonts.
136 \value FontDialog.MonospacedFonts Show monospaced fonts.
137 \value FontDialog.ProportionalFonts Show proportional fonts.
138 \value FontDialog.NoButtons Don't display \uicontrol OK and \uicontrol Cancel buttons (useful for "live dialogs").
139*/
140QFontDialogOptions::FontDialogOptions QQuickLabsPlatformFontDialog::options() const
141{
142 return m_options->options();
143}
144
145void QQuickLabsPlatformFontDialog::setOptions(QFontDialogOptions::FontDialogOptions options)
146{
147 if (options == m_options->options())
148 return;
149
150 m_options->setOptions(options);
151 emit optionsChanged();
152}
153
154bool QQuickLabsPlatformFontDialog::useNativeDialog() const
155{
156 return QQuickLabsPlatformDialog::useNativeDialog()
157 && !m_options->testOption(option: QFontDialogOptions::DontUseNativeDialog);
158}
159
160void QQuickLabsPlatformFontDialog::onCreate(QPlatformDialogHelper *dialog)
161{
162 if (QPlatformFontDialogHelper *fontDialog = qobject_cast<QPlatformFontDialogHelper *>(object: dialog)) {
163 connect(sender: fontDialog, signal: &QPlatformFontDialogHelper::currentFontChanged, context: this, slot: &QQuickLabsPlatformFontDialog::currentFontChanged);
164 fontDialog->setOptions(m_options);
165 }
166}
167
168void QQuickLabsPlatformFontDialog::onShow(QPlatformDialogHelper *dialog)
169{
170 m_options->setWindowTitle(title());
171 if (QPlatformFontDialogHelper *fontDialog = qobject_cast<QPlatformFontDialogHelper *>(object: dialog))
172 fontDialog->setOptions(m_options);
173}
174
175void QQuickLabsPlatformFontDialog::accept()
176{
177 setFont(currentFont());
178 QQuickLabsPlatformDialog::accept();
179}
180
181QT_END_NAMESPACE
182
183#include "moc_qquicklabsplatformfontdialog_p.cpp"
184
185#endif // QT_DEPRECATED_SINCE(6, 9)
186

source code of qtdeclarative/src/labs/platform/qquicklabsplatformfontdialog.cpp