aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@digia.com>2014-08-05 17:42:54 +0200
committerMitch Curtis <mitch.curtis@digia.com>2014-08-08 10:42:47 +0200
commit94669499fd943fc72095afd739a21cd8e675f14f (patch)
treed902820ebbc41f75699326fa1cf887ee49fc6746 /src/quick
parentc0e47716c402da2333bfa4082c7b40e3b376135c (diff)
Add declarative QQuickTextMetrics API from QQuickFontMetrics invokables
This allows use of a declarative API for the functions in QFontMetricsF which take parameters. This solves the problem of QML expressions that use QML methods not being re-evaluated when the internal data changes, by allowing the user to set the arguments as properties of a TextMetrics object, that would otherwise have to be passed to QML methods. For example: FontMetrics { id: fontMetrics } property rect r: fontMetrics.boundingRect("Blah") At some point, the font of the FontMetrics object is changed, but r will still represent the original bounding rect. Instead, the user will now write: TextMetrics { id: textMetrics text: "Blah" } property rect r: textMetrics.boundingRect The QML methods remain in QQuickFontMetrics for those who need an imperative API. [ChangeLog][QtQuick] Added QQuickTextMetrics, which provides a declarative API for the functions in QFontMetricsF which take arguments. Change-Id: I019dc4639531906fc751ba61281cc1c695742287 Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
Diffstat (limited to 'src/quick')
-rw-r--r--src/quick/util/qquickfontmetrics.cpp22
-rw-r--r--src/quick/util/qquicktextmetrics.cpp268
-rw-r--r--src/quick/util/qquicktextmetrics_p.h111
-rw-r--r--src/quick/util/qquickutilmodule.cpp2
-rw-r--r--src/quick/util/util.pri6
5 files changed, 404 insertions, 5 deletions
diff --git a/src/quick/util/qquickfontmetrics.cpp b/src/quick/util/qquickfontmetrics.cpp
index bf533995a5..47e4fc1c7e 100644
--- a/src/quick/util/qquickfontmetrics.cpp
+++ b/src/quick/util/qquickfontmetrics.cpp
@@ -65,12 +65,12 @@ QT_BEGIN_NAMESPACE
}
Rectangle {
- width: fontMetrics.height() * 4
- height: fontMetrics.height() * 2
+ width: fontMetrics.height * 4
+ height: fontMetrics.height * 2
}
\endcode
- \sa QFontMetricsF
+ \sa QFontMetricsF, TextMetrics
*/
QQuickFontMetrics::QQuickFontMetrics(QObject *parent) :
QObject(parent),
@@ -280,6 +280,10 @@ qreal QQuickFontMetrics::lineWidth() const
This is the distance from the position of the string to where the next
string should be drawn.
+ This method is offered as an imperative alternative to the
+ \l {QQuickTextMetrics::advanceWidth}{advanceWidth} property of
+ \l {QQuickTextMetrics::advanceWidth}{TextMetrics}.
+
\sa {QFontMetricsF::width()}, height()
*/
qreal QQuickFontMetrics::advanceWidth(const QString &text) const
@@ -293,6 +297,10 @@ qreal QQuickFontMetrics::advanceWidth(const QString &text) const
This method returns the bounding rectangle of the characters in the string
specified by \a text.
+ This method is offered as an imperative alternative to the
+ \l {QQuickTextMetrics::boundingRect}{boundingRect} property of
+ \l {QQuickTextMetrics::boundingRect}{TextMetrics}.
+
\sa {QFontMetricsF::boundingRect()}, tightBoundingRect()
*/
QRectF QQuickFontMetrics::boundingRect(const QString &text) const
@@ -306,6 +314,10 @@ QRectF QQuickFontMetrics::boundingRect(const QString &text) const
This method returns a tight bounding rectangle around the characters in the
string specified by \a text.
+ This method is offered as an imperative alternative to the
+ \l {QQuickTextMetrics::tightBoundingRect}{tightBoundingRect} property of
+ \l {QQuickTextMetrics::tightBoundingRect}{TextMetrics}.
+
\sa {QFontMetricsF::tightBoundingRect()}, boundingRect()
*/
QRectF QQuickFontMetrics::tightBoundingRect(const QString &text) const
@@ -324,6 +336,10 @@ QRectF QQuickFontMetrics::tightBoundingRect(const QString &text) const
The \a flags argument is optional and currently only supports
\l {Qt::TextShowMnemonic}.
+ This method is offered as an imperative alternative to the
+ \l {QQuickTextMetrics::elidedText}{elidedText} property of
+ \l {QQuickTextMetrics::elidedText}{TextMetrics}.
+
\sa {QFontMetricsF::elidedText()}
*/
QString QQuickFontMetrics::elidedText(const QString &text, Qt::TextElideMode mode, qreal width, int flags) const
diff --git a/src/quick/util/qquicktextmetrics.cpp b/src/quick/util/qquicktextmetrics.cpp
new file mode 100644
index 0000000000..09af99b82b
--- /dev/null
+++ b/src/quick/util/qquicktextmetrics.cpp
@@ -0,0 +1,268 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtQuick module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qquicktextmetrics_p.h"
+
+#include <QFont>
+
+QT_BEGIN_NAMESPACE
+
+/*!
+ \qmltype TextMetrics
+ \instantiates QQuickTextMetrics
+ \inqmlmodule QtQuick
+ \ingroup qtquick-text-utility
+ \brief Provides metrics for a given font and text
+
+ TextMetrics calculates various properties of a given string of text for a
+ particular font.
+
+ It provides a declarative API for the functions in \l QFontMetricsF which
+ take arguments.
+
+ \code
+ TextMetrics {
+ id: textMetrics
+ font.family: "Arial"
+ elide: Text.ElideMiddle
+ elideWidth: 100
+ text: "Hello World"
+ }
+
+ MyItem {
+ text: textMetrics.elidedText
+ }
+ \endcode
+
+ \sa QFontMetricsF, FontMetrics
+*/
+QQuickTextMetrics::QQuickTextMetrics(QObject *parent) :
+ QObject(parent),
+ m_metrics(m_font),
+ m_elide(Qt::ElideNone),
+ m_elideWidth(0)
+{
+}
+
+QQuickTextMetrics::~QQuickTextMetrics()
+{
+}
+
+/*!
+ \qmlproperty font QtQuick::TextMetrics::font
+
+ This property holds the font used for the metrics calculations.
+*/
+QFont QQuickTextMetrics::font() const
+{
+ return m_font;
+}
+
+void QQuickTextMetrics::setFont(const QFont &font)
+{
+ if (m_font != font) {
+ m_font = font;
+ m_metrics = QFontMetricsF(m_font);
+ emit fontChanged();
+ emit metricsChanged();
+ }
+}
+
+/*!
+ \qmlproperty string QtQuick::TextMetrics::text
+
+ This property holds the text used for the metrics calculations.
+*/
+QString QQuickTextMetrics::text() const
+{
+ return m_text;
+}
+
+void QQuickTextMetrics::setText(const QString &text)
+{
+ if (m_text != text) {
+ m_text = text;
+ emit textChanged();
+ emit metricsChanged();
+ }
+}
+
+/*!
+ \qmlproperty enumeration QtQuick::TextMetrics::elide
+
+ This property holds the elide mode of the text. This determines the
+ position in which the string is elided. The possible values are:
+
+ \list
+ \li \c Qt::ElideNone - No eliding; this is the default value.
+ \li \c Qt::ElideLeft - For example: "...World"
+ \li \c Qt::ElideMiddle - For example: "He...ld"
+ \li \c Qt::ElideRight - For example: "Hello..."
+ \endlist
+
+ \sa elideWidth, elidedText
+*/
+Qt::TextElideMode QQuickTextMetrics::elide() const
+{
+ return m_elide;
+}
+
+void QQuickTextMetrics::setElide(Qt::TextElideMode elide)
+{
+ if (m_elide != elide) {
+ m_elide = elide;
+ emit elideChanged();
+ emit metricsChanged();
+ }
+}
+
+/*!
+ \qmlproperty real QtQuick::TextMetrics::elideWidth
+
+ This property holds the largest width the text can have (in pixels) before
+ eliding will occur.
+
+ \sa elide, elidedText
+*/
+qreal QQuickTextMetrics::elideWidth() const
+{
+ return m_elideWidth;
+}
+
+void QQuickTextMetrics::setElideWidth(qreal elideWidth)
+{
+ if (m_elideWidth != elideWidth) {
+ m_elideWidth = elideWidth;
+ emit elideWidthChanged();
+ emit metricsChanged();
+ }
+}
+
+/*!
+ \qmlproperty real QtQuick::TextMetrics::advanceWidth
+
+ This property holds the advance in pixels of the characters in \l text.
+ This is the distance from the position of the string to where the next
+ string should be drawn.
+
+ \sa {QFontMetricsF::width()}
+*/
+qreal QQuickTextMetrics::advanceWidth() const
+{
+ return m_metrics.width(m_text);
+}
+
+/*!
+ \qmlproperty rect QtQuick::TextMetrics::boundingRect
+
+ This property holds the bounding rectangle of the characters in the string
+ specified by \l text.
+
+ \sa {QFontMetricsF::boundingRect()}, tightBoundingRect
+*/
+QRectF QQuickTextMetrics::boundingRect() const
+{
+ return m_metrics.boundingRect(m_text);
+}
+
+/*!
+ \qmlproperty real QtQuick::TextMetrics::width
+
+ This property holds the width of the bounding rectangle of the characters
+ in the string specified by \l text. It is equivalent to:
+
+ \code
+ textMetrics.boundingRect.width
+ \endcode
+
+ \sa boundingRect
+*/
+qreal QQuickTextMetrics::width() const
+{
+ return boundingRect().width();
+}
+
+/*!
+ \qmlproperty real QtQuick::TextMetrics::height
+
+ This property holds the height of the bounding rectangle of the characters
+ in the string specified by \l text. It is equivalent to:
+
+ \code
+ textMetrics.boundingRect.height
+ \endcode
+
+ \sa boundingRect
+*/
+qreal QQuickTextMetrics::height() const
+{
+ return boundingRect().height();
+}
+
+/*!
+ \qmlproperty rect QtQuick::TextMetrics::tightBoundingRect
+
+ This property holds a tight bounding rectangle around the characters in the
+ string specified by \l text.
+
+ \sa {QFontMetricsF::tightBoundingRect()}, boundingRect
+*/
+QRectF QQuickTextMetrics::tightBoundingRect() const
+{
+ return m_metrics.tightBoundingRect(m_text);
+}
+
+/*!
+ \qmlmethod string QtQuick::TextMetrics::elidedText
+
+ This property holds an elided version of the string (i.e., a string with
+ "..." in it) if the string \l text is wider than \l elideWidth. If the
+ text is not wider than \l elideWidth, or \l elide is set to
+ \c Qt::ElideNone, this property will be equal to the original string.
+
+ \sa {QFontMetricsF::elidedText()}
+*/
+QString QQuickTextMetrics::elidedText() const
+{
+ return m_metrics.elidedText(m_text, m_elide, m_elideWidth);
+}
+
+QT_END_NAMESPACE
diff --git a/src/quick/util/qquicktextmetrics_p.h b/src/quick/util/qquicktextmetrics_p.h
new file mode 100644
index 0000000000..ec993229e5
--- /dev/null
+++ b/src/quick/util/qquicktextmetrics_p.h
@@ -0,0 +1,111 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtQuick module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QQUICKTEXTMETRICS_H
+#define QQUICKTEXTMETRICS_H
+
+#include <qqml.h>
+
+#include <QtGui/QFontMetricsF>
+#include <QtCore/QObject>
+
+QT_BEGIN_NAMESPACE
+
+class QFont;
+
+class Q_AUTOTEST_EXPORT QQuickTextMetrics : public QObject
+{
+ Q_OBJECT
+
+ Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged FINAL)
+ Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged FINAL)
+ Q_PROPERTY(qreal advanceWidth READ advanceWidth NOTIFY metricsChanged FINAL)
+ Q_PROPERTY(QRectF boundingRect READ boundingRect NOTIFY metricsChanged FINAL)
+ Q_PROPERTY(qreal width READ width NOTIFY metricsChanged FINAL)
+ Q_PROPERTY(qreal height READ height NOTIFY metricsChanged FINAL)
+ Q_PROPERTY(QRectF tightBoundingRect READ tightBoundingRect NOTIFY metricsChanged FINAL)
+ Q_PROPERTY(QString elidedText READ elidedText NOTIFY metricsChanged FINAL)
+ Q_PROPERTY(Qt::TextElideMode elide READ elide WRITE setElide NOTIFY elideChanged FINAL)
+ Q_PROPERTY(qreal elideWidth READ elideWidth WRITE setElideWidth NOTIFY elideWidthChanged FINAL)
+
+public:
+ explicit QQuickTextMetrics(QObject *parent = 0);
+ ~QQuickTextMetrics();
+
+ QFont font() const;
+ void setFont(const QFont &font);
+
+ QString text() const;
+ void setText(const QString &text);
+
+ Qt::TextElideMode elide() const;
+ void setElide(Qt::TextElideMode elide);
+
+ qreal elideWidth() const;
+ void setElideWidth(qreal elideWidth);
+
+ qreal advanceWidth() const;
+ QRectF boundingRect() const;
+ qreal width() const;
+ qreal height() const;
+ QRectF tightBoundingRect() const;
+ QString elidedText() const;
+
+Q_SIGNALS:
+ void fontChanged();
+ void textChanged();
+ void elideChanged();
+ void elideWidthChanged();
+ void metricsChanged();
+
+private:
+ QString m_text;
+ QFont m_font;
+ QFontMetricsF m_metrics;
+ Qt::TextElideMode m_elide;
+ qreal m_elideWidth;
+};
+
+QT_END_NAMESPACE
+
+QML_DECLARE_TYPE(QQuickTextMetrics)
+
+#endif // QQUICKTEXTMETRICS_H
diff --git a/src/quick/util/qquickutilmodule.cpp b/src/quick/util/qquickutilmodule.cpp
index 1d82cfca43..8ea903b010 100644
--- a/src/quick/util/qquickutilmodule.cpp
+++ b/src/quick/util/qquickutilmodule.cpp
@@ -53,6 +53,7 @@
#include "qquickstate_p.h"
#include "qquickstate_p_p.h"
#include "qquicksystempalette_p.h"
+#include "qquicktextmetrics_p.h"
#include "qquicktransition_p.h"
#include "qquickanimator_p.h"
#include <qqmlinfo.h>
@@ -109,4 +110,5 @@ void QQuickUtilModule::defineModule()
qmlRegisterUncreatableType<QKeySequence, 2>("QtQuick", 2, 2, "StandardKey", QStringLiteral("Cannot create an instance of StandardKey."));
qmlRegisterType<QQuickFontMetrics>("QtQuick", 2, 4, "FontMetrics");
+ qmlRegisterType<QQuickTextMetrics>("QtQuick", 2, 4, "TextMetrics");
}
diff --git a/src/quick/util/util.pri b/src/quick/util/util.pri
index 35553070dc..76cf1996bc 100644
--- a/src/quick/util/util.pri
+++ b/src/quick/util/util.pri
@@ -27,7 +27,8 @@ SOURCES += \
$$PWD/qquickanimatorjob.cpp \
$$PWD/qquickanimatorcontroller.cpp \
$$PWD/qquickprofiler.cpp \
- $$PWD/qquickfontmetrics.cpp
+ $$PWD/qquickfontmetrics.cpp \
+ $$PWD/qquicktextmetrics.cpp
HEADERS += \
$$PWD/qquickapplication_p.h\
@@ -62,4 +63,5 @@ HEADERS += \
$$PWD/qquickanimatorjob_p.h \
$$PWD/qquickanimatorcontroller_p.h \
$$PWD/qquickprofiler_p.h \
- $$PWD/qquickfontmetrics_p.h
+ $$PWD/qquickfontmetrics_p.h \
+ $$PWD/qquicktextmetrics_p.h