From c94e78fdc4b0b6f1be79074e2ddfe8fd5a937dd8 Mon Sep 17 00:00:00 2001 From: RickyRister Date: Sat, 1 Feb 2025 17:18:41 -0800 Subject: [PATCH] refactor generateIcon --- .../src/client/ui/pixel_map_generator.cpp | 51 ++++++++++++------- .../src/client/ui/pixel_map_generator.h | 9 +++- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/cockatrice/src/client/ui/pixel_map_generator.cpp b/cockatrice/src/client/ui/pixel_map_generator.cpp index 37bc3e4f1..4f51b3fd8 100644 --- a/cockatrice/src/client/ui/pixel_map_generator.cpp +++ b/cockatrice/src/client/ui/pixel_map_generator.cpp @@ -147,9 +147,9 @@ QIcon changeSVGColor(const QString &iconPath, const QString &colorLeft, const st auto docElem = doc.documentElement(); - SetAttrRecur(docElem, "path", "fill", "left", colorLeft); + setAttrRecur(docElem, "path", "fill", "left", colorLeft); if (colorRight.has_value()) { - SetAttrRecur(docElem, "path", "fill", "right", colorRight.value()); + setAttrRecur(docElem, "path", "fill", "right", colorRight.value()); } QSvgRenderer svgRenderer(doc.toByteArray()); @@ -168,7 +168,7 @@ QPixmap UserLevelPixmapGenerator::generatePixmap(int height, UserLevelFlags userLevel, ServerInfo_User::PawnColorsOverride pawnColorsOverride, bool isBuddy, - QString privLevel) + const QString &privLevel) { return generateIcon(height, userLevel, pawnColorsOverride, isBuddy, privLevel).pixmap(height, height); } @@ -177,7 +177,7 @@ QIcon UserLevelPixmapGenerator::generateIcon(int height, UserLevelFlags userLevel, ServerInfo_User::PawnColorsOverride pawnColorsOverride, bool isBuddy, - QString privLevel) + const QString &privLevel) { std::optional colorLeft = std::nullopt; if (pawnColorsOverride.has_left_side()) { @@ -191,23 +191,18 @@ QIcon UserLevelPixmapGenerator::generateIcon(int height, // Has Color Override if (colorLeft.has_value()) { - QString key = QString::number(height * 10000) + ":" + colorLeft.value_or("") + ":" + colorRight.value_or(""); - if (iconCache.contains(key)) { - return iconCache.value(key); - } - - QIcon icon; - if (colorRight.has_value()) { - icon = changeSVGColor("theme:usericons/pawn_double.svg", colorLeft.value(), colorRight); - } else { - icon = changeSVGColor("theme:usericons/pawn_single.svg", colorLeft.value(), colorRight); - } - - iconCache.insert(key, icon); - return icon; + return generateIconWithColorOverride(height, colorLeft, colorRight); } // Has No Color Override + return generateIconDefault(height, userLevel, isBuddy, privLevel); +} + +QIcon UserLevelPixmapGenerator::generateIconDefault(int height, + UserLevelFlags userLevel, + bool isBuddy, + const QString &privLevel) +{ QString key = QString::number(height * 10000) + ":" + (short)userLevel + ":" + (short)isBuddy + ":" + privLevel; if (iconCache.contains(key)) { return iconCache.value(key); @@ -244,6 +239,26 @@ QIcon UserLevelPixmapGenerator::generateIcon(int height, return icon; } +QIcon UserLevelPixmapGenerator::generateIconWithColorOverride(int height, + std::optional colorLeft, + std::optional colorRight) +{ + QString key = QString::number(height * 10000) + ":" + colorLeft.value_or("") + ":" + colorRight.value_or(""); + if (iconCache.contains(key)) { + return iconCache.value(key); + } + + QIcon icon; + if (colorRight.has_value()) { + icon = changeSVGColor("theme:usericons/pawn_double.svg", colorLeft.value(), colorRight); + } else { + icon = changeSVGColor("theme:usericons/pawn_single.svg", colorLeft.value(), colorRight); + } + + iconCache.insert(key, icon); + return icon; +} + QMap UserLevelPixmapGenerator::iconCache; QPixmap LockPixmapGenerator::generatePixmap(int height) diff --git a/cockatrice/src/client/ui/pixel_map_generator.h b/cockatrice/src/client/ui/pixel_map_generator.h index d1297cded..4a385d372 100644 --- a/cockatrice/src/client/ui/pixel_map_generator.h +++ b/cockatrice/src/client/ui/pixel_map_generator.h @@ -64,17 +64,22 @@ class UserLevelPixmapGenerator private: static QMap iconCache; + static QIcon generateIconDefault(int height, UserLevelFlags userLevel, bool isBuddy, const QString &privLevel); + static QIcon + generateIconWithColorOverride(int height, std::optional colorLeft, std::optional colorRight); + public: static QPixmap generatePixmap(int height, UserLevelFlags userLevel, ServerInfo_User::PawnColorsOverride pawnColors, bool isBuddy, - QString privLevel = "NONE"); + const QString &privLevel = "NONE"); + static QIcon generateIcon(int height, UserLevelFlags userLevel, ServerInfo_User::PawnColorsOverride pawnColors, bool isBuddy, - QString privLevel = "NONE"); + const QString &privLevel = "NONE"); static void clear() { iconCache.clear();