LibKmahjongg

kmahjonggbackground.cpp
1/*
2 SPDX-FileCopyrightText: 1997 Mathias Mueller <in5y158@public.uni-hamburg.de>
3 SPDX-FileCopyrightText: 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8// own
9#include "kmahjonggbackground.h"
10
11// Qt
12#include <QFile>
13#include <QPainter>
14#include <QPixmap>
15#include <QPixmapCache>
16#include <QSvgRenderer>
17#include <QGuiApplication>
18
19// KF
20#include <KConfig>
21#include <KConfigGroup>
22#include <KLocalizedString>
23
24// LibKMahjongg
25#include "libkmahjongg_debug.h"
26
27class KMahjonggBackgroundPrivate
28{
29public:
30 KMahjonggBackgroundPrivate() = default;
31
32public:
33 QString name;
34 QString description;
35 QString license;
36 QString copyrightText;
37 QString version;
38 QString website;
39 QString bugReportUrl;
40 QString authorName;
41 QString authorEmailAddress;
42
43 QString pixmapCacheNameFromElementId(const QString &elementid, short width, short height);
44 QPixmap renderBG(short width, short height);
45
46 QPixmap backgroundPixmap;
47 QBrush backgroundBrush;
48 QString filename;
49 QString graphicspath;
50 short w = 1;
51 short h = 1;
52
53 QSvgRenderer svg;
54
55 bool graphicsLoaded = false;
56 bool isPlain = false;
57 bool isTiled = true;
58 bool isSVG = false;
59};
60
61KMahjonggBackground::KMahjonggBackground()
62 : d_ptr(new KMahjonggBackgroundPrivate)
63{
64}
65
66KMahjonggBackground::~KMahjonggBackground() = default;
67
68bool KMahjonggBackground::loadDefault()
69{
70 // Set default background here.
71 const QString bgPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kmahjongglib/backgrounds/egyptian.desktop"));
72 qCDebug(LIBKMAHJONGG_LOG) << "Inside LoadDefault(), located background at" << bgPath;
73 if (bgPath.isEmpty()) {
74 return false;
75 }
76 return load(bgPath, 0, 0);
77}
78
79#define kBGVersionFormat 1
80
81bool KMahjonggBackground::load(const QString &file, short width, short height)
82{
84
85 // qCDebug(LIBKMAHJONGG_LOG) << "Background loading";
86 d->isSVG = false;
87
88 // qCDebug(LIBKMAHJONGG_LOG) << "Attempting to load .desktop at" << file;
89
90 // verify if it is a valid file first and if we can open it
91 QFile bgfile(file);
92 if (!bgfile.open(QIODevice::ReadOnly)) {
93 return false;
94 }
95 bgfile.close();
96
97 KConfig bgconfig(file, KConfig::SimpleConfig);
98 KConfigGroup group = bgconfig.group(QStringLiteral("KMahjonggBackground"));
99
100 d->isPlain = group.readEntry("Plain", 0) != 0;
101 d->name = group.readEntry("Name"); // Returns translated data
102 d->description = group.readEntry("Description");
103 d->license = group.readEntry("License");
104 d->copyrightText = group.readEntry("Copyright");
105 d->version = group.readEntry("Version");
106 d->website = group.readEntry("Website");
107 d->bugReportUrl = group.readEntry("BugReportUrl");
108 d->authorName = group.readEntry("Author");
109 d->authorEmailAddress = group.readEntry("AuthorEmail");
110
111 // Version control
112 int bgversion = group.readEntry("VersionFormat", 0);
113 // Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
114 if (bgversion > kBGVersionFormat) {
115 return false;
116 }
117
118 if (d->isPlain) {
119 // qCDebug(LIBKMAHJONGG_LOG) << "Using plain background";
120 d->graphicspath.clear();
121 d->filename = file;
122 return true;
123 }
124
125 QString graphName = group.readEntry("FileName");
126
127 d->graphicspath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kmahjongglib/backgrounds/") + graphName);
128
129 qCDebug(LIBKMAHJONGG_LOG) << "Using background at" << d->graphicspath;
130
131 if (d->graphicspath.isEmpty()) {
132 return false;
133 }
134
135 if (group.readEntry("Tiled", 0) != 0) {
136 d->w = group.readEntry("Width", 0);
137 d->h = group.readEntry("Height", 0);
138 d->isTiled = true;
139 } else {
140 d->w = width;
141 d->h = height;
142 d->isTiled = false;
143 }
144 d->graphicsLoaded = false;
145 d->filename = file;
146 return true;
147}
148
149bool KMahjonggBackground::loadGraphics()
150{
152
153 if (d->graphicsLoaded || d->isPlain) {
154 return true;
155 }
156
157 d->svg.load(d->graphicspath);
158 if (d->svg.isValid()) {
159 d->isSVG = true;
160 } else {
161 // qCDebug(LIBKMAHJONGG_LOG) << "could not load svg";
162 return false;
163 }
164 return true;
165}
166
167void KMahjonggBackground::sizeChanged(int newW, int newH)
168{
170
171 // in tiled mode we do not care about the whole field size
172 if (d->isTiled || d->isPlain) {
173 return;
174 }
175
176 if (newW == d->w && newH == d->h) {
177 return;
178 }
179 d->w = newW;
180 d->h = newH;
181}
182
183QString KMahjonggBackgroundPrivate::pixmapCacheNameFromElementId(const QString &elementid, short width, short height)
184{
185 return name + elementid + QStringLiteral("W%1H%2").arg(width).arg(height);
186}
187
188QPixmap KMahjonggBackgroundPrivate::renderBG(short width, short height)
189{
190 QPixmap qiRend(width, height);
191 qiRend.fill(Qt::transparent);
192
193 if (svg.isValid()) {
194 QPainter p(&qiRend);
195 svg.render(&p);
196 }
197 return qiRend;
198}
199
200QBrush &KMahjonggBackground::getBackground()
201{
203
204 if (d->isPlain) {
205 d->backgroundBrush = QBrush(QPixmap());
206 } else {
207 const qreal dpr = qApp->devicePixelRatio();
208 const short width = d->w * dpr;
209 const short height = d->h * dpr;
210
211 // using raw pixmap size with cache id, as the rendering is done dpr-ignorant
212 const QString pixmapCacheName = d->pixmapCacheNameFromElementId(d->filename, width, height);
213 if (!QPixmapCache::find(pixmapCacheName, &d->backgroundPixmap)) {
214 d->backgroundPixmap = d->renderBG(width, height);
215 d->backgroundPixmap.setDevicePixelRatio(dpr);
216 QPixmapCache::insert(pixmapCacheName, d->backgroundPixmap);
217 }
218 d->backgroundBrush = QBrush(d->backgroundPixmap);
219 }
220 return d->backgroundBrush;
221}
222
223QString KMahjonggBackground::path() const
224{
226
227 return d->filename;
228}
229
230QString KMahjonggBackground::name() const
231{
233
234 return d->name;
235}
236
237QString KMahjonggBackground::description() const
238{
240
241 return d->description;
242}
243
244QString KMahjonggBackground::license() const
245{
247
248 return d->license;
249}
250
251QString KMahjonggBackground::copyrightText() const
252{
254
255 return d->copyrightText;
256}
257
258QString KMahjonggBackground::version() const
259{
261
262 return d->version;
263}
264
265QString KMahjonggBackground::website() const
266{
268
269 return d->website;
270}
271
272QString KMahjonggBackground::bugReportUrl() const
273{
275
276 return d->bugReportUrl;
277}
278
279QString KMahjonggBackground::authorName() const
280{
282
283 return d->authorName;
284}
285
286QString KMahjonggBackground::authorEmailAddress() const
287{
289
290 return d->authorEmailAddress;
291}
292
293bool KMahjonggBackground::isPlain() const
294{
296
297 return d->isPlain;
298}
KConfigGroup group(const QString &group)
QString readEntry(const char *key, const char *aDefault=nullptr) const
bool find(const Key &key, QPixmap *pixmap)
Key insert(const QPixmap &pixmap)
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
QString arg(Args &&... args) const const
bool isEmpty() const const
bool isValid() const const
void render(QPainter *painter)
transparent
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:55:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.