From 9d6a1abd3954eea717ad2e17c8f6a4cdcca750f1 Mon Sep 17 00:00:00 2001 From: luisangelsm Date: Wed, 4 Feb 2026 18:51:28 +0100 Subject: [PATCH] Improve icon utils by supporting explicit file name destination Useful for using svg templates that can generate multiple icons, e.g. labels --- YACReader/themes/theme_factory.cpp | 8 ++++---- common/themes/icon_utils.cpp | 24 ++++++++++++------------ common/themes/icon_utils.h | 13 +++++++++---- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/YACReader/themes/theme_factory.cpp b/YACReader/themes/theme_factory.cpp index d0ecc7d9..3f48955a 100644 --- a/YACReader/themes/theme_factory.cpp +++ b/YACReader/themes/theme_factory.cpp @@ -66,11 +66,11 @@ void setToolbarIconPair(QIcon &icon, const QString normalPath = recoloredSvgToThemeFile(basePath, iconColor, themeName); const QString normalPath18 = recoloredSvgToThemeFile(path18, iconColor, themeName); // Disabled - const QString disabledPath = recoloredSvgToThemeFile(basePath, disabledColor, themeName, "_disabled"); - const QString disabledPath18 = recoloredSvgToThemeFile(path18, disabledColor, themeName, "_disabled"); + const QString disabledPath = recoloredSvgToThemeFile(basePath, disabledColor, themeName, { .suffix = "_disabled" }); + const QString disabledPath18 = recoloredSvgToThemeFile(path18, disabledColor, themeName, { .suffix = "_disabled" }); // Checked (On state) - const QString checkedPath = recoloredSvgToThemeFile(basePath, checkedColor, themeName, "_checked"); - const QString checkedPath18 = recoloredSvgToThemeFile(path18, checkedColor, themeName, "_checked"); + const QString checkedPath = recoloredSvgToThemeFile(basePath, checkedColor, themeName, { .suffix = "_checked" }); + const QString checkedPath18 = recoloredSvgToThemeFile(path18, checkedColor, themeName, { .suffix = "_checked" }); icon.addFile(normalPath, QSize(), QIcon::Normal, QIcon::Off); icon.addFile(disabledPath, QSize(), QIcon::Disabled, QIcon::Off); diff --git a/common/themes/icon_utils.cpp b/common/themes/icon_utils.cpp index 0de175da..bfa0fead 100644 --- a/common/themes/icon_utils.cpp +++ b/common/themes/icon_utils.cpp @@ -16,18 +16,18 @@ QString readSvg(const QString &resourcePath) return svg; } -QString writeSvg(const QString &svg, const QString &resourcePath, const QString &themeName, const QString &suffix = QString()) +QString writeSvg(const QString &svg, const QString &resourcePath, const QString &themeName, const RecolorOptions &options) { const QString basePath = YACReader::getSettingsPath() + "/themes/" + themeName; QDir().mkpath(basePath); - QString fileName = QFileInfo(resourcePath).completeBaseName(); - if (!suffix.isEmpty()) { - fileName += suffix; + QString outFileName = options.fileName.isEmpty() ? QFileInfo(resourcePath).completeBaseName() : options.fileName; + if (!options.suffix.isEmpty()) { + outFileName += options.suffix; } - fileName += "." + QFileInfo(resourcePath).suffix(); - const QString outPath = basePath + "/" + fileName; + outFileName += "." + QFileInfo(resourcePath).suffix(); + const QString outPath = basePath + "/" + outFileName; QFile out(outPath); if (!out.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) { @@ -51,27 +51,27 @@ QString recolorSvgXML(QString &svg, QString recoloredSvgToThemeFile(const QString &resourcePath, const QColor &color, // #f0f (magenta) const QString &themeName, - const QString &suffix) + const RecolorOptions &options) { auto svg = readSvg(resourcePath); recolorSvgXML(svg, "#f0f", color); - return writeSvg(svg, resourcePath, themeName, suffix); + return writeSvg(svg, resourcePath, themeName, options); } QString recoloredSvgToThemeFile(const QString &resourcePath, const QColor &color1, // #f0f (magenta) const QColor &color2, // #0ff (cyan) const QString &themeName, - const QString &suffix) + const RecolorOptions &options) { auto svg = readSvg(resourcePath); recolorSvgXML(svg, "#f0f", color1); recolorSvgXML(svg, "#0ff", color2); - return writeSvg(svg, resourcePath, themeName, suffix); + return writeSvg(svg, resourcePath, themeName, options); } QString recoloredSvgToThemeFile(const QString &resourcePath, @@ -79,7 +79,7 @@ QString recoloredSvgToThemeFile(const QString &resourcePath, const QColor &color2, // #0ff (cyan) const QColor &color3, // #ff0 (yellow) const QString &themeName, - const QString &suffix) + const RecolorOptions &options) { auto svg = readSvg(resourcePath); @@ -87,5 +87,5 @@ QString recoloredSvgToThemeFile(const QString &resourcePath, recolorSvgXML(svg, "#0ff", color2); recolorSvgXML(svg, "#ff0", color3); - return writeSvg(svg, resourcePath, themeName, suffix); + return writeSvg(svg, resourcePath, themeName, options); } diff --git a/common/themes/icon_utils.h b/common/themes/icon_utils.h index 132c4845..8575590d 100644 --- a/common/themes/icon_utils.h +++ b/common/themes/icon_utils.h @@ -3,24 +3,29 @@ #include +struct RecolorOptions { + QString suffix; + QString fileName; +}; + QString readSvg(const QString &resourcePath); -QString writeSvg(const QString &svg, const QString &resourcePath, const QString &themeName); +QString writeSvg(const QString &svg, const QString &resourcePath, const QString &themeName, const RecolorOptions &options = {}); QString recolorSvgXML(QString &svg, const QString &placeHolder, const QColor &color); QString recoloredSvgToThemeFile(const QString &resourcePath, const QColor &color, // #f0f (magenta) const QString &themeName, - const QString &suffix = QString()); + const RecolorOptions &options = {}); QString recoloredSvgToThemeFile(const QString &resourcePath, const QColor &color1, // #f0f (magenta) const QColor &color2, // #0ff (cyan) const QString &themeName, - const QString &suffix = QString()); + const RecolorOptions &options = {}); QString recoloredSvgToThemeFile(const QString &resourcePath, const QColor &color1, // #f0f (magenta) const QColor &color2, // #0ff (cyan) const QColor &color3, // #ff0 (yellow) const QString &themeName, - const QString &suffix = QString()); + const RecolorOptions &options = {}); #endif // ICON_UTILS_H