From 83cabda862dced9477d155c84df9440047c856cf Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 7 Mar 2012 08:56:41 +0100 Subject: Add fonts to QPlatformTheme. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove QPlatformFontDatabase::defaultFonts() returning a hash containing widget name ->font and the Windows implementation. - Add enumeration and font accessor to QPlatformTheme. The value returned for the enumeration value overwrites the default font of the font database. - Implement for Windows, Mac and KDE. - Add more Windows palettes. Task-number: QTBUG-23686 Change-Id: I8a2abdfd216df23daa7c9630c54264cdf61295db Reviewed-by: Morten Johan Sørvig --- .../themes/genericunix/qgenericunixthemes.cpp | 51 +++++++++++++++++++--- .../themes/genericunix/qgenericunixthemes_p.h | 23 ++++++++-- 2 files changed, 64 insertions(+), 10 deletions(-) (limited to 'src/platformsupport') diff --git a/src/platformsupport/themes/genericunix/qgenericunixthemes.cpp b/src/platformsupport/themes/genericunix/qgenericunixthemes.cpp index 257fe9156f..602dd6264a 100644 --- a/src/platformsupport/themes/genericunix/qgenericunixthemes.cpp +++ b/src/platformsupport/themes/genericunix/qgenericunixthemes.cpp @@ -43,6 +43,7 @@ #include "../../services/genericunix/qgenericunixservices_p.h" #include +#include #include #include #include @@ -54,6 +55,20 @@ QT_BEGIN_NAMESPACE +ResourceHelper::ResourceHelper() +{ + qFill(palettes, palettes + QPlatformTheme::NPalettes, static_cast(0)); + qFill(fonts, fonts + QPlatformTheme::NFonts, static_cast(0)); +} + +void ResourceHelper::clear() +{ + qDeleteAll(palettes, palettes + QPlatformTheme::NPalettes); + qDeleteAll(fonts, fonts + QPlatformTheme::NFonts); + qFill(palettes, palettes + QPlatformTheme::NPalettes, static_cast(0)); + qFill(fonts, fonts + QPlatformTheme::NFonts, static_cast(0)); +} + /*! \class QGenericX11ThemeQKdeTheme \brief QGenericX11Theme is a generic theme implementation for X11. @@ -148,19 +163,40 @@ QKdeTheme::QKdeTheme(const QString &kdeHome, int kdeVersion) : m_kdeHome(kdeHome), m_kdeVersion(kdeVersion), m_toolButtonStyle(Qt::ToolButtonTextBesideIcon), m_toolBarIconSize(0) { - qFill(m_palettes, m_palettes + NPalettes, static_cast(0)); refresh(); } -void QKdeTheme::clearPalettes() +static inline QFont *readKdeFontSetting(const QSettings &settings, const QString &key) { - qDeleteAll(m_palettes, m_palettes + NPalettes); - qFill(m_palettes, m_palettes + NPalettes, static_cast(0)); + const QVariant fontValue = settings.value(key); + if (fontValue.isValid()) { + // Read font value: Might be a QStringList as KDE stores fonts without quotes. + // Also retrieve the family for the constructor since we cannot use the + // default constructor of QFont, which accesses QGuiApplication::systemFont() + // causing recursion. + QString fontDescription; + QString fontFamily; + if (fontValue.type() == QVariant::StringList) { + const QStringList list = fontValue.toStringList(); + if (!list.isEmpty()) { + fontFamily = list.first(); + fontDescription = list.join(QStringLiteral(",")); + } + } else { + fontDescription = fontFamily = fontValue.toString(); + } + if (!fontDescription.isEmpty()) { + QFont font(fontFamily); + if (font.fromString(fontDescription)) + return new QFont(font); + } + } + return 0; } void QKdeTheme::refresh() { - clearPalettes(); + m_resources.clear(); m_toolButtonStyle = Qt::ToolButtonTextBesideIcon; m_toolBarIconSize = 0; @@ -177,7 +213,7 @@ void QKdeTheme::refresh() QPalette systemPalette; if (readKdeSystemPalette(kdeSettings, &systemPalette)) - m_palettes[SystemPalette] = new QPalette(systemPalette); + m_resources.palettes[SystemPalette] = new QPalette(systemPalette); //## TODO tooltip color const QVariant styleValue = kdeSettings.value(QStringLiteral("widgetStyle")); @@ -205,6 +241,9 @@ void QKdeTheme::refresh() else if (toolBarStyle == QStringLiteral("TextUnderIcon")) m_toolButtonStyle = Qt::ToolButtonTextUnderIcon; } + + // Read system font, ignore 'fixed' 'smallestReadableFont' + m_resources.fonts[SystemFont] = readKdeFontSetting(kdeSettings, QStringLiteral("font")); } QString QKdeTheme::globalSettingsFile() const diff --git a/src/platformsupport/themes/genericunix/qgenericunixthemes_p.h b/src/platformsupport/themes/genericunix/qgenericunixthemes_p.h index 12937a205f..a9db29e8cd 100644 --- a/src/platformsupport/themes/genericunix/qgenericunixthemes_p.h +++ b/src/platformsupport/themes/genericunix/qgenericunixthemes_p.h @@ -50,6 +50,18 @@ QT_BEGIN_HEADER QT_BEGIN_NAMESPACE +class ResourceHelper +{ +public: + ResourceHelper(); + ~ResourceHelper() { clear(); } + + void clear(); + + QPalette *palettes[QPlatformTheme::NPalettes]; + QFont *fonts[QPlatformTheme::NFonts]; +}; + class QGenericUnixTheme : public QPlatformTheme { public: @@ -66,21 +78,24 @@ class QKdeTheme : public QPlatformTheme { QKdeTheme(const QString &kdeHome, int kdeVersion); public: - ~QKdeTheme() { clearPalettes(); } static QPlatformTheme *createKdeTheme(); virtual QVariant themeHint(ThemeHint hint) const; + virtual const QPalette *palette(Palette type = SystemPalette) const - { return m_palettes[type]; } + { return m_resources.palettes[type]; } + + virtual const QFont *font(Font type) const + { return m_resources.fonts[type]; } private: QString globalSettingsFile() const; - void clearPalettes(); void refresh(); const QString m_kdeHome; const int m_kdeVersion; - QPalette *m_palettes[NPalettes]; + + ResourceHelper m_resources; QString m_iconThemeName; QString m_iconFallbackThemeName; QStringList m_styleNames; -- cgit v1.2.3