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