9 #include "kmahjonggtileset.h"
16 #include <QGuiApplication>
20 #include <QPixmapCache>
21 #include <QStandardPaths>
22 #include <QSvgRenderer>
26 #include <KConfigGroup>
27 #include <KLocalizedString>
30 #include "libkmahjongg_debug.h"
32 class KMahjonggTilesetMetricsData
42 KMahjonggTilesetMetricsData()
53 class KMahjonggTilesetPrivate
56 KMahjonggTilesetPrivate()
58 , graphicsLoaded(false)
64 KMahjonggTilesetMetricsData originaldata;
65 KMahjonggTilesetMetricsData scaleddata;
76 KMahjonggTileset::KMahjonggTileset()
77 : d(new KMahjonggTilesetPrivate)
79 buildElementIdTable();
81 static bool _inited =
false;
90 KMahjonggTileset::~KMahjonggTileset() =
default;
92 void KMahjonggTileset::updateScaleInfo(
short tilew,
short tileh)
94 d->scaleddata.w = tilew;
95 d->scaleddata.h = tileh;
96 double ratio = (
static_cast<qreal
>(d->scaleddata.w)) / (
static_cast<qreal
>(d->originaldata.w));
97 d->scaleddata.lvloffx =
static_cast<short>(d->originaldata.lvloffx * ratio);
98 d->scaleddata.lvloffy =
static_cast<short>(d->originaldata.lvloffy * ratio);
99 d->scaleddata.fw =
static_cast<short>(d->originaldata.fw * ratio);
100 d->scaleddata.fh =
static_cast<short>(d->originaldata.fh * ratio);
103 QSize KMahjonggTileset::preferredTileSize(
const QSize & boardsize,
int horizontalCells,
int verticalCells)
106 qreal newtilew, newtileh, aspectratio;
107 qreal bw = boardsize.
width();
108 qreal bh = boardsize.
height();
111 qreal fullh = (d->originaldata.fh * verticalCells) + d->originaldata.h;
112 qreal fullw = (d->originaldata.fw * horizontalCells) + d->originaldata.w;
113 qreal floatw = d->originaldata.w;
114 qreal floath = d->originaldata.h;
116 if ((fullw / fullh) > (bw / bh)) {
118 aspectratio = bw / fullw;
120 aspectratio = bh / fullh;
122 newtilew = aspectratio * floatw;
123 newtileh = aspectratio * floath;
124 return QSize(
static_cast<short>(newtilew),
static_cast<short>(newtileh));
127 bool KMahjonggTileset::loadDefault()
129 QString idx = QStringLiteral(
"default.desktop");
132 qCDebug(LIBKMAHJONGG_LOG) <<
"Inside LoadDefault(), located path at" << tilesetPath;
136 return loadTileset(tilesetPath);
139 QString KMahjonggTileset::authorProperty(
const QString & key)
const
141 return d->authorproperties[key];
144 short KMahjonggTileset::width()
const
146 return d->scaleddata.w;
149 short KMahjonggTileset::height()
const
151 return d->scaleddata.h;
154 short KMahjonggTileset::levelOffsetX()
const
156 return d->scaleddata.lvloffx;
159 short KMahjonggTileset::levelOffsetY()
const
161 return d->scaleddata.lvloffy;
164 short KMahjonggTileset::qWidth()
const
166 return static_cast<short>(d->scaleddata.fw / 2.0);
169 short KMahjonggTileset::qHeight()
const
171 return static_cast<short>(d->scaleddata.fh / 2.0);
174 QString KMahjonggTileset::path()
const
179 #define kTilesetVersionFormat 1
182 bool KMahjonggTileset::loadTileset(
const QString & tilesetPath)
187 d->authorproperties.clear();
190 QFile tilesetfile(tilesetPath);
199 d->authorproperties.insert(QStringLiteral(
"Name"), group.
readEntry(
"Name"));
200 d->authorproperties.insert(QStringLiteral(
"Author"), group.
readEntry(
"Author"));
201 d->authorproperties.insert(QStringLiteral(
"Description"), group.
readEntry(
"Description"));
202 d->authorproperties.insert(QStringLiteral(
"AuthorEmail"), group.
readEntry(
"AuthorEmail"));
205 int tileversion = group.
readEntry(
"VersionFormat", 0);
207 if (tileversion > kTilesetVersionFormat) {
218 if (d->graphicspath.isEmpty()) {
222 d->originaldata.w = group.
readEntry(
"TileWidth", 30);
223 d->originaldata.h = group.
readEntry(
"TileHeight", 50);
224 d->originaldata.fw = group.
readEntry(
"TileFaceWidth", 30);
225 d->originaldata.fh = group.
readEntry(
"TileFaceHeight", 50);
226 d->originaldata.lvloffx = group.
readEntry(
"LevelOffsetX", 10);
227 d->originaldata.lvloffy = group.
readEntry(
"LevelOffsetY", 10);
230 d->graphicsLoaded =
false;
231 d->filename = tilesetPath;
237 bool KMahjonggTileset::loadGraphics()
239 if (d->graphicsLoaded) {
244 d->svg.load(d->graphicspath);
245 if (d->svg.isValid()) {
248 d->graphicsLoaded =
true;
249 reloadTileset(
QSize(d->originaldata.w, d->originaldata.h));
262 bool KMahjonggTileset::reloadTileset(
const QSize & newTilesize)
264 if (
QSize(d->scaleddata.w, d->scaleddata.h) == newTilesize) {
269 if (d->svg.isValid()) {
270 updateScaleInfo(newTilesize.
width(), newTilesize.
height());
283 void KMahjonggTileset::buildElementIdTable()
287 for (
short idx = 1; idx <= 4; idx++) {
288 d->elementIdTable.append(QStringLiteral(
"TILE_%1").arg(idx));
291 for (
short idx = 1; idx <= 4; idx++) {
292 d->elementIdTable.append(QStringLiteral(
"TILE_%1_SEL").arg(idx));
295 for (
short idx = 1; idx <= 9; idx++) {
296 d->elementIdTable.append(QStringLiteral(
"CHARACTER_%1").arg(idx));
298 for (
short idx = 1; idx <= 9; idx++) {
299 d->elementIdTable.append(QStringLiteral(
"BAMBOO_%1").arg(idx));
301 for (
short idx = 1; idx <= 9; idx++) {
302 d->elementIdTable.append(QStringLiteral(
"ROD_%1").arg(idx));
304 for (
short idx = 1; idx <= 4; idx++) {
305 d->elementIdTable.append(QStringLiteral(
"SEASON_%1").arg(idx));
307 for (
short idx = 1; idx <= 4; idx++) {
308 d->elementIdTable.append(QStringLiteral(
"WIND_%1").arg(idx));
310 for (
short idx = 1; idx <= 3; idx++) {
311 d->elementIdTable.append(QStringLiteral(
"DRAGON_%1").arg(idx));
313 for (
short idx = 1; idx <= 4; idx++) {
314 d->elementIdTable.append(QStringLiteral(
"FLOWER_%1").arg(idx));
318 QString KMahjonggTileset::pixmapCacheNameFromElementId(
const QString & elementid)
320 return authorProperty(QStringLiteral(
"Name")) + elementid + QStringLiteral(
"W%1H%2").
arg(d->scaleddata.w).
arg(d->scaleddata.h);
323 QPixmap KMahjonggTileset::renderElement(
short width,
short height,
const QString & elementid)
328 height = height * dpr;
332 if (d->svg.isValid()) {
334 d->svg.render(&p, elementid);
336 qiRend.setDevicePixelRatio(dpr);
340 QPixmap KMahjonggTileset::selectedTile(
int num)
343 QString elemId = d->elementIdTable.at(num + 4);
346 pm = renderElement(d->scaleddata.w, d->scaleddata.h, elemId);
352 QPixmap KMahjonggTileset::unselectedTile(
int num)
355 QString elemId = d->elementIdTable.at(num);
358 pm = renderElement(d->scaleddata.w, d->scaleddata.h, elemId);
364 QPixmap KMahjonggTileset::tileface(
int num)
367 if ((num + 8) >= d->elementIdTable.count()) {
372 QString elemId = d->elementIdTable.at(num + 8);
375 pm = renderElement(d->scaleddata.fw, d->scaleddata.fh, elemId);