2007-03-08 00:27:37 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QMatrix>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPixmapCache>
|
|
|
|
|
|
|
|
#include <KDebug>
|
2007-03-08 21:14:34 +01:00
|
|
|
#include <KSvgRenderer>
|
2007-03-08 00:27:37 +01:00
|
|
|
|
|
|
|
#include "svg.h"
|
|
|
|
#include "theme.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class Svg::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private( const QString& image )
|
|
|
|
: renderer( 0 ),
|
|
|
|
themePath( image )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
delete renderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeFromCache()
|
|
|
|
{
|
|
|
|
if ( id.isEmpty() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmapCache::remove( id );
|
|
|
|
id.clear();
|
|
|
|
}
|
|
|
|
|
2007-05-19 00:05:00 +02:00
|
|
|
void findInCache( QPainter* painter, QPixmap& p, const QString& elementId )
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
|
|
|
if ( path.isNull() ) {
|
|
|
|
path = Plasma::Theme::self()->image( themePath );
|
|
|
|
|
|
|
|
if ( path.isNull() ) {
|
|
|
|
// bad theme path
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMatrix matrix = painter->worldMatrix();
|
|
|
|
|
|
|
|
//TODO: if the id changes, should we remove it or just let QPixmapCache do that for us?
|
|
|
|
id = QString::fromLatin1( "%7_%1_%2_%3_%4_%5_%6" )
|
|
|
|
.arg( size.width() )
|
|
|
|
.arg( size.height() )
|
|
|
|
.arg( matrix.m11() )
|
|
|
|
.arg( matrix.m12() )
|
|
|
|
.arg( matrix.m21() )
|
|
|
|
.arg( matrix.m22() )
|
|
|
|
.arg( themePath );
|
|
|
|
|
2007-05-19 00:05:00 +02:00
|
|
|
if (!elementId.isEmpty()) {
|
|
|
|
id.append(elementId);
|
|
|
|
}
|
|
|
|
|
2007-03-08 00:27:37 +01:00
|
|
|
if ( QPixmapCache::find( id, p ) ) {
|
2007-03-18 07:57:48 +01:00
|
|
|
//kDebug() << "found cached version of " << id << endl;
|
2007-03-08 00:27:37 +01:00
|
|
|
return;
|
2007-03-18 07:57:48 +01:00
|
|
|
}/* else {
|
2007-03-08 00:27:37 +01:00
|
|
|
kDebug() << "didn't find cached version of " << id << ", so re-rendering" << endl;
|
2007-03-18 07:57:48 +01:00
|
|
|
}*/
|
2007-03-08 00:27:37 +01:00
|
|
|
|
|
|
|
// we have to re-render this puppy
|
|
|
|
if ( ! renderer ) {
|
|
|
|
//TODO: connect the renderer's repaintNeeded to the Plasma::Svg signal
|
|
|
|
// take into consideration for cache, e.g. don't cache if svg is animated
|
2007-03-08 21:14:34 +01:00
|
|
|
renderer = new KSvgRenderer( path );
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 00:05:00 +02:00
|
|
|
p = QPixmap( size.toSize() );
|
2007-03-08 00:27:37 +01:00
|
|
|
p.fill(Qt::transparent);
|
|
|
|
QPainter renderPainter( &p );
|
|
|
|
renderPainter.setWorldMatrix( matrix );
|
2007-05-19 00:05:00 +02:00
|
|
|
|
|
|
|
if ( elementId.isEmpty() ) {
|
|
|
|
renderer->render( &renderPainter, p.rect() );
|
|
|
|
} else {
|
|
|
|
renderer->render( &renderPainter, elementId, p.rect() );
|
|
|
|
}
|
2007-03-08 00:27:37 +01:00
|
|
|
renderPainter.end();
|
|
|
|
QPixmapCache::insert( id, p );
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: share renderers between Svg objects with identical themePath
|
2007-03-08 21:14:34 +01:00
|
|
|
KSvgRenderer* renderer;
|
2007-03-08 00:27:37 +01:00
|
|
|
QString themePath;
|
|
|
|
QString path;
|
|
|
|
QString id;
|
2007-05-19 00:05:00 +02:00
|
|
|
QSizeF size;
|
2007-03-08 00:27:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Svg::Svg( const QString& imagePath, QObject* parent )
|
|
|
|
: QObject( parent ),
|
|
|
|
d( new Private( imagePath ) )
|
|
|
|
{
|
|
|
|
connect( Plasma::Theme::self(), SIGNAL(changed()), this, SLOT(themeChanged()) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Svg::~Svg()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-19 00:05:00 +02:00
|
|
|
void Svg::paint( QPainter* painter, const QPointF& point, const QString& elementID )
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
|
|
|
QPixmap pix;
|
2007-05-19 00:05:00 +02:00
|
|
|
d->findInCache( painter, pix, elementID );
|
2007-03-08 00:27:37 +01:00
|
|
|
|
2007-05-19 10:32:22 +02:00
|
|
|
/* QMatrix matrix = painter->worldMatrix();
|
|
|
|
painter->setWorldMatrix( QMatrix() );*/
|
2007-05-19 00:05:00 +02:00
|
|
|
painter->drawPixmap( point.toPoint(), pix );
|
2007-05-19 10:32:22 +02:00
|
|
|
// painter->setWorldMatrix( matrix );
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 00:05:00 +02:00
|
|
|
void Svg::paint( QPainter* painter, int x, int y, const QString& elementID )
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2007-05-19 00:05:00 +02:00
|
|
|
paint( painter, QPointF( x, y ), elementID );
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2007-05-19 00:05:00 +02:00
|
|
|
void Svg::paint( QPainter* painter, const QRectF& rect, const QString& elementID )
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
|
|
|
QPixmap pix;
|
2007-05-19 00:05:00 +02:00
|
|
|
d->findInCache( painter, pix, elementID );
|
2007-03-08 00:27:37 +01:00
|
|
|
|
2007-05-19 10:32:22 +02:00
|
|
|
/* QMatrix matrix = painter->worldMatrix();
|
|
|
|
painter->setWorldMatrix( QMatrix() );*/
|
|
|
|
painter->drawPixmap( rect, pix, rect );
|
|
|
|
// painter->setWorldMatrix( matrix );
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Svg::resize( int width, int height )
|
|
|
|
{
|
|
|
|
resize( QSize( width, height ) );
|
|
|
|
}
|
|
|
|
|
2007-05-19 00:05:00 +02:00
|
|
|
void Svg::resize( const QSizeF& size )
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
|
|
|
d->size = size;
|
|
|
|
}
|
|
|
|
|
2007-05-19 00:05:00 +02:00
|
|
|
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();;
|
|
|
|
}
|
|
|
|
|
2007-03-08 00:27:37 +01:00
|
|
|
void Svg::themeChanged()
|
|
|
|
{
|
|
|
|
d->removeFromCache();
|
|
|
|
d->path.clear();
|
|
|
|
delete d->renderer;
|
|
|
|
d->renderer = 0;
|
|
|
|
emit repaintNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "svg.moc"
|
|
|
|
|