From a9ebfb6a14616f3c480a2a824929a32166c0917c Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Fri, 18 May 2007 22:05:00 +0000 Subject: [PATCH] * allow painting of given elements * find the size of an element (given the current size of the Plasma::Svg) svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=666140 --- svg.cpp | 50 +++++++++++++++++++++++++++++++++++++------------- svg.h | 15 +++++++++++---- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/svg.cpp b/svg.cpp index 59f97facb..b18f9e820 100644 --- a/svg.cpp +++ b/svg.cpp @@ -53,7 +53,7 @@ class Svg::Private id.clear(); } - void findInCache( QPainter* painter, QPixmap& p ) + void findInCache( QPainter* painter, QPixmap& p, const QString& elementId ) { if ( path.isNull() ) { path = Plasma::Theme::self()->image( themePath ); @@ -76,6 +76,10 @@ class Svg::Private .arg( matrix.m22() ) .arg( themePath ); + if (!elementId.isEmpty()) { + id.append(elementId); + } + if ( QPixmapCache::find( id, p ) ) { //kDebug() << "found cached version of " << id << endl; return; @@ -90,11 +94,16 @@ class Svg::Private renderer = new KSvgRenderer( path ); } - p = QPixmap( size ); + p = QPixmap( size.toSize() ); p.fill(Qt::transparent); QPainter renderPainter( &p ); renderPainter.setWorldMatrix( matrix ); - renderer->render( &renderPainter, p.rect() ); + + if ( elementId.isEmpty() ) { + renderer->render( &renderPainter, p.rect() ); + } else { + renderer->render( &renderPainter, elementId, p.rect() ); + } renderPainter.end(); QPixmapCache::insert( id, p ); } @@ -104,7 +113,7 @@ class Svg::Private QString themePath; QString path; QString id; - QSize size; + QSizeF size; }; Svg::Svg( const QString& imagePath, QObject* parent ) @@ -119,30 +128,30 @@ Svg::~Svg() } -void Svg::paint( QPainter* painter, const QPoint& point ) +void Svg::paint( QPainter* painter, const QPointF& point, const QString& elementID ) { QPixmap pix; - d->findInCache( painter, pix ); + d->findInCache( painter, pix, elementID ); QMatrix matrix = painter->worldMatrix(); painter->setWorldMatrix( QMatrix() ); - painter->drawPixmap( point, pix ); + painter->drawPixmap( point.toPoint(), pix ); painter->setWorldMatrix( matrix ); } -void Svg::paint( QPainter* painter, int x, int y ) +void Svg::paint( QPainter* painter, int x, int y, const QString& elementID ) { - paint( painter, QPoint( x, y ) ); + paint( painter, QPointF( x, y ), elementID ); } -void Svg::paint( QPainter* painter, const QRect& rect ) +void Svg::paint( QPainter* painter, const QRectF& rect, const QString& elementID ) { QPixmap pix; - d->findInCache( painter, pix ); + d->findInCache( painter, pix, elementID ); QMatrix matrix = painter->worldMatrix(); painter->setWorldMatrix( QMatrix() ); - painter->drawPixmap( rect, pix ); + painter->drawPixmap( rect.toRect(), pix ); painter->setWorldMatrix( matrix ); } @@ -151,11 +160,26 @@ void Svg::resize( int width, int height ) resize( QSize( width, height ) ); } -void Svg::resize( const QSize& size ) +void Svg::resize( const QSizeF& size ) { d->size = size; } +QSize Svg::elementSize(const QString& elementId) +{ + if (!d->renderer) { + return QSize(); + } + + QSizeF elementSize = d->renderer->boundsOnElement(elementId).size(); + QSizeF naturalSize = d->renderer->defaultSize(); + qreal dx = d->size.width() / naturalSize.width(); + qreal dy = d->size.height() / naturalSize.height(); + elementSize.scale( elementSize.width() * dx, elementSize.height() * dy, Qt::IgnoreAspectRatio ); + + return elementSize.toSize();; +} + void Svg::themeChanged() { d->removeFromCache(); diff --git a/svg.h b/svg.h index a0928992a..848e5f0ba 100644 --- a/svg.h +++ b/svg.h @@ -59,7 +59,7 @@ class KDE_EXPORT Svg : public QObject * @arg point the position to start drawing; the entire svg will be * drawn starting at this point. */ - void paint( QPainter* painter, const QPoint& point ); + void paint( QPainter* painter, const QPointF& point, const QString& elementID = QString() ); /** * Paints the SVG represented by this object @@ -67,7 +67,7 @@ class KDE_EXPORT Svg : public QObject * @arg x the horizontal coordinate to start painting from * @arg y the vertical coordinate to start painting from */ - void paint( QPainter* painter, int x, int y ); + void paint( QPainter* painter, int x, int y, const QString& elementID = QString() ); /** * Paints the SVG represented by this object @@ -76,7 +76,7 @@ class KDE_EXPORT Svg : public QObject * of the * drawn starting at this point. */ - void paint( QPainter* painter, const QRect& rect ); + void paint( QPainter* painter, const QRectF& rect, const QString& elementID = QString() ); /** * Resizes the rendered image. Rendering will actually take place on @@ -91,7 +91,14 @@ class KDE_EXPORT Svg : public QObject * the next call to paint. * @arg size the new size of the image **/ - void resize( const QSize& size ); + void resize( const QSizeF& size ); + + /** + * Size of a given element + * @arg elementId the id of the element to check + * @return the current size of a given element + **/ + QSize elementSize( const QString& elementId ); Q_SIGNALS: void repaintNeeded();