1/*
2 Copyright (C) 1997 Mathias Mueller <in5y158@public.uni-hamburg.de>
3 Copyright (C) 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
4
5 Libkmahjongg is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18*/
19
20#include "kmahjonggbackground.h"
21
22#include <kstandarddirs.h>
23#include <klocale.h>
24#include <kconfig.h>
25#include <kconfiggroup.h>
26#include <qsvgrenderer.h>
27#include <QImage>
28#include <QFile>
29#include <QMap>
30#include <QPixmap>
31#include <QPixmapCache>
32#include <QPainter>
33#include <KDebug>
34
35class KMahjonggBackgroundPrivate
36{
37 public:
38 KMahjonggBackgroundPrivate()
39 : w(1), h(1), graphicsLoaded(false), isTiled(true), isSVG(false)
40 {
41 }
42
43 QMap<QString, QString> authorproperties;
44 QString pixmapCacheNameFromElementId(const QString &elementid);
45 QPixmap renderBG(short width, short height);
46
47 QPixmap backgroundPixmap;
48 QBrush backgroundBrush;
49 QString filename;
50 QString graphicspath;
51 short w;
52 short h;
53
54 QSvgRenderer svg;
55
56 bool graphicsLoaded;
57 bool isPlain;
58 bool isTiled;
59 bool isSVG;
60};
61
62KMahjonggBackground::KMahjonggBackground()
63 : d(new KMahjonggBackgroundPrivate)
64{
65 static bool _inited = false;
66 if (_inited)
67 return;
68 KGlobal::dirs()->addResourceType("kmahjonggbackground", "data", QString::fromLatin1("kmahjongglib/backgrounds/"));
69
70 KGlobal::locale()->insertCatalog( QLatin1String( "libkmahjongglib" ));
71 _inited = true;
72}
73
74KMahjonggBackground::~KMahjonggBackground() {
75 delete d;
76}
77
78bool KMahjonggBackground::loadDefault()
79{
80 QLatin1String idx( "default.desktop" );
81
82 QString bgPath = KStandardDirs::locate("kmahjonggbackground", idx);
83 kDebug() << "Inside LoadDefault(), located background at" << bgPath;
84 if (bgPath.isEmpty()) {
85 return false;
86 }
87 return load(bgPath, 0, 0);
88}
89
90#define kBGVersionFormat 1
91
92bool KMahjonggBackground::load(const QString &file, short width, short height) {
93 kDebug() << "Background loading";
94 d->isSVG = false;
95
96 kDebug() << "Attempting to load .desktop at" << file;
97
98 // verify if it is a valid file first and if we can open it
99 QFile bgfile(file);
100 if (!bgfile.open(QIODevice::ReadOnly)) {
101 return (false);
102 }
103 bgfile.close();
104
105 KConfig bgconfig(file, KConfig::SimpleConfig);
106 KConfigGroup group = bgconfig.group("KMahjonggBackground");
107
108 d->authorproperties.insert(QLatin1String( "Name" ), group.readEntry("Name"));// Returns translated data
109 d->authorproperties.insert(QLatin1String( "Author" ), group.readEntry("Author"));
110 d->authorproperties.insert(QLatin1String( "Description" ), group.readEntry("Description"));
111 d->authorproperties.insert(QLatin1String( "AuthorEmail" ), group.readEntry("AuthorEmail"));
112 //The "Plain" key is set to 1 by the color_plain background.
113 d->isPlain = group.readEntry("Plain", 0) != 0;
114 d->authorproperties.insert(QLatin1String( "Plain" ), d->isPlain ? QLatin1String("1") : QLatin1String("0"));
115
116 //Version control
117 int bgversion = group.readEntry("VersionFormat",0);
118 //Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
119 if (bgversion > kBGVersionFormat) {
120 return false;
121 }
122
123 if (d->isPlain) {
124 kDebug() << "Using plain background";
125 d->graphicspath.clear();
126 d->filename = file;
127 return true;
128 }
129
130 QString graphName = group.readEntry("FileName");
131
132 d->graphicspath = KStandardDirs::locate("kmahjonggbackground", graphName);
133 kDebug() << "Using background at" << d->graphicspath;
134
135 if (d->graphicspath.isEmpty()) return (false);
136
137 if (group.readEntry("Tiled",0)) {
138 d->w = group.readEntry("Width",0);
139 d->h = group.readEntry("Height",0);
140 d->isTiled = true;
141 } else {
142 d->w = width;
143 d->h = height;
144 d->isTiled = false;
145 }
146 d->graphicsLoaded = false;
147 d->filename = file;
148 return true;
149}
150
151bool KMahjonggBackground::loadGraphics() {
152 if (d->graphicsLoaded || d->isPlain) return (true) ;
153
154 d->svg.load(d->graphicspath);
155 if (d->svg.isValid()) {
156 d->isSVG = true;
157 } else {
158 kDebug() << "could not load svg";
159 return( false );
160 }
161 return (true);
162}
163
164void KMahjonggBackground::sizeChanged(int newW, int newH) {
165 //in tiled mode we do not care about the whole field size
166 if (d->isTiled || d->isPlain) return;
167
168 if (newW == d->w && newH == d->h)
169 return;
170 d->w = newW;
171 d->h = newH;
172}
173
174QString KMahjonggBackgroundPrivate::pixmapCacheNameFromElementId(const QString &elementid) {
175 return authorproperties[QLatin1String( "Name" )]+ elementid+QString::fromLatin1( "W%1H%2").arg(w).arg(h);
176}
177
178QPixmap KMahjonggBackgroundPrivate::renderBG(short width, short height) {
179 QImage qiRend(QSize(width, height),QImage::Format_ARGB32_Premultiplied);
180 qiRend.fill(0);
181
182 if (svg.isValid()) {
183 QPainter p(&qiRend);
184 svg.render(&p);
185 }
186 return QPixmap::fromImage(qiRend);
187}
188
189QBrush & KMahjonggBackground::getBackground() {
190 if (d->isPlain) {
191 d->backgroundBrush = QBrush(QPixmap());
192 } else {
193 if (!QPixmapCache::find(d->pixmapCacheNameFromElementId(d->filename), &d->backgroundPixmap)) {
194 d->backgroundPixmap = d->renderBG(d->w, d->h);
195 QPixmapCache::insert(d->pixmapCacheNameFromElementId(d->filename), d->backgroundPixmap);
196 }
197 d->backgroundBrush = QBrush(d->backgroundPixmap);
198 }
199 return d->backgroundBrush;
200}
201
202QString KMahjonggBackground::path() const {
203 return d->filename;
204}
205
206QString KMahjonggBackground::authorProperty(const QString &key) const {
207 return d->authorproperties[key];
208}
209
210