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 <jerome.pasion@digia.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: David Faure (KDE) <faure@kde.org>
This commit is contained in:
Aleix Pol 2013-06-25 19:12:08 +02:00 committed by The Qt Project
parent ea7da7c241
commit 691cb20d95
7 changed files with 115 additions and 12 deletions

View File

@ -154,6 +154,7 @@ public:
ComboLineEditFont,
SmallFont,
MiniFont,
FixedFont,
NFonts
};

View File

@ -53,6 +53,7 @@
#include <QtGui/private/qguiapplication_p.h>
#include <qpa/qplatformfontdatabase.h>
#include <qpa/qplatformtheme.h>
#include <stdlib.h>
#include <limits.h>
@ -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

View File

@ -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<int> 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);

View File

@ -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;
}
}
/*!

View File

@ -264,6 +264,10 @@ QHash<QPlatformTheme::Font, QFont *> 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;
}

View File

@ -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'

View File

@ -49,6 +49,7 @@
#include <QStringList>
#include <QVariant>
#include <QFont>
#include <QFontDatabase>
#include <QSysInfo>
#include <QLibraryInfo>
#include <QStandardPaths>
@ -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))