From 67091fff9cf892239352c19892c9dd49ba37b75e Mon Sep 17 00:00:00 2001 From: Zach H Date: Thu, 6 Feb 2025 08:40:10 -0500 Subject: [PATCH] Fixup custom theme image loading (#5568) --- .../src/client/ui/pixel_map_generator.cpp | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/cockatrice/src/client/ui/pixel_map_generator.cpp b/cockatrice/src/client/ui/pixel_map_generator.cpp index 187068aa3..c7303cd37 100644 --- a/cockatrice/src/client/ui/pixel_map_generator.cpp +++ b/cockatrice/src/client/ui/pixel_map_generator.cpp @@ -18,7 +18,7 @@ /** * Loads in an svg from file and scales it without affecting image quality. * - * @param svgPath The path to the svg file. Automatically appends ".svg" to the end if not present + * @param svgPath The path to the svg file, with file extension. * @param size The desired size of the pixmap. * @param expandOnly If true, then keep the size of the initial pixmap to at least the svg size. * @@ -26,15 +26,10 @@ */ static QPixmap loadSvg(const QString &svgPath, const QSize &size, bool expandOnly = false) { - QString path = svgPath; - if (!path.endsWith(".svg")) { - path += ".svg"; - } - - QSvgRenderer svgRenderer(path); + QSvgRenderer svgRenderer(svgPath); if (!svgRenderer.isValid()) { - qCWarning(PixelMapGeneratorLog) << "Failed to load" << path; + qCWarning(PixelMapGeneratorLog) << "Failed to load" << svgPath; return {}; } @@ -55,6 +50,29 @@ static QPixmap loadSvg(const QString &svgPath, const QSize &size, bool expandOnl return pix; } +/** + * Try to load path image from non-SVG formats, otherwise fall back to SVG. + * This is to allow custom themes to support non-SVG format type overrides, since SVG requires custom loading. + * @param path The path to the file, with no file extension. File formats will be automatically detected. + * @param size The desired size of the pixmap. + * @param expandOnly If true, then keep the size of the initial pixmap to at least the size (Only relevant if SVG). + * + * @return The loaded image into a Pixmap with the given size, or an empty Pixmap if the loading failed. + */ +static QPixmap tryLoadImage(const QString &path, const QSize &size, bool expandOnly = false) +{ + const auto formats = {"png", "jpg"}; + + QPixmap returnPixmap; + for (const auto &format : formats) { + if (returnPixmap.load(path, format)) { + return returnPixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + } + + return loadSvg(path + ".svg", size, expandOnly); +} + QMap PhasePixmapGenerator::pmCache; QPixmap PhasePixmapGenerator::generatePixmap(int height, QString name) @@ -63,7 +81,7 @@ QPixmap PhasePixmapGenerator::generatePixmap(int height, QString name) if (pmCache.contains(key)) return pmCache.value(key); - QPixmap pixmap = loadSvg("theme:phases/" + name, QSize(height, height)); + QPixmap pixmap = tryLoadImage("theme:phases/" + name, QSize(height, height)); pmCache.insert(key, pixmap); return pixmap; @@ -84,14 +102,14 @@ QPixmap CounterPixmapGenerator::generatePixmap(int height, QString name, bool hi if (pmCache.contains(key)) return pmCache.value(key); - QPixmap pixmap = loadSvg("theme:counters/" + name, QSize(height, height)); + QPixmap pixmap = tryLoadImage("theme:counters/" + name, QSize(height, height)); // fall back to colorless counter if the name can't be found if (pixmap.isNull()) { name = "general"; if (highlight) name.append("_highlight"); - pixmap = loadSvg("theme:counters/" + name, QSize(height, height)); + pixmap = tryLoadImage("theme:counters/" + name, QSize(height, height)); } pmCache.insert(key, pixmap); @@ -135,7 +153,7 @@ QPixmap CountryPixmapGenerator::generatePixmap(int height, const QString &countr return pmCache.value(key); int width = height * 2; - QPixmap pixmap = loadSvg("theme:countries/" + countryCode.toLower(), QSize(width, height), true); + QPixmap pixmap = tryLoadImage("theme:countries/" + countryCode.toLower(), QSize(width, height), true); QPainter painter(&pixmap); painter.setPen(Qt::black); @@ -332,7 +350,7 @@ QPixmap LockPixmapGenerator::generatePixmap(int height) if (pmCache.contains(key)) return pmCache.value(key); - QPixmap pixmap = loadSvg("theme:icons/lock", QSize(height, height), true); + QPixmap pixmap = tryLoadImage("theme:icons/lock", QSize(height, height), true); pmCache.insert(key, pixmap); return pixmap; }