libkmahjongg
kmahjonggbackground.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kmahjonggbackground.h"
00021
00022 #include <kstandarddirs.h>
00023 #include <klocale.h>
00024 #include <kconfig.h>
00025 #include <kconfiggroup.h>
00026 #include <ksvgrenderer.h>
00027 #include <QImage>
00028 #include <QFile>
00029 #include <QMap>
00030 #include <QPixmap>
00031 #include <QPixmapCache>
00032 #include <QPainter>
00033 #include <KDebug>
00034
00035 class KMahjonggBackgroundPrivate
00036 {
00037 public:
00038 KMahjonggBackgroundPrivate()
00039 : w(1), h(1), graphicsLoaded(false), isTiled(true), isSVG(false)
00040 {
00041 }
00042
00043 QMap<QString, QString> authorproperties;
00044 QString pixmapCacheNameFromElementId(const QString &elementid);
00045 QPixmap renderBG(short width, short height);
00046
00047 QPixmap backgroundPixmap;
00048 QBrush backgroundBrush;
00049 QString filename;
00050 QString graphicspath;
00051 short w;
00052 short h;
00053
00054 KSvgRenderer svg;
00055
00056 bool graphicsLoaded;
00057 bool isTiled;
00058 bool isSVG;
00059 };
00060
00061 KMahjonggBackground::KMahjonggBackground()
00062 : d(new KMahjonggBackgroundPrivate)
00063 {
00064 static bool _inited = false;
00065 if (_inited)
00066 return;
00067 KGlobal::dirs()->addResourceType("kmahjonggbackground", "data", QString::fromLatin1("kmahjongglib/backgrounds/"));
00068
00069 KGlobal::locale()->insertCatalog("libkmahjongglib");
00070 _inited = true;
00071 }
00072
00073 KMahjonggBackground::~KMahjonggBackground() {
00074 delete d;
00075 }
00076
00077 bool KMahjonggBackground::loadDefault()
00078 {
00079 QString idx = "default.desktop";
00080
00081 QString bgPath = KStandardDirs::locate("kmahjonggbackground", idx);
00082 kDebug() << "Inside LoadDefault(), located background at" << bgPath;
00083 if (bgPath.isEmpty()) {
00084 return false;
00085 }
00086 return load(bgPath, 0, 0);
00087 }
00088
00089 #define kBGVersionFormat 1
00090
00091 bool KMahjonggBackground::load(const QString &file, short width, short height) {
00092 kDebug() << "Background loading";
00093 d->isSVG = false;
00094
00095 kDebug() << "Attempting to load .desktop at" << file;
00096
00097
00098 QFile bgfile(file);
00099 if (!bgfile.open(QIODevice::ReadOnly)) {
00100 return (false);
00101 }
00102 bgfile.close();
00103
00104 KConfig bgconfig(file, KConfig::SimpleConfig);
00105 KConfigGroup group = bgconfig.group("KMahjonggBackground");
00106
00107 d->authorproperties.insert("Name", group.readEntry("Name"));
00108 d->authorproperties.insert("Author", group.readEntry("Author"));
00109 d->authorproperties.insert("Description", group.readEntry("Description"));
00110 d->authorproperties.insert("AuthorEmail", group.readEntry("AuthorEmail"));
00111
00112
00113 int bgversion = group.readEntry("VersionFormat",0);
00114
00115 if (bgversion > kBGVersionFormat) {
00116 return false;
00117 }
00118
00119 QString graphName = group.readEntry("FileName");
00120
00121 d->graphicspath = KStandardDirs::locate("kmahjonggbackground", graphName);
00122 kDebug() << "Using background at" << d->graphicspath;
00123
00124 if (d->graphicspath.isEmpty()) return (false);
00125
00126 if (group.readEntry("Tiled",0))
00127 {
00128 d->w = group.readEntry("Width",0);
00129 d->h = group.readEntry("Height",0);
00130 d->isTiled = true;
00131 } else {
00132 d->w = width;
00133 d->h = height;
00134 d->isTiled = false;
00135 }
00136 d->graphicsLoaded = false;
00137 d->filename = file;
00138 return true;
00139 }
00140
00141 bool KMahjonggBackground::loadGraphics() {
00142 if (d->graphicsLoaded == true) return (true) ;
00143
00144 d->svg.load(d->graphicspath);
00145 if (d->svg.isValid()) {
00146 d->isSVG = true;
00147 } else {
00148 kDebug() << "could not load svg";
00149 return( false );
00150 }
00151 return (true);
00152 }
00153
00154 void KMahjonggBackground::sizeChanged(int newW, int newH) {
00155
00156 if (d->isTiled) return;
00157
00158 if (newW == d->w && newH == d->h)
00159 return;
00160 d->w = newW;
00161 d->h = newH;
00162 }
00163
00164 QString KMahjonggBackgroundPrivate::pixmapCacheNameFromElementId(const QString &elementid) {
00165 return authorproperties["Name"]+ elementid+QString("W%1H%2").arg(w).arg(h);
00166 }
00167
00168 QPixmap KMahjonggBackgroundPrivate::renderBG(short width, short height) {
00169 QImage qiRend(QSize(width, height),QImage::Format_ARGB32_Premultiplied);
00170 qiRend.fill(0);
00171
00172 if (svg.isValid()) {
00173 QPainter p(&qiRend);
00174 svg.render(&p);
00175 }
00176 return QPixmap::fromImage(qiRend);
00177 }
00178
00179 QBrush & KMahjonggBackground::getBackground() {
00180 if (!QPixmapCache::find(d->pixmapCacheNameFromElementId(d->filename), d->backgroundPixmap)) {
00181 d->backgroundPixmap = d->renderBG(d->w, d->h);
00182 QPixmapCache::insert(d->pixmapCacheNameFromElementId(d->filename), d->backgroundPixmap);
00183 }
00184 d->backgroundBrush = QBrush(d->backgroundPixmap);
00185 return d->backgroundBrush;
00186 }
00187
00188 QString KMahjonggBackground::path() const {
00189 return d->filename;
00190 }
00191
00192 QString KMahjonggBackground::authorProperty(const QString &key) const {
00193 return d->authorproperties[key];
00194 }