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

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