1/***************************************************************************
2 * Copyright 2012 Viranch Mehta <viranch.mehta@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU Library General Public License *
6 * version 2 as published by the Free Software Foundation *
7 * *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU Library General Public License for more details. *
12 * *
13 * You should have received a copy of the GNU Library General Public *
14 * License along with this program; if not, write to the *
15 * Free Software Foundation, Inc., *
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
17 ***************************************************************************/
18
19#include "kgimageprovider_p.h"
20
21#include <QPainter>
22#include <KgThemeProvider>
23
24KgImageProvider::KgImageProvider(KgThemeProvider* prov) :
25 QDeclarativeImageProvider(QDeclarativeImageProvider::Image),
26 m_provider(prov)
27{
28 reloadRenderer();
29}
30
31void KgImageProvider::reloadRenderer()
32{
33 m_renderer.load(m_provider->currentTheme()->graphicsPath());
34 m_themeName = m_provider->currentThemeName();
35}
36
37QImage KgImageProvider::requestImage(const QString& source, QSize *size, const QSize& requestedSize)
38{
39 Q_UNUSED(requestedSize); // this is always QSize(-1,-1) for some reason
40
41 QImage image;
42
43 const QStringList tokens = source.split("/");
44 if (tokens.size() > 2) {
45 const QString theme = tokens[0];
46 const QString spriteKey = tokens[1];
47 const QStringList size = tokens[2].split("x");
48 uint width = qRound(size[0].toDouble());
49 uint height = qRound(size[1].toDouble());
50
51 if (theme != m_themeName) {
52 reloadRenderer();
53 }
54
55 if (m_renderer.isValid()) {
56 if (width == 0 || height == 0) {
57 image = QImage(m_renderer.boundsOnElement(spriteKey).size().toSize(), QImage::Format_ARGB32_Premultiplied);
58 } else {
59 image = QImage(width, height, QImage::Format_ARGB32_Premultiplied);
60 }
61 image.fill(Qt::transparent);
62 QPainter* painter = new QPainter(&image);
63 m_renderer.render(painter, spriteKey);
64 delete painter;
65 }
66 }
67
68 if (size) *size = image.size();
69
70 return image;
71}
72
73#include "kgimageprovider_p.moc"
74