1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qquicktextmetrics_p.h"
41
42#include <QFont>
43
44QT_BEGIN_NAMESPACE
45
46/*!
47 \qmltype TextMetrics
48 \instantiates QQuickTextMetrics
49 \inqmlmodule QtQuick
50 \since 5.4
51 \ingroup qtquick-text-utility
52 \brief Provides metrics for a given font and text.
53
54 TextMetrics calculates various properties of a given string of text for a
55 particular font.
56
57 It provides a declarative API for the functions in \l QFontMetricsF which
58 take arguments.
59
60 \code
61 TextMetrics {
62 id: textMetrics
63 font.family: "Arial"
64 elide: Text.ElideMiddle
65 elideWidth: 100
66 text: "Hello World"
67 }
68
69 MyItem {
70 text: textMetrics.elidedText
71 }
72 \endcode
73
74 \sa QFontMetricsF, FontMetrics
75*/
76QQuickTextMetrics::QQuickTextMetrics(QObject *parent) :
77 QObject(parent),
78 m_metrics(m_font),
79 m_elide(Qt::ElideNone),
80 m_elideWidth(0)
81{
82}
83
84QQuickTextMetrics::~QQuickTextMetrics()
85{
86}
87
88/*!
89 \qmlproperty font QtQuick::TextMetrics::font
90
91 This property holds the font used for the metrics calculations.
92*/
93QFont QQuickTextMetrics::font() const
94{
95 return m_font;
96}
97
98void QQuickTextMetrics::setFont(const QFont &font)
99{
100 if (m_font != font) {
101 m_font = font;
102 m_metrics = QFontMetricsF(m_font);
103 emit fontChanged();
104 emit metricsChanged();
105 }
106}
107
108/*!
109 \qmlproperty string QtQuick::TextMetrics::text
110
111 This property holds the text used for the metrics calculations.
112*/
113QString QQuickTextMetrics::text() const
114{
115 return m_text;
116}
117
118void QQuickTextMetrics::setText(const QString &text)
119{
120 if (m_text != text) {
121 m_text = text;
122 emit textChanged();
123 emit metricsChanged();
124 }
125}
126
127/*!
128 \qmlproperty enumeration QtQuick::TextMetrics::elide
129
130 This property holds the elide mode of the text. This determines the
131 position in which the string is elided. The possible values are:
132
133 \list
134 \li \c Qt::ElideNone - No eliding; this is the default value.
135 \li \c Qt::ElideLeft - For example: "...World"
136 \li \c Qt::ElideMiddle - For example: "He...ld"
137 \li \c Qt::ElideRight - For example: "Hello..."
138 \endlist
139
140 \sa elideWidth, QFontMetrics::elidedText
141*/
142Qt::TextElideMode QQuickTextMetrics::elide() const
143{
144 return m_elide;
145}
146
147void QQuickTextMetrics::setElide(Qt::TextElideMode elide)
148{
149 if (m_elide != elide) {
150 m_elide = elide;
151 emit elideChanged();
152 emit metricsChanged();
153 }
154}
155
156/*!
157 \qmlproperty real QtQuick::TextMetrics::elideWidth
158
159 This property holds the largest width the text can have (in pixels) before
160 eliding will occur.
161
162 \sa elide, QFontMetrics::elidedText
163*/
164qreal QQuickTextMetrics::elideWidth() const
165{
166 return m_elideWidth;
167}
168
169void QQuickTextMetrics::setElideWidth(qreal elideWidth)
170{
171 if (m_elideWidth != elideWidth) {
172 m_elideWidth = elideWidth;
173 emit elideWidthChanged();
174 emit metricsChanged();
175 }
176}
177
178/*!
179 \qmlproperty real QtQuick::TextMetrics::advanceWidth
180
181 This property holds the advance in pixels of the characters in \l text.
182 This is the distance from the position of the string to where the next
183 string should be drawn.
184
185 \sa {QFontMetricsF::width()}
186*/
187qreal QQuickTextMetrics::advanceWidth() const
188{
189 return m_metrics.horizontalAdvance(string: m_text);
190}
191
192/*!
193 \qmlproperty rect QtQuick::TextMetrics::boundingRect
194
195 This property holds the bounding rectangle of the characters in the string
196 specified by \l text.
197
198 \sa {QFontMetricsF::boundingRect()}, tightBoundingRect
199*/
200QRectF QQuickTextMetrics::boundingRect() const
201{
202 return m_metrics.boundingRect(string: m_text);
203}
204
205/*!
206 \qmlproperty real QtQuick::TextMetrics::width
207
208 This property holds the width of the bounding rectangle of the characters
209 in the string specified by \l text. It is equivalent to:
210
211 \code
212 textMetrics.boundingRect.width
213 \endcode
214
215 \sa boundingRect
216*/
217qreal QQuickTextMetrics::width() const
218{
219 return boundingRect().width();
220}
221
222/*!
223 \qmlproperty real QtQuick::TextMetrics::height
224
225 This property holds the height of the bounding rectangle of the characters
226 in the string specified by \l text. It is equivalent to:
227
228 \code
229 textMetrics.boundingRect.height
230 \endcode
231
232 \sa boundingRect
233*/
234qreal QQuickTextMetrics::height() const
235{
236 return boundingRect().height();
237}
238
239/*!
240 \qmlproperty rect QtQuick::TextMetrics::tightBoundingRect
241
242 This property holds a tight bounding rectangle around the characters in the
243 string specified by \l text.
244
245 \sa {QFontMetricsF::tightBoundingRect()}, boundingRect
246*/
247QRectF QQuickTextMetrics::tightBoundingRect() const
248{
249 return m_metrics.tightBoundingRect(text: m_text);
250}
251
252/*!
253 \qmlproperty string QtQuick::TextMetrics::elidedText
254
255 This property holds an elided version of the string (i.e., a string with
256 "..." in it) if the string \l text is wider than \l elideWidth. If the
257 text is not wider than \l elideWidth, or \l elide is set to
258 \c Qt::ElideNone, this property will be equal to the original string.
259
260 \sa {QFontMetricsF::elidedText()}
261*/
262QString QQuickTextMetrics::elidedText() const
263{
264 return m_metrics.elidedText(text: m_text, mode: m_elide, width: m_elideWidth);
265}
266
267QT_END_NAMESPACE
268
269#include "moc_qquicktextmetrics_p.cpp"
270

source code of qtdeclarative/src/quick/util/qquicktextmetrics.cpp