2 Copyright (C) 1997 Mathias Mueller <in5y158@public.uni-hamburg.de>
3 Copyright (C) 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
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.
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.
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.
20 #include "kmahjonggbackground.h"
22 #include <kstandarddirs.h>
25 #include <kconfiggroup.h>
26 #include <ksvgrenderer.h>
31 #include <QPixmapCache>
35 class KMahjonggBackgroundPrivate
38 KMahjonggBackgroundPrivate()
39 : w(1), h(1), graphicsLoaded(false), isTiled(true), isSVG(false)
43 QMap
<QString
, QString
> authorproperties
;
44 QString
pixmapCacheNameFromElementId(const QString
&elementid
);
45 QPixmap
renderBG(short width
, short height
);
47 QPixmap backgroundPixmap
;
48 QBrush backgroundBrush
;
61 KMahjonggBackground::KMahjonggBackground()
62 : d(new KMahjonggBackgroundPrivate
)
64 static bool _inited
= false;
67 KGlobal::dirs()->addResourceType("kmahjonggbackground", "data", QString::fromLatin1("kmahjongglib/backgrounds/"));
69 KGlobal::locale()->insertCatalog("libkmahjongglib");
73 KMahjonggBackground::~KMahjonggBackground() {
77 bool KMahjonggBackground::loadDefault()
79 QString idx
= "default.desktop";
81 QString bgPath
= KStandardDirs::locate("kmahjonggbackground", idx
);
82 kDebug() << "Inside LoadDefault(), located background at" << bgPath
;
83 if (bgPath
.isEmpty()) {
86 return load(bgPath
, 0, 0);
89 #define kBGVersionFormat 1
91 bool KMahjonggBackground::load(const QString
&file
, short width
, short height
) {
92 kDebug() << "Background loading";
95 kDebug() << "Attempting to load .desktop at" << file
;
97 // verify if it is a valid file first and if we can open it
99 if (!bgfile
.open(QIODevice::ReadOnly
)) {
104 KConfig
bgconfig(file
, KConfig::SimpleConfig
);
105 KConfigGroup group
= bgconfig
.group("KMahjonggBackground");
107 d
->authorproperties
.insert("Name", group
.readEntry("Name"));// Returns translated data
108 d
->authorproperties
.insert("Author", group
.readEntry("Author"));
109 d
->authorproperties
.insert("Description", group
.readEntry("Description"));
110 d
->authorproperties
.insert("AuthorEmail", group
.readEntry("AuthorEmail"));
113 int bgversion
= group
.readEntry("VersionFormat",0);
114 //Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
115 if (bgversion
> kBGVersionFormat
) {
119 QString graphName
= group
.readEntry("FileName");
121 d
->graphicspath
= KStandardDirs::locate("kmahjonggbackground", graphName
);
122 kDebug() << "Using background at" << d
->graphicspath
;
124 if (d
->graphicspath
.isEmpty()) return (false);
126 if (group
.readEntry("Tiled",0))
128 d
->w
= group
.readEntry("Width",0);
129 d
->h
= group
.readEntry("Height",0);
136 d
->graphicsLoaded
= false;
141 bool KMahjonggBackground::loadGraphics() {
142 if (d
->graphicsLoaded
== true) return (true) ;
144 d
->svg
.load(d
->graphicspath
);
145 if (d
->svg
.isValid()) {
148 kDebug() << "could not load svg";
154 void KMahjonggBackground::sizeChanged(int newW
, int newH
) {
155 //in tiled mode we do not care about the whole field size
156 if (d
->isTiled
) return;
158 if (newW
== d
->w
&& newH
== d
->h
)
164 QString
KMahjonggBackgroundPrivate::pixmapCacheNameFromElementId(const QString
&elementid
) {
165 return authorproperties
["Name"]+ elementid
+QString("W%1H%2").arg(w
).arg(h
);
168 QPixmap
KMahjonggBackgroundPrivate::renderBG(short width
, short height
) {
169 QImage
qiRend(QSize(width
, height
),QImage::Format_ARGB32_Premultiplied
);
176 return QPixmap::fromImage(qiRend
);
179 QBrush
& KMahjonggBackground::getBackground() {
180 if (!QPixmapCache::find(d
->pixmapCacheNameFromElementId(d
->filename
), d
->backgroundPixmap
)) {
181 d
->backgroundPixmap
= d
->renderBG(d
->w
, d
->h
);
182 QPixmapCache::insert(d
->pixmapCacheNameFromElementId(d
->filename
), d
->backgroundPixmap
);
184 d
->backgroundBrush
= QBrush(d
->backgroundPixmap
);
185 return d
->backgroundBrush
;
188 QString
KMahjonggBackground::path() const {
192 QString
KMahjonggBackground::authorProperty(const QString
&key
) const {
193 return d
->authorproperties
[key
];