1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Labs Platform module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickplatformfontdialog_p.h"
38
39QT_BEGIN_NAMESPACE
40
41/*!
42 \qmltype FontDialog
43 \inherits Dialog
44//! \instantiates QQuickPlatformFontDialog
45 \inqmlmodule Qt.labs.platform
46 \since 5.8
47 \brief A native font dialog.
48
49 The FontDialog type provides a QML API for native platform font dialogs.
50
51 \image qtlabsplatform-fontdialog-gtk.png
52
53 To show a font dialog, construct an instance of FontDialog, set the
54 desired properties, and call \l {Dialog::}{open()}. The \l currentFont
55 property can be used to determine the currently selected font in the
56 dialog. The \l font property is updated only after the final selection
57 has been made by accepting the dialog.
58
59 \code
60 MenuItem {
61 text: "Font"
62 onTriggered: fontDialog.open()
63 }
64
65 FontDialog {
66 id: fontDialog
67 currentFont.family: document.font
68 }
69
70 MyDocument {
71 id: document
72 font: fontDialog.font
73 }
74 \endcode
75
76 \section2 Availability
77
78 A native platform font dialog is currently available on the following platforms:
79
80 \list
81 \li macOS
82 \li Linux (when running with the GTK+ platform theme)
83 \endlist
84
85 \input includes/widgets.qdocinc 1
86
87 \labs
88*/
89
90QQuickPlatformFontDialog::QQuickPlatformFontDialog(QObject *parent)
91 : QQuickPlatformDialog(QPlatformTheme::FontDialog, parent),
92 m_options(QFontDialogOptions::create())
93{
94}
95
96/*!
97 \qmlproperty font Qt.labs.platform::FontDialog::font
98
99 This property holds the final accepted font.
100
101 Unlike the \l currentFont property, the \c font property is not updated
102 while the user is selecting fonts in the dialog, but only after the final
103 selection has been made. That is, when the user has clicked \uicontrol OK
104 to accept a font. Alternatively, the \l {Dialog::}{accepted()} signal
105 can be handled to get the final selection.
106
107 \sa currentFont, {Dialog::}{accepted()}
108*/
109QFont QQuickPlatformFontDialog::font() const
110{
111 return m_font;
112}
113
114void QQuickPlatformFontDialog::setFont(const QFont &font)
115{
116 if (m_font == font)
117 return;
118
119 m_font = font;
120 setCurrentFont(font);
121 emit fontChanged();
122}
123
124/*!
125 \qmlproperty font Qt.labs.platform::FontDialog::currentFont
126
127 This property holds the currently selected font in the dialog.
128
129 Unlike the \l font property, the \c currentFont property is updated
130 while the user is selecting fonts in the dialog, even before the final
131 selection has been made.
132
133 \sa font
134*/
135QFont QQuickPlatformFontDialog::currentFont() const
136{
137 if (QPlatformFontDialogHelper *fontDialog = qobject_cast<QPlatformFontDialogHelper *>(object: handle()))
138 return fontDialog->currentFont();
139 return m_currentFont;
140}
141
142void QQuickPlatformFontDialog::setCurrentFont(const QFont &font)
143{
144 if (QPlatformFontDialogHelper *fontDialog = qobject_cast<QPlatformFontDialogHelper *>(object: handle()))
145 fontDialog->setCurrentFont(font);
146 m_currentFont = font;
147}
148
149/*!
150 \qmlproperty flags Qt.labs.platform::FontDialog::options
151
152 This property holds the various options that affect the look and feel of the dialog.
153
154 By default, all options are disabled.
155
156 Options should be set before showing the dialog. Setting them while the dialog is
157 visible is not guaranteed to have an immediate effect on the dialog (depending on
158 the option and on the platform).
159
160 Available options:
161 \value FontDialog.ScalableFonts Show scalable fonts.
162 \value FontDialog.NonScalableFonts Show non-scalable fonts.
163 \value FontDialog.MonospacedFonts Show monospaced fonts.
164 \value FontDialog.ProportionalFonts Show proportional fonts.
165 \value FontDialog.NoButtons Don't display \uicontrol OK and \uicontrol Cancel buttons (useful for "live dialogs").
166*/
167QFontDialogOptions::FontDialogOptions QQuickPlatformFontDialog::options() const
168{
169 return m_options->options();
170}
171
172void QQuickPlatformFontDialog::setOptions(QFontDialogOptions::FontDialogOptions options)
173{
174 if (options == m_options->options())
175 return;
176
177 m_options->setOptions(options);
178 emit optionsChanged();
179}
180
181bool QQuickPlatformFontDialog::useNativeDialog() const
182{
183 return QQuickPlatformDialog::useNativeDialog()
184 && !m_options->testOption(option: QFontDialogOptions::DontUseNativeDialog);
185}
186
187void QQuickPlatformFontDialog::onCreate(QPlatformDialogHelper *dialog)
188{
189 if (QPlatformFontDialogHelper *fontDialog = qobject_cast<QPlatformFontDialogHelper *>(object: dialog)) {
190 connect(sender: fontDialog, signal: &QPlatformFontDialogHelper::currentFontChanged, receiver: this, slot: &QQuickPlatformFontDialog::currentFontChanged);
191 fontDialog->setOptions(m_options);
192 }
193}
194
195void QQuickPlatformFontDialog::onShow(QPlatformDialogHelper *dialog)
196{
197 m_options->setWindowTitle(title());
198 if (QPlatformFontDialogHelper *fontDialog = qobject_cast<QPlatformFontDialogHelper *>(object: dialog))
199 fontDialog->setOptions(m_options);
200}
201
202void QQuickPlatformFontDialog::accept()
203{
204 setFont(currentFont());
205 QQuickPlatformDialog::accept();
206}
207
208QT_END_NAMESPACE
209

source code of qtquickcontrols2/src/imports/platform/qquickplatformfontdialog.cpp