From 0946029d25d4baf7c5c86a5cfa03103e7a60a17d Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Thu, 20 Dec 2007 16:28:47 +0000 Subject: [PATCH] very basic svg icon painting svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=750938 --- widgets/icon.cpp | 30 ++++++++++++++++++++++++------ widgets/icon_p.h | 4 +++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/widgets/icon.cpp b/widgets/icon.cpp index eb99c6595..a3d11032b 100644 --- a/widgets/icon.cpp +++ b/widgets/icon.cpp @@ -54,6 +54,7 @@ namespace Plasma Icon::Private::Private() : svg("widgets/iconbutton"), + iconSvg(0), svgElements(0), iconSize(48, 48), states(Private::NoState), @@ -70,6 +71,7 @@ Icon::Private::Private() Icon::Private::~Private() { qDeleteAll(cornerActions); + delete iconSvg; } void Icon::Private::checkSvgElements() @@ -454,7 +456,11 @@ void Icon::calculateSize(const QStyleOptionGraphicsItem *option) 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) @@ -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; QIcon::Mode mode = option->state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled; QIcon::State state = option->state & QStyle::State_Open ? QIcon::On : QIcon::Off; - const QSize size = icon.actualSize(iconSize.toSize(), mode, state); - result = icon.pixmap(size, mode, state); - if (!result.isNull() && useHoverEffect) - { + 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); + result = icon.pixmap(size, mode, state); + } + + if (!result.isNull() && useHoverEffect) { KIconEffect *effect = KIconLoader::global()->iconEffect(); // Note that in KIconLoader terminology, active = hover. diff --git a/widgets/icon_p.h b/widgets/icon_p.h index 9c8bad329..3ed2d1aae 100644 --- a/widgets/icon_p.h +++ b/widgets/icon_p.h @@ -111,7 +111,7 @@ public: void drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option, 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; QSizeF displaySizeHint(const QStyleOptionGraphicsItem *option) const; @@ -170,6 +170,8 @@ public: QString text; QString infoText; Svg svg; + Svg *iconSvg; + QPixmap iconSvgPixmap; int svgElements; QSizeF size; QSizeF iconSize;