Consult QIcon::fallbackThemeName() even for themes with explicit parents

We would previously only use the fallback theme for themes that did
not exist, or for themes that did not declare any parent theme.

We now unconditionally use the fallback theme, even for themes that
declare their own parent themes, so that a QIcon::fromTheme("foo")
that doesn't exist in the current theme, nor any of its parents,
nor in "hicolor", will still be looked up in the fallback theme.

The reason this seemed to work in the existing tests was because
our test themes inherit system themes such as crystalsvg and gnome,
and we didn't provide a hicolor theme. Any of these themes missing
would lead us into the code path where we use the fallback theme
for a missing theme, masking that fact that we had not added the
fallback theme to the list of fallbacks for the theme that had
explicit parents declared.

The logic has been moved out of the theme parsing and into an
accessor in QIconTheme, so that we're not caching the fallback
theme lookup.

[ChangeLog][QtGui][QIcon] QIcon::fallbackThemeName() will now be
used as fallback even for themes that declare a parent theme.

Pick-to: 6.5 6.6
Change-Id: Ib0ce1dfe97030f23893460ed624073a719a3ebd1
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Tor Arne Vestbø 2023-05-10 10:25:02 +02:00
parent ae36c1dc9c
commit f4dca7c512
8 changed files with 36 additions and 14 deletions

View File

@ -1233,6 +1233,10 @@ QString QIcon::fallbackThemeName()
Sets the fallback icon theme to \a name.
The fallback icon theme is used for last resort lookup of icons
not provided by the \l{themeName()}{current icon theme},
or if the \l{themeName()}{current icon theme} does not exist.
The \a name should correspond to a directory name in the
themeSearchPath() containing an index.theme
file describing its contents.

View File

@ -394,21 +394,27 @@ QIconTheme::QIconTheme(const QString &themeName)
// Parent themes provide fallbacks for missing icons
m_parents = indexReader.value("Icon Theme/Inherits"_L1).toStringList();
m_parents.removeAll(QString());
// Ensure a default platform fallback for all themes
if (m_parents.isEmpty()) {
const QString fallback = QIconLoader::instance()->fallbackThemeName();
if (!fallback.isEmpty())
m_parents.append(fallback);
}
// Ensure that all themes fall back to hicolor
if (!m_parents.contains("hicolor"_L1))
m_parents.append("hicolor"_L1);
}
#endif // settings
}
QStringList QIconTheme::parents() const
{
// Respect explicitly declared parents
QStringList result = m_parents;
// Ensure a default fallback for all themes
const QString fallback = QIconLoader::instance()->fallbackThemeName();
if (!fallback.isEmpty())
result.append(fallback);
// Ensure that all themes fall back to hicolor
if (!result.contains("hicolor"_L1))
result.append("hicolor"_L1);
return result;
}
QDebug operator<<(QDebug debug, const std::unique_ptr<QIconLoaderEngineEntry> &entry)
{
QDebugStateSaver saver(debug);

View File

@ -145,7 +145,7 @@ class QIconTheme
public:
QIconTheme(const QString &name);
QIconTheme() : m_valid(false) {}
QStringList parents() { return m_parents; }
QStringList parents() const;
QList<QIconDirInfo> keyList() { return m_keyList; }
QStringList contentDirs() { return m_contentDirs; }
bool isValid() { return m_valid; }

View File

@ -58,6 +58,8 @@ set(tst_qicon_resource_files
"./icons/themeparent/scalable/actions/appointment-new.svg"
"./icons/fallbacktheme/index.theme"
"./icons/fallbacktheme/16x16/edit-cut.png"
"./icons/hicolor/index.theme"
"./icons/hicolor/16x16/hicolor-icon.png"
"./second_icons/testtheme/32x32/actions/appointment-new.png"
"./styles/commonstyle/images/standardbutton-open-128.png"
"./styles/commonstyle/images/standardbutton-open-16.png"

Binary file not shown.

After

Width:  |  Height:  |  Size: 267 B

View File

@ -0,0 +1,11 @@
[Icon Theme]
Name=hicolor
# Provide a minimal hicolor theme, so that our hicolor fallback
# lookup during testing will find that theme on all systems.
Directories=16x16
[16x16]
Size=16
Type=Fixed

View File

@ -1,7 +1,7 @@
[Icon Theme]
_Name=Test
_Comment=Test Theme
Inherits=crystalsvg, themeparent
Inherits=themeparent
Example=x-directory-normal
# KDE Specific Stuff

View File

@ -1,7 +1,6 @@
[Icon Theme]
_Name=Test
_Comment=Test Theme
Inherits=gnome,crystalsvg
Example=x-directory-normal
# KDE Specific Stuff