Allow setting the QIcon fallback theme

This allows apps that know their icons are
in a given icon theme to set that theme as
fallback, so in case the user theme does
not include them, the icons can still be found.

This solves the problem of missing icons
that often happens when running KDE applications
on a GNOME desktop.

Change-Id: I4e5543d598012352a29ff79dab0357506d986b6d
Reviewed-by: Dominik Haumann <dhaumann@kde.org>
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
bb10
Albert Astals Cid 2018-08-16 15:42:41 +02:00
parent 67cc8fea10
commit 0e7724079f
4 changed files with 53 additions and 6 deletions

View File

@ -1194,7 +1194,7 @@ void QIcon::setFallbackSearchPaths(const QStringList &paths)
The \a name should correspond to a directory name in the
themeSearchPath() containing an index.theme
file describing it's contents.
file describing its contents.
\sa themeSearchPaths(), themeName()
*/
@ -1219,6 +1219,37 @@ QString QIcon::themeName()
return QIconLoader::instance()->themeName();
}
/*!
\since 5.12
Returns the name of the fallback icon theme.
On X11, if not set, the fallback icon theme depends on your desktop
settings. On other platforms it is not set by default.
\sa setFallbackThemeName(), themeName()
*/
QString QIcon::fallbackThemeName()
{
return QIconLoader::instance()->fallbackThemeName();
}
/*!
\since 5.12
Sets the fallback icon theme to \a name.
The \a name should correspond to a directory name in the
themeSearchPath() containing an index.theme
file describing its contents.
\sa fallbackThemeName(), themeSearchPaths(), themeName()
*/
void QIcon::setFallbackThemeName(const QString &name)
{
QIconLoader::instance()->setFallbackThemeName(name);
}
/*!
\since 4.6

View File

@ -124,6 +124,9 @@ public:
static QString themeName();
static void setThemeName(const QString &path);
static QString fallbackThemeName();
static void setFallbackThemeName(const QString &name);
Q_DUMMY_COMPARISON_OPERATOR(QIcon)
private:

View File

@ -60,7 +60,7 @@ QT_BEGIN_NAMESPACE
Q_GLOBAL_STATIC(QIconLoader, iconLoaderInstance)
/* Theme to use in last resort, if the theme does not have the icon, neither the parents */
static QString fallbackTheme()
static QString systemFallbackThemeName()
{
if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
const QVariant themeHint = theme->themeHint(QPlatformTheme::SystemIconFallbackThemeName);
@ -117,7 +117,7 @@ void QIconLoader::ensureInitialized()
m_systemTheme = systemThemeName();
if (m_systemTheme.isEmpty())
m_systemTheme = fallbackTheme();
m_systemTheme = systemFallbackThemeName();
if (qt_iconEngineFactoryLoader()->keyMap().key(QLatin1String("svg"), -1) != -1)
m_supportsSvg = true;
}
@ -137,7 +137,7 @@ void QIconLoader::updateSystemTheme()
if (m_userTheme.isEmpty()) {
QString theme = systemThemeName();
if (theme.isEmpty())
theme = fallbackTheme();
theme = fallbackThemeName();
if (theme != m_systemTheme) {
m_systemTheme = theme;
invalidateKey();
@ -151,6 +151,16 @@ void QIconLoader::setThemeName(const QString &themeName)
invalidateKey();
}
QString QIconLoader::fallbackThemeName() const
{
return m_userFallbackTheme.isEmpty() ? systemFallbackThemeName() : m_userFallbackTheme;
}
void QIconLoader::setFallbackThemeName(const QString &themeName)
{
m_userFallbackTheme = themeName;
}
void QIconLoader::setThemeSearchPath(const QStringList &searchPaths)
{
m_iconDirs = searchPaths;
@ -388,7 +398,7 @@ QIconTheme::QIconTheme(const QString &themeName)
// Ensure a default platform fallback for all themes
if (m_parents.isEmpty()) {
const QString fallback = fallbackTheme();
const QString fallback = QIconLoader::instance()->fallbackThemeName();
if (!fallback.isEmpty())
m_parents.append(fallback);
}
@ -414,7 +424,7 @@ QThemeIconInfo QIconLoader::findIconHelper(const QString &themeName,
if (!theme.isValid()) {
theme = QIconTheme(themeName);
if (!theme.isValid())
theme = QIconTheme(fallbackTheme());
theme = QIconTheme(fallbackThemeName());
}
const QStringList contentDirs = theme.contentDirs();

View File

@ -177,6 +177,8 @@ public:
QString themeName() const { return m_userTheme.isEmpty() ? m_systemTheme : m_userTheme; }
void setThemeName(const QString &themeName);
QString fallbackThemeName() const;
void setFallbackThemeName(const QString &themeName);
QIconTheme theme() { return themeList.value(themeName()); }
void setThemeSearchPath(const QStringList &searchPaths);
QStringList themeSearchPaths() const;
@ -200,6 +202,7 @@ private:
bool m_initialized;
mutable QString m_userTheme;
mutable QString m_userFallbackTheme;
mutable QString m_systemTheme;
mutable QStringList m_iconDirs;
mutable QHash <QString, QIconTheme> themeList;