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

liblancelot

  • sources
  • kde-4.14
  • workspace
  • kdeplasma-addons
  • libs
  • lancelot
Global.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010 Ivan Cukic <ivan.cukic(at)kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser/Library General Public License version 2,
6  * or (at your option) any later version, as published by the Free
7  * Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser/Library General Public License for more details
13  *
14  * You should have received a copy of the GNU Lesser/Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "Global.h"
21 #include "Global_p.h"
22 
23 #include <QMetaObject>
24 #include <QMetaProperty>
25 #include <QMutex>
26 
27 #include <KGlobal>
28 #include <KStandardDirs>
29 #include <KDebug>
30 
31 #include <Plasma/Theme>
32 
33 namespace Lancelot
34 {
35 
36 // Group
37 Group::ColorScheme::ColorScheme()
38  : normal(QColor()), disabled(QColor()), active(QColor())
39 {
40 }
41 
42 GroupPrivate::GroupPrivate()
43  : name(QString()), backgroundSvg(NULL),
44  ownsBackgroundSvg(false), loaded(false)
45  // TODO : Add caching?
46  //cachedBackgroundNormal(NULL), cachedBackgroundActive(NULL), cachedBackgroundDisabled(NULL)
47 {
48 }
49 
50 
51 GroupPrivate::~GroupPrivate()
52 {
53  if (ownsBackgroundSvg) {
54  delete backgroundSvg;
55  }
56  //delete d->cachedBackgroundNormal;
57  //delete d->cachedBackgroundActive;
58  //delete d->cachedBackgroundDisabled;
59 }
60 
61 void GroupPrivate::setObjectProperty(QObject * object,
62  const QString & property, const QVariant & value)
63 {
64  object->setProperty(property.toAscii(), value);
65 }
66 
67 // clearing all info
68 void GroupPrivate::reset()
69 {
70  loaded = false;
71 
72  // kDebug() << name << "Persistent properties:"
73  // << persistentProperties << "of"
74  // << properties;
75 
76  QMutableMapIterator < QString, QVariant > i(properties);
77  while (i.hasNext()) {
78  i.next();
79  if (!persistentProperties.contains(i.key())) {
80  // kDebug() << "reloading property:" << i.key();
81  i.remove();
82  }
83  }
84 
85  if (ownsBackgroundSvg) {
86  delete backgroundSvg;
87  }
88  backgroundSvg = NULL;
89 
90  foregroundColor = Group::ColorScheme();
91  backgroundColor = Group::ColorScheme();
92 
93 }
94 
95 void GroupPrivate::copyFrom(GroupPrivate * d)
96 {
97  if (this == d) return;
98 
99  QMap < QString, QVariant > savedProperties;
100 
101  foreach (const QString & key, persistentProperties) {
102  savedProperties[key] = properties[key];
103  }
104 
105  properties = d->properties;
106  persistentProperties += d->persistentProperties;
107 
108  QMapIterator < QString, QVariant > i( savedProperties );
109  while (i.hasNext()) {
110  i.next();
111 
112  properties[i.key()] = i.value();
113  }
114 
115  foregroundColor = d->foregroundColor;
116  backgroundColor = d->backgroundColor;
117 
118  if (ownsBackgroundSvg) {
119  delete backgroundSvg;
120  }
121  backgroundSvg = d->backgroundSvg;
122  ownsBackgroundSvg = false;
123 }
124 
125 KConfigGroup GroupPrivate::confGroupTheme()
126 {
127  return KConfigGroup(Global::self()->theme(), "Group-" + name);
128 }
129 
130 Group::Group(QString name)
131  : d(new GroupPrivate())
132 {
133  d->name = name;
134 }
135 
136 Group::~Group()
137 {
138  delete d;
139 }
140 
141 void Group::add(QObject * object)
142 {
143  if (!object) return;
144 
145  if (d->objects.contains(object)) return;
146  d->objects << object;
147 
148  QMapIterator < QString, QVariant > i(d->properties);
149  while (i.hasNext()) {
150  i.next();
151  d->setObjectProperty(object,
152  i.key(), i.value());
153  }
154 }
155 
156 void Group::remove(QObject * object, bool setDefaultGroup)
157 {
158  if (Global::self()->defaultGroup() == this && setDefaultGroup) return;
159 
160  if (!d->objects.contains(object)) return;
161  d->objects.remove(object);
162 }
163 
164 bool Group::hasProperty(const QString & property) const
165 {
166  return d->properties.contains(property);
167 }
168 
169 QVariant Group::property(const QString & property) const
170 {
171  return d->properties.value(property);
172 }
173 
174 void Group::setProperty(const QString & property, const QVariant & value, bool persistent)
175 {
176  // kDebug() << property << value;
177 
178  d->properties[property] = value;
179 
180  if (persistent) {
181  d->persistentProperties << property;
182  }
183 
184  foreach (QObject * child, d->objects) {
185  d->setObjectProperty(child, property, value);
186  }
187 }
188 
189 void Group::clearProperty(const QString & property)
190 {
191  d->properties.remove(property);
192 }
193 
194 QString Group::name() const
195 {
196  return d->name;
197 }
198 
199 Plasma::FrameSvg * Group::backgroundSvg() const
200 {
201  return d->backgroundSvg;
202 }
203 
204 const Group::ColorScheme * Group::backgroundColor() const
205 {
206  if (!hasProperty("WholeColorBackground") &&
207  !hasProperty("TextColorBackground")) {
208  return NULL;
209  }
210  return & d->backgroundColor;
211 }
212 
213 const Group::ColorScheme * Group::foregroundColor() const
214 {
215  return & d->foregroundColor;
216 }
217 
218 void Group::load(bool full)
219 {
220  if (d->loaded && !full) return;
221 
222  // kDebug() << name();
223 
224  d->reset();
225  d->loaded = true;
226 
227  Group * group;
228 
229  KConfigGroup confGroupTheme = d->confGroupTheme();
230  if (!confGroupTheme.exists()) {
231  group = Global::self()->defaultGroup();
232  if (group == this) return;
233 
234  d->copyFrom(group->d);
235  return;
236  }
237 
238  QString parentName = confGroupTheme.readEntry("parent", "Default");
239  if (Global::self()->groupExists(parentName)) {
240  group = Global::self()->group(confGroupTheme.readEntry("parent", "Default"));
241  if (group != this) {
242  group->load(false);
243  d->copyFrom(group->d);
244  }
245  }
246 
247  // Load properties from theme configuration file
248  d->foregroundColor.normal = confGroupTheme.readEntry("foreground.color.normal", d->foregroundColor.normal);
249  d->foregroundColor.active = confGroupTheme.readEntry("foreground.color.active", d->foregroundColor.active);
250  d->foregroundColor.disabled = confGroupTheme.readEntry("foreground.color.disabled", d->foregroundColor.disabled);
251 
252  QString type = confGroupTheme.readEntry("background.type", "none");
253  if (type == "color" || type == "color-compact") {
254  // kDebug() << "loading color background";
255  if (type == "color") {
256  setProperty("WholeColorBackground", 1, false);
257  } else {
258  setProperty("TextColorBackground", 1, false);
259  }
260  d->backgroundColor.normal = confGroupTheme.readEntry("background.color.normal", d->backgroundColor.normal);
261  d->backgroundColor.active = confGroupTheme.readEntry("background.color.active", d->backgroundColor.active);
262  d->backgroundColor.disabled = confGroupTheme.readEntry("background.color.disabled", d->backgroundColor.disabled);
263  } else if (type == "svg") {
264  // kDebug() << "loading svg background";
265  // we have already deleted the backgroundSvg
266  // if (d->ownsBackgroundSvg) {
267  // delete d->backgroundSvg;
268  // }
269 
270  d->backgroundSvg = new Plasma::FrameSvg(NULL);
271  QString imagePath = Plasma::Theme::defaultTheme()->imagePath(
272  confGroupTheme.readEntry("background.svg"));
273 
274  d->backgroundSvg->setImagePath(imagePath);
275  // kDebug() << "Background is: " <<
276  // d->backgroundSvg->imagePath();
277  d->backgroundSvg->setCacheAllRenderedFrames(true);
278  d->ownsBackgroundSvg = true;
279 
280  if (!d->backgroundSvg->isValid()) {
281  // kDebug() << "Background is not valid: " <<
282  // d->backgroundSvg->imagePath();
283  delete d->backgroundSvg;
284  d->backgroundSvg = NULL;
285  d->ownsBackgroundSvg = false;
286  } else {
287  setProperty("SvgBackground", 1, false);
288  }
289  }
290 
291  if (!confGroupTheme.readEntry(
292  "foreground.blurtextshadow", QString()).isEmpty()) {
293  setProperty("BlurTextShadow", 1, false);
294  }
295 }
296 
297 bool Group::contains(QObject * object)
298 {
299  return d->objects.contains(object);
300 }
301 
302 
303 // Global
304 
305 Global * Global::Private::instance = NULL;
306 
307 Global * Global::self()
308 {
309  if (!Global::Private::instance) {
310  Global::Private::instance = new Global();
311  }
312  return Global::Private::instance;
313 }
314 
315 Global::Private::Private()
316  : confMain(NULL),
317  confTheme(NULL)
318 {
319  connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
320  this, SLOT(themeChanged()));
321 }
322 
323 Global::Private::~Private()
324 {
325 }
326 
327 void Global::Private::objectDeleted(QObject * object)
328 {
329  Group * group = Global::self()->groupForObject(object);
330  if (group) {
331  group->remove(object);
332  }
333 }
334 
335 void Global::Private::createConfTheme()
336 {
337  QString app = KGlobal::mainComponent().componentName();
338  if (app == "lancelot") {
339  app = "";
340  } else {
341  app += '-';
342  }
343 
344  delete confTheme;
345  QString search = "desktoptheme/" +
346  Plasma::Theme::defaultTheme()->themeName()
347  + "/lancelot/" + app + "theme.config";
348  QString path = KStandardDirs::locate( "data", search );
349  // kDebug() << "path:" << search << "=" << path;
350 
351  // if we didn't find the theme specific for this application
352  // we'll use the main theme file
353  if (path.isEmpty()) {
354  search = "desktoptheme/" +
355  Plasma::Theme::defaultTheme()->themeName()
356  + "/lancelot/theme.config";
357  path = KStandardDirs::locate( "data", search );
358  // kDebug() << "path:" << search << "=" << path;
359  }
360 
361  // if the above fails, we are loading the default theme's
362  // file
363  if (path.isEmpty()) {
364  search = "desktoptheme/default/lancelot/theme.config";
365  path = KStandardDirs::locate( "data", search );
366  // kDebug() << "path:" << search << "=" << path;
367  }
368 
369  // this doesn't really do anything useful
370  // TODO: remove later
371  if (path.isEmpty()) {
372  path = "lancelotrc";
373  }
374 
375  confTheme = new KConfig(path);
376 }
377 
378 void Global::Private::themeChanged()
379 {
380  createConfTheme();
381  loadAllGroups(true);
382 }
383 
384 void Global::Private::loadAllGroups(bool clearFirst)
385 {
386  if (clearFirst) {
387  foreach(Group * group, groups) {
388  group->d->reset();
389  }
390  }
391 
392  foreach(Group * group, groups) {
393  group->load(true);
394  }
395 }
396 
397 Global::Global()
398  : d(new Private())
399 {
400  d->confMain = new KConfig("lancelotrc");
401 
402  Plasma::Theme::defaultTheme()->setUseGlobalSettings(true);
403  d->createConfTheme();
404 
405  connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
406  d, SLOT(themeChanged()));
407 }
408 
409 Global::~Global()
410 {
411  foreach (Group * group, d->groups) {
412  delete group;
413  }
414  delete d->confMain;
415  delete d->confTheme;
416 
417  delete d;
418 }
419 
420 KConfig * Global::theme() const
421 {
422  return d->confTheme;
423 }
424 
425 KConfig * Global::config()
426 {
427  return d->confMain;
428 }
429 
430 bool Global::groupExists(const QString & name) const
431 {
432  QString groupName = name;
433  if (groupName.isEmpty()) {
434  groupName = "Default";
435  }
436 
437  return (!d->groups.contains(groupName))
438  || theme()->groupList().contains(
439  "Group-" + groupName);
440 }
441 
442 Group * Global::group(const QString & name)
443 {
444  QString groupName = name;
445  if (groupName.isEmpty()) {
446  groupName = "Default";
447  }
448 
449  if (!d->groups.contains(groupName)) {
450  Group * group = new Group(groupName);
451  d->groups.insert(groupName, group);
452  group->load();
453  }
454 
455  return d->groups[groupName];
456 }
457 
458 Group * Global::defaultGroup()
459 {
460  return group("Default");
461 }
462 
463 Group * Global::groupForObject(QObject * object)
464 {
465  foreach (Group * group, d->groups) {
466  if (group->contains(object)) {
467  return group;
468  }
469  }
470  return NULL;
471 }
472 
473 void Global::setGroupForObject(QObject * object, Group * group) {
474  Group * oldGroup = groupForObject(object);
475  if (oldGroup == group) {
476  return;
477  }
478 
479  if (oldGroup) {
480  oldGroup->remove(object);
481  }
482 
483  if (group) {
484  group->add(object);
485  connect(object, SIGNAL(destroyed(QObject*)),
486  d, SLOT(objectDeleted(QObject*)));
487  }
488 }
489 
490 void Global::setImmutability(const Plasma::ImmutabilityType immutable)
491 {
492  if (d->immutability == immutable) {
493  return;
494  }
495 
496  d->immutability = immutable;
497  emit immutabilityChanged(immutable);
498 }
499 
500 Plasma::ImmutabilityType Global::immutability() const
501 {
502  return d->immutability;
503 }
504 
505 bool Global::config(const QString & group, const QString & field, bool defaultValue) const
506 {
507  KConfigGroup config(Global::self()->config(), group);
508  return config.readEntry(field, defaultValue);
509 }
510 
511 int Global::config(const QString & group, const QString & field, int defaultValue) const
512 {
513  KConfigGroup config(Global::self()->config(), group);
514  return config.readEntry(field, defaultValue);
515 }
516 
517 QString Global::config(const QString & group, const QString & field, const QString & defaultValue) const
518 {
519  KConfigGroup config(Global::self()->config(), group);
520  return config.readEntry(field, defaultValue);
521 }
522 
523 
524 } // namespace Lancelot
525 
QObject::child
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
Lancelot::GroupPrivate::backgroundColor
Group::ColorScheme backgroundColor
Definition: Global_p.h:53
QMutableMapIterator
Lancelot::Global::theme
KConfig * theme() const
Definition: Global.cpp:420
QMap::contains
bool contains(const Key &key) const
QMutableMapIterator::key
const Key & key() const
Lancelot::Global::Private::groups
QMap< QString, Group * > groups
Definition: Global_p.h:69
Lancelot::Global::immutabilityChanged
void immutabilityChanged(const Plasma::ImmutabilityType immutable)
Lancelot::Global::self
static Global * self()
Definition: Global.cpp:307
Lancelot::Global::setImmutability
void setImmutability(const Plasma::ImmutabilityType immutable)
Sets immutability.
Definition: Global.cpp:490
Lancelot::Group::ColorScheme::normal
QColor normal
Definition: Global.h:75
Lancelot::Global::groupForObject
Group * groupForObject(QObject *object)
Definition: Global.cpp:463
Lancelot::Global::Private::objectDeleted
void objectDeleted(QObject *object)
Definition: Global.cpp:327
QMap< QString, QVariant >
Lancelot::Global::setGroupForObject
void setGroupForObject(QObject *object, Group *group)
Sets the group for the object.
Definition: Global.cpp:473
Lancelot::Global::Private::confMain
KConfig * confMain
Definition: Global_p.h:71
Lancelot::Global::defaultGroup
Group * defaultGroup()
Definition: Global.cpp:458
Lancelot::GroupPrivate::confGroupTheme
KConfigGroup confGroupTheme()
Definition: Global.cpp:125
Lancelot::GroupPrivate::objects
QSet< QObject * > objects
Definition: Global_p.h:50
Lancelot::GroupPrivate
Definition: Global_p.h:36
Lancelot::Group::backgroundColor
const ColorScheme * backgroundColor() const
Background color is one of the common properties, so a direct function that accesses it is provided...
Definition: Global.cpp:204
Lancelot::Group::ColorScheme::active
QColor active
Definition: Global.h:75
Lancelot::Global::~Global
virtual ~Global()
Destroys this Lancelot::Global.
Definition: Global.cpp:409
Lancelot::GroupPrivate::loaded
bool loaded
Definition: Global_p.h:57
Lancelot::GroupPrivate::ownsBackgroundSvg
bool ownsBackgroundSvg
Definition: Global_p.h:56
Lancelot::Global::group
Group * group(const QString &name)
Definition: Global.cpp:442
Lancelot::Global::Private::confTheme
KConfig * confTheme
Definition: Global_p.h:72
Lancelot::Group::backgroundSvg
Plasma::FrameSvg * backgroundSvg() const
Background SVG image is one of the common properties, so a direct function that accesses it is provid...
Definition: Global.cpp:199
Lancelot::Global::Private::Private
Private()
Definition: Global.cpp:315
QObject::name
const char * name() const
Lancelot::Global::Private::immutability
Plasma::ImmutabilityType immutability
Definition: Global_p.h:74
QMapIterator
Lancelot::Group::contains
bool contains(QObject *object)
Definition: Global.cpp:297
Lancelot::Group::add
void add(QObject *object)
Adds object to the group.
Definition: Global.cpp:141
Lancelot::GroupPrivate::properties
QMap< QString, QVariant > properties
Definition: Global_p.h:47
Lancelot::Global::Private::~Private
~Private()
Definition: Global.cpp:323
QMapIterator::next
Item next()
QObject
Lancelot::GroupPrivate::persistentProperties
QSet< QString > persistentProperties
Definition: Global_p.h:48
Lancelot::Global::Private::themeChanged
void themeChanged()
Definition: Global.cpp:378
Lancelot::Group
Represents a group of object.
Definition: Global.h:63
QString::isEmpty
bool isEmpty() const
Lancelot::GroupPrivate::backgroundSvg
Plasma::FrameSvg * backgroundSvg
Definition: Global_p.h:54
Lancelot::Global::config
KConfig * config()
Definition: Global.cpp:425
Lancelot::Group::ColorScheme::ColorScheme
ColorScheme()
Definition: Global.cpp:37
Global.h
QString
QColor
QMapIterator::key
const Key & key() const
QMapIterator::value
const T & value() const
Lancelot::Group::name
QString name() const
Definition: Global.cpp:194
Lancelot::GroupPrivate::GroupPrivate
GroupPrivate()
Definition: Global.cpp:42
Lancelot::Global::Private::loadAllGroups
void loadAllGroups(bool clearFirst=true)
Definition: Global.cpp:384
QMutableMapIterator::hasNext
bool hasNext() const
QSet::contains
bool contains(const T &value) const
Lancelot::GroupPrivate::name
QString name
Definition: Global_p.h:46
Lancelot::GroupPrivate::copyFrom
void copyFrom(GroupPrivate *d)
Definition: Global.cpp:95
QMutableMapIterator::next
Item next()
Lancelot::Group::clearProperty
void clearProperty(const QString &property)
Clears the value of the specified property.
Definition: Global.cpp:189
Lancelot::Group::remove
void remove(QObject *object, bool setDefaultGroup=true)
Removes the object from the group.
Definition: Global.cpp:156
Lancelot::Group::hasProperty
bool hasProperty(const QString &property) const
Definition: Global.cpp:164
QSet::remove
bool remove(const T &value)
Lancelot::Global::immutability
Plasma::ImmutabilityType immutability() const
Definition: Global.cpp:500
QMutableMapIterator::remove
void remove()
Lancelot::GroupPrivate::~GroupPrivate
~GroupPrivate()
Definition: Global.cpp:51
Lancelot::Group::ColorScheme
This class contains the triplet od colors for three standard object states - normal, disabled and active.
Definition: Global.h:71
Global_p.h
Lancelot::Global::Private::instance
static Global * instance
Definition: Global_p.h:67
Lancelot::Group::property
QVariant property(const QString &property) const
Definition: Global.cpp:169
Lancelot::Global::Private::createConfTheme
void createConfTheme()
Definition: Global.cpp:335
Lancelot::Global
Global object represents one instance of Lancelot-based process inside a main application.
Definition: Global.h:184
Lancelot::GroupPrivate::foregroundColor
Group::ColorScheme foregroundColor
Definition: Global_p.h:52
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Lancelot::Group::load
void load(bool full=false)
Loads the group properties from a configuration file.
Definition: Global.cpp:218
Lancelot::Group::ColorScheme::disabled
QColor disabled
Definition: Global.h:75
Lancelot::Group::foregroundColor
const ColorScheme * foregroundColor() const
Foreground color is one of the common properties, so a direct function that accesses it is provided...
Definition: Global.cpp:213
Lancelot::Group::setProperty
void setProperty(const QString &property, const QVariant &value, bool persistent=true)
Sets the value of the specified property.
Definition: Global.cpp:174
Lancelot::GroupPrivate::setObjectProperty
void setObjectProperty(QObject *object, const QString &property, const QVariant &value)
Definition: Global.cpp:61
QObject::destroyed
void destroyed(QObject *obj)
QString::toAscii
QByteArray toAscii() const
QMapIterator::hasNext
bool hasNext() const
Lancelot::Global::groupExists
bool groupExists(const QString &name) const
Definition: Global.cpp:430
Lancelot::Global::Private
Definition: Global_p.h:60
Lancelot::GroupPrivate::reset
void reset()
Definition: Global.cpp:68
QMap::value
const T value(const Key &key) const
QMap::remove
int remove(const Key &key)
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:43:01 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

liblancelot

Skip menu "liblancelot"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

workspace API Reference

Skip menu "workspace API Reference"
  • kdeplasma-addons
  •       GroupingDesktop
  •     liblancelot

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