Make the fact that the time is shown in hours and minutes more explicit.
[kdegames.git] / libkmahjongg / kmahjonggbackground.cpp
blob3c69a529b37a1002d225f78f65d2d53ce08ef532
1 /*
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>
23 #include <klocale.h>
24 #include <kconfig.h>
25 #include <kconfiggroup.h>
26 #include <ksvgrenderer.h>
27 #include <QImage>
28 #include <QFile>
29 #include <QMap>
30 #include <QPixmap>
31 #include <QPixmapCache>
32 #include <QPainter>
33 #include <KDebug>
35 class KMahjonggBackgroundPrivate
37 public:
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;
49 QString filename;
50 QString graphicspath;
51 short w;
52 short h;
54 KSvgRenderer svg;
56 bool graphicsLoaded;
57 bool isTiled;
58 bool isSVG;
61 KMahjonggBackground::KMahjonggBackground()
62 : d(new KMahjonggBackgroundPrivate)
64 static bool _inited = false;
65 if (_inited)
66 return;
67 KGlobal::dirs()->addResourceType("kmahjonggbackground", "data", QString::fromLatin1("kmahjongglib/backgrounds/"));
69 KGlobal::locale()->insertCatalog("libkmahjongglib");
70 _inited = true;
73 KMahjonggBackground::~KMahjonggBackground() {
74 delete d;
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()) {
84 return false;
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";
93 d->isSVG = false;
95 kDebug() << "Attempting to load .desktop at" << file;
97 // verify if it is a valid file first and if we can open it
98 QFile bgfile(file);
99 if (!bgfile.open(QIODevice::ReadOnly)) {
100 return (false);
102 bgfile.close();
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"));
112 //Version control
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) {
116 return false;
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);
130 d->isTiled = true;
131 } else {
132 d->w = width;
133 d->h = height;
134 d->isTiled = false;
136 d->graphicsLoaded = false;
137 d->filename = file;
138 return true;
141 bool KMahjonggBackground::loadGraphics() {
142 if (d->graphicsLoaded == true) return (true) ;
144 d->svg.load(d->graphicspath);
145 if (d->svg.isValid()) {
146 d->isSVG = true;
147 } else {
148 kDebug() << "could not load svg";
149 return( false );
151 return (true);
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)
159 return;
160 d->w = newW;
161 d->h = newH;
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);
170 qiRend.fill(0);
172 if (svg.isValid()) {
173 QPainter p(&qiRend);
174 svg.render(&p);
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 {
189 return d->filename;
192 QString KMahjonggBackground::authorProperty(const QString &key) const {
193 return d->authorproperties[key];