2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2006-2007 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 as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 "svg.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QMatrix>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QSharedData>
|
2008-12-01 15:35:02 +01:00
|
|
|
#include <QTimer>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kcolorscheme.h>
|
|
|
|
#include <kconfiggroup.h>
|
|
|
|
#include <kdebug.h>
|
|
|
|
#include <kiconeffect.h>
|
|
|
|
#include <kglobalsettings.h>
|
|
|
|
#include <ksharedptr.h>
|
|
|
|
#include <ksvgrenderer.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-06 04:45:44 +01:00
|
|
|
#include "applet.h"
|
|
|
|
#include "package.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
#include "theme.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
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.";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SvgPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SvgPrivate(Svg *svg)
|
|
|
|
: q(svg),
|
2008-12-01 15:35:02 +01:00
|
|
|
saveTimer(0),
|
2008-11-04 00:08:39 +01:00
|
|
|
renderer(0),
|
|
|
|
multipleImages(false),
|
|
|
|
themed(false),
|
|
|
|
applyColors(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~SvgPrivate()
|
|
|
|
{
|
|
|
|
eraseRenderer();
|
|
|
|
}
|
|
|
|
|
2008-12-05 20:50:01 +01:00
|
|
|
//This function is meant for the rects cache
|
2008-11-04 00:08:39 +01:00
|
|
|
QString cacheId(const QString &elementId)
|
|
|
|
{
|
|
|
|
if (size.isValid() && size != naturalSize) {
|
|
|
|
return QString("%3_%2_%1").arg(int(size.height()))
|
|
|
|
.arg(int(size.width()))
|
|
|
|
.arg(elementId);
|
|
|
|
} else {
|
|
|
|
return QString("%2_%1").arg("Natural")
|
|
|
|
.arg(elementId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-05 20:50:01 +01:00
|
|
|
//This function is meant for the pixmap cache
|
2008-12-09 10:03:12 +01:00
|
|
|
QString cachePath(const QString &path, const QSize &size)
|
2008-12-05 20:50:01 +01:00
|
|
|
{
|
2008-12-09 10:03:12 +01:00
|
|
|
return QString("%3_%2_%1_").arg(int(size.height()))
|
|
|
|
.arg(int(size.width()))
|
2008-12-05 20:50:01 +01:00
|
|
|
.arg(path);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
bool setImagePath(const QString &imagePath, Svg *q)
|
|
|
|
{
|
|
|
|
bool isThemed = !QDir::isAbsolutePath(imagePath);
|
|
|
|
|
|
|
|
// lets check to see if we're already set to this file
|
|
|
|
if (isThemed == themed &&
|
|
|
|
((themed && themePath == imagePath) ||
|
|
|
|
(!themed && path == imagePath))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we don't have any path right now and are going to set one,
|
|
|
|
// then lets not schedule a repaint because we are just initializing!
|
|
|
|
bool updateNeeded = true; //!path.isEmpty() || !themePath.isEmpty();
|
|
|
|
|
|
|
|
if (themed) {
|
|
|
|
QObject::disconnect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
|
|
|
|
q, SLOT(themeChanged()));
|
|
|
|
QObject::disconnect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()),
|
|
|
|
q, SLOT(colorsChanged()));
|
|
|
|
}
|
|
|
|
|
|
|
|
themed = isThemed;
|
|
|
|
path.clear();
|
|
|
|
themePath.clear();
|
|
|
|
|
|
|
|
if (themed) {
|
|
|
|
themePath = imagePath;
|
|
|
|
QObject::connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
|
|
|
|
q, SLOT(themeChanged()));
|
|
|
|
|
|
|
|
// check if svg wants colorscheme applied
|
|
|
|
checkApplyColorHint();
|
|
|
|
if (applyColors && !Theme::defaultTheme()->colorScheme()) {
|
|
|
|
QObject::connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()),
|
|
|
|
q, SLOT(colorsChanged()));
|
|
|
|
}
|
|
|
|
} else if (QFile::exists(imagePath)) {
|
|
|
|
path = imagePath;
|
|
|
|
} else {
|
|
|
|
kDebug() << "file '" << path << "' does not exist!";
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateNeeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap findInCache(const QString &elementId, const QSizeF &s = QSizeF())
|
|
|
|
{
|
|
|
|
QSize size;
|
|
|
|
if (elementId.isEmpty() || (multipleImages && s.isValid())) {
|
|
|
|
size = s.toSize();
|
|
|
|
} else {
|
|
|
|
size = elementRect(elementId).size().toSize();
|
|
|
|
}
|
|
|
|
|
2008-12-09 17:22:03 +01:00
|
|
|
if (size.isEmpty()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
return QPixmap();
|
|
|
|
}
|
|
|
|
|
2008-12-09 10:03:12 +01:00
|
|
|
QString id = cachePath(path, size);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (!elementId.isEmpty()) {
|
|
|
|
id.append(elementId);
|
|
|
|
}
|
|
|
|
|
|
|
|
//kDebug() << "id is " << id;
|
|
|
|
|
|
|
|
Theme *theme = Theme::defaultTheme();
|
|
|
|
QPixmap p;
|
|
|
|
|
|
|
|
if (theme->findInCache(id, p)) {
|
|
|
|
//kDebug() << "found cached version of " << id << p.size();
|
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
//kDebug() << "didn't find cached version of " << id << ", so re-rendering";
|
|
|
|
}
|
|
|
|
|
|
|
|
//kDebug() << "size for " << elementId << " is " << s;
|
|
|
|
// we have to re-render this puppy
|
|
|
|
|
|
|
|
p = QPixmap(size);
|
|
|
|
|
|
|
|
p.fill(Qt::transparent);
|
|
|
|
QPainter renderPainter(&p);
|
|
|
|
|
|
|
|
createRenderer();
|
|
|
|
if (elementId.isEmpty()) {
|
|
|
|
renderer->render(&renderPainter);
|
|
|
|
} else {
|
|
|
|
renderer->render(&renderPainter, elementId);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPainter.end();
|
|
|
|
|
|
|
|
// Apply current color scheme if the svg asks for it
|
|
|
|
if (applyColors) {
|
|
|
|
QImage itmp = p.toImage();
|
|
|
|
KIconEffect::colorize(itmp, theme->color(Theme::BackgroundColor), 1.0);
|
|
|
|
p = p.fromImage(itmp);
|
|
|
|
}
|
|
|
|
|
2008-12-07 13:44:10 +01:00
|
|
|
if (!itemsToSave.contains(id)) {
|
|
|
|
itemsToSave.insert(id, p);
|
|
|
|
saveTimer->start(300);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-12-01 15:35:02 +01:00
|
|
|
void scheduledCacheUpdate()
|
|
|
|
{
|
2008-12-08 22:27:42 +01:00
|
|
|
QHash<QString, QPixmap>::const_iterator i = itemsToSave.constBegin();
|
2008-12-01 15:35:02 +01:00
|
|
|
|
2008-12-08 22:16:55 +01:00
|
|
|
while (i != itemsToSave.constEnd()) {
|
2008-12-07 13:44:10 +01:00
|
|
|
//kDebug()<<"Saving item to cache: "<<i.key();
|
|
|
|
Theme::defaultTheme()->insertIntoCache(i.key(), i.value());
|
2008-12-01 15:35:02 +01:00
|
|
|
++i;
|
|
|
|
}
|
2008-12-08 22:16:55 +01:00
|
|
|
|
2008-12-01 15:35:02 +01:00
|
|
|
itemsToSave.clear();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void createRenderer()
|
|
|
|
{
|
|
|
|
if (renderer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//kDebug() << kBacktrace();
|
|
|
|
if (themed && path.isEmpty()) {
|
2009-01-06 04:45:44 +01:00
|
|
|
Applet *applet = qobject_cast<Applet*>(q->parent());
|
|
|
|
if (applet && applet->package()) {
|
|
|
|
path = applet->package()->filePath("images", themePath + ".svg");
|
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
path = applet->package()->filePath("images", themePath + ".svgz");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
path = Plasma::Theme::defaultTheme()->imagePath(themePath);
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//kDebug() << "********************************";
|
|
|
|
//kDebug() << "FAIL! **************************";
|
|
|
|
//kDebug() << path << "**";
|
|
|
|
|
2009-01-06 14:09:05 +01:00
|
|
|
QHash<QString, SharedSvgRenderer::Ptr>::const_iterator it = s_renderers.constFind(path);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-06 14:09:05 +01:00
|
|
|
if (it != s_renderers.constEnd()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
//kDebug() << "gots us an existing one!";
|
|
|
|
renderer = it.value();
|
|
|
|
} else {
|
|
|
|
renderer = new SharedSvgRenderer(path);
|
|
|
|
s_renderers[path] = renderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == QSizeF()) {
|
|
|
|
size = renderer->defaultSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void eraseRenderer()
|
|
|
|
{
|
|
|
|
if (renderer && renderer.count() == 2) {
|
2009-01-04 20:07:46 +01:00
|
|
|
// this and the cache reference it
|
2008-11-04 00:08:39 +01:00
|
|
|
s_renderers.erase(s_renderers.find(path));
|
2008-12-11 03:45:45 +01:00
|
|
|
Plasma::Theme::defaultTheme()->releaseRectsCache(path);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
renderer = 0;
|
2008-12-01 19:07:20 +01:00
|
|
|
localRectCache.clear();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF elementRect(const QString &elementId)
|
|
|
|
{
|
|
|
|
if (themed && path.isEmpty()) {
|
|
|
|
path = Plasma::Theme::defaultTheme()->imagePath(themePath);
|
|
|
|
}
|
|
|
|
|
2008-12-01 19:07:20 +01:00
|
|
|
QString id = cacheId(elementId);
|
|
|
|
if (localRectCache.contains(id)) {
|
2008-12-08 22:16:55 +01:00
|
|
|
return localRectCache.value(id);
|
2008-12-01 19:07:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF rect;
|
|
|
|
bool found = Theme::defaultTheme()->findInRectsCache(path, id, rect);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (found) {
|
2008-12-01 19:07:20 +01:00
|
|
|
localRectCache.insert(id, rect);
|
2008-11-04 00:08:39 +01:00
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return findAndCacheElementRect(elementId);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF findAndCacheElementRect(const QString &elementId)
|
|
|
|
{
|
|
|
|
createRenderer();
|
|
|
|
QRectF elementRect = renderer->elementExists(elementId) ?
|
|
|
|
renderer->boundsOnElement(elementId) : QRectF();
|
|
|
|
naturalSize = renderer->defaultSize();
|
|
|
|
qreal dx = size.width() / naturalSize.width();
|
|
|
|
qreal dy = size.height() / naturalSize.height();
|
|
|
|
|
|
|
|
elementRect = QRectF(elementRect.x() * dx, elementRect.y() * dy,
|
|
|
|
elementRect.width() * dx, elementRect.height() * dy);
|
|
|
|
Theme::defaultTheme()->insertIntoRectsCache(path, cacheId(elementId), elementRect);
|
|
|
|
|
|
|
|
return elementRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMatrix matrixForElement(const QString &elementId)
|
|
|
|
{
|
|
|
|
createRenderer();
|
|
|
|
return renderer->matrixForElement(elementId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkApplyColorHint()
|
|
|
|
{
|
2008-11-16 21:53:24 +01:00
|
|
|
QRectF elementRect;
|
|
|
|
bool found = Theme::defaultTheme()->findInRectsCache(themePath, cacheId("hint-apply-color-scheme"), elementRect);
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
applyColors = elementRect.isValid();
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2008-11-16 21:53:24 +01:00
|
|
|
findAndCacheElementRect("hint-apply-color-scheme");
|
2008-11-04 00:08:39 +01:00
|
|
|
applyColors = renderer->elementExists("hint-apply-color-scheme");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void themeChanged()
|
|
|
|
{
|
|
|
|
if (!themed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString newPath = Theme::defaultTheme()->imagePath(themePath);
|
|
|
|
|
|
|
|
if (path == newPath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = newPath;
|
|
|
|
//delete d->renderer; we're a KSharedPtr
|
|
|
|
eraseRenderer();
|
|
|
|
|
|
|
|
// check if new theme svg wants colorscheme applied
|
|
|
|
bool wasApplyColors = applyColors;
|
|
|
|
checkApplyColorHint();
|
|
|
|
if (applyColors && !Theme::defaultTheme()->colorScheme()) {
|
|
|
|
if (!wasApplyColors) {
|
|
|
|
QObject::connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()),
|
|
|
|
q, SLOT(colorsChanged()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QObject::disconnect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()),
|
|
|
|
q, SLOT(colorsChanged()));
|
|
|
|
}
|
|
|
|
|
2008-12-10 15:35:44 +01:00
|
|
|
localRectCache.clear();
|
|
|
|
itemsToSave.clear();
|
|
|
|
saveTimer->stop();
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
//kDebug() << themePath << ">>>>>>>>>>>>>>>>>> theme changed";
|
|
|
|
emit q->repaintNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void colorsChanged()
|
|
|
|
{
|
|
|
|
if (!applyColors) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
eraseRenderer();
|
2008-12-19 02:48:42 +01:00
|
|
|
itemsToSave.clear();
|
|
|
|
saveTimer->stop();
|
2008-11-04 00:08:39 +01:00
|
|
|
emit q->repaintNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
Svg *q;
|
|
|
|
static QHash<QString, SharedSvgRenderer::Ptr> s_renderers;
|
2008-12-01 19:07:20 +01:00
|
|
|
QHash<QString, QRectF> localRectCache;
|
2008-12-08 22:16:55 +01:00
|
|
|
QHash<QString, QPixmap> itemsToSave;
|
2008-12-01 15:35:02 +01:00
|
|
|
QTimer *saveTimer;
|
2008-11-04 00:08:39 +01:00
|
|
|
SharedSvgRenderer::Ptr renderer;
|
|
|
|
QString themePath;
|
|
|
|
QString path;
|
|
|
|
QSizeF size;
|
|
|
|
QSizeF naturalSize;
|
|
|
|
bool multipleImages;
|
|
|
|
bool themed;
|
|
|
|
bool applyColors;
|
|
|
|
};
|
|
|
|
|
|
|
|
QHash<QString, SharedSvgRenderer::Ptr> SvgPrivate::s_renderers;
|
|
|
|
|
|
|
|
Svg::Svg(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new SvgPrivate(this))
|
|
|
|
{
|
2008-12-01 15:35:02 +01:00
|
|
|
d->saveTimer = new QTimer(this);
|
|
|
|
d->saveTimer->setSingleShot(true);
|
|
|
|
connect(d->saveTimer, SIGNAL(timeout()), this, SLOT(scheduledCacheUpdate()));
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Svg::~Svg()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap Svg::pixmap(const QString &elementID)
|
|
|
|
{
|
|
|
|
if (elementID.isNull() || d->multipleImages) {
|
|
|
|
return d->findInCache(elementID, size());
|
|
|
|
} else {
|
|
|
|
return d->findInCache(elementID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Svg::paint(QPainter *painter, const QPointF &point, const QString &elementID)
|
|
|
|
{
|
|
|
|
QPixmap pix(elementID.isNull() ? d->findInCache(elementID, size()) :
|
|
|
|
d->findInCache(elementID));
|
|
|
|
|
|
|
|
if (pix.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
painter->drawPixmap(QRectF(point, pix.size()), pix, QRectF(QPointF(0, 0), pix.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Svg::paint(QPainter *painter, int x, int y, const QString &elementID)
|
|
|
|
{
|
|
|
|
paint(painter, QPointF(x, y), elementID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Svg::paint(QPainter *painter, const QRectF &rect, const QString &elementID)
|
|
|
|
{
|
|
|
|
QPixmap pix(d->findInCache(elementID, rect.size()));
|
|
|
|
painter->drawPixmap(rect, pix, QRectF(QPointF(0, 0), pix.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Svg::paint(QPainter *painter, int x, int y, int width, int height, const QString &elementID)
|
|
|
|
{
|
|
|
|
QPixmap pix(d->findInCache(elementID, QSizeF(width, height)));
|
|
|
|
painter->drawPixmap(x, y, pix, 0, 0, pix.size().width(), pix.size().height());
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize Svg::size() const
|
|
|
|
{
|
|
|
|
return d->size.toSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Svg::resize(qreal width, qreal height)
|
|
|
|
{
|
|
|
|
resize(QSize(width, height));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Svg::resize(const QSizeF &size)
|
|
|
|
{
|
2008-12-01 22:40:13 +01:00
|
|
|
if (qFuzzyCompare(size.width(), d->size.width()) &&
|
|
|
|
qFuzzyCompare(size.height(), d->size.height())) {
|
2008-12-01 22:10:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d->size = size;
|
2008-12-01 22:10:33 +01:00
|
|
|
d->localRectCache.clear();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Svg::resize()
|
|
|
|
{
|
2008-12-01 22:10:33 +01:00
|
|
|
QSizeF newSize;
|
2008-11-04 00:08:39 +01:00
|
|
|
if (d->renderer) {
|
2008-12-01 22:10:33 +01:00
|
|
|
newSize = d->renderer->defaultSize();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2008-12-01 22:10:33 +01:00
|
|
|
|
2008-12-01 22:40:13 +01:00
|
|
|
if (qFuzzyCompare(newSize.width(), d->size.width()) &&
|
|
|
|
qFuzzyCompare(newSize.height(), d->size.height())) {
|
2008-12-01 22:10:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->size = newSize;
|
|
|
|
d->localRectCache.clear();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize Svg::elementSize(const QString &elementId) const
|
|
|
|
{
|
|
|
|
return d->elementRect(elementId).size().toSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Svg::elementRect(const QString &elementId) const
|
|
|
|
{
|
|
|
|
return d->elementRect(elementId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Svg::hasElement(const QString &elementId) const
|
|
|
|
{
|
|
|
|
if (d->path.isNull() && d->themePath.isNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-04 15:25:40 +01:00
|
|
|
QString id = d->cacheId(elementId);
|
|
|
|
if (d->localRectCache.contains(id)) {
|
2008-12-08 22:26:46 +01:00
|
|
|
return d->localRectCache.value(id).isValid();
|
2008-12-04 15:25:40 +01:00
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QRectF elementRect;
|
2008-12-04 15:25:40 +01:00
|
|
|
bool found = Theme::defaultTheme()->findInRectsCache(d->path, id, elementRect);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (found) {
|
2009-01-16 03:46:33 +01:00
|
|
|
d->localRectCache.insert(id, elementRect);
|
2008-11-04 00:08:39 +01:00
|
|
|
return elementRect.isValid();
|
|
|
|
} else {
|
|
|
|
// kDebug() << "** ** *** !!!!!!!! *** ** ** creating renderer due to hasElement miss" << d->path << elementId;
|
2009-01-16 03:46:33 +01:00
|
|
|
return d->findAndCacheElementRect(elementId).isValid();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Svg::elementAtPoint(const QPoint &point) const
|
|
|
|
{
|
2008-12-01 15:35:02 +01:00
|
|
|
Q_UNUSED(point)
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
return QString();
|
|
|
|
/*
|
|
|
|
FIXME: implement when Qt can support us!
|
|
|
|
d->createRenderer();
|
|
|
|
QSizeF naturalSize = d->renderer->defaultSize();
|
|
|
|
qreal dx = d->size.width() / naturalSize.width();
|
|
|
|
qreal dy = d->size.height() / naturalSize.height();
|
|
|
|
//kDebug() << point << "is really"
|
|
|
|
// << QPoint(point.x() *dx, naturalSize.height() - point.y() * dy);
|
|
|
|
|
|
|
|
return QString(); // d->renderer->elementAtPoint(QPoint(point.x() *dx, naturalSize.height() - point.y() * dy));
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Svg::isValid() const
|
|
|
|
{
|
|
|
|
if (d->path.isNull() && d->themePath.isNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->createRenderer();
|
|
|
|
return d->renderer->isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Svg::setContainsMultipleImages(bool multiple)
|
|
|
|
{
|
|
|
|
d->multipleImages = multiple;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Svg::containsMultipleImages() const
|
|
|
|
{
|
|
|
|
return d->multipleImages;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Svg::setImagePath(const QString &svgFilePath)
|
|
|
|
{
|
2008-12-01 19:07:20 +01:00
|
|
|
d->setImagePath(svgFilePath, this);
|
|
|
|
d->eraseRenderer();
|
|
|
|
emit repaintNeeded();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Svg::imagePath() const
|
|
|
|
{
|
|
|
|
return d->themed ? d->themePath : d->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "svg.moc"
|
|
|
|
|