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>
|
2007-05-29 09:56:21 +02:00
|
|
|
#include <QSharedData>
|
2007-03-08 00:27:37 +01:00
|
|
|
|
2008-02-22 02:05:11 +01:00
|
|
|
#include <KColorScheme>
|
2008-10-15 22:55:07 +02:00
|
|
|
#include <KConfigGroup>
|
|
|
|
#include <KDebug>
|
2008-02-22 02:05:11 +01:00
|
|
|
#include <KIconEffect>
|
|
|
|
#include <KGlobalSettings>
|
2008-10-15 22:55:07 +02:00
|
|
|
#include <KSharedPtr>
|
|
|
|
#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()
|
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "leaving this world for a better one.";
|
2007-05-29 09:56:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
class SvgPrivate
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
|
|
|
public:
|
2008-07-01 20:56:43 +02:00
|
|
|
SvgPrivate(Svg *svg)
|
2008-04-16 22:30:18 +02:00
|
|
|
: q(svg),
|
|
|
|
renderer(0),
|
2008-04-17 11:09:48 +02:00
|
|
|
multipleImages(false),
|
2008-04-27 23:49:09 +02:00
|
|
|
themed(false),
|
|
|
|
applyColors(false)
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2008-01-07 21:20:02 +01:00
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
~SvgPrivate()
|
2008-01-07 21:20:02 +01:00
|
|
|
{
|
|
|
|
eraseRenderer();
|
|
|
|
}
|
|
|
|
|
2008-10-17 04:30:08 +02:00
|
|
|
QString cacheId(const QString &elementId)
|
|
|
|
{
|
2008-10-17 23:27:32 +02:00
|
|
|
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-10-17 04:30:08 +02:00
|
|
|
}
|
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
bool setImagePath(const QString &imagePath, Svg *q)
|
2008-01-07 21:20:02 +01:00
|
|
|
{
|
2008-10-15 22:55:07 +02:00
|
|
|
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();
|
|
|
|
|
2008-02-07 05:33:57 +01:00
|
|
|
if (themed) {
|
2008-10-12 00:48:38 +02:00
|
|
|
QObject::disconnect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
|
|
|
|
q, SLOT(themeChanged()));
|
|
|
|
QObject::disconnect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()),
|
|
|
|
q, SLOT(colorsChanged()));
|
2008-02-07 05:33:57 +01:00
|
|
|
}
|
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
themed = isThemed;
|
2008-10-12 13:05:42 +02:00
|
|
|
path.clear();
|
|
|
|
themePath.clear();
|
2008-01-07 21:20:02 +01:00
|
|
|
|
|
|
|
if (themed) {
|
|
|
|
themePath = imagePath;
|
2008-10-12 00:48:38 +02:00
|
|
|
QObject::connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
|
|
|
|
q, SLOT(themeChanged()));
|
2008-02-22 02:05:11 +01:00
|
|
|
|
|
|
|
// check if svg wants colorscheme applied
|
2008-10-15 22:55:07 +02:00
|
|
|
checkApplyColorHint();
|
2008-04-22 23:10:14 +02:00
|
|
|
if (applyColors && !Theme::defaultTheme()->colorScheme()) {
|
2008-10-12 00:48:38 +02:00
|
|
|
QObject::connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()),
|
|
|
|
q, SLOT(colorsChanged()));
|
2008-02-22 02:05:11 +01:00
|
|
|
}
|
2008-10-15 22:55:07 +02:00
|
|
|
} else if (QFile::exists(imagePath)) {
|
2007-05-27 10:01:31 +02:00
|
|
|
path = imagePath;
|
2008-10-15 22:55:07 +02:00
|
|
|
} else {
|
|
|
|
kDebug() << "file '" << path << "' does not exist!";
|
2008-02-19 05:28:17 +01:00
|
|
|
}
|
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
return updateNeeded;
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2008-09-19 18:41:39 +02:00
|
|
|
QPixmap findInCache(const QString &elementId, const QSizeF &s = QSizeF())
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2008-04-22 01:00:58 +02:00
|
|
|
QSize size;
|
2008-10-31 10:43:51 +01:00
|
|
|
if (elementId.isEmpty() || (multipleImages && s.isValid())) {
|
2008-04-22 01:00:58 +02:00
|
|
|
size = s.toSize();
|
2008-04-13 19:27:02 +02:00
|
|
|
} else {
|
2008-10-17 04:30:08 +02:00
|
|
|
size = elementRect(elementId).size().toSize();
|
2008-04-13 19:27:02 +02:00
|
|
|
}
|
|
|
|
|
2008-04-22 01:00:58 +02:00
|
|
|
if (!size.isValid()) {
|
2008-09-19 18:41:39 +02:00
|
|
|
return QPixmap();
|
2008-04-13 19:27:02 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
QString id = QString::fromLatin1("%3_%2_%1_").
|
|
|
|
arg(size.width()).arg(size.height()).arg(path);
|
2008-04-22 01:00:58 +02:00
|
|
|
|
2007-05-19 00:05:00 +02:00
|
|
|
if (!elementId.isEmpty()) {
|
|
|
|
id.append(elementId);
|
|
|
|
}
|
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
//kDebug() << "id is " << id;
|
2008-02-22 02:05:11 +01:00
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
Theme *theme = Theme::defaultTheme();
|
2008-09-19 18:41:39 +02:00
|
|
|
QPixmap p;
|
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
if (theme->findInCache(id, p)) {
|
|
|
|
//kDebug() << "found cached version of " << id << p.size();
|
2008-09-19 18:41:39 +02:00
|
|
|
return p;
|
2007-05-21 00:30:41 +02:00
|
|
|
} else {
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "didn't find cached version of " << id << ", so re-rendering";
|
2007-05-21 00:30:41 +02:00
|
|
|
}
|
2008-04-21 21:32:26 +02:00
|
|
|
|
2008-04-14 16:41:41 +02:00
|
|
|
//kDebug() << "size for " << elementId << " is " << s;
|
2008-04-22 01:00:58 +02:00
|
|
|
// we have to re-render this puppy
|
2008-04-21 21:32:26 +02:00
|
|
|
|
2008-04-22 01:00:58 +02:00
|
|
|
p = QPixmap(size);
|
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
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
createRenderer();
|
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
|
|
|
}
|
2008-01-07 21:20:02 +01:00
|
|
|
|
2007-03-08 00:27:37 +01:00
|
|
|
renderPainter.end();
|
2008-01-07 21:20:02 +01:00
|
|
|
|
2008-02-22 02:05:11 +01:00
|
|
|
// Apply current color scheme if the svg asks for it
|
|
|
|
if (applyColors) {
|
|
|
|
QImage itmp = p.toImage();
|
2008-10-15 22:55:07 +02:00
|
|
|
KIconEffect::colorize(itmp, theme->color(Theme::BackgroundColor), 1.0);
|
2008-02-22 02:05:11 +01:00
|
|
|
p = p.fromImage(itmp);
|
|
|
|
}
|
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
theme->insertIntoCache(id, p);
|
2008-09-19 18:41:39 +02:00
|
|
|
return p;
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2007-05-27 10:01:31 +02:00
|
|
|
void createRenderer()
|
|
|
|
{
|
|
|
|
if (renderer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-17 04:30:08 +02:00
|
|
|
//kDebug() << kBacktrace();
|
2008-01-07 21:20:02 +01:00
|
|
|
if (themed && path.isEmpty()) {
|
2008-04-22 23:10:14 +02:00
|
|
|
path = Plasma::Theme::defaultTheme()->imagePath(themePath);
|
2007-05-27 10:01:31 +02:00
|
|
|
}
|
|
|
|
|
2008-10-18 01:41:13 +02:00
|
|
|
//kDebug() << "********************************";
|
|
|
|
//kDebug() << "FAIL! **************************";
|
|
|
|
//kDebug() << path << "**";
|
2008-10-17 07:17:46 +02:00
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
QHash<QString, SharedSvgRenderer::Ptr>::const_iterator it = s_renderers.find(path);
|
2007-05-29 09:56:21 +02:00
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
if (it != s_renderers.end()) {
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "gots us an existing one!";
|
2007-05-29 09:56:21 +02:00
|
|
|
renderer = it.value();
|
|
|
|
} else {
|
|
|
|
renderer = new SharedSvgRenderer(path);
|
2008-10-15 22:55:07 +02:00
|
|
|
s_renderers[path] = renderer;
|
2007-05-29 09:56:21 +02:00
|
|
|
}
|
2007-08-31 17:15:04 +02:00
|
|
|
|
2008-10-17 04:52:51 +02:00
|
|
|
if (size == QSizeF()) {
|
2008-10-17 04:30:08 +02:00
|
|
|
size = renderer->defaultSize();
|
|
|
|
}
|
2007-05-27 10:01:31 +02:00
|
|
|
}
|
|
|
|
|
2007-12-19 21:29:22 +01:00
|
|
|
void eraseRenderer()
|
|
|
|
{
|
2008-10-12 00:48:38 +02:00
|
|
|
if (renderer && renderer.count() == 2) {
|
2007-12-19 21:29:22 +01:00
|
|
|
// this and the cache reference it; and boy is this not thread safe ;)
|
2008-10-15 22:55:07 +02:00
|
|
|
s_renderers.erase(s_renderers.find(path));
|
2007-12-19 21:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
renderer = 0;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
QRectF elementRect(const QString &elementId)
|
2007-07-27 01:07:26 +02:00
|
|
|
{
|
2008-10-16 20:25:20 +02:00
|
|
|
QRectF rect;
|
2008-10-17 23:27:32 +02:00
|
|
|
|
|
|
|
if (themed && path.isEmpty()) {
|
|
|
|
path = Plasma::Theme::defaultTheme()->imagePath(themePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found = Theme::defaultTheme()->findInRectsCache(path, cacheId(elementId), rect);
|
2008-10-16 20:25:20 +02:00
|
|
|
|
2008-10-17 04:30:08 +02:00
|
|
|
if (found) {
|
2008-10-16 20:25:20 +02:00
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2008-10-17 04:30:08 +02:00
|
|
|
return findAndCacheElementRect(elementId);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF findAndCacheElementRect(const QString &elementId)
|
|
|
|
{
|
2007-07-27 01:07:26 +02:00
|
|
|
createRenderer();
|
2008-10-17 04:30:08 +02:00
|
|
|
QRectF elementRect = renderer->elementExists(elementId) ?
|
|
|
|
renderer->boundsOnElement(elementId) : QRectF();
|
2008-10-17 23:27:32 +02:00
|
|
|
naturalSize = renderer->defaultSize();
|
2007-07-27 01:07:26 +02:00
|
|
|
qreal dx = size.width() / naturalSize.width();
|
|
|
|
qreal dy = size.height() / naturalSize.height();
|
2007-11-19 00:03:57 +01:00
|
|
|
|
2008-10-16 20:25:20 +02:00
|
|
|
elementRect = QRectF(elementRect.x() * dx, elementRect.y() * dy,
|
|
|
|
elementRect.width() * dx, elementRect.height() * dy);
|
2008-10-17 04:30:08 +02:00
|
|
|
Theme::defaultTheme()->insertIntoRectsCache(path, cacheId(elementId), elementRect);
|
2008-10-16 20:25:20 +02:00
|
|
|
|
|
|
|
return elementRect;
|
2007-07-27 01:07:26 +02:00
|
|
|
}
|
2008-02-19 05:28:17 +01:00
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
QMatrix matrixForElement(const QString &elementId)
|
2007-08-02 18:14:35 +02:00
|
|
|
{
|
|
|
|
createRenderer();
|
|
|
|
return renderer->matrixForElement(elementId);
|
|
|
|
}
|
2007-07-27 01:07:26 +02:00
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
void checkApplyColorHint()
|
|
|
|
{
|
|
|
|
KConfigGroup cg(KGlobal::config(), "SvgHints");
|
|
|
|
QString cgKey = themePath + "-hint-apply-color-scheme";
|
|
|
|
if (cg.hasKey(cgKey)) {
|
|
|
|
applyColors = cg.readEntry(cgKey, false);
|
|
|
|
} else {
|
|
|
|
createRenderer();
|
|
|
|
applyColors = renderer->elementExists("hint-apply-color-scheme");
|
|
|
|
cg.writeEntry(cgKey, applyColors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-16 22:30:18 +02:00
|
|
|
void themeChanged()
|
|
|
|
{
|
|
|
|
if (!themed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-22 23:10:14 +02:00
|
|
|
QString newPath = Theme::defaultTheme()->imagePath(themePath);
|
2008-04-16 22:30:18 +02:00
|
|
|
|
|
|
|
if (path == newPath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = newPath;
|
|
|
|
//delete d->renderer; we're a KSharedPtr
|
|
|
|
eraseRenderer();
|
2008-06-04 04:51:31 +02:00
|
|
|
|
|
|
|
// check if new theme svg wants colorscheme applied
|
2008-10-15 22:55:07 +02:00
|
|
|
bool wasApplyColors = applyColors;
|
|
|
|
checkApplyColorHint();
|
2008-06-04 04:51:31 +02:00
|
|
|
if (applyColors && !Theme::defaultTheme()->colorScheme()) {
|
2008-10-15 22:55:07 +02:00
|
|
|
if (!wasApplyColors) {
|
|
|
|
QObject::connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()),
|
|
|
|
q, SLOT(colorsChanged()));
|
|
|
|
}
|
2008-06-04 04:51:31 +02:00
|
|
|
} else {
|
2008-10-12 00:48:38 +02:00
|
|
|
QObject::disconnect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()),
|
|
|
|
q, SLOT(colorsChanged()));
|
2008-06-04 04:51:31 +02:00
|
|
|
}
|
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
//kDebug() << themePath << ">>>>>>>>>>>>>>>>>> theme changed";
|
2008-04-16 22:30:18 +02:00
|
|
|
emit q->repaintNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void colorsChanged()
|
|
|
|
{
|
|
|
|
if (!applyColors) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
eraseRenderer();
|
|
|
|
emit q->repaintNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
Svg *q;
|
2008-10-15 22:55:07 +02:00
|
|
|
static QHash<QString, SharedSvgRenderer::Ptr> s_renderers;
|
2007-05-29 09:56:21 +02:00
|
|
|
SharedSvgRenderer::Ptr renderer;
|
2007-03-08 00:27:37 +01:00
|
|
|
QString themePath;
|
|
|
|
QString path;
|
2007-05-19 00:05:00 +02:00
|
|
|
QSizeF size;
|
2008-10-17 23:27:32 +02:00
|
|
|
QSizeF naturalSize;
|
2008-04-17 11:09:48 +02:00
|
|
|
bool multipleImages;
|
2007-05-27 10:01:31 +02:00
|
|
|
bool themed;
|
2008-02-22 02:05:11 +01:00
|
|
|
bool applyColors;
|
2007-03-08 00:27:37 +01:00
|
|
|
};
|
|
|
|
|
2008-10-15 22:55:07 +02:00
|
|
|
QHash<QString, SharedSvgRenderer::Ptr> SvgPrivate::s_renderers;
|
2007-05-29 09:56:21 +02:00
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
Svg::Svg(QObject *parent)
|
2007-05-29 22:27:51 +02:00
|
|
|
: QObject(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new SvgPrivate(this))
|
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
|
|
|
}
|
|
|
|
|
2008-09-19 18:41:39 +02:00
|
|
|
QPixmap Svg::pixmap(const QString &elementID)
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2008-09-23 17:52:45 +02:00
|
|
|
if (elementID.isNull() || d->multipleImages) {
|
2008-09-19 18:41:39 +02:00
|
|
|
return d->findInCache(elementID, size());
|
2008-06-19 12:15:09 +02:00
|
|
|
} else {
|
2008-09-19 18:41:39 +02:00
|
|
|
return d->findInCache(elementID);
|
2008-06-19 12:15:09 +02:00
|
|
|
}
|
2008-09-19 18:41:39 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
void Svg::paint(QPainter *painter, const QPointF &point, const QString &elementID)
|
2008-09-19 18:41:39 +02:00
|
|
|
{
|
|
|
|
QPixmap pix(elementID.isNull() ? d->findInCache(elementID, size()) :
|
|
|
|
d->findInCache(elementID));
|
2008-04-21 21:32:26 +02:00
|
|
|
|
|
|
|
if (pix.isNull()) {
|
2008-04-13 19:27:02 +02:00
|
|
|
return;
|
2008-04-21 21:32:26 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
painter->drawPixmap(QRectF(point, pix.size()), pix, QRectF(QPointF(0, 0), pix.size()));
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +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
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
void Svg::paint(QPainter *painter, const QRectF &rect, const QString &elementID)
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2008-09-19 18:41:39 +02:00
|
|
|
QPixmap pix(d->findInCache(elementID, rect.size()));
|
2008-10-12 00:48:38 +02:00
|
|
|
painter->drawPixmap(rect, pix, QRectF(QPointF(0, 0), pix.size()));
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2008-10-17 19:45:18 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2008-04-16 16:31:43 +02:00
|
|
|
QSize Svg::size() const
|
|
|
|
{
|
|
|
|
return d->size.toSize();
|
|
|
|
}
|
|
|
|
|
2008-10-11 18:19:04 +02:00
|
|
|
void Svg::resize(qreal width, qreal height)
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2008-10-11 18:19:04 +02:00
|
|
|
resize(QSize(width, height));
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2008-10-11 18:19:04 +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()
|
|
|
|
{
|
2008-10-15 22:55:07 +02:00
|
|
|
if (d->renderer) {
|
|
|
|
d->size = d->renderer->defaultSize();
|
|
|
|
} else {
|
|
|
|
d->size = QSizeF();
|
|
|
|
}
|
2007-05-21 00:56:38 +02:00
|
|
|
}
|
|
|
|
|
2008-10-11 18:19:04 +02:00
|
|
|
QSize Svg::elementSize(const QString &elementId) const
|
2007-05-19 00:05:00 +02:00
|
|
|
{
|
2008-10-17 04:30:08 +02:00
|
|
|
return d->elementRect(elementId).size().toSize();
|
2007-05-21 00:39:38 +02:00
|
|
|
}
|
|
|
|
|
2008-10-11 18:19:04 +02:00
|
|
|
QRectF Svg::elementRect(const QString &elementId) const
|
2007-07-27 01:07:26 +02:00
|
|
|
{
|
|
|
|
return d->elementRect(elementId);
|
|
|
|
}
|
|
|
|
|
2008-10-11 18:19:04 +02:00
|
|
|
bool Svg::hasElement(const QString &elementId) const
|
2007-05-29 22:27:51 +02:00
|
|
|
{
|
2008-10-15 22:55:07 +02:00
|
|
|
if (d->path.isNull() && d->themePath.isNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-17 04:30:08 +02:00
|
|
|
|
2008-10-16 20:25:20 +02:00
|
|
|
QRectF elementRect;
|
2008-10-17 04:30:08 +02:00
|
|
|
bool found = Theme::defaultTheme()->findInRectsCache(d->path, d->cacheId(elementId), elementRect);
|
2008-10-16 20:25:20 +02:00
|
|
|
|
2008-10-17 04:30:08 +02:00
|
|
|
if (found) {
|
|
|
|
return elementRect.isValid();
|
2008-10-16 20:25:20 +02:00
|
|
|
} else {
|
2008-10-17 07:17:46 +02:00
|
|
|
// kDebug() << "** ** *** !!!!!!!! *** ** ** creating renderer due to hasElement miss" << d->path << elementId;
|
2008-10-17 04:30:08 +02:00
|
|
|
d->findAndCacheElementRect(elementId);
|
2008-10-16 20:25:20 +02:00
|
|
|
return d->renderer->elementExists(elementId);
|
|
|
|
}
|
2007-05-29 22:27:51 +02:00
|
|
|
}
|
|
|
|
|
2007-11-17 22:34:28 +01:00
|
|
|
QString Svg::elementAtPoint(const QPoint &point) const
|
|
|
|
{
|
2008-09-19 18:41:39 +02:00
|
|
|
return QString();
|
|
|
|
/*
|
|
|
|
FIXME: implement when Qt can support us!
|
2007-11-17 22:34:28 +01:00
|
|
|
d->createRenderer();
|
|
|
|
QSizeF naturalSize = d->renderer->defaultSize();
|
|
|
|
qreal dx = d->size.width() / naturalSize.width();
|
|
|
|
qreal dy = d->size.height() / naturalSize.height();
|
2008-10-12 00:48:38 +02:00
|
|
|
//kDebug() << point << "is really"
|
|
|
|
// << QPoint(point.x() *dx, naturalSize.height() - point.y() * dy);
|
2007-11-17 22:34:28 +01:00
|
|
|
|
|
|
|
return QString(); // d->renderer->elementAtPoint(QPoint(point.x() *dx, naturalSize.height() - point.y() * dy));
|
2008-09-19 18:41:39 +02:00
|
|
|
*/
|
2007-11-17 22:34:28 +01:00
|
|
|
}
|
|
|
|
|
2007-07-23 02:50:10 +02:00
|
|
|
bool Svg::isValid() const
|
|
|
|
{
|
2008-10-15 22:55:07 +02:00
|
|
|
if (d->path.isNull() && d->themePath.isNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-23 02:50:10 +02:00
|
|
|
d->createRenderer();
|
|
|
|
return d->renderer->isValid();
|
|
|
|
}
|
|
|
|
|
2008-04-17 11:09:48 +02:00
|
|
|
void Svg::setContainsMultipleImages(bool multiple)
|
2007-05-29 22:27:51 +02:00
|
|
|
{
|
2008-04-17 11:09:48 +02:00
|
|
|
d->multipleImages = multiple;
|
2007-05-29 22:27:51 +02:00
|
|
|
}
|
|
|
|
|
2008-04-17 11:09:48 +02:00
|
|
|
bool Svg::containsMultipleImages() const
|
2007-05-29 22:27:51 +02:00
|
|
|
{
|
2008-04-17 11:09:48 +02:00
|
|
|
return d->multipleImages;
|
2007-05-29 22:27:51 +02:00
|
|
|
}
|
|
|
|
|
2008-04-16 22:48:53 +02:00
|
|
|
void Svg::setImagePath(const QString &svgFilePath)
|
2007-12-19 21:29:22 +01:00
|
|
|
{
|
2008-10-15 22:55:07 +02:00
|
|
|
if (d->setImagePath(svgFilePath, this)) {
|
|
|
|
}
|
|
|
|
d->eraseRenderer();
|
|
|
|
emit repaintNeeded();
|
2007-12-19 21:29:22 +01:00
|
|
|
}
|
|
|
|
|
2008-04-16 22:48:53 +02:00
|
|
|
QString Svg::imagePath() const
|
2007-12-19 21:29:22 +01:00
|
|
|
{
|
2008-02-06 02:26:18 +01:00
|
|
|
return d->themed ? d->themePath : d->path;
|
2007-12-19 21:29:22 +01:00
|
|
|
}
|
|
|
|
|
2007-03-08 00:27:37 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "svg.moc"
|
|
|
|
|