File: thememanager.cpp

package info (click to toggle)
kfourinline 4:18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,784 kB
  • sloc: cpp: 7,339; sh: 8; makefile: 5
file content (270 lines) | stat: -rw-r--r-- 7,398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
   This file is part of the KDE games kwin4 program
   Copyright (c) 2006 Martin Heni <kde@heni-online.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// Header includes
#include "thememanager.h"

// General includes
#include "kfourinline_debug.h"

// Qt includes
#include <QImage>
#include <QPainter>
#include <QPixmap>
#include <QRectF>
#include <QStandardPaths>

// KDE includes
#include <KConfigGroup>
#include <KLocalizedString>

// Local includes
#include "kwin4global.h"

// Constructor for the theme manager
ThemeManager::ThemeManager(const QString &themefile, QObject* parent, int initialSize)
    : QObject(parent), mRenderer(0), mConfig( 0 )
{
  mScale            = initialSize;
  mAspectRatio      = 1.0;
  mThemeFileChanged = false;
  updateTheme(themefile);
}

ThemeManager::~ThemeManager()
{
    delete mConfig;
    delete mRenderer;
}

// Register an object with the manager
void ThemeManager::registerTheme(Themeable* ob)
{
  //We want to make sure that we draw the items registered last, first.
  mObjects.prepend(ob);
}


// Unregister an object from the manager
void ThemeManager::unregisterTheme(Themeable* ob)
{
  mObjects.removeAll(ob);
}


// Check whether the theme is alright
int ThemeManager::checkTheme()
{
  // Check theme
  if (mRenderer == 0) return 1;
  return 0; // Ok
}


// Check the reason of the theme change (rescale or new theme)
bool ThemeManager::themefileChanged()
{
  return mThemeFileChanged;
}


// Force an refresh of the theme object given
void ThemeManager::updateTheme(Themeable* ob)
{
  ob->changeTheme();
}


// Update the theme file and refresh all registered objects. Used
// to really change the theme.
void ThemeManager::updateTheme(const QString &themefile)
{
  mThemeFileChanged = true;

  // Empty cache
  mPixmapCache.clear();

  // Process dirs
  QString rcfile = QStandardPaths::locate(QStandardPaths::DataLocation, QStringLiteral("grafix/") + themefile);
  qCDebug(KFOURINLINE_LOG) << "ThemeManager LOAD with theme "<<rcfile;

  // Read config and SVG file for theme
  delete mConfig;
  mConfig = new KConfig(rcfile, KConfig::NoGlobals);
  QString svgfile = config(QStringLiteral("general")).readEntry("svgfile");
  svgfile = QStandardPaths::locate(QStandardPaths::DataLocation, QStringLiteral("grafix/") + svgfile);
  qCDebug(KFOURINLINE_LOG) << "Reading SVG master file  =" << svgfile;
  mAspectRatio     =  config(QStringLiteral("general")).readEntry("aspect-ratio", 1.0);
  qCDebug(KFOURINLINE_LOG) << "Aspect ratio =" << mAspectRatio;
  mColorNamePlayer[0] = i18nc("Player 0 color", config(QStringLiteral("general")).readEntry("colorNamePlayer0").toUtf8());
  qCDebug(KFOURINLINE_LOG) << "Player 0 color name =" << mColorNamePlayer[0];
  mColorNamePlayer[1] = i18nc("Player 1 color", config(QStringLiteral("general")).readEntry("colorNamePlayer1").toUtf8());
  qCDebug(KFOURINLINE_LOG) << "Player 1 color name =" << mColorNamePlayer[1];

  delete mRenderer;
  mRenderer = new QSvgRenderer(this);
  bool result = mRenderer->load(svgfile);
  if (!result)
  {
    delete mRenderer;
    mRenderer = 0;
    qCCritical(KFOURINLINE_LOG) << "Cannot open file" << svgfile;
  }
  qCDebug(KFOURINLINE_LOG) << "Renderer" << mRenderer<<" =" << result;

  // Notify all theme objects of a change
  foreach(Themeable *object, mObjects) {
      object->changeTheme();
  }
}


// Rescale the theme. Call all registered objects so that they can refresh.
void ThemeManager::rescale(int scale, QPoint offset)
{
  if (global_debug > 0)
     qCDebug(KFOURINLINE_LOG) << "THEMEMANAGER::Rescaling theme to " << scale<<" offset to " << offset;

  mThemeFileChanged = false;

  if (global_debug > 1)
  {
    if (scale==mScale)
      qCDebug(KFOURINLINE_LOG) <<" No scale change to" << scale << ". If this happends too often it is BAD";
  }
  //if (scale==mScale) return;
  mScale = scale;
  mOffset = offset;

  foreach(Themeable *object, mObjects) {
      object->changeTheme();
  }
}


// Retrieve the theme's scale
double ThemeManager::getScale()
{
  return (double)mScale;
}


// Retrieve the theme offset
QPoint ThemeManager::getOffset()
{
  return mOffset;
}


// Retrieve the current theme configuration file.
KConfigGroup ThemeManager::config(const QString &id)
{
   KConfigGroup grp = mConfig->group(id);
   return grp;
}


// Get a pixmap when its size is given (this can distort the image)
const QPixmap ThemeManager::getPixmap(const QString &svgid,const QSize &size)
{
  if (size.width() < 1 || size.height() < 1)
    qCCritical(KFOURINLINE_LOG) << "ThemeManager::getPixmap Cannot create svgid ID " << svgid << " with zero size" << size;

  QPixmap pixmap;

  //  Cached pixmap?
  if (mPixmapCache.contains(svgid))
  {
    pixmap = mPixmapCache[svgid];
    if (pixmap.size() == size)
    {
      return pixmap;
    }
  }

  // Create new image
  QImage image(size, QImage::Format_ARGB32_Premultiplied);
  image.fill(0);
  QPainter p(&image);
  mRenderer->render(&p, svgid);
  p.end();
  pixmap = QPixmap::fromImage(image);
  if (pixmap.isNull())
    qCCritical(KFOURINLINE_LOG) << "ThemeManager::getPixmap Cannot load svgid ID " << svgid;

  // Cache image
  mPixmapCache[svgid] = pixmap;

  return pixmap;
}


// Get a pixmap when only width is given (this keeps the aspect ratio)
const QPixmap ThemeManager::getPixmap(const QString &svgid, double width)
{
  QRectF rect   = mRenderer->boundsOnElement(svgid);
  double factor = width/rect.width();
  QSize size    = QSize(int(width),  int(rect.height()*factor));
  return getPixmap(svgid, size);
}


// Get a pixmap with original properties and a scale factor given with respect to
// another SVG item.
const QPixmap ThemeManager::getPixmap(const QString &svgid, const QString &svgref, double refwidth)
{
  QRectF refrect    = mRenderer->boundsOnElement(svgref);
  QRectF rect       = mRenderer->boundsOnElement(svgid);
  double factor     = refwidth/refrect.width();
  QSize size        = QSize(int(rect.width()*factor),  int(rect.height()*factor));
  return getPixmap(svgid, size);
}


// ========================== Themeable interface ===============================

// Constructs a themeable interface
Themeable::Themeable()
{
  mScale        = 1.0;
  mThemeManager = 0;
}


// Constructs a themeable interface given its id and the master theme manager.
// This automatically registeres the object with the manager.
Themeable::Themeable(const QString &id, ThemeManager* thememanager)
{
  mScale        = 1.0;
  mId           = id;
  mThemeManager = thememanager;
  if (!thememanager) return;
  thememanager->registerTheme(this);
}


// Destructs the themeable object
Themeable::~Themeable()
{
  if (mThemeManager) mThemeManager->unregisterTheme(this);
}