a little class that loads files from the configured l&f package with fallback to the default one provides a signal when the theme changes, for things that need to react at runtimewilder-5.14
parent
fdce67b80d
commit
20e1b7de88
5 changed files with 188 additions and 5 deletions
@ -0,0 +1,120 @@ |
|||||||
|
/*
|
||||||
|
* Copyright 2014 Marco Martin <notmart@gmail.com> |
||||||
|
* |
||||||
|
* This program 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, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
*/ |
||||||
|
|
||||||
|
|
||||||
|
#include "lookandfeelaccess.h" |
||||||
|
|
||||||
|
#include "shellpluginloader.h" |
||||||
|
|
||||||
|
#include <QDebug> |
||||||
|
#include <KDirWatch> |
||||||
|
#include <KSharedConfig> |
||||||
|
|
||||||
|
class LookAndFeelAccessPrivate |
||||||
|
{ |
||||||
|
public: |
||||||
|
LookAndFeelAccessPrivate(LookAndFeelAccess *access) |
||||||
|
: q(access) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
void settingsFileChanged(); |
||||||
|
|
||||||
|
|
||||||
|
LookAndFeelAccess *q; |
||||||
|
Plasma::Package package; |
||||||
|
Plasma::Package defaultPackage; |
||||||
|
KSharedConfig::Ptr config; |
||||||
|
}; |
||||||
|
|
||||||
|
void LookAndFeelAccessPrivate::settingsFileChanged() |
||||||
|
{ |
||||||
|
config->reparseConfiguration(); |
||||||
|
KConfigGroup cg(config, "KDE"); |
||||||
|
|
||||||
|
const QString packageName = cg.readEntry("LookAndFeelPackage", QString()); |
||||||
|
const QString oldPackageName = package.isValid() ? package.metadata().pluginName() : QString(); |
||||||
|
|
||||||
|
if (packageName == oldPackageName) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
//Invalid
|
||||||
|
if (packageName.isEmpty() && !oldPackageName.isEmpty()) { |
||||||
|
package = Plasma::Package(); |
||||||
|
} else if (!packageName.isEmpty()) { |
||||||
|
package = Plasma::PluginLoader::self()->loadPackage("Plasma/LookAndFeel"); |
||||||
|
package.setPath(packageName); |
||||||
|
} |
||||||
|
|
||||||
|
emit q->packageChanged(); |
||||||
|
} |
||||||
|
|
||||||
|
LookAndFeelAccess::LookAndFeelAccess(QObject *parent) |
||||||
|
: QObject(parent), |
||||||
|
d(new LookAndFeelAccessPrivate(this)) |
||||||
|
{ |
||||||
|
ShellPluginLoader::init(); |
||||||
|
d->defaultPackage = Plasma::PluginLoader::self()->loadPackage("Plasma/LookAndFeel"); |
||||||
|
d->defaultPackage.setPath("org.kde.lookandfeel"); |
||||||
|
|
||||||
|
d->config = KSharedConfig::openConfig("kdeglobals"); |
||||||
|
KConfigGroup cg(d->config, "KDE"); |
||||||
|
const QString packageName = cg.readEntry("LookAndFeelPackage", QString()); |
||||||
|
if (!packageName.isEmpty()) { |
||||||
|
d->package = Plasma::PluginLoader::self()->loadPackage("Plasma/LookAndFeel"); |
||||||
|
d->package.setPath(packageName); |
||||||
|
} |
||||||
|
|
||||||
|
const QString configFile = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/kdeglobals"; |
||||||
|
KDirWatch::self()->addFile(configFile); |
||||||
|
|
||||||
|
// Catch both, direct changes to the config file ...
|
||||||
|
connect(KDirWatch::self(), SIGNAL(dirty()), this, SLOT(settingsFileChanged())); |
||||||
|
// ... but also remove/recreate cycles, like KConfig does it
|
||||||
|
connect(KDirWatch::self(), SIGNAL(created()), this, SLOT(settingsFileChanged())); |
||||||
|
} |
||||||
|
|
||||||
|
LookAndFeelAccess::~LookAndFeelAccess() |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
QString LookAndFeelAccess::filePath(const char *key, const QString &filename) const |
||||||
|
{ |
||||||
|
const QString path = d->package.filePath(key, filename); |
||||||
|
|
||||||
|
if (!path.isEmpty()) { |
||||||
|
return path; |
||||||
|
} else { |
||||||
|
return d->defaultPackage.filePath(key, filename); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
KPluginInfo LookAndFeelAccess::metadata() const |
||||||
|
{ |
||||||
|
if (d->package.isValid()) { |
||||||
|
return d->package.metadata(); |
||||||
|
} else { |
||||||
|
return d->defaultPackage.metadata(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
#include "moc_lookandfeelaccess.cpp" |
||||||
@ -0,0 +1,64 @@ |
|||||||
|
/*
|
||||||
|
* Copyright 2014 Marco Martin <notmart@gmail.com> |
||||||
|
* |
||||||
|
* This program 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, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef LOOKANDFEELACCESS_H |
||||||
|
#define LOOKANDFEELACCESS_H |
||||||
|
|
||||||
|
#include <QtCore/QObject> |
||||||
|
|
||||||
|
#include <KPluginInfo> |
||||||
|
|
||||||
|
class LookAndFeelAccessPrivate; |
||||||
|
|
||||||
|
class LookAndFeelAccess : public QObject |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
|
||||||
|
public: |
||||||
|
LookAndFeelAccess(QObject *parent = 0); |
||||||
|
~LookAndFeelAccess(); |
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the path to a given file based on the key and an optional filename. |
||||||
|
* Example: finding the main script in a scripting package: |
||||||
|
* filePath("mainscript") |
||||||
|
* |
||||||
|
* Example: finding a specific image in the images directory: |
||||||
|
* filePath("images", "myimage.png") |
||||||
|
* |
||||||
|
* @param key the key of the file type to look for, |
||||||
|
* @param filename optional name of the file to locate within the package |
||||||
|
* @return path to the file on disk. QString() if not found. |
||||||
|
**/ |
||||||
|
QString filePath(const char *key, const QString &filename = QString()) const; |
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the package metadata object. |
||||||
|
*/ |
||||||
|
KPluginInfo metadata() const; |
||||||
|
|
||||||
|
Q_SIGNALS: |
||||||
|
void packageChanged(); |
||||||
|
|
||||||
|
private: |
||||||
|
LookAndFeelAccessPrivate *const d; |
||||||
|
Q_PRIVATE_SLOT(d, void settingsFileChanged()) |
||||||
|
}; |
||||||
|
|
||||||
|
#endif |
||||||
Loading…
Reference in new issue