KDEGames

kgimageprovider.cpp
1 /*
2  SPDX-FileCopyrightText: 2012 Viranch Mehta <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-only
5 */
6 
7 #include "kgimageprovider_p.h"
8 
9 // own
10 #include "kgthemeprovider.h"
11 // Qt
12 #include <QPainter>
13 #include <QGuiApplication>
14 
15 KgImageProvider::KgImageProvider(KgThemeProvider* prov) :
16  QQuickImageProvider(Image),
17  m_provider(prov)
18 {
19  reloadRenderer();
20 }
21 
22 void KgImageProvider::reloadRenderer()
23 {
24  m_renderer.load(m_provider->currentTheme()->graphicsPath());
25  m_themeName = m_provider->currentThemeName();
26 }
27 
28 QImage KgImageProvider::requestImage(const QString& source, QSize *size, const QSize& requestedSize)
29 {
30  Q_UNUSED(requestedSize); // this is always QSize(-1,-1) for some reason
31 
32  QImage image;
33 
34  const QStringList tokens = source.split(QStringLiteral("/"));
35  if (tokens.size() > 2) {
36  const QString theme = tokens[0];
37  const QString spriteKey = tokens[1];
38  const QStringList size = tokens[2].split(QLatin1Char('x'));
39  uint width = qRound(size[0].toDouble());
40  uint height = qRound(size[1].toDouble());
41 
42  if (theme != m_themeName) {
43  reloadRenderer();
44  }
45 
46  if (m_renderer.isValid()) {
47  if (width == 0 || height == 0) {
48  image = QImage(m_renderer.boundsOnElement(spriteKey).size().toSize()*qApp->devicePixelRatio(), QImage::Format_ARGB32_Premultiplied);
49  } else {
50  image = QImage(QSize(width, height)*qApp->devicePixelRatio(), QImage::Format_ARGB32_Premultiplied);
51  }
52  image.fill(Qt::transparent);
53  QPainter* painter = new QPainter(&image);
54  m_renderer.render(painter, spriteKey);
55 
56  //this is deliberately set after .render as Qt <= 5.4 has a bug in QSVGRenderer which makes it not fill the image properly
57  image.setDevicePixelRatio(qApp->devicePixelRatio());
58  delete painter;
59  }
60  }
61 
62  if (size) *size = image.size();
63 
64  return image;
65 }
66 
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-2021 The KDE developers.
Generated on Tue Dec 7 2021 22:34:15 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.