From 289521a47e7bd52cfe35697cd7cfdd1b7c28865f Mon Sep 17 00:00:00 2001 From: RickyRister Date: Fri, 31 Jan 2025 22:17:26 -0800 Subject: [PATCH] got something to work --- cmake/FindQtRuntime.cmake | 1 + cockatrice/resources/userlevels/base.svg | 2 +- .../src/client/ui/pixel_map_generator.cpp | 64 +++++++++++++++++-- .../src/client/ui/pixel_map_generator.h | 2 + .../src/server/user/user_list_widget.cpp | 4 +- 5 files changed, 65 insertions(+), 8 deletions(-) diff --git a/cmake/FindQtRuntime.cmake b/cmake/FindQtRuntime.cmake index 6db821dd7..3025934f4 100644 --- a/cmake/FindQtRuntime.cmake +++ b/cmake/FindQtRuntime.cmake @@ -23,6 +23,7 @@ if(WITH_CLIENT) Svg WebSockets Widgets + Xml ) endif() if(WITH_ORACLE) diff --git a/cockatrice/resources/userlevels/base.svg b/cockatrice/resources/userlevels/base.svg index f5af5499b..312255eda 100644 --- a/cockatrice/resources/userlevels/base.svg +++ b/cockatrice/resources/userlevels/base.svg @@ -292,7 +292,7 @@ id="layer1" transform="translate(0,-952.36218)"> diff --git a/cockatrice/src/client/ui/pixel_map_generator.cpp b/cockatrice/src/client/ui/pixel_map_generator.cpp index 09dc2d1ae..4564b7ad2 100644 --- a/cockatrice/src/client/ui/pixel_map_generator.cpp +++ b/cockatrice/src/client/ui/pixel_map_generator.cpp @@ -4,8 +4,12 @@ #include #include +#include +#include #include #include +#include +#include QMap PhasePixmapGenerator::pmCache; @@ -96,12 +100,61 @@ QPixmap CountryPixmapGenerator::generatePixmap(int height, const QString &countr QMap CountryPixmapGenerator::pmCache; +void SetAttrRecur(QDomElement &elem, QString tagName, QString attrName, QString attrValue) +{ + // if it has the tagname then overwritte desired attribute + if (elem.tagName().compare(tagName) == 0) { + elem.setAttribute(attrName, attrValue); + } + // loop all children + for (int i = 0; i < elem.childNodes().count(); i++) { + if (!elem.childNodes().at(i).isElement()) { + continue; + } + QDomElement docElem = elem.childNodes().at(i).toElement(); //<-- make const "variable" + SetAttrRecur(docElem, tagName, attrName, attrValue); + } +} + +QIcon changeSVGColor(const QString &iconPath, const QString &color) +{ + // open svg resource load contents to qbytearray + QFile file(iconPath); + if (!file.open(QIODevice::ReadOnly)) + return {}; + QByteArray baData = file.readAll(); + // load svg contents to xml document and edit contents + QDomDocument doc; + doc.setContent(baData); + // recurivelly change color + QDomElement docElem = doc.documentElement(); //<-- make const "variable" + qDebug() << docElem.text(); + SetAttrRecur(docElem, "path", "fill", color); + // create svg renderer with edited contents + QSvgRenderer svgRenderer(doc.toByteArray()); + // create pixmap target (could be a QImage) + QPixmap pix(svgRenderer.defaultSize()); + pix.fill(Qt::transparent); + // create painter to act over pixmap + QPainter pixPainter(&pix); + // use renderer to render over painter which paints on pixmap + svgRenderer.render(&pixPainter); + QIcon myicon(pix); + return myicon; +} + QPixmap UserLevelPixmapGenerator::generatePixmap(int height, UserLevelFlags userLevel, bool isBuddy, QString privLevel) +{ + return generateIcon(height, userLevel, isBuddy, privLevel).pixmap(QSize()); +} + +QIcon UserLevelPixmapGenerator::generateIcon(int height, UserLevelFlags userLevel, bool isBuddy, QString privLevel) { QString key = QString::number(height * 10000) + ":" + (short)userLevel + ":" + (short)isBuddy + ":" + privLevel; - if (pmCache.contains(key)) + /*if (pmCache.contains(key)) return pmCache.value(key); + */ QString levelString; if (userLevel.testFlag(ServerInfo_User::IsAdmin)) { @@ -122,11 +175,12 @@ QPixmap UserLevelPixmapGenerator::generatePixmap(int height, UserLevelFlags user if (isBuddy) levelString.append("_buddy"); - QPixmap pixmap = QPixmap("theme:userlevels/" + levelString) - .scaled(height, height, Qt::KeepAspectRatio, Qt::SmoothTransformation); + QString color = "#FF3399"; - pmCache.insert(key, pixmap); - return pixmap; + QIcon icon = + changeSVGColor("/Users/Ricky/Documents/GitHub/Cockatrice/cockatrice/resources/userlevels/base.svg", color); + + return icon; } QMap UserLevelPixmapGenerator::pmCache; diff --git a/cockatrice/src/client/ui/pixel_map_generator.h b/cockatrice/src/client/ui/pixel_map_generator.h index 65f9a96fc..5444249b8 100644 --- a/cockatrice/src/client/ui/pixel_map_generator.h +++ b/cockatrice/src/client/ui/pixel_map_generator.h @@ -5,6 +5,7 @@ #include #include +#include class PhasePixmapGenerator { @@ -65,6 +66,7 @@ private: public: static QPixmap generatePixmap(int height, UserLevelFlags userLevel, bool isBuddy, QString privLevel = "NONE"); + static QIcon generateIcon(int height, UserLevelFlags userLevel, bool isBuddy, QString privLevel = "NONE"); static void clear() { pmCache.clear(); diff --git a/cockatrice/src/server/user/user_list_widget.cpp b/cockatrice/src/server/user/user_list_widget.cpp index 8077f4cab..e427d4d69 100644 --- a/cockatrice/src/server/user/user_list_widget.cpp +++ b/cockatrice/src/server/user/user_list_widget.cpp @@ -344,8 +344,8 @@ void UserListTWI::setUserInfo(const ServerInfo_User &_userInfo) userInfo = _userInfo; setData(0, Qt::UserRole, userInfo.user_level()); - setIcon(0, QIcon(UserLevelPixmapGenerator::generatePixmap(12, UserLevelFlags(userInfo.user_level()), false, - QString::fromStdString(userInfo.privlevel())))); + setIcon(0, UserLevelPixmapGenerator::generateIcon(12, UserLevelFlags(userInfo.user_level()), false, + QString::fromStdString(userInfo.privlevel()))); setIcon(1, QIcon(CountryPixmapGenerator::generatePixmap(12, QString::fromStdString(userInfo.country())))); setData(2, Qt::UserRole, QString::fromStdString(userInfo.name())); setData(2, Qt::DisplayRole, QString::fromStdString(userInfo.name()));