If anyone is interested, i have created his class
graphicscolorsvgitem.h
#ifndef GRAPHICSCOLORSVGITEM_H
#define GRAPHICSCOLORSVGITEM_H
#include <QGraphicsSvgItem>
#include <QDomDocument>
#include <QList>
{
public:
virtual ~GraphicsColorSvgItem();
private:
};
#endif // GRAPHICSCOLORSVGITEM_H
#ifndef GRAPHICSCOLORSVGITEM_H
#define GRAPHICSCOLORSVGITEM_H
#include <QGraphicsSvgItem>
#include <QDomDocument>
#include <QList>
class GraphicsColorSvgItem : public QGraphicsSvgItem
{
public:
GraphicsColorSvgItem(QString svgContent, QGraphicsItem *parent = 0);
virtual ~GraphicsColorSvgItem();
void setColor(QColor c);
private:
QDomDocument _svgXML;
void changeAttributes(QString attName, QString attValue);
void recursiveChangeAttributes(QDomNode node, QString attName, QString attValue);
};
#endif // GRAPHICSCOLORSVGITEM_H
To copy to clipboard, switch view to plain text mode
graphicscolorsvgitem.cpp
#include "graphicscolorsvgitem.h"
#include <QSvgRenderer>
#include <QRgb>
#include <QDebug>
_svgXML()
{
_svgXML.setContent(svgContent);
}
GraphicsColorSvgItem::~GraphicsColorSvgItem()
{
delete renderer();
}
void GraphicsColorSvgItem
::setColor(QColor c
) {
changeAttributes("fill", c.name().toUpper());
changeAttributes("stroke", c.name().toUpper());
renderer()->load(_svgXML.toByteArray());
}
void GraphicsColorSvgItem
::changeAttributes(QString attName,
QString attValue
) {
while(!n.isNull())
{
if(n.isElement())
{
if(e.hasAttribute(attName))
e.setAttribute(attName, attValue);
if(n.hasChildNodes())
recursiveChangeAttributes(n.firstChild(), attName, attValue);
}
n = n.nextSibling();
}
}
{
while(!n.isNull())
{
if(n.isElement())
{
if(e.hasAttribute(attName))
e.setAttribute(attName, attValue);
if(n.hasChildNodes())
recursiveChangeAttributes(n.firstChild(), attName, attValue);
}
n = n.nextSibling();
}
}
#include "graphicscolorsvgitem.h"
#include <QSvgRenderer>
#include <QRgb>
#include <QDebug>
GraphicsColorSvgItem::GraphicsColorSvgItem(QString svgContent, QGraphicsItem *parent) :
QGraphicsSvgItem(parent),
_svgXML()
{
_svgXML.setContent(svgContent);
setSharedRenderer(new QSvgRenderer(_svgXML.toByteArray()));
}
GraphicsColorSvgItem::~GraphicsColorSvgItem()
{
delete renderer();
}
void GraphicsColorSvgItem::setColor(QColor c)
{
changeAttributes("fill", c.name().toUpper());
changeAttributes("stroke", c.name().toUpper());
renderer()->load(_svgXML.toByteArray());
}
void GraphicsColorSvgItem::changeAttributes(QString attName, QString attValue)
{
QDomElement rootElem = _svgXML.documentElement();
QDomNode n = rootElem.firstChild();
while(!n.isNull())
{
if(n.isElement())
{
QDomElement e = n.toElement();
if(e.hasAttribute(attName))
e.setAttribute(attName, attValue);
if(n.hasChildNodes())
recursiveChangeAttributes(n.firstChild(), attName, attValue);
}
n = n.nextSibling();
}
}
void GraphicsColorSvgItem::recursiveChangeAttributes(QDomNode node, QString attName, QString attValue)
{
QDomNode n = node;
while(!n.isNull())
{
if(n.isElement())
{
QDomElement e = n.toElement();
if(e.hasAttribute(attName))
e.setAttribute(attName, attValue);
if(n.hasChildNodes())
recursiveChangeAttributes(n.firstChild(), attName, attValue);
}
n = n.nextSibling();
}
}
To copy to clipboard, switch view to plain text mode
If you have some remarks, I'm open.
Bookmarks