File indexing completed on 2022-09-27 13:20:30

0001 /*
0002     SPDX-FileCopyrightText: 1997 Mathias Mueller <in5y158@public.uni-hamburg.de>
0003     SPDX-FileCopyrightText: 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 // own
0009 #include "kmahjonggbackground.h"
0010 
0011 // Qt
0012 #include <QFile>
0013 #include <QImage>
0014 #include <QMap>
0015 #include <QPainter>
0016 #include <QPixmap>
0017 #include <QPixmapCache>
0018 #include <QSvgRenderer>
0019 
0020 // KF
0021 #include <KConfig>
0022 #include <KConfigGroup>
0023 #include <KLocalizedString>
0024 
0025 // LibKMahjongg
0026 #include "libkmahjongg_debug.h"
0027 
0028 class KMahjonggBackgroundPrivate
0029 {
0030 public:
0031     KMahjonggBackgroundPrivate()
0032         : w(1)
0033         , h(1)
0034         , graphicsLoaded(false)
0035         , isPlain(false)
0036         , isTiled(true)
0037         , isSVG(false)
0038     {
0039     }
0040 
0041     QMap<QString, QString> authorproperties;
0042     QString pixmapCacheNameFromElementId(const QString & elementid);
0043     QPixmap renderBG(short width, short height);
0044 
0045     QPixmap backgroundPixmap;
0046     QBrush backgroundBrush;
0047     QString filename;
0048     QString graphicspath;
0049     short w;
0050     short h;
0051 
0052     QSvgRenderer svg;
0053 
0054     bool graphicsLoaded;
0055     bool isPlain;
0056     bool isTiled;
0057     bool isSVG;
0058 };
0059 
0060 KMahjonggBackground::KMahjonggBackground()
0061     : d(new KMahjonggBackgroundPrivate)
0062 {
0063     static bool _inited = false;
0064     if (_inited) {
0065         return;
0066     }
0067     _inited = true;
0068 }
0069 
0070 KMahjonggBackground::~KMahjonggBackground() = default;
0071 
0072 bool KMahjonggBackground::loadDefault()
0073 {
0074     // Set default background here.
0075     QLatin1String idx("egyptian.desktop");
0076 
0077     QString bgPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kmahjongglib/backgrounds/") + idx);
0078     qCDebug(LIBKMAHJONGG_LOG) << "Inside LoadDefault(), located background at" << bgPath;
0079     if (bgPath.isEmpty()) {
0080         return false;
0081     }
0082     return load(bgPath, 0, 0);
0083 }
0084 
0085 #define kBGVersionFormat 1
0086 
0087 bool KMahjonggBackground::load(const QString & file, short width, short height)
0088 {
0089     //qCDebug(LIBKMAHJONGG_LOG) << "Background loading";
0090     d->isSVG = false;
0091 
0092     //qCDebug(LIBKMAHJONGG_LOG) << "Attempting to load .desktop at" << file;
0093 
0094     // verify if it is a valid file first and if we can open it
0095     QFile bgfile(file);
0096     if (!bgfile.open(QIODevice::ReadOnly)) {
0097         return false;
0098     }
0099     bgfile.close();
0100 
0101     KConfig bgconfig(file, KConfig::SimpleConfig);
0102     KConfigGroup group = bgconfig.group("KMahjonggBackground");
0103 
0104     d->authorproperties.insert(QStringLiteral("Name"), group.readEntry("Name")); // Returns translated data
0105     d->authorproperties.insert(QStringLiteral("Author"), group.readEntry("Author"));
0106     d->authorproperties.insert(QStringLiteral("Description"), group.readEntry("Description"));
0107     d->authorproperties.insert(QStringLiteral("AuthorEmail"), group.readEntry("AuthorEmail"));
0108     //The "Plain" key is set to 1 by the color_plain background.
0109     d->isPlain = group.readEntry("Plain", 0) != 0;
0110     d->authorproperties.insert(QStringLiteral("Plain"), d->isPlain ? QStringLiteral("1") : QStringLiteral("0"));
0111 
0112     //Version control
0113     int bgversion = group.readEntry("VersionFormat", 0);
0114     //Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
0115     if (bgversion > kBGVersionFormat) {
0116         return false;
0117     }
0118 
0119     if (d->isPlain) {
0120         //qCDebug(LIBKMAHJONGG_LOG) << "Using plain background";
0121         d->graphicspath.clear();
0122         d->filename = file;
0123         return true;
0124     }
0125 
0126     QString graphName = group.readEntry("FileName");
0127 
0128     d->graphicspath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kmahjongglib/backgrounds/") + graphName);
0129 
0130     qCDebug(LIBKMAHJONGG_LOG) << "Using background at" << d->graphicspath;
0131 
0132     if (d->graphicspath.isEmpty()) {
0133         return false;
0134     }
0135 
0136     if (group.readEntry("Tiled", 0) != 0) {
0137         d->w = group.readEntry("Width", 0);
0138         d->h = group.readEntry("Height", 0);
0139         d->isTiled = true;
0140     } else {
0141         d->w = width;
0142         d->h = height;
0143         d->isTiled = false;
0144     }
0145     d->graphicsLoaded = false;
0146     d->filename = file;
0147     return true;
0148 }
0149 
0150 bool KMahjonggBackground::loadGraphics()
0151 {
0152     if (d->graphicsLoaded || d->isPlain) {
0153         return true;
0154     }
0155 
0156     d->svg.load(d->graphicspath);
0157     if (d->svg.isValid()) {
0158         d->isSVG = true;
0159     } else {
0160         //qCDebug(LIBKMAHJONGG_LOG) << "could not load svg";
0161         return false;
0162     }
0163     return true;
0164 }
0165 
0166 void KMahjonggBackground::sizeChanged(int newW, int newH)
0167 {
0168     //in tiled mode we do not care about the whole field size
0169     if (d->isTiled || d->isPlain) {
0170         return;
0171     }
0172 
0173     if (newW == d->w && newH == d->h) {
0174         return;
0175     }
0176     d->w = newW;
0177     d->h = newH;
0178 }
0179 
0180 QString KMahjonggBackgroundPrivate::pixmapCacheNameFromElementId(const QString & elementid)
0181 {
0182     return authorproperties[QStringLiteral("Name")] + elementid + QStringLiteral("W%1H%2").arg(w).arg(h);
0183 }
0184 
0185 QPixmap KMahjonggBackgroundPrivate::renderBG(short width, short height)
0186 {
0187     QImage qiRend(QSize(width, height), QImage::Format_ARGB32_Premultiplied);
0188     qiRend.fill(0);
0189 
0190     if (svg.isValid()) {
0191         QPainter p(&qiRend);
0192         svg.render(&p);
0193     }
0194     return QPixmap::fromImage(qiRend);
0195 }
0196 
0197 QBrush & KMahjonggBackground::getBackground()
0198 {
0199     if (d->isPlain) {
0200         d->backgroundBrush = QBrush(QPixmap());
0201     } else {
0202         if (!QPixmapCache::find(d->pixmapCacheNameFromElementId(d->filename), &d->backgroundPixmap)) {
0203             d->backgroundPixmap = d->renderBG(d->w, d->h);
0204             QPixmapCache::insert(d->pixmapCacheNameFromElementId(d->filename), d->backgroundPixmap);
0205         }
0206         d->backgroundBrush = QBrush(d->backgroundPixmap);
0207     }
0208     return d->backgroundBrush;
0209 }
0210 
0211 QString KMahjonggBackground::path() const
0212 {
0213     return d->filename;
0214 }
0215 
0216 QString KMahjonggBackground::authorProperty(const QString & key) const
0217 {
0218     return d->authorproperties[key];
0219 }