refactor generateIcon

This commit is contained in:
RickyRister 2025-02-01 17:18:41 -08:00
parent 58ec4c395f
commit c94e78fdc4
2 changed files with 40 additions and 20 deletions

View file

@ -147,9 +147,9 @@ QIcon changeSVGColor(const QString &iconPath, const QString &colorLeft, const st
auto docElem = doc.documentElement(); auto docElem = doc.documentElement();
SetAttrRecur(docElem, "path", "fill", "left", colorLeft); setAttrRecur(docElem, "path", "fill", "left", colorLeft);
if (colorRight.has_value()) { if (colorRight.has_value()) {
SetAttrRecur(docElem, "path", "fill", "right", colorRight.value()); setAttrRecur(docElem, "path", "fill", "right", colorRight.value());
} }
QSvgRenderer svgRenderer(doc.toByteArray()); QSvgRenderer svgRenderer(doc.toByteArray());
@ -168,7 +168,7 @@ QPixmap UserLevelPixmapGenerator::generatePixmap(int height,
UserLevelFlags userLevel, UserLevelFlags userLevel,
ServerInfo_User::PawnColorsOverride pawnColorsOverride, ServerInfo_User::PawnColorsOverride pawnColorsOverride,
bool isBuddy, bool isBuddy,
QString privLevel) const QString &privLevel)
{ {
return generateIcon(height, userLevel, pawnColorsOverride, isBuddy, privLevel).pixmap(height, height); return generateIcon(height, userLevel, pawnColorsOverride, isBuddy, privLevel).pixmap(height, height);
} }
@ -177,7 +177,7 @@ QIcon UserLevelPixmapGenerator::generateIcon(int height,
UserLevelFlags userLevel, UserLevelFlags userLevel,
ServerInfo_User::PawnColorsOverride pawnColorsOverride, ServerInfo_User::PawnColorsOverride pawnColorsOverride,
bool isBuddy, bool isBuddy,
QString privLevel) const QString &privLevel)
{ {
std::optional<QString> colorLeft = std::nullopt; std::optional<QString> colorLeft = std::nullopt;
if (pawnColorsOverride.has_left_side()) { if (pawnColorsOverride.has_left_side()) {
@ -191,23 +191,18 @@ QIcon UserLevelPixmapGenerator::generateIcon(int height,
// Has Color Override // Has Color Override
if (colorLeft.has_value()) { if (colorLeft.has_value()) {
QString key = QString::number(height * 10000) + ":" + colorLeft.value_or("") + ":" + colorRight.value_or(""); return generateIconWithColorOverride(height, colorLeft, colorRight);
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;
} }
// Has No Color Override // 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; QString key = QString::number(height * 10000) + ":" + (short)userLevel + ":" + (short)isBuddy + ":" + privLevel;
if (iconCache.contains(key)) { if (iconCache.contains(key)) {
return iconCache.value(key); return iconCache.value(key);
@ -244,6 +239,26 @@ QIcon UserLevelPixmapGenerator::generateIcon(int height,
return icon; return icon;
} }
QIcon UserLevelPixmapGenerator::generateIconWithColorOverride(int height,
std::optional<QString> colorLeft,
std::optional<QString> 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<QString, QIcon> UserLevelPixmapGenerator::iconCache; QMap<QString, QIcon> UserLevelPixmapGenerator::iconCache;
QPixmap LockPixmapGenerator::generatePixmap(int height) QPixmap LockPixmapGenerator::generatePixmap(int height)

View file

@ -64,17 +64,22 @@ class UserLevelPixmapGenerator
private: private:
static QMap<QString, QIcon> iconCache; static QMap<QString, QIcon> iconCache;
static QIcon generateIconDefault(int height, UserLevelFlags userLevel, bool isBuddy, const QString &privLevel);
static QIcon
generateIconWithColorOverride(int height, std::optional<QString> colorLeft, std::optional<QString> colorRight);
public: public:
static QPixmap generatePixmap(int height, static QPixmap generatePixmap(int height,
UserLevelFlags userLevel, UserLevelFlags userLevel,
ServerInfo_User::PawnColorsOverride pawnColors, ServerInfo_User::PawnColorsOverride pawnColors,
bool isBuddy, bool isBuddy,
QString privLevel = "NONE"); const QString &privLevel = "NONE");
static QIcon generateIcon(int height, static QIcon generateIcon(int height,
UserLevelFlags userLevel, UserLevelFlags userLevel,
ServerInfo_User::PawnColorsOverride pawnColors, ServerInfo_User::PawnColorsOverride pawnColors,
bool isBuddy, bool isBuddy,
QString privLevel = "NONE"); const QString &privLevel = "NONE");
static void clear() static void clear()
{ {
iconCache.clear(); iconCache.clear();