KDEGames

kgameimageprovider.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Viranch Mehta <viranch.mehta@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-only
5*/
6
7#include "kgameimageprovider_p.h"
8
9// own
10#include "kgamethemeprovider.h"
11// Qt
12#include <QGuiApplication>
13#include <QPainter>
14
15KGameImageProvider::KGameImageProvider(KGameThemeProvider *prov)
16 : QQuickImageProvider(Image)
17 , m_provider(prov)
18{
19 reloadRenderer();
20}
21
22void KGameImageProvider::reloadRenderer()
23{
24 m_renderer.load(m_provider->currentTheme()->graphicsPath());
25 m_themeName = m_provider->currentThemeName();
26}
27
28QImage KGameImageProvider::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 QList<QStringView> tokens = QStringView(source).split(QLatin1Char('/'));
35 if (tokens.size() > 2) {
36 const QStringView &theme = tokens[0];
37 const QString spriteKey = tokens[1].toString();
38 const QList<QStringView> 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)
63 *size = image.size();
64
65 return image;
66}
A theme provider manages KGameTheme instances, and maintains a selection of the currentTheme().
Format_ARGB32_Premultiplied
void fill(Qt::GlobalColor color)
void setDevicePixelRatio(qreal scaleFactor)
QSize size() const const
qsizetype size() const const
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
transparent
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:49:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.