22 #include <kstandarddirs.h>
25 #include <kconfiggroup.h>
26 #include <qsvgrenderer.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;
63 : d(new KMahjonggBackgroundPrivate)
65 static bool _inited =
false;
68 KGlobal::dirs()->addResourceType(
"kmahjonggbackground",
"data", QString::fromLatin1(
"kmahjongglib/backgrounds/"));
70 KGlobal::locale()->insertCatalog( QLatin1String(
"libkmahjongglib" ));
80 QLatin1String idx(
"default.desktop" );
82 QString bgPath = KStandardDirs::locate(
"kmahjonggbackground", idx);
83 kDebug() <<
"Inside LoadDefault(), located background at" << bgPath;
84 if (bgPath.isEmpty()) {
87 return load(bgPath, 0, 0);
90 #define kBGVersionFormat 1
93 kDebug() <<
"Background loading";
96 kDebug() <<
"Attempting to load .desktop at" << file;
100 if (!bgfile.open(QIODevice::ReadOnly)) {
105 KConfig bgconfig(file, KConfig::SimpleConfig);
106 KConfigGroup group = bgconfig.group(
"KMahjonggBackground");
108 d->authorproperties.insert(QLatin1String(
"Name" ), group.readEntry(
"Name"));
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"));
113 d->isPlain = group.readEntry(
"Plain", 0) != 0;
114 d->authorproperties.insert(QLatin1String(
"Plain" ), d->isPlain ? QLatin1String(
"1") : QLatin1String(
"0"));
117 int bgversion = group.readEntry(
"VersionFormat",0);
124 kDebug() <<
"Using plain background";
125 d->graphicspath.clear();
130 QString graphName = group.readEntry(
"FileName");
132 d->graphicspath = KStandardDirs::locate(
"kmahjonggbackground", graphName);
133 kDebug() <<
"Using background at" << d->graphicspath;
135 if (d->graphicspath.isEmpty())
return (
false);
137 if (group.readEntry(
"Tiled",0)) {
138 d->w = group.readEntry(
"Width",0);
139 d->h = group.readEntry(
"Height",0);
146 d->graphicsLoaded =
false;
152 if (d->graphicsLoaded || d->isPlain)
return (
true) ;
154 d->svg.load(d->graphicspath);
155 if (d->svg.isValid()) {
158 kDebug() <<
"could not load svg";
166 if (d->isTiled || d->isPlain)
return;
168 if (newW == d->w && newH == d->h)
174 QString KMahjonggBackgroundPrivate::pixmapCacheNameFromElementId(
const QString &elementid) {
175 return authorproperties[QLatin1String(
"Name" )]+ elementid+QString::fromLatin1(
"W%1H%2").arg(w).arg(h);
178 QPixmap KMahjonggBackgroundPrivate::renderBG(
short width,
short height) {
179 QImage qiRend(QSize(width, height),QImage::Format_ARGB32_Premultiplied);
186 return QPixmap::fromImage(qiRend);
191 d->backgroundBrush = QBrush(QPixmap());
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);
197 d->backgroundBrush = QBrush(d->backgroundPixmap);
199 return d->backgroundBrush;
207 return d->authorproperties[key];
QString authorProperty(const QString &key) const
bool load(const QString &file, short width, short height)
void sizeChanged(int newW, int newH)