got something to work

This commit is contained in:
RickyRister 2025-01-31 22:17:26 -08:00
parent 6b88b35108
commit 289521a47e
5 changed files with 65 additions and 8 deletions

View file

@ -4,8 +4,12 @@
#include <QApplication>
#include <QDebug>
#include <QDomDocument>
#include <QFile>
#include <QPainter>
#include <QPalette>
#include <QSvgRenderer>
#include <QtGui/qicon.h>
QMap<QString, QPixmap> PhasePixmapGenerator::pmCache;
@ -96,12 +100,61 @@ QPixmap CountryPixmapGenerator::generatePixmap(int height, const QString &countr
QMap<QString, QPixmap> 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<QString, QPixmap> UserLevelPixmapGenerator::pmCache;

View file

@ -5,6 +5,7 @@
#include <QMap>
#include <QPixmap>
#include <QtGui/qicon.h>
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();

View file

@ -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()));