diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-05-09 15:40:45 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-05-31 09:07:01 +0000 |
commit | f2264e2684ebc5ff775515e6fcc358c68298ef3e (patch) | |
tree | 1e476570219c4fef7e3c6e1c15759ae7b4fc7d66 | |
parent | 7d592bcbfb604b1179004e311aa6a972fab6707c (diff) |
Add support for configurable fonts
A style's default font is specified in qtquickcontrols2.conf in
a "Font" group under the style's section. QSettings supports the
following two alternative syntaxes:
[Default]
Font\Family=Open Sans
Font\PixelSize=20
or
[Default\Font]
Family=Open Sans
PixelSize=20
[ChangeLog][Controls] Added support for specifying the default
font for different styles in qtquickcontrols2.conf.
Change-Id: I54e1efb79a2913eab35174dbf09b6956fe740e28
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
16 files changed, 288 insertions, 24 deletions
diff --git a/src/imports/controls/doc/src/qtquickcontrols2-configuration.qdoc b/src/imports/controls/doc/src/qtquickcontrols2-configuration.qdoc index 36bce98d..1e0a1e42 100644 --- a/src/imports/controls/doc/src/qtquickcontrols2-configuration.qdoc +++ b/src/imports/controls/doc/src/qtquickcontrols2-configuration.qdoc @@ -63,6 +63,53 @@ \include qquickuniversalstyle.qdocinc conf + The default \l {Control::font}{font} can be specified in a \c Font sub-group + in each style's section in the configuration file. The \c Font sub-group can + be defined in two alternative ways: + + \code + [Default] + Font\Family=Open Sans + Font\PixelSize=20 + + [Material\Font] + Family=Open Sans + PixelSize=20 + \endcode + + Supported font attributes: + \table + \header + \li Variable + \li Description + \row + \li \c Family + \li The \l {QFont::family}{font family}. + \row + \li \c PointSize + \li The \l {QFont::pointSizeF}{point size}. + \row + \li \c PixelSize + \li The \l {QFont::pixelSize}{pixel size}. + \row + \li \c StyleHint + \li The \l {QFont::styleHint}{style hint}. + Available values: \c SansSerif, \c Helvetica, \c Serif, \c Times, \c TypeWriter, \c Courier, + \c OldEnglish, \c Decorative, \c Monospace, \c Fantasy, \c Cursive. + \row + \li \c Weight + \li The \l {QFont::}{weight}. Qt uses a weighting scale from \c 0 to \c 99 similar to, + but not the same as, the scales used in Windows or CSS. A weight of \c 0 will be thin, + whilst \c 99 will be extremely black. + Available pre-defined weights: \c Thin (0), \c ExtraLight (12), \c Light (25), \c Normal (50), + \c Medium (57), \c DemiBold (63), \c Bold (75), \c ExtraBold (81), + \c Black (87). + \row + \li \c Style + \li The \l {QFont::}{style}. + Available values: \c StyleNormal, \c StyleItalic, \c StyleOblique. + \endtable + In order to make it possible for Qt Quick Controls 2 to find the configuration file, it must be built into application's resources using the \l {The Qt Resource System}. Here's an example \c .qrc file: diff --git a/src/imports/controls/fusion/qquickfusiontheme.cpp b/src/imports/controls/fusion/qquickfusiontheme.cpp index 54536101..1c5025a4 100644 --- a/src/imports/controls/fusion/qquickfusiontheme.cpp +++ b/src/imports/controls/fusion/qquickfusiontheme.cpp @@ -38,8 +38,8 @@ QT_BEGIN_NAMESPACE -QQuickFusionTheme::QQuickFusionTheme(QPlatformTheme *theme) - : QQuickProxyTheme(theme) +QQuickFusionTheme::QQuickFusionTheme() + : QQuickTheme(QStringLiteral("Fusion")) { } diff --git a/src/imports/controls/fusion/qquickfusiontheme_p.h b/src/imports/controls/fusion/qquickfusiontheme_p.h index c5949848..19cb8617 100644 --- a/src/imports/controls/fusion/qquickfusiontheme_p.h +++ b/src/imports/controls/fusion/qquickfusiontheme_p.h @@ -50,14 +50,14 @@ #include <QtCore/qvariant.h> #include <QtGui/qpalette.h> -#include <QtQuickControls2/private/qquickproxytheme_p.h> +#include <QtQuickControls2/private/qquicktheme_p.h> QT_BEGIN_NAMESPACE -class QQuickFusionTheme : public QQuickProxyTheme +class QQuickFusionTheme : public QQuickTheme { public: - explicit QQuickFusionTheme(QPlatformTheme *theme = nullptr); + explicit QQuickFusionTheme(); const QPalette *palette(Palette type = SystemPalette) const override; }; diff --git a/src/imports/controls/material/qquickmaterialtheme.cpp b/src/imports/controls/material/qquickmaterialtheme.cpp index f75fa8a5..7ef2f0e7 100644 --- a/src/imports/controls/material/qquickmaterialtheme.cpp +++ b/src/imports/controls/material/qquickmaterialtheme.cpp @@ -42,8 +42,8 @@ QT_BEGIN_NAMESPACE -QQuickMaterialTheme::QQuickMaterialTheme(QPlatformTheme *theme) - : QQuickProxyTheme(theme) +QQuickMaterialTheme::QQuickMaterialTheme() + : QQuickTheme(QStringLiteral("Material")) { QFont font; font.setFamily(QLatin1String("Roboto")); @@ -65,22 +65,29 @@ QQuickMaterialTheme::QQuickMaterialTheme(QPlatformTheme *theme) } systemFont.setPixelSize(14); + systemFont = resolveFont(systemFont); buttonFont.setPixelSize(14); buttonFont.setCapitalization(QFont::AllUppercase); buttonFont.setWeight(QFont::Medium); + buttonFont = resolveFont(buttonFont); toolTipFont.setPixelSize(14); toolTipFont.setWeight(QFont::Medium); + toolTipFont = resolveFont(toolTipFont); itemViewFont.setPixelSize(14); itemViewFont.setWeight(QFont::Medium); + itemViewFont = resolveFont(itemViewFont); listViewFont.setPixelSize(16); + listViewFont = resolveFont(listViewFont); menuItemFont.setPixelSize(16); + menuItemFont = resolveFont(menuItemFont); editorFont.setPixelSize(16); + editorFont = resolveFont(editorFont); } const QFont *QQuickMaterialTheme::font(QPlatformTheme::Font type) const diff --git a/src/imports/controls/material/qquickmaterialtheme_p.h b/src/imports/controls/material/qquickmaterialtheme_p.h index f31ab58f..0e791386 100644 --- a/src/imports/controls/material/qquickmaterialtheme_p.h +++ b/src/imports/controls/material/qquickmaterialtheme_p.h @@ -50,14 +50,14 @@ #include <QtCore/qvariant.h> #include <QtGui/qfont.h> -#include <QtQuickControls2/private/qquickproxytheme_p.h> +#include <QtQuickControls2/private/qquicktheme_p.h> QT_BEGIN_NAMESPACE -class QQuickMaterialTheme : public QQuickProxyTheme +class QQuickMaterialTheme : public QQuickTheme { public: - explicit QQuickMaterialTheme(QPlatformTheme *theme = nullptr); + explicit QQuickMaterialTheme(); const QFont *font(Font type = SystemFont) const override; diff --git a/src/imports/controls/qquickdefaulttheme.cpp b/src/imports/controls/qquickdefaulttheme.cpp index 69bc49c5..f9cdcddc 100644 --- a/src/imports/controls/qquickdefaulttheme.cpp +++ b/src/imports/controls/qquickdefaulttheme.cpp @@ -39,7 +39,7 @@ QT_BEGIN_NAMESPACE QQuickDefaultTheme::QQuickDefaultTheme() - : QQuickProxyTheme() + : QQuickTheme(QStringLiteral("Default")) { } diff --git a/src/imports/controls/qquickdefaulttheme_p.h b/src/imports/controls/qquickdefaulttheme_p.h index f3337dd9..e0811dcb 100644 --- a/src/imports/controls/qquickdefaulttheme_p.h +++ b/src/imports/controls/qquickdefaulttheme_p.h @@ -48,11 +48,11 @@ // We mean it. // -#include <QtQuickControls2/private/qquickproxytheme_p.h> +#include <QtQuickControls2/private/qquicktheme_p.h> QT_BEGIN_NAMESPACE -class QQuickDefaultTheme : public QQuickProxyTheme +class QQuickDefaultTheme : public QQuickTheme { public: explicit QQuickDefaultTheme(); diff --git a/src/imports/controls/universal/qquickuniversaltheme.cpp b/src/imports/controls/universal/qquickuniversaltheme.cpp index a7bcbb8c..549840b6 100644 --- a/src/imports/controls/universal/qquickuniversaltheme.cpp +++ b/src/imports/controls/universal/qquickuniversaltheme.cpp @@ -41,8 +41,8 @@ QT_BEGIN_NAMESPACE -QQuickUniversalTheme::QQuickUniversalTheme(QPlatformTheme *theme) - : QQuickProxyTheme(theme) +QQuickUniversalTheme::QQuickUniversalTheme() + : QQuickTheme(QStringLiteral("Universal")) { const QFont font(QLatin1String("Segoe UI")); if (QFontInfo(font).family() == QLatin1String("Segoe UI")) { @@ -53,12 +53,15 @@ QQuickUniversalTheme::QQuickUniversalTheme(QPlatformTheme *theme) } systemFont.setPixelSize(15); + systemFont = resolveFont(systemFont); groupBoxTitleFont.setPixelSize(15); groupBoxTitleFont.setWeight(QFont::DemiBold); + groupBoxTitleFont = resolveFont(groupBoxTitleFont); tabButtonFont.setPixelSize(24); tabButtonFont.setWeight(QFont::Light); + tabButtonFont = resolveFont(tabButtonFont); } const QFont *QQuickUniversalTheme::font(QPlatformTheme::Font type) const diff --git a/src/imports/controls/universal/qquickuniversaltheme_p.h b/src/imports/controls/universal/qquickuniversaltheme_p.h index 7e18a483..f15dee3e 100644 --- a/src/imports/controls/universal/qquickuniversaltheme_p.h +++ b/src/imports/controls/universal/qquickuniversaltheme_p.h @@ -49,14 +49,14 @@ // #include <QtGui/qfont.h> -#include <QtQuickControls2/private/qquickproxytheme_p.h> +#include <QtQuickControls2/private/qquicktheme_p.h> QT_BEGIN_NAMESPACE -class QQuickUniversalTheme : public QQuickProxyTheme +class QQuickUniversalTheme : public QQuickTheme { public: - explicit QQuickUniversalTheme(QPlatformTheme *theme = nullptr); + explicit QQuickUniversalTheme(); const QFont *font(Font type = SystemFont) const override; diff --git a/src/quickcontrols2/qquicktheme.cpp b/src/quickcontrols2/qquicktheme.cpp new file mode 100644 index 00000000..54653e6b --- /dev/null +++ b/src/quickcontrols2/qquicktheme.cpp @@ -0,0 +1,112 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquicktheme_p.h" +#include "qquickstyle_p.h" + +#include <QtCore/qmetaobject.h> +#include <QtCore/qsettings.h> + +#include <functional> + +QT_BEGIN_NAMESPACE + +#if QT_CONFIG(settings) +static void readValue(const QSharedPointer<QSettings> &settings, const QString &name, std::function<void(const QVariant &)> setValue) +{ + const QVariant var = settings->value(name); + if (var.isValid()) + setValue(var); +} + +template <typename Enum> +static Enum toEnumValue(const QVariant &var) +{ + // ### TODO: expose QFont enums to the meta object system using Q_ENUM + //QMetaEnum enumeration = QMetaEnum::fromType<Enum>(); + //bool ok = false; + //int value = enumeration.keyToValue(var.toByteArray(), &ok); + //if (!ok) + // value = var.toInt(); + //return static_cast<Enum>(value); + + return static_cast<Enum>(var.toInt()); +} + +QFont *readFont(const QSharedPointer<QSettings> &settings) +{ + const QVariant var = settings->value(QStringLiteral("Font")); + if (var.isValid()) + return new QFont(var.value<QFont>()); + + QFont f; + settings->beginGroup(QStringLiteral("Font")); + readValue(settings, QStringLiteral("Family"), [&f](const QVariant &var) { f.setFamily(var.toString()); }); + readValue(settings, QStringLiteral("PointSize"), [&f](const QVariant &var) { f.setPointSizeF(var.toReal()); }); + readValue(settings, QStringLiteral("PixelSize"), [&f](const QVariant &var) { f.setPixelSize(var.toInt()); }); + readValue(settings, QStringLiteral("StyleHint"), [&f](const QVariant &var) { f.setStyleHint(toEnumValue<QFont::StyleHint>(var.toInt())); }); + readValue(settings, QStringLiteral("Weight"), [&f](const QVariant &var) { f.setWeight(toEnumValue<QFont::Weight>(var)); }); + readValue(settings, QStringLiteral("Style"), [&f](const QVariant &var) { f.setStyle(toEnumValue<QFont::Style>(var.toInt())); }); + settings->endGroup(); + return new QFont(f); +} +#endif // QT_CONFIG(settings) + +QQuickTheme::QQuickTheme(const QString &style) + : QQuickProxyTheme() +{ +#if QT_CONFIG(settings) + QSharedPointer<QSettings> settings = QQuickStylePrivate::settings(style); + if (settings) + m_styleFont.reset(readFont(settings)); +#endif +} + +const QFont *QQuickTheme::font(Font type) const +{ + Q_UNUSED(type); + return m_styleFont.data(); +} + +QFont QQuickTheme::resolveFont(const QFont &font) const +{ + if (!m_styleFont) + return font; + + return m_styleFont->resolve(font); +} + +QT_END_NAMESPACE diff --git a/src/quickcontrols2/qquicktheme_p.h b/src/quickcontrols2/qquicktheme_p.h new file mode 100644 index 00000000..7ddd1d1d --- /dev/null +++ b/src/quickcontrols2/qquicktheme_p.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKTHEME_P_H +#define QQUICKTHEME_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtQuickControls2/private/qquickproxytheme_p.h> +#include <QtCore/qscopedpointer.h> +#include <QtGui/qfont.h> + +QT_BEGIN_NAMESPACE + +class Q_QUICKCONTROLS2_PRIVATE_EXPORT QQuickTheme : public QQuickProxyTheme +{ +public: + QQuickTheme(const QString &name); + + const QFont *font(Font type = SystemFont) const override; + +protected: + QFont resolveFont(const QFont &font) const; + +private: + QScopedPointer<QFont> m_styleFont; +}; + +QT_END_NAMESPACE + +#endif // QQUICKTHEME_P_H diff --git a/src/quickcontrols2/quickcontrols2.pri b/src/quickcontrols2/quickcontrols2.pri index bf96607b..31b8e66f 100644 --- a/src/quickcontrols2/quickcontrols2.pri +++ b/src/quickcontrols2/quickcontrols2.pri @@ -14,7 +14,8 @@ HEADERS += \ $$PWD/qquickstyle_p.h \ $$PWD/qquickstyleplugin_p.h \ $$PWD/qquickstyleselector_p.h \ - $$PWD/qquickstyleselector_p_p.h + $$PWD/qquickstyleselector_p_p.h \ + $$PWD/qquicktheme_p.h SOURCES += \ $$PWD/qquickanimatednode.cpp \ @@ -28,7 +29,8 @@ SOURCES += \ $$PWD/qquickproxytheme.cpp \ $$PWD/qquickstyle.cpp \ $$PWD/qquickstyleplugin.cpp \ - $$PWD/qquickstyleselector.cpp + $$PWD/qquickstyleselector.cpp \ + $$PWD/qquicktheme.cpp qtConfig(quick-listview):qtConfig(quick-pathview) { HEADERS += \ diff --git a/tests/auto/qquickmaterialstyleconf/qtquickcontrols2.conf b/tests/auto/qquickmaterialstyleconf/qtquickcontrols2.conf index 78634834..30cee878 100644 --- a/tests/auto/qquickmaterialstyleconf/qtquickcontrols2.conf +++ b/tests/auto/qquickmaterialstyleconf/qtquickcontrols2.conf @@ -4,3 +4,7 @@ Style=Material [Material] Background=#444444 Foreground=Red +Font\PixelSize=22 + +[Material\Font] +Family=Courier diff --git a/tests/auto/qquickmaterialstyleconf/tst_qquickmaterialstyleconf.cpp b/tests/auto/qquickmaterialstyleconf/tst_qquickmaterialstyleconf.cpp index cc81afea..17d1ea6d 100644 --- a/tests/auto/qquickmaterialstyleconf/tst_qquickmaterialstyleconf.cpp +++ b/tests/auto/qquickmaterialstyleconf/tst_qquickmaterialstyleconf.cpp @@ -55,16 +55,22 @@ void tst_qquickmaterialstyleconf::conf() { QQuickApplicationHelper helper(this, QLatin1String("applicationwindow.qml")); + QFont customFont; + customFont.setFamily("Courier"); + customFont.setPixelSize(22); + QQuickApplicationWindow *window = helper.appWindow; window->show(); QVERIFY(QTest::qWaitForWindowExposed(window)); - // We specified a custom background color, so the window should have it. + // We specified a custom background color and font, so the window should have them. QCOMPARE(window->property("color").value<QColor>(), QColor("#444444")); + QCOMPARE(window->property("font").value<QFont>(), customFont); - // We specified a custom foreground color, so the label should have it. + // We specified a custom foreground color and font, so the label should have them. QQuickItem *label = window->property("label").value<QQuickItem*>(); QVERIFY(label); QCOMPARE(label->property("color").value<QColor>(), QColor("#F44336")); + QCOMPARE(label->property("font").value<QFont>(), customFont); } QTEST_MAIN(tst_qquickmaterialstyleconf) diff --git a/tests/auto/qquickuniversalstyleconf/qtquickcontrols2.conf b/tests/auto/qquickuniversalstyleconf/qtquickcontrols2.conf index 836372c9..0ec13258 100644 --- a/tests/auto/qquickuniversalstyleconf/qtquickcontrols2.conf +++ b/tests/auto/qquickuniversalstyleconf/qtquickcontrols2.conf @@ -4,3 +4,7 @@ Style=Universal [Universal] Background=#444444 Foreground=Red +Font\PixelSize=22 + +[Universal\Font] +Family=Courier diff --git a/tests/auto/qquickuniversalstyleconf/tst_qquickuniversalstyleconf.cpp b/tests/auto/qquickuniversalstyleconf/tst_qquickuniversalstyleconf.cpp index c6b28be3..51cc5883 100644 --- a/tests/auto/qquickuniversalstyleconf/tst_qquickuniversalstyleconf.cpp +++ b/tests/auto/qquickuniversalstyleconf/tst_qquickuniversalstyleconf.cpp @@ -55,16 +55,22 @@ void tst_qquickuniversalstyleconf::conf() { QQuickApplicationHelper helper(this, QLatin1String("applicationwindow.qml")); + QFont customFont; + customFont.setFamily("Courier"); + customFont.setPixelSize(22); + QQuickApplicationWindow *window = helper.appWindow; window->show(); QVERIFY(QTest::qWaitForWindowExposed(window)); - // We specified a custom background color, so the window should have it. + // We specified a custom background color and font, so the window should have them. QCOMPARE(window->property("color").value<QColor>(), QColor("#444444")); + QCOMPARE(window->property("font").value<QFont>(), customFont); - // We specified a custom foreground color, so the label should have it. + // We specified a custom foreground color and font, so the label should have them. QQuickItem *label = window->property("label").value<QQuickItem*>(); QVERIFY(label); QCOMPARE(label->property("color").value<QColor>(), QColor("#E51400")); + QCOMPARE(label->property("font").value<QFont>(), customFont); } QTEST_MAIN(tst_qquickuniversalstyleconf) |