• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

libkmahjongg

  • sources
  • kde-4.14
  • kdegames
  • libkmahjongg
kmahjonggbackground.cpp
Go to the documentation of this file.
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 
35 class 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 
62 KMahjonggBackground::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 
74 KMahjonggBackground::~KMahjonggBackground() {
75  delete d;
76 }
77 
78 bool 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 
92 bool 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 
151 bool 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 
164 void 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 
174 QString KMahjonggBackgroundPrivate::pixmapCacheNameFromElementId(const QString &elementid) {
175  return authorproperties[QLatin1String( "Name" )]+ elementid+QString::fromLatin1( "W%1H%2").arg(w).arg(h);
176 }
177 
178 QPixmap 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 
189 QBrush & 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 
202 QString KMahjonggBackground::path() const {
203  return d->filename;
204 }
205 
206 QString KMahjonggBackground::authorProperty(const QString &key) const {
207  return d->authorproperties[key];
208 }
209 
KMahjonggBackground::getBackground
QBrush & getBackground()
Definition: kmahjonggbackground.cpp:189
KMahjonggBackground::path
QString path() const
Definition: kmahjonggbackground.cpp:202
KMahjonggBackground::loadGraphics
bool loadGraphics()
Definition: kmahjonggbackground.cpp:151
QMap< QString, QString >
QSvgRenderer
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QBrush
QFile
KMahjonggBackground::~KMahjonggBackground
~KMahjonggBackground()
Definition: kmahjonggbackground.cpp:74
QPainter
QString::isEmpty
bool isEmpty() const
kmahjonggbackground.h
KMahjonggBackground::authorProperty
QString authorProperty(const QString &key) const
Definition: kmahjonggbackground.cpp:206
QString
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QPixmap
KMahjonggBackground::load
bool load(const QString &file, short width, short height)
Definition: kmahjonggbackground.cpp:92
QSize
QFile::close
virtual void close()
QImage
KMahjonggBackground::sizeChanged
void sizeChanged(int newW, int newH)
Definition: kmahjonggbackground.cpp:164
QLatin1String
QPixmapCache::find
QPixmap * find(const QString &key)
KMahjonggBackground::loadDefault
bool loadDefault()
Definition: kmahjonggbackground.cpp:78
QString::fromLatin1
QString fromLatin1(const char *str, int size)
kBGVersionFormat
#define kBGVersionFormat
Definition: kmahjonggbackground.cpp:90
QPixmapCache::insert
bool insert(const QString &key, const QPixmap &pixmap)
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
KMahjonggBackground::KMahjonggBackground
KMahjonggBackground()
Definition: kmahjonggbackground.cpp:62
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkmahjongg

Skip menu "libkmahjongg"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal