9#include "kmahjonggtileset.h"
16#include <QGuiApplication>
18#include <QPixmapCache>
19#include <QStandardPaths>
20#include <QSvgRenderer>
24#include <KConfigGroup>
25#include <KLocalizedString>
28#include "libkmahjongg_debug.h"
30class KMahjonggTilesetMetricsData
40 KMahjonggTilesetMetricsData() =
default;
43class KMahjonggTilesetPrivate
46 KMahjonggTilesetPrivate() =
default;
48 void updateScaleInfo(
short tilew,
short tileh);
49 void buildElementIdTable();
50 QString pixmapCacheNameFromElementId(
const QString &elementid,
short width,
short height)
const;
51 QPixmap renderElement(
short width,
short height,
const QString &elementid)
const;
66 KMahjonggTilesetMetricsData originaldata;
67 KMahjonggTilesetMetricsData scaleddata;
73 bool graphicsLoaded =
false;
78KMahjonggTileset::KMahjonggTileset()
79 : d_ptr(new KMahjonggTilesetPrivate)
83 d->buildElementIdTable();
88KMahjonggTileset::~KMahjonggTileset() =
default;
90void KMahjonggTilesetPrivate::updateScaleInfo(
short tilew,
short tileh)
94 const double ratio = (
static_cast<qreal
>(scaleddata.w)) / (
static_cast<qreal
>(originaldata.w));
95 scaleddata.lvloffx =
static_cast<short>(originaldata.lvloffx * ratio);
96 scaleddata.lvloffy =
static_cast<short>(originaldata.lvloffy * ratio);
97 scaleddata.fw =
static_cast<short>(originaldata.fw * ratio);
98 scaleddata.fh =
static_cast<short>(originaldata.fh * ratio);
101QSize KMahjonggTileset::preferredTileSize(
QSize boardsize,
int horizontalCells,
int verticalCells)
const
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));
127bool KMahjonggTileset::loadDefault()
132 qCDebug(LIBKMAHJONGG_LOG) <<
"Inside LoadDefault(), located path at" << tilesetPath;
136 return loadTileset(tilesetPath);
139QString KMahjonggTileset::name()
const
146QString KMahjonggTileset::description()
const
150 return d->description;
153QString KMahjonggTileset::license()
const
160QString KMahjonggTileset::copyrightText()
const
164 return d->copyrightText;
167QString KMahjonggTileset::version()
const
174QString KMahjonggTileset::website()
const
181QString KMahjonggTileset::bugReportUrl()
const
185 return d->bugReportUrl;
188QString KMahjonggTileset::authorName()
const
192 return d->authorName;
195QString KMahjonggTileset::authorEmailAddress()
const
199 return d->authorEmailAddress;
202short KMahjonggTileset::width()
const
206 return d->scaleddata.w;
209short KMahjonggTileset::height()
const
213 return d->scaleddata.h;
216short KMahjonggTileset::levelOffsetX()
const
220 return d->scaleddata.lvloffx;
223short KMahjonggTileset::levelOffsetY()
const
227 return d->scaleddata.lvloffy;
230short KMahjonggTileset::qWidth()
const
234 return static_cast<short>(d->scaleddata.fw / 2.0);
237short KMahjonggTileset::qHeight()
const
241 return static_cast<short>(d->scaleddata.fh / 2.0);
244QString KMahjonggTileset::path()
const
251#define kTilesetVersionFormat 1
254bool KMahjonggTileset::loadTileset(
const QString &tilesetPath)
261 QFile tilesetfile(tilesetPath);
264 d->description.clear();
266 d->copyrightText.clear();
269 d->bugReportUrl.clear();
270 d->authorName.clear();
271 d->authorEmailAddress.clear();
280 d->description = group.
readEntry(
"Description");
282 d->copyrightText = group.
readEntry(
"Copyright");
285 d->bugReportUrl = group.
readEntry(
"BugReportUrl");
286 d->authorName = group.
readEntry(
"Author");
287 d->authorEmailAddress = group.
readEntry(
"AuthorEmail");
290 int tileversion = group.
readEntry(
"VersionFormat", 0);
292 if (tileversion > kTilesetVersionFormat) {
303 if (d->graphicspath.isEmpty()) {
307 d->originaldata.w = group.
readEntry(
"TileWidth", 30);
308 d->originaldata.h = group.
readEntry(
"TileHeight", 50);
309 d->originaldata.fw = group.
readEntry(
"TileFaceWidth", 30);
310 d->originaldata.fh = group.
readEntry(
"TileFaceHeight", 50);
311 d->originaldata.lvloffx = group.
readEntry(
"LevelOffsetX", 10);
312 d->originaldata.lvloffy = group.
readEntry(
"LevelOffsetY", 10);
315 d->graphicsLoaded =
false;
316 d->filename = tilesetPath;
322bool KMahjonggTileset::loadGraphics()
326 if (d->graphicsLoaded) {
331 d->svg.load(d->graphicspath);
332 if (d->svg.isValid()) {
335 d->graphicsLoaded =
true;
336 reloadTileset(
QSize(d->originaldata.w, d->originaldata.h));
349bool KMahjonggTileset::reloadTileset(
QSize newTilesize)
353 if (
QSize(d->scaleddata.w, d->scaleddata.h) == newTilesize) {
358 if (d->svg.isValid()) {
359 d->updateScaleInfo(newTilesize.
width(), newTilesize.
height());
372void KMahjonggTilesetPrivate::buildElementIdTable()
374 constexpr int tileCount = 4;
375 constexpr int characterCount = 9;
376 constexpr int bambooCount = 9;
377 constexpr int rodCount = 9;
378 constexpr int seasonCount = 4;
379 constexpr int windCount = 4;
380 constexpr int dragonCount = 3;
381 constexpr int flowerCount = 4;
383 elementIdTable.
reserve(2 * tileCount + characterCount + bambooCount + rodCount + seasonCount + windCount + dragonCount + flowerCount);
387 for (
short idx = 1; idx <= tileCount; idx++) {
388 elementIdTable.
append(QStringLiteral(
"TILE_%1").arg(idx));
391 for (
short idx = 1; idx <= tileCount; idx++) {
392 elementIdTable.
append(QStringLiteral(
"TILE_%1_SEL").arg(idx));
395 for (
short idx = 1; idx <= characterCount; idx++) {
396 elementIdTable.
append(QStringLiteral(
"CHARACTER_%1").arg(idx));
398 for (
short idx = 1; idx <= bambooCount; idx++) {
399 elementIdTable.
append(QStringLiteral(
"BAMBOO_%1").arg(idx));
401 for (
short idx = 1; idx <= rodCount; idx++) {
402 elementIdTable.
append(QStringLiteral(
"ROD_%1").arg(idx));
404 for (
short idx = 1; idx <= seasonCount; idx++) {
405 elementIdTable.
append(QStringLiteral(
"SEASON_%1").arg(idx));
407 for (
short idx = 1; idx <= windCount; idx++) {
408 elementIdTable.
append(QStringLiteral(
"WIND_%1").arg(idx));
410 for (
short idx = 1; idx <= dragonCount; idx++) {
411 elementIdTable.
append(QStringLiteral(
"DRAGON_%1").arg(idx));
413 for (
short idx = 1; idx <= flowerCount; idx++) {
414 elementIdTable.
append(QStringLiteral(
"FLOWER_%1").arg(idx));
418QString KMahjonggTilesetPrivate::pixmapCacheNameFromElementId(
const QString &elementid,
short width,
short height)
const
420 return name + elementid + QStringLiteral(
"W%1H%2").
arg(width).
arg(height);
423QPixmap KMahjonggTilesetPrivate::renderElement(
short width,
short height,
const QString &elementid)
const
431 svg.
render(&p, elementid);
436QPixmap KMahjonggTileset::selectedTile(
int num)
const
444 const short width = d->scaleddata.w * dpr;
445 const short height = d->scaleddata.h * dpr;
446 QString elemId = d->elementIdTable.
at(num + 4);
448 const QString pixmapCacheName = d->pixmapCacheNameFromElementId(elemId, width, height);
450 pm = d->renderElement(width, height, elemId);
457QPixmap KMahjonggTileset::unselectedTile(
int num)
const
463 const qreal dpr = qApp->devicePixelRatio();
465 const short width = d->scaleddata.w * dpr;
466 const short height = d->scaleddata.h * dpr;
467 QString elemId = d->elementIdTable.
at(num);
469 const QString pixmapCacheName = d->pixmapCacheNameFromElementId(elemId, width, height);
471 pm = d->renderElement(width, height, elemId);
478QPixmap KMahjonggTileset::tileface(
int num)
const
483 if ((num + 8) >= d->elementIdTable.count()) {
488 const qreal dpr = qApp->devicePixelRatio();
490 const short width = d->scaleddata.fw * dpr;
491 const short height = d->scaleddata.fh * dpr;
492 QString elemId = d->elementIdTable.
at(num + 8);
494 const QString pixmapCacheName = d->pixmapCacheNameFromElementId(elemId, width, height);
496 pm = d->renderElement(width, height, elemId);
KConfigGroup group(const QString &group)
QString readEntry(const char *key, const char *aDefault=nullptr) const
void append(QList< T > &&value)
void reserve(qsizetype size)
qreal devicePixelRatio() const const
void setDevicePixelRatio(qreal scaleFactor)
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
const QChar at(qsizetype position) const const
bool isEmpty() const const
bool isValid() const const
void render(QPainter *painter)