2007-03-08 00:27:37 +01:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
2007-03-08 00:27:37 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-08-06 13:20:02 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
2007-03-08 00:27:37 +01:00
|
|
|
*
|
|
|
|
* 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()
|
|
|
|
{
|
2007-08-05 14:29:06 +02:00
|
|
|
//kDebug() << "leaving this world for a better one.";
|
2007-05-29 09:56:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-07-16 16:19:36 +02:00
|
|
|
if (QDir::isAbsolutePath(imagePath)) {
|
2007-05-27 10:01:31 +02:00
|
|
|
path = imagePath;
|
|
|
|
themed = false;
|
2007-07-23 02:50:10 +02:00
|
|
|
|
|
|
|
if (!QFile::exists(path)) {
|
2007-08-02 15:12:59 +02:00
|
|
|
kDebug() << "Plasma::Svg: file '" << path << "' does not exist!";
|
2007-07-23 02:50:10 +02:00
|
|
|
}
|
2007-05-27 10:01:31 +02:00
|
|
|
} 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-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "id is " << id;
|
2007-05-19 00:05:00 +02:00
|
|
|
|
2007-05-27 10:01:31 +02:00
|
|
|
if (QPixmapCache::find(id, p)) {
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "found cached version of " << id;
|
2007-03-08 00:27:37 +01:00
|
|
|
return;
|
2007-05-21 00:30:41 +02:00
|
|
|
} else {
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "didn't find cached version of " << id << ", so re-rendering";
|
2007-05-21 00:30:41 +02:00
|
|
|
}
|
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-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "size for " << elementId << " is " << s;
|
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();
|
2007-08-17 08:35:19 +02:00
|
|
|
bool inserted = QPixmapCache::insert( id, p );
|
|
|
|
if (!inserted)
|
|
|
|
kDebug() << "pixmap cache is too small for inserting" << id << "of size" << s;
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2007-05-27 10:01:31 +02:00
|
|
|
void createRenderer()
|
|
|
|
{
|
|
|
|
if (renderer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (themed && path.isNull()) {
|
|
|
|
path = Plasma::Theme::self()->image(themePath);
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:56:21 +02:00
|
|
|
QHash<QString, SharedSvgRenderer::Ptr>::const_iterator it = renderers.find(path);
|
|
|
|
|
|
|
|
if (it != renderers.end()) {
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "gots us an existing one!";
|
2007-05-29 09:56:21 +02:00
|
|
|
renderer = it.value();
|
|
|
|
} else {
|
|
|
|
renderer = new SharedSvgRenderer(path);
|
|
|
|
renderers[path] = renderer;
|
|
|
|
}
|
2007-08-31 17:15:04 +02:00
|
|
|
|
|
|
|
size = renderer->defaultSize();
|
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-07-27 01:07:26 +02:00
|
|
|
QRect elementRect(const QString& elementId)
|
|
|
|
{
|
|
|
|
createRenderer();
|
|
|
|
QRectF elementRect = renderer->boundsOnElement(elementId);
|
|
|
|
QSizeF naturalSize = renderer->defaultSize();
|
|
|
|
qreal dx = size.width() / naturalSize.width();
|
|
|
|
qreal dy = size.height() / naturalSize.height();
|
2007-08-02 18:14:35 +02:00
|
|
|
|
2007-07-27 01:07:26 +02:00
|
|
|
return QRect(elementRect.x() * dx, elementRect.y() * dy,
|
|
|
|
elementRect.width() * dx, elementRect.height() * dy);
|
|
|
|
}
|
2007-08-02 18:14:35 +02:00
|
|
|
|
|
|
|
QMatrix matrixForElement(const QString& elementId)
|
|
|
|
{
|
|
|
|
createRenderer();
|
|
|
|
return renderer->matrixForElement(elementId);
|
|
|
|
}
|
2007-07-27 01:07:26 +02:00
|
|
|
|
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-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "pix size is " << pix.size();
|
2007-05-29 22:27:51 +02:00
|
|
|
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-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "pix size is " << pix.size();
|
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
|
|
|
{
|
2007-08-31 17:15:04 +02:00
|
|
|
d->createRenderer();
|
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-10-01 07:31:32 +02:00
|
|
|
d->size = d->renderer->defaultSize();
|
2007-05-21 00:56:38 +02:00
|
|
|
}
|
|
|
|
|
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-07-27 01:07:26 +02:00
|
|
|
QRect Svg::elementRect(const QString& elementId) const
|
|
|
|
{
|
|
|
|
return d->elementRect(elementId);
|
|
|
|
}
|
|
|
|
|
2007-05-29 22:27:51 +02:00
|
|
|
bool Svg::elementExists(const QString& elementId) const
|
|
|
|
{
|
|
|
|
d->createRenderer();
|
|
|
|
return d->renderer->elementExists(elementId);
|
|
|
|
}
|
|
|
|
|
2007-08-02 18:14:35 +02:00
|
|
|
QMatrix Svg::matrixForElement(const QString& elementId) const
|
|
|
|
{
|
|
|
|
d->createRenderer();
|
|
|
|
return d->renderer->matrixForElement(elementId);
|
|
|
|
}
|
|
|
|
|
2007-07-23 02:50:10 +02:00
|
|
|
bool Svg::isValid() const
|
|
|
|
{
|
|
|
|
d->createRenderer();
|
|
|
|
return d->renderer->isValid();
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
|