KDEGames

kgimageprovider.cpp
1 /***************************************************************************
2  * Copyright 2012 Viranch Mehta <[email protected]> *
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 <QGuiApplication>
23 #include <KgThemeProvider>
24 
25 KgImageProvider::KgImageProvider(KgThemeProvider* prov) :
26  QQuickImageProvider(Image),
27  m_provider(prov)
28 {
29  reloadRenderer();
30 }
31 
32 void KgImageProvider::reloadRenderer()
33 {
34  m_renderer.load(m_provider->currentTheme()->graphicsPath());
35  m_themeName = m_provider->currentThemeName();
36 }
37 
38 QImage KgImageProvider::requestImage(const QString& source, QSize *size, const QSize& requestedSize)
39 {
40  Q_UNUSED(requestedSize); // this is always QSize(-1,-1) for some reason
41 
42  QImage image;
43 
44  const QStringList tokens = source.split(QStringLiteral("/"));
45  if (tokens.size() > 2) {
46  const QString theme = tokens[0];
47  const QString spriteKey = tokens[1];
48  const QStringList size = tokens[2].split(QLatin1Char('x'));
49  uint width = qRound(size[0].toDouble());
50  uint height = qRound(size[1].toDouble());
51 
52  if (theme != m_themeName) {
53  reloadRenderer();
54  }
55 
56  if (m_renderer.isValid()) {
57  if (width == 0 || height == 0) {
58  image = QImage(m_renderer.boundsOnElement(spriteKey).size().toSize()*qApp->devicePixelRatio(), QImage::Format_ARGB32_Premultiplied);
59  } else {
60  image = QImage(QSize(width, height)*qApp->devicePixelRatio(), QImage::Format_ARGB32_Premultiplied);
61  }
62  image.fill(Qt::transparent);
63  QPainter* painter = new QPainter(&image);
64  m_renderer.render(painter, spriteKey);
65 
66  //this is deliberately set after .render as Qt <= 5.4 has a bug in QSVGRenderer which makes it not fill the image properly
67  image.setDevicePixelRatio(qApp->devicePixelRatio());
68  delete painter;
69  }
70  }
71 
72  if (size) *size = image.size();
73 
74  return image;
75 }
76 
Format_ARGB32_Premultiplied
A theme provider manages KgTheme instances, and maintains a selection of the currentTheme().
void setDevicePixelRatio(qreal scaleFactor)
int size() const const
void fill(uint pixelValue)
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QSize size() const const
transparent
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:37:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.