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 /src/quickcontrols2 | |
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>
Diffstat (limited to 'src/quickcontrols2')
-rw-r--r-- | src/quickcontrols2/qquicktheme.cpp | 112 | ||||
-rw-r--r-- | src/quickcontrols2/qquicktheme_p.h | 73 | ||||
-rw-r--r-- | src/quickcontrols2/quickcontrols2.pri | 6 |
3 files changed, 189 insertions, 2 deletions
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 += \ |