From 691cb20d95357cdaa7d158a3c26f3ca66cdbe537 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Tue, 25 Jun 2013 19:12:08 +0200 Subject: [PATCH] Add API to let applications integrate with the system fonts This patch does 2 things mainly: - Adds a QPlatformTheme font type for fixed fonts. It's important because some OS provide specific monospaced fonts and we want to let our applications to use the preferred fonts by default. - Adds a new method and enum to QFontDatabase that expose the font types that applications might need, so that they can make the applications use the specific fonts that the system recommends. This data was already available within Qt through the QPlatformTheme, but it was not possible to use this data by Qt users. This new method exposes such data. Change-Id: Ic194c1e4bc07a70640672afd82ba756b87606985 Reviewed-by: Jerome Pasion Reviewed-by: Friedemann Kleint Reviewed-by: David Faure (KDE) --- src/gui/kernel/qplatformtheme.h | 1 + src/gui/text/qfontdatabase.cpp | 49 +++++++++++++++++++ src/gui/text/qfontdatabase.h | 10 ++++ .../themes/genericunix/qgenericunixthemes.cpp | 46 ++++++++++++----- .../platforms/cocoa/qcocoasystemsettings.mm | 4 ++ .../platforms/windows/qwindowstheme.cpp | 3 ++ tests/manual/qpainfo/main.cpp | 14 +++++- 7 files changed, 115 insertions(+), 12 deletions(-) diff --git a/src/gui/kernel/qplatformtheme.h b/src/gui/kernel/qplatformtheme.h index f974f18908..1d7a1ccffa 100644 --- a/src/gui/kernel/qplatformtheme.h +++ b/src/gui/kernel/qplatformtheme.h @@ -154,6 +154,7 @@ public: ComboLineEditFont, SmallFont, MiniFont, + FixedFont, NFonts }; diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp index 62379cd592..2e4f341e39 100644 --- a/src/gui/text/qfontdatabase.cpp +++ b/src/gui/text/qfontdatabase.cpp @@ -53,6 +53,7 @@ #include #include +#include #include #include @@ -1102,6 +1103,17 @@ QFontDatabase::QFontDatabase() \omitvalue WritingSystemsCount */ +/*! + \enum QFontDatabase::SystemFont + + \value GeneralFont The default system font. + \value FixedFont The fixed font that the system recommends. + \value TitleFont The system standard font for titles. + \value SmallestReadableFont The smallest readable system font. + + \since 5.2 +*/ + /*! Returns a sorted list of the available writing systems. This is list generated from information about all installed fonts on the @@ -2113,6 +2125,43 @@ QStringList QFontDatabase::applicationFontFamilies(int id) return privateDb()->applicationFonts.value(id).families; } +/*! + \since 5.2 + + Returns the most adequate font for a given \a type case for proper integration + with the system's look and feel. + + \sa QGuiApplication::font() +*/ + +QFont QFontDatabase::systemFont(QFontDatabase::SystemFont type) +{ + const QFont *font = 0; + if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) { + switch (type) { + case GeneralFont: + font = theme->font(QPlatformTheme::SystemFont); + break; + case FixedFont: + font = theme->font(QPlatformTheme::FixedFont); + break; + case TitleFont: + font = theme->font(QPlatformTheme::TitleBarFont); + break; + case SmallestReadableFont: + font = theme->font(QPlatformTheme::MiniFont); + break; + } + } + + if (font) + return *font; + else if (QPlatformIntegration *integration = QGuiApplicationPrivate::platformIntegration()) + return integration->fontDatabase()->defaultFont(); + else + return QFont(); +} + /*! \fn bool QFontDatabase::removeApplicationFont(int id) \since 4.2 diff --git a/src/gui/text/qfontdatabase.h b/src/gui/text/qfontdatabase.h index 05f1a85f24..bd603c3c4a 100644 --- a/src/gui/text/qfontdatabase.h +++ b/src/gui/text/qfontdatabase.h @@ -60,6 +60,7 @@ class Q_GUI_EXPORT QFontDatabase { Q_GADGET Q_ENUMS(WritingSystem) + Q_ENUMS(SystemFont) public: // do not re-order or delete entries from this enum without updating the // QPF2 format and makeqpf!! @@ -106,6 +107,13 @@ public: WritingSystemsCount }; + enum SystemFont { + GeneralFont, + FixedFont, + TitleFont, + SmallestReadableFont + }; + static QList standardSizes(); QFontDatabase(); @@ -144,6 +152,8 @@ public: static bool supportsThreadedFontRendering(); + static QFont systemFont(SystemFont type); + private: static void createDatabase(); static void parseFontName(const QString &name, QString &foundry, QString &family); diff --git a/src/platformsupport/themes/genericunix/qgenericunixthemes.cpp b/src/platformsupport/themes/genericunix/qgenericunixthemes.cpp index 21a6b050de..fb50230382 100644 --- a/src/platformsupport/themes/genericunix/qgenericunixthemes.cpp +++ b/src/platformsupport/themes/genericunix/qgenericunixthemes.cpp @@ -95,9 +95,13 @@ public: QGenericUnixThemePrivate() : QPlatformThemePrivate() , systemFont(QLatin1String(defaultSystemFontNameC), defaultSystemFontSize) - { } + , fixedFont(QStringLiteral("monospace"), systemFont.pointSize()) + { + fixedFont.setStyleHint(QFont::TypeWriter); + } const QFont systemFont; + QFont fixedFont; }; QGenericUnixTheme::QGenericUnixTheme() @@ -108,9 +112,14 @@ QGenericUnixTheme::QGenericUnixTheme() const QFont *QGenericUnixTheme::font(Font type) const { Q_D(const QGenericUnixTheme); - if (type == QPlatformTheme::SystemFont) + switch (type) { + case QPlatformTheme::SystemFont: return &d->systemFont; - return 0; + case QPlatformTheme::FixedFont: + return &d->fixedFont; + default: + return 0; + } } // Helper to return the icon theme paths from XDG. @@ -240,11 +249,18 @@ void QKdeThemePrivate::refresh() toolButtonStyle = Qt::ToolButtonTextUnderIcon; } - // Read system font, ignore 'fixed' 'smallestReadableFont' - if (QFont *systemFont = readKdeFontSetting(kdeSettings, QStringLiteral("font"))) { + // Read system font, ignore 'smallestReadableFont' + if (QFont *systemFont = readKdeFontSetting(kdeSettings, QStringLiteral("font"))) resources.fonts[QPlatformTheme::SystemFont] = systemFont; - } else { + else resources.fonts[QPlatformTheme::SystemFont] = new QFont(QLatin1String(defaultSystemFontNameC), defaultSystemFontSize); + + if (QFont *fixedFont = readKdeFontSetting(kdeSettings, QStringLiteral("fixed"))) { + resources.fonts[QPlatformTheme::FixedFont] = fixedFont; + } else { + fixedFont = new QFont(QLatin1String(defaultSystemFontNameC), defaultSystemFontSize); + fixedFont->setStyleHint(QFont::TypeWriter); + resources.fonts[QPlatformTheme::FixedFont] = fixedFont; } } @@ -461,9 +477,13 @@ class QGnomeThemePrivate : public QPlatformThemePrivate public: QGnomeThemePrivate() : systemFont(QLatin1Literal(defaultSystemFontNameC), defaultSystemFontSize) - {} + , fixedFont(QStringLiteral("monospace"), systemFont.pointSize()) + { + fixedFont.setStyleHint(QFont::TypeWriter); + } const QFont systemFont; + QFont fixedFont; }; QGnomeTheme::QGnomeTheme() @@ -501,10 +521,14 @@ QVariant QGnomeTheme::themeHint(QPlatformTheme::ThemeHint hint) const const QFont *QGnomeTheme::font(Font type) const { Q_D(const QGnomeTheme); - if (type == QPlatformTheme::SystemFont) - return &d->systemFont; - - return 0; + switch (type) { + case QPlatformTheme::SystemFont: + return &d->systemFont; + case QPlatformTheme::FixedFont: + return &d->fixedFont; + default: + return 0; + } } /*! diff --git a/src/plugins/platforms/cocoa/qcocoasystemsettings.mm b/src/plugins/platforms/cocoa/qcocoasystemsettings.mm index af817bd4c5..194394d11a 100644 --- a/src/plugins/platforms/cocoa/qcocoasystemsettings.mm +++ b/src/plugins/platforms/cocoa/qcocoasystemsettings.mm @@ -264,6 +264,10 @@ QHash qt_mac_createRoleFonts() fonts.insert(QPlatformTheme::SmallFont, qt_mac_qfontForThemeFont(kThemeSmallSystemFont)); fonts.insert(QPlatformTheme::MiniFont, qt_mac_qfontForThemeFont(kThemeMiniSystemFont)); + QFont* fixedFont = new QFont(QStringLiteral("Monaco"), fonts[QPlatformTheme::SystemFont]->pointSize()); + fixedFont->setStyleHint(QFont::TypeWriter); + fonts.insert(QPlatformTheme::FixedFont, fixedFont); + return fonts; } diff --git a/src/plugins/platforms/windows/qwindowstheme.cpp b/src/plugins/platforms/windows/qwindowstheme.cpp index 2531dc681a..83d93b08bd 100644 --- a/src/plugins/platforms/windows/qwindowstheme.cpp +++ b/src/plugins/platforms/windows/qwindowstheme.cpp @@ -404,6 +404,8 @@ void QWindowsTheme::refreshFonts() const QFont messageBoxFont = QWindowsFontDatabase::LOGFONT_to_QFont(ncm.lfMessageFont); const QFont statusFont = QWindowsFontDatabase::LOGFONT_to_QFont(ncm.lfStatusFont); const QFont titleFont = QWindowsFontDatabase::LOGFONT_to_QFont(ncm.lfCaptionFont); + QFont fixedFont(QStringLiteral("Courier New"), messageBoxFont.pointSize()); + fixedFont.setStyleHint(QFont::TypeWriter); LOGFONT lfIconTitleFont; SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lfIconTitleFont), &lfIconTitleFont, 0); @@ -418,6 +420,7 @@ void QWindowsTheme::refreshFonts() m_fonts[MdiSubWindowTitleFont] = new QFont(titleFont); m_fonts[DockWidgetTitleFont] = new QFont(titleFont); m_fonts[ItemViewFont] = new QFont(iconTitleFont); + m_fonts[FixedFont] = new QFont(fixedFont); if (QWindowsContext::verboseTheming) qDebug() << __FUNCTION__ << '\n' diff --git a/tests/manual/qpainfo/main.cpp b/tests/manual/qpainfo/main.cpp index 6b712304a0..0f5119bab3 100644 --- a/tests/manual/qpainfo/main.cpp +++ b/tests/manual/qpainfo/main.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -85,6 +86,12 @@ std::ostream &operator<<(std::ostream &str, const QStringList &l) return str; } +std::ostream &operator<<(std::ostream &str, const QFont &f) +{ + std::cout << '"' << f.family().toStdString() << "\" " << f.pointSize(); + return str; +} + static QStringList toNativeSeparators(QStringList in) { for (int i = 0; i < in.size(); ++i) @@ -184,7 +191,12 @@ int main(int argc, char **argv) << " from " << platformTheme->themeHint(QPlatformTheme::IconThemeSearchPaths).toStringList() << '\n'; } if (const QFont *systemFont = platformTheme->font()) - std::cout << " System font: \"" << systemFont->family().toStdString() << "\" " << systemFont->pointSize() << '\n'; + std::cout << " System font: " << *systemFont<< '\n'; + std::cout << " General font : " << QFontDatabase::systemFont(QFontDatabase::GeneralFont) << '\n' + << " Fixed font : " << QFontDatabase::systemFont(QFontDatabase::FixedFont) << '\n' + << " Title font : " << QFontDatabase::systemFont(QFontDatabase::TitleFont) << '\n' + << " Smallest font: " << QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont) << "\n\n"; + if (platformTheme->usePlatformNativeDialog(QPlatformTheme::FileDialog)) std::cout << " Native file dialog\n"; if (platformTheme->usePlatformNativeDialog(QPlatformTheme::ColorDialog))