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.
|
|
|
|
*/
|
|
|
|
|
2007-05-21 16:28:03 +02:00
|
|
|
#include "svg.h"
|
|
|
|
|
2007-05-27 10:01:31 +02:00
|
|
|
#include <QDir>
|
2007-03-08 00:27:37 +01:00
|
|
|
#include <QMatrix>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPixmapCache>
|
2007-05-29 09:56:21 +02:00
|
|
|
#include <QSharedData>
|
2007-03-08 00:27:37 +01:00
|
|
|
|
|
|
|
#include <KDebug>
|
2007-05-29 09:56:21 +02:00
|
|
|
#include <KSharedPtr>
|
2007-03-08 21:14:34 +01:00
|
|
|
#include <KSvgRenderer>
|
2007-05-29 09:56:21 +02:00
|
|
|
|
2007-03-08 00:27:37 +01:00
|
|
|
#include "theme.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-05-29 09:56:21 +02:00
|
|
|
class SharedSvgRenderer : public KSvgRenderer, public QSharedData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef KSharedPtr<SharedSvgRenderer> Ptr;
|
|
|
|
|
|
|
|
SharedSvgRenderer(QObject *parent = 0)
|
|
|
|
: KSvgRenderer(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
SharedSvgRenderer(const QString &filename, QObject *parent = 0)
|
|
|
|
: KSvgRenderer(filename, parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
SharedSvgRenderer(const QByteArray &contents, QObject *parent = 0)
|
|
|
|
: KSvgRenderer(contents, parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~SharedSvgRenderer()
|
|
|
|
{
|
|
|
|
kDebug() << "leaving this world for a better one." << endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-03-08 00:27:37 +01:00
|
|
|
class Svg::Private
|
|
|
|
{
|
|
|
|
public:
|
2007-05-29 22:27:51 +02:00
|
|
|
Private(const QString& imagePath)
|
|
|
|
: renderer(0),
|
|
|
|
contentType(Svg::SingleImage)
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2007-05-27 10:01:31 +02:00
|
|
|
if (QDir::isAbsolutePath(themePath)) {
|
|
|
|
path = imagePath;
|
|
|
|
themed = false;
|
|
|
|
} else {
|
|
|
|
themePath = imagePath;
|
|
|
|
themed = true;
|
|
|
|
}
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
2007-05-29 09:56:21 +02:00
|
|
|
if (renderer.count() == 2) {
|
|
|
|
// this and the cache reference it; and boy is this not thread safe ;)
|
|
|
|
renderers.erase(renderers.find(themePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer = 0;
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void removeFromCache()
|
|
|
|
{
|
|
|
|
if ( id.isEmpty() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmapCache::remove( id );
|
|
|
|
id.clear();
|
|
|
|
}
|
|
|
|
|
2007-05-22 02:20:52 +02:00
|
|
|
void findInCache(QPixmap& p, const QString& elementId)
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2007-05-27 10:01:31 +02:00
|
|
|
createRenderer();
|
2007-05-22 02:20:52 +02:00
|
|
|
id = QString::fromLatin1("%3_%2_%1")
|
2007-05-27 10:01:31 +02:00
|
|
|
.arg(size.width())
|
|
|
|
.arg(size.height())
|
|
|
|
.arg(path);
|
2007-05-19 00:05:00 +02:00
|
|
|
if (!elementId.isEmpty()) {
|
|
|
|
id.append(elementId);
|
|
|
|
}
|
2007-05-29 22:27:51 +02:00
|
|
|
//kDebug() << "id is " << id << endl;
|
2007-05-19 00:05:00 +02:00
|
|
|
|
2007-05-27 10:01:31 +02: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-05-21 00:30:41 +02:00
|
|
|
} else {
|
|
|
|
//kDebug() << "didn't find cached version of " << id << ", so re-rendering" << endl;
|
|
|
|
}
|
2007-03-08 00:27:37 +01:00
|
|
|
|
|
|
|
// we have to re-render this puppy
|
2007-05-21 00:30:41 +02:00
|
|
|
QSize s;
|
2007-05-29 22:27:51 +02:00
|
|
|
if (elementId.isEmpty() || contentType == Svg::ImageSet) {
|
2007-05-21 00:30:41 +02:00
|
|
|
s = size.toSize();
|
|
|
|
} else {
|
2007-05-29 01:47:41 +02:00
|
|
|
s = elementSize(elementId);
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
2007-05-21 00:30:41 +02:00
|
|
|
//kDebug() << "size for " << elementId << " is " << s << endl;
|
2007-03-08 00:27:37 +01:00
|
|
|
|
2007-05-21 00:30:41 +02:00
|
|
|
p = QPixmap(s);
|
2007-03-08 00:27:37 +01:00
|
|
|
p.fill(Qt::transparent);
|
2007-05-27 10:01:31 +02:00
|
|
|
QPainter renderPainter(&p);
|
2007-05-19 00:05:00 +02:00
|
|
|
|
2007-05-27 10:01:31 +02:00
|
|
|
if (elementId.isEmpty()) {
|
|
|
|
renderer->render(&renderPainter);
|
2007-05-19 00:05:00 +02:00
|
|
|
} else {
|
2007-05-27 10:01:31 +02:00
|
|
|
renderer->render(&renderPainter, elementId);
|
2007-05-19 00:05:00 +02:00
|
|
|
}
|
2007-03-08 00:27:37 +01:00
|
|
|
renderPainter.end();
|
|
|
|
QPixmapCache::insert( id, p );
|
|
|
|
}
|
|
|
|
|
2007-05-27 10:01:31 +02:00
|
|
|
void createRenderer()
|
|
|
|
{
|
|
|
|
if (renderer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (themed && path.isNull()) {
|
|
|
|
path = Plasma::Theme::self()->image(themePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
//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-05-29 09:56:21 +02:00
|
|
|
QHash<QString, SharedSvgRenderer::Ptr>::const_iterator it = renderers.find(path);
|
|
|
|
|
|
|
|
if (it != renderers.end()) {
|
2007-05-29 22:27:51 +02:00
|
|
|
//kDebug() << "gots us an existing one!" << endl;
|
2007-05-29 09:56:21 +02:00
|
|
|
renderer = it.value();
|
|
|
|
} else {
|
|
|
|
renderer = new SharedSvgRenderer(path);
|
|
|
|
renderers[path] = renderer;
|
|
|
|
}
|
2007-05-27 10:01:31 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 22:27:51 +02:00
|
|
|
QSize elementSize(const QString& elementId)
|
2007-05-29 01:47:41 +02:00
|
|
|
{
|
|
|
|
createRenderer();
|
|
|
|
QSizeF elementSize = renderer->boundsOnElement(elementId).size();
|
|
|
|
QSizeF naturalSize = renderer->defaultSize();
|
|
|
|
qreal dx = size.width() / naturalSize.width();
|
|
|
|
qreal dy = size.height() / naturalSize.height();
|
2007-05-29 22:27:51 +02:00
|
|
|
elementSize.scale(elementSize.width() * dx, elementSize.height() * dy, Qt::IgnoreAspectRatio);
|
2007-05-29 01:47:41 +02:00
|
|
|
|
|
|
|
return elementSize.toSize();
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:56:21 +02:00
|
|
|
static QHash<QString, SharedSvgRenderer::Ptr> renderers;
|
|
|
|
SharedSvgRenderer::Ptr 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-05-27 10:01:31 +02:00
|
|
|
bool themed;
|
2007-05-29 22:27:51 +02:00
|
|
|
Svg::ContentType contentType;
|
2007-03-08 00:27:37 +01:00
|
|
|
};
|
|
|
|
|
2007-05-29 09:56:21 +02:00
|
|
|
QHash<QString, SharedSvgRenderer::Ptr> Svg::Private:: renderers;
|
|
|
|
|
2007-05-29 22:27:51 +02:00
|
|
|
Svg::Svg(const QString& imagePath, QObject* parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new Private(imagePath))
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2007-05-27 10:01:31 +02:00
|
|
|
if (d->themed) {
|
|
|
|
connect(Plasma::Theme::self(), SIGNAL(changed()), this, SLOT(themeChanged()));
|
|
|
|
}
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Svg::~Svg()
|
|
|
|
{
|
2007-06-07 22:57:18 +02:00
|
|
|
delete d;
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2007-05-22 02:20:52 +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-22 02:20:52 +02:00
|
|
|
d->findInCache(pix, elementID);
|
2007-05-29 22:27:51 +02:00
|
|
|
//kDebug() << "pix size is " << pix.size() << endl;
|
|
|
|
painter->drawPixmap(QRectF(point, pix.size()), pix, QRectF(QPointF(0,0), pix.size()));
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2007-05-22 02:20:52 +02:00
|
|
|
void Svg::paint(QPainter* painter, int x, int y, const QString& elementID)
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2007-05-29 22:27:51 +02:00
|
|
|
paint(painter, QPointF(x, y), elementID);
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2007-05-22 02:20:52 +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-22 02:20:52 +02:00
|
|
|
d->findInCache(pix, elementID);
|
2007-05-29 22:27:51 +02:00
|
|
|
//kDebug() << "pix size is " << pix.size() << endl;
|
2007-05-28 22:09:50 +02:00
|
|
|
painter->drawPixmap(rect, pix, QRectF(QPointF(0,0), pix.size()));
|
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-21 00:56:38 +02:00
|
|
|
void Svg::resize()
|
|
|
|
{
|
2007-05-27 10:01:31 +02:00
|
|
|
d->createRenderer();
|
2007-05-21 00:56:38 +02:00
|
|
|
d->size = d->renderer->defaultSize();
|
|
|
|
}
|
|
|
|
|
2007-05-21 01:31:44 +02:00
|
|
|
QSize Svg::elementSize(const QString& elementId) const
|
2007-05-19 00:05:00 +02:00
|
|
|
{
|
2007-05-29 01:47:41 +02:00
|
|
|
return d->elementSize(elementId);
|
2007-05-21 00:39:38 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 22:27:51 +02:00
|
|
|
bool Svg::elementExists(const QString& elementId) const
|
|
|
|
{
|
|
|
|
d->createRenderer();
|
|
|
|
return d->renderer->elementExists(elementId);
|
|
|
|
}
|
|
|
|
|
2007-05-21 01:31:44 +02:00
|
|
|
QSize Svg::size() const
|
2007-05-21 00:39:38 +02:00
|
|
|
{
|
|
|
|
return d->size.toSize();
|
2007-05-19 00:05:00 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 22:27:51 +02:00
|
|
|
void Svg::setContentType(ContentType contentType)
|
|
|
|
{
|
|
|
|
d->contentType = contentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
Svg::ContentType Svg::contentType()
|
|
|
|
{
|
|
|
|
return d->contentType;
|
|
|
|
}
|
|
|
|
|
2007-03-08 00:27:37 +01:00
|
|
|
void Svg::themeChanged()
|
|
|
|
{
|
|
|
|
d->removeFromCache();
|
|
|
|
d->path.clear();
|
2007-05-29 09:56:21 +02:00
|
|
|
//delete d->renderer; we're a KSharedPtr
|
2007-03-08 00:27:37 +01:00
|
|
|
d->renderer = 0;
|
|
|
|
emit repaintNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "svg.moc"
|
|
|
|
|