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-02-07 11:54:38 +01:00
|
|
|
#include <QX11Info>
|
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-02-07 11:54:38 +01:00
|
|
|
#include <KSelectionWatcher>
|
2006-12-17 00:04:44 +01:00
|
|
|
#include <KSharedConfig>
|
|
|
|
#include <KStandardDirs>
|
|
|
|
|
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:
|
|
|
|
Private()
|
2008-02-09 05:54:43 +01:00
|
|
|
: locolor(false),
|
|
|
|
compositingActive(KWindowSystem::compositingActive())
|
2007-07-17 22:51:12 +02:00
|
|
|
{
|
2007-12-21 00:44:08 +01:00
|
|
|
generalFont = QApplication::font();
|
2007-07-17 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2007-11-04 04:04:55 +01:00
|
|
|
KConfigGroup config()
|
|
|
|
{
|
|
|
|
QString groupName = "Theme";
|
|
|
|
if (!app.isEmpty() && app != "plasma") {
|
|
|
|
groupName.append("-").append(app);
|
|
|
|
}
|
|
|
|
|
2007-11-04 07:57:42 +01:00
|
|
|
KSharedConfigPtr config = KSharedConfig::openConfig("plasmarc");
|
2007-11-04 04:04:55 +01:00
|
|
|
return KConfigGroup(config, groupName);
|
|
|
|
}
|
|
|
|
|
2008-02-09 05:54:43 +01:00
|
|
|
QString findInTheme(const QString &image, const QString &theme) const;
|
|
|
|
static const char *defaultTheme;
|
|
|
|
|
2007-07-17 22:51:12 +02:00
|
|
|
QString themeName;
|
|
|
|
QString app;
|
2007-11-04 07:57:42 +01:00
|
|
|
KSharedConfigPtr colors;
|
2007-12-21 00:44:08 +01:00
|
|
|
QFont generalFont;
|
2008-02-09 05:54:43 +01:00
|
|
|
bool locolor;
|
2008-02-07 11:54:38 +01:00
|
|
|
bool compositingActive;
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
KSelectionWatcher *compositeWatch;
|
|
|
|
#endif
|
2006-12-17 00:04:44 +01:00
|
|
|
};
|
|
|
|
|
2008-02-09 05:54:43 +01:00
|
|
|
const char *Theme::Private::defaultTheme = "default";
|
|
|
|
|
2007-03-08 00:27:37 +01:00
|
|
|
class ThemeSingleton
|
|
|
|
{
|
2007-07-17 22:51:12 +02:00
|
|
|
public:
|
|
|
|
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
|
|
|
|
|
|
|
Theme* Theme::self()
|
|
|
|
{
|
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),
|
|
|
|
d(new Private)
|
|
|
|
{
|
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-11-04 04:04:55 +01:00
|
|
|
d->config().writeEntry("name", d->themeName);
|
2007-06-07 22:57:18 +02:00
|
|
|
delete d;
|
2006-12-17 00:04:44 +01:00
|
|
|
}
|
|
|
|
|
2008-02-15 11:35:00 +01:00
|
|
|
PackageStructure Theme::packageStructure()
|
|
|
|
{
|
|
|
|
return ThemePackageStructure();
|
|
|
|
}
|
|
|
|
|
2007-07-17 22:51:12 +02:00
|
|
|
void Theme::setApplication(const QString &appname)
|
|
|
|
{
|
|
|
|
if (d->app != appname) {
|
|
|
|
d->app = appname;
|
|
|
|
settingsChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2008-02-07 11:54:38 +01:00
|
|
|
void Theme::compositingChanged()
|
|
|
|
{
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
bool compositingActive = d->compositeWatch->owner() != None;
|
|
|
|
|
|
|
|
if (d->compositingActive != compositingActive) {
|
|
|
|
d->compositingActive = compositingActive;
|
|
|
|
emit changed();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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-01-25 07:10:44 +01:00
|
|
|
QString themePath = KStandardDirs::locate("data", "desktoptheme/" + theme + "/");
|
|
|
|
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 << "*******************";
|
|
|
|
|
|
|
|
if (colorsFile.isEmpty()) {
|
2007-11-04 07:57:42 +01:00
|
|
|
d->colors = 0;
|
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
|
|
|
|
|
|
|
emit changed();
|
2007-07-17 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
QString Theme::themeName() const
|
|
|
|
{
|
|
|
|
return d->themeName;
|
|
|
|
}
|
|
|
|
|
2008-02-09 05:54:43 +01:00
|
|
|
QString Theme::Private::findInTheme(const QString &image, const QString &theme) const
|
2006-12-17 00:04:44 +01:00
|
|
|
{
|
2008-02-09 05:54:43 +01:00
|
|
|
//TODO: this should be using Package
|
2008-01-10 21:53:13 +01:00
|
|
|
QString search;
|
2008-02-09 05:54:43 +01:00
|
|
|
|
|
|
|
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);
|
2008-01-10 21:53:13 +01:00
|
|
|
}
|
2008-02-09 05:54:43 +01:00
|
|
|
|
2008-01-10 21:53:13 +01:00
|
|
|
//not found or compositing enabled
|
2008-02-09 05:54:43 +01:00
|
|
|
if (search.isEmpty()) {
|
|
|
|
search = "desktoptheme/" + theme + '/' + image + ".svg";
|
|
|
|
search = KStandardDirs::locate("data", search);
|
2008-01-10 21:53:13 +01:00
|
|
|
}
|
2007-03-08 00:27:37 +01:00
|
|
|
|
2008-02-09 05:54:43 +01:00
|
|
|
return search;
|
|
|
|
}
|
2007-11-04 04:04:55 +01:00
|
|
|
|
2008-02-09 05:54:43 +01:00
|
|
|
QString Theme::image(const QString& name) const
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-11-04 07:57:42 +01:00
|
|
|
KSharedConfigPtr Theme::colors() const
|
|
|
|
{
|
|
|
|
return d->colors;
|
|
|
|
}
|
|
|
|
|
2007-11-27 22:56:47 +01:00
|
|
|
QColor Theme::textColor() const
|
|
|
|
{
|
2007-11-28 04:20:56 +01:00
|
|
|
KColorScheme colors(QPalette::Active, KColorScheme::Window, Theme::self()->colors());
|
2007-11-27 22:56:47 +01:00
|
|
|
return colors.foreground(KColorScheme::NormalText).color();
|
|
|
|
}
|
|
|
|
|
2008-01-08 02:43:13 +01:00
|
|
|
QColor Theme::backgroundColor() const
|
|
|
|
{
|
|
|
|
KColorScheme colors(QPalette::Active, KColorScheme::Window, Theme::self()->colors());
|
|
|
|
return colors.background().color();
|
|
|
|
}
|
|
|
|
|
2007-12-21 00:44:08 +01:00
|
|
|
void Theme::setFont(const QFont &font)
|
|
|
|
{
|
|
|
|
d->generalFont = font;
|
|
|
|
}
|
|
|
|
|
2007-12-19 20:56:39 +01:00
|
|
|
QFont Theme::font() const
|
|
|
|
{
|
|
|
|
//TODO: allow this to be overridden with a plasma specific font?
|
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
|
|
|
}
|
|
|
|
|
2006-12-17 00:04:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include <theme.moc>
|