very basic svg icon painting

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=750938
This commit is contained in:
Aaron J. Seigo 2007-12-20 16:28:47 +00:00
parent 50bd42b0e9
commit 0946029d25
2 changed files with 27 additions and 7 deletions

View File

@ -54,6 +54,7 @@ namespace Plasma
Icon::Private::Private() Icon::Private::Private()
: svg("widgets/iconbutton"), : svg("widgets/iconbutton"),
iconSvg(0),
svgElements(0), svgElements(0),
iconSize(48, 48), iconSize(48, 48),
states(Private::NoState), states(Private::NoState),
@ -70,6 +71,7 @@ Icon::Private::Private()
Icon::Private::~Private() Icon::Private::~Private()
{ {
qDeleteAll(cornerActions); qDeleteAll(cornerActions);
delete iconSvg;
} }
void Icon::Private::checkSvgElements() void Icon::Private::checkSvgElements()
@ -454,7 +456,11 @@ void Icon::calculateSize(const QStyleOptionGraphicsItem *option)
void Icon::setSvg(const QString &svgFilePath) void Icon::setSvg(const QString &svgFilePath)
{ {
d->svg.setFile(svgFilePath); if (!d->iconSvg) {
d->iconSvg = new Plasma::Svg(svgFilePath);
} else {
d->iconSvg->setFile(svgFilePath);
}
} }
void Icon::Private::drawBackground(QPainter *painter, IconState state) void Icon::Private::drawBackground(QPainter *painter, IconState state)
@ -515,17 +521,29 @@ void Icon::Private::drawForeground(QPainter *painter, IconState state)
} }
} }
QPixmap Icon::Private::decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect) const QPixmap Icon::Private::decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect)
{ {
QPixmap result; QPixmap result;
QIcon::Mode mode = option->state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled; QIcon::Mode mode = option->state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled;
QIcon::State state = option->state & QStyle::State_Open ? QIcon::On : QIcon::Off; QIcon::State state = option->state & QStyle::State_Open ? QIcon::On : QIcon::Off;
if (iconSvg) {
if (iconSvgPixmap.size() != iconSize.toSize()) {
QImage img(iconSize.toSize(), QImage::Format_ARGB32_Premultiplied);
{
QPainter p(&img);
iconSvg->paint(&p, img.rect());
}
iconSvgPixmap = QPixmap::fromImage(img);
}
result = iconSvgPixmap;
} else {
const QSize size = icon.actualSize(iconSize.toSize(), mode, state); const QSize size = icon.actualSize(iconSize.toSize(), mode, state);
result = icon.pixmap(size, mode, state); result = icon.pixmap(size, mode, state);
}
if (!result.isNull() && useHoverEffect) if (!result.isNull() && useHoverEffect) {
{
KIconEffect *effect = KIconLoader::global()->iconEffect(); KIconEffect *effect = KIconLoader::global()->iconEffect();
// Note that in KIconLoader terminology, active = hover. // Note that in KIconLoader terminology, active = hover.

View File

@ -111,7 +111,7 @@ public:
void drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option, void drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option,
const QTextLayout &labelLayout, const QTextLayout &infoLayout) const; const QTextLayout &labelLayout, const QTextLayout &infoLayout) const;
QPixmap decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect) const; QPixmap decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect);
QPointF iconPosition(const QStyleOptionGraphicsItem *option, const QPixmap &pixmap) const; QPointF iconPosition(const QStyleOptionGraphicsItem *option, const QPixmap &pixmap) const;
QSizeF displaySizeHint(const QStyleOptionGraphicsItem *option) const; QSizeF displaySizeHint(const QStyleOptionGraphicsItem *option) const;
@ -170,6 +170,8 @@ public:
QString text; QString text;
QString infoText; QString infoText;
Svg svg; Svg svg;
Svg *iconSvg;
QPixmap iconSvgPixmap;
int svgElements; int svgElements;
QSizeF size; QSizeF size;
QSizeF iconSize; QSizeF iconSize;