2006-12-17 00:04:44 +01:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2006-2007 Aaron Seigo <aseigo@kde.org>
|
2006-12-17 00:04:44 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-09-14 21:06:18 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
2006-12-17 00:04:44 +01:00
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2007-05-21 16:28:03 +02:00
|
|
|
#include "theme.h"
|
|
|
|
|
2007-12-19 20:56:39 +01:00
|
|
|
#include <QApplication>
|
2007-11-04 07:57:42 +01:00
|
|
|
#include <QFile>
|
2008-03-05 20:06:03 +01:00
|
|
|
#ifdef Q_WS_X11
|
2008-02-07 11:54:38 +01:00
|
|
|
#include <QX11Info>
|
2008-03-05 20:06:03 +01:00
|
|
|
#endif
|
2007-11-04 07:57:42 +01:00
|
|
|
|
2008-01-10 21:53:13 +01:00
|
|
|
#include <KWindowSystem>
|
2007-11-27 22:56:47 +01:00
|
|
|
#include <KColorScheme>
|
|
|
|
#include <KConfigGroup>
|
|
|
|
#include <KDebug>
|
2008-04-24 00:24:07 +02:00
|
|
|
#include <KGlobal>
|
2008-02-07 11:54:38 +01:00
|
|
|
#include <KSelectionWatcher>
|
2006-12-17 00:04:44 +01:00
|
|
|
#include <KSharedConfig>
|
|
|
|
#include <KStandardDirs>
|
2008-02-26 06:08:08 +01:00
|
|
|
#include <KGlobalSettings>
|
2006-12-17 00:04:44 +01:00
|
|
|
|
2008-02-15 11:35:00 +01:00
|
|
|
#include "plasma/packages_p.h"
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class Theme::Private
|
|
|
|
{
|
2007-07-17 22:51:12 +02:00
|
|
|
public:
|
2008-04-24 00:55:30 +02:00
|
|
|
Private(Theme *theme)
|
|
|
|
: q(theme),
|
|
|
|
locolor(false),
|
|
|
|
compositingActive(KWindowSystem::compositingActive()),
|
2008-05-12 06:53:13 +02:00
|
|
|
isDefault(false),
|
|
|
|
useGlobal(true)
|
2008-04-24 00:55:30 +02:00
|
|
|
{
|
|
|
|
generalFont = QApplication::font();
|
|
|
|
}
|
2007-07-17 22:51:12 +02:00
|
|
|
|
2008-04-24 00:24:07 +02:00
|
|
|
KConfigGroup& config()
|
|
|
|
{
|
|
|
|
if (!cfg.isValid()) {
|
|
|
|
QString groupName = "Theme";
|
2007-11-04 04:04:55 +01:00
|
|
|
|
2008-05-12 06:53:13 +02:00
|
|
|
if (!useGlobal) {
|
|
|
|
QString app = KGlobal::mainComponent().componentName();
|
|
|
|
|
|
|
|
if (!app.isEmpty() && app != "plasma") {
|
|
|
|
kDebug() << "using theme for app" << app;
|
|
|
|
groupName.append("-").append(app);
|
|
|
|
}
|
2008-04-24 00:24:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg = KConfigGroup(KSharedConfig::openConfig("plasmarc"), groupName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg;
|
|
|
|
}
|
2007-11-04 04:04:55 +01:00
|
|
|
|
2008-02-09 05:54:43 +01:00
|
|
|
QString findInTheme(const QString &image, const QString &theme) const;
|
2008-04-24 00:55:30 +02:00
|
|
|
void compositingChanged();
|
2008-02-09 05:54:43 +01:00
|
|
|
|
2008-04-24 00:55:30 +02:00
|
|
|
static const char *defaultTheme;
|
2008-02-26 05:08:51 +01:00
|
|
|
static PackageStructure::Ptr packageStructure;
|
2008-04-24 00:55:30 +02:00
|
|
|
|
|
|
|
Theme *q;
|
2008-04-23 06:50:44 +02:00
|
|
|
QString themeName;
|
|
|
|
KSharedConfigPtr colors;
|
2008-04-24 00:24:07 +02:00
|
|
|
KConfigGroup cfg;
|
2008-04-23 06:50:44 +02:00
|
|
|
QFont generalFont;
|
2008-02-07 11:54:38 +01:00
|
|
|
#ifdef Q_WS_X11
|
2008-04-23 06:50:44 +02:00
|
|
|
KSelectionWatcher *compositeWatch;
|
2008-02-07 11:54:38 +01:00
|
|
|
#endif
|
2008-05-12 06:53:13 +02:00
|
|
|
bool locolor : 1;
|
|
|
|
bool compositingActive : 1;
|
|
|
|
bool isDefault : 1;
|
|
|
|
bool useGlobal : 1;
|
2006-12-17 00:04:44 +01:00
|
|
|
};
|
|
|
|
|
2008-02-26 05:08:51 +01:00
|
|
|
PackageStructure::Ptr Theme::Private::packageStructure(0);
|
2008-02-09 05:54:43 +01:00
|
|
|
const char *Theme::Private::defaultTheme = "default";
|
|
|
|
|
2008-04-24 00:55:30 +02:00
|
|
|
QString Theme::Private::findInTheme(const QString &image, const QString &theme) const
|
|
|
|
{
|
|
|
|
//TODO: this should be using Package
|
|
|
|
QString search;
|
|
|
|
|
|
|
|
if (locolor) {
|
|
|
|
search = "desktoptheme/" + theme + "/locolor/" + image + ".svg";
|
|
|
|
search = KStandardDirs::locate("data", search);
|
|
|
|
} else if (!compositingActive) {
|
|
|
|
search = "desktoptheme/" + theme + "/opaque/" + image + ".svg";
|
|
|
|
search = KStandardDirs::locate("data", search);
|
|
|
|
}
|
|
|
|
|
|
|
|
//not found or compositing enabled
|
|
|
|
if (search.isEmpty()) {
|
|
|
|
search = "desktoptheme/" + theme + '/' + image + ".svg";
|
|
|
|
search = KStandardDirs::locate("data", search);
|
|
|
|
}
|
|
|
|
|
|
|
|
return search;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Theme::Private::compositingChanged()
|
|
|
|
{
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
bool nowCompositingActive = compositeWatch->owner() != None;
|
|
|
|
|
|
|
|
if (compositingActive != nowCompositingActive) {
|
|
|
|
compositingActive = nowCompositingActive;
|
|
|
|
emit q->themeChanged();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-03-08 00:27:37 +01:00
|
|
|
class ThemeSingleton
|
|
|
|
{
|
2007-07-17 22:51:12 +02:00
|
|
|
public:
|
2008-04-23 06:50:44 +02:00
|
|
|
ThemeSingleton()
|
|
|
|
{
|
|
|
|
self.d->isDefault = true;
|
|
|
|
}
|
|
|
|
|
2007-07-17 22:51:12 +02:00
|
|
|
Theme self;
|
2007-03-08 00:27:37 +01:00
|
|
|
};
|
|
|
|
|
2007-07-21 17:58:44 +02:00
|
|
|
K_GLOBAL_STATIC( ThemeSingleton, privateThemeSelf )
|
2007-03-08 00:27:37 +01:00
|
|
|
|
2008-04-22 23:10:14 +02:00
|
|
|
Theme* Theme::defaultTheme()
|
2007-03-08 00:27:37 +01:00
|
|
|
{
|
2007-07-21 17:58:44 +02:00
|
|
|
return &privateThemeSelf->self;
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
Theme::Theme(QObject* parent)
|
|
|
|
: QObject(parent),
|
2008-04-24 00:55:30 +02:00
|
|
|
d(new Private(this))
|
2006-12-17 00:04:44 +01:00
|
|
|
{
|
2007-07-17 22:51:12 +02:00
|
|
|
settingsChanged();
|
2008-02-07 11:54:38 +01:00
|
|
|
|
|
|
|
#ifdef Q_WS_X11
|
2008-02-09 05:54:43 +01:00
|
|
|
Display *dpy = QX11Info::display();
|
|
|
|
int screen = DefaultScreen(dpy);
|
|
|
|
d->locolor = DefaultDepth(dpy, screen) < 16;
|
|
|
|
|
|
|
|
if (!d->locolor) {
|
|
|
|
char net_wm_cm_name[100];
|
|
|
|
sprintf(net_wm_cm_name, "_NET_WM_CM_S%d", screen);
|
|
|
|
d->compositeWatch = new KSelectionWatcher(net_wm_cm_name, -1, this);
|
|
|
|
connect(d->compositeWatch, SIGNAL(newOwner(Window)), this, SLOT(compositingChanged()));
|
|
|
|
connect(d->compositeWatch, SIGNAL(lostOwner()), this, SLOT(compositingChanged()));
|
|
|
|
}
|
2008-02-07 11:54:38 +01:00
|
|
|
#endif
|
2006-12-17 00:04:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Theme::~Theme()
|
|
|
|
{
|
2007-06-07 22:57:18 +02:00
|
|
|
delete d;
|
2006-12-17 00:04:44 +01:00
|
|
|
}
|
|
|
|
|
2008-02-26 05:08:51 +01:00
|
|
|
PackageStructure::Ptr Theme::packageStructure()
|
2008-02-15 11:35:00 +01:00
|
|
|
{
|
2008-02-26 05:08:51 +01:00
|
|
|
if (!Private::packageStructure) {
|
|
|
|
Private::packageStructure = new ThemePackage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Private::packageStructure;
|
2008-02-15 11:35:00 +01:00
|
|
|
}
|
|
|
|
|
2007-07-17 22:51:12 +02:00
|
|
|
void Theme::settingsChanged()
|
|
|
|
{
|
2008-02-09 05:54:43 +01:00
|
|
|
setThemeName(d->config().readEntry("name", Private::defaultTheme));
|
2007-11-04 04:04:55 +01:00
|
|
|
}
|
2007-07-17 22:51:12 +02:00
|
|
|
|
2007-11-04 04:04:55 +01:00
|
|
|
void Theme::setThemeName(const QString &themeName)
|
|
|
|
{
|
2008-01-25 07:10:44 +01:00
|
|
|
QString theme = themeName;
|
|
|
|
if (theme.isEmpty() || theme == d->themeName) {
|
2007-11-04 07:57:42 +01:00
|
|
|
// let's try and get the default theme at least
|
|
|
|
if (d->themeName.isEmpty()) {
|
2008-02-09 05:54:43 +01:00
|
|
|
theme = Private::defaultTheme;
|
2008-01-25 07:10:44 +01:00
|
|
|
} else {
|
|
|
|
return;
|
2007-11-04 07:57:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: should we care about names with relative paths in them?
|
2008-05-09 07:32:15 +02:00
|
|
|
QString themePath = KStandardDirs::locate("data", "desktoptheme/" + theme + '/');
|
2008-01-25 07:10:44 +01:00
|
|
|
if (themePath.isEmpty() && d->themeName.isEmpty()) {
|
|
|
|
themePath = KStandardDirs::locate("data", "desktoptheme/default/");
|
|
|
|
|
|
|
|
if (themePath.isEmpty()) {
|
|
|
|
return;
|
2007-11-04 07:57:42 +01:00
|
|
|
}
|
2008-01-25 07:10:44 +01:00
|
|
|
|
2008-02-09 05:54:43 +01:00
|
|
|
theme = Private::defaultTheme;
|
2007-11-04 07:57:42 +01:00
|
|
|
}
|
|
|
|
|
2008-01-25 07:10:44 +01:00
|
|
|
d->themeName = theme;
|
2007-11-04 07:57:42 +01:00
|
|
|
|
|
|
|
// load the color scheme config
|
2008-01-25 07:10:44 +01:00
|
|
|
QString colorsFile = KStandardDirs::locate("data", "desktoptheme/" + theme + "/colors");
|
|
|
|
//kDebug() << "we're going for..." << colorsFile << "*******************";
|
|
|
|
|
2008-04-24 00:55:30 +02:00
|
|
|
disconnect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), this, SIGNAL(themeChanged()));
|
2008-01-25 07:10:44 +01:00
|
|
|
if (colorsFile.isEmpty()) {
|
2007-11-04 07:57:42 +01:00
|
|
|
d->colors = 0;
|
2008-04-24 00:55:30 +02:00
|
|
|
connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), this, SIGNAL(themeChanged()));
|
2008-01-25 07:10:44 +01:00
|
|
|
} else {
|
|
|
|
d->colors = KSharedConfig::openConfig(colorsFile);
|
2007-07-17 22:51:12 +02:00
|
|
|
}
|
2007-11-04 07:57:42 +01:00
|
|
|
|
2008-04-23 06:50:44 +02:00
|
|
|
if (d->isDefault) {
|
|
|
|
// we're the default theme, let's save our state
|
2008-04-24 00:24:07 +02:00
|
|
|
KConfigGroup &cg = d->config();
|
2008-04-22 22:27:56 +02:00
|
|
|
if (Private::defaultTheme == d->themeName) {
|
|
|
|
cg.deleteEntry("name");
|
|
|
|
} else {
|
|
|
|
cg.writeEntry("name", d->themeName);
|
|
|
|
}
|
2008-02-15 11:47:56 +01:00
|
|
|
}
|
|
|
|
|
2008-04-23 10:35:35 +02:00
|
|
|
emit themeChanged();
|
2007-07-17 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
QString Theme::themeName() const
|
|
|
|
{
|
|
|
|
return d->themeName;
|
|
|
|
}
|
|
|
|
|
2008-04-16 17:53:16 +02:00
|
|
|
QString Theme::imagePath(const QString& name) const
|
2008-02-09 05:54:43 +01:00
|
|
|
{
|
|
|
|
QString path = d->findInTheme(name, d->themeName);
|
|
|
|
|
|
|
|
if (path.isEmpty() && d->themeName != Private::defaultTheme) {
|
|
|
|
path = d->findInTheme(name, Private::defaultTheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
kDebug() << "Theme says: bad image path " << name;
|
2007-03-08 00:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
2006-12-17 00:04:44 +01:00
|
|
|
}
|
|
|
|
|
2008-04-16 17:53:16 +02:00
|
|
|
KSharedConfigPtr Theme::colorScheme() const
|
2007-11-04 07:57:42 +01:00
|
|
|
{
|
|
|
|
return d->colors;
|
|
|
|
}
|
|
|
|
|
2008-04-24 00:36:00 +02:00
|
|
|
QColor Theme::color(ColorRole role) const
|
2007-11-27 22:56:47 +01:00
|
|
|
{
|
2008-04-22 23:10:14 +02:00
|
|
|
KColorScheme colorScheme(QPalette::Active, KColorScheme::Window, Theme::defaultTheme()->colorScheme());
|
2007-11-27 22:56:47 +01:00
|
|
|
|
2008-04-24 00:36:00 +02:00
|
|
|
switch (role) {
|
|
|
|
case TextColor:
|
|
|
|
return colorScheme.foreground(KColorScheme::NormalText).color();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BackgroundColor:
|
|
|
|
return colorScheme.background().color();
|
|
|
|
break;
|
|
|
|
}
|
2008-04-24 00:55:30 +02:00
|
|
|
|
|
|
|
return QColor();
|
2008-01-08 02:43:13 +01:00
|
|
|
}
|
|
|
|
|
2008-04-24 00:36:00 +02:00
|
|
|
void Theme::setFont(const QFont &font, FontRole role)
|
2007-12-21 00:44:08 +01:00
|
|
|
{
|
2008-04-24 00:36:00 +02:00
|
|
|
Q_UNUSED(role)
|
2007-12-21 00:44:08 +01:00
|
|
|
d->generalFont = font;
|
|
|
|
}
|
|
|
|
|
2008-04-24 00:36:00 +02:00
|
|
|
QFont Theme::font(FontRole role) const
|
2007-12-19 20:56:39 +01:00
|
|
|
{
|
2008-04-24 00:36:00 +02:00
|
|
|
Q_UNUSED(role)
|
2007-12-21 00:44:08 +01:00
|
|
|
return d->generalFont;
|
2007-12-19 20:56:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QFontMetrics Theme::fontMetrics() const
|
|
|
|
{
|
|
|
|
//TODO: allow this to be overridden with a plasma specific font?
|
2007-12-21 00:44:08 +01:00
|
|
|
return QFontMetrics(d->generalFont);
|
2007-12-19 20:56:39 +01:00
|
|
|
}
|
|
|
|
|
2008-04-24 00:55:30 +02:00
|
|
|
bool Theme::windowTranslucencyEnabled() const
|
2008-03-05 22:03:27 +01:00
|
|
|
{
|
|
|
|
return d->compositingActive;
|
|
|
|
}
|
|
|
|
|
2008-05-12 06:53:13 +02:00
|
|
|
void Theme::setUseGlobalSettings(bool useGlobal)
|
|
|
|
{
|
|
|
|
d->useGlobal = useGlobal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Theme::useGlobalSettings() const
|
|
|
|
{
|
|
|
|
return d->useGlobal;
|
|
|
|
}
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include <theme.moc>
|