Scripted effects become configurable

The scripted effects can define their configuration through a
KConfigXT file in the same way as a packaged Plasmoid. Because of
that the ScriptedEffect also uses the Plasma::ConfigLoader to load
and manage the configuration. The config group used for the scripted
effect is like any other effect the "Effect-name" group.

In difference to the Plasmoid JavaScript API effects are not allowed
to change their configuration.
This commit is contained in:
Martin Gräßlin 2012-02-01 14:26:54 +01:00
parent e363c6bb18
commit 8996876044
3 changed files with 94 additions and 6 deletions

View file

@ -1241,7 +1241,7 @@ bool EffectsHandlerImpl::loadScriptedEffect(const QString& name, KService *servi
kDebug(1212) << "Could not locate the effect script";
return false;
}
ScriptedEffect *effect = ScriptedEffect::create(scriptFile);
ScriptedEffect *effect = ScriptedEffect::create(name, scriptFile);
if (!effect) {
return false;
}

View file

@ -22,6 +22,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "meta.h"
// KDE
#include <KDE/KDebug>
#include <KDE/KStandardDirs>
#include <KDE/Plasma/ConfigLoader>
// Qt
#include <QtCore/QFile>
#include <QtScript/QScriptEngine>
@ -87,10 +89,10 @@ void fpx2FromScriptValue(const QScriptValue &value, KWin::FPx2 &fpx2)
}
}
ScriptedEffect *ScriptedEffect::create(const QString &pathToScript)
ScriptedEffect *ScriptedEffect::create(const QString& effectName, const QString& pathToScript)
{
ScriptedEffect *effect = new ScriptedEffect();
if (!effect->init(pathToScript)) {
if (!effect->init(effectName, pathToScript)) {
delete effect;
return NULL;
}
@ -100,17 +102,22 @@ ScriptedEffect *ScriptedEffect::create(const QString &pathToScript)
ScriptedEffect::ScriptedEffect()
: AnimationEffect()
, m_engine(new QScriptEngine(this))
, m_scriptFile(QString())
, m_currentConfig(QString("main"))
{
connect(m_engine, SIGNAL(signalHandlerException(QScriptValue)), SLOT(signalHandlerException(QScriptValue)));
}
bool ScriptedEffect::init(const QString &pathToScript)
bool ScriptedEffect::init(const QString &effectName, const QString &pathToScript)
{
QFile scriptFile(pathToScript);
if (!scriptFile.open(QIODevice::ReadOnly)) {
return false;
}
m_effectName = effectName;
m_scriptFile = pathToScript;
loadConfig("main");
QScriptValue effectsObject = m_engine->newQObject(effects, QScriptEngine::QtOwnership, QScriptEngine::ExcludeDeleteLater);
m_engine->globalObject().setProperty("effects", effectsObject, QScriptValue::Undeletable);
m_engine->globalObject().setProperty("Effect", m_engine->newQMetaObject(&ScriptedEffect::staticMetaObject));
@ -162,4 +169,57 @@ bool ScriptedEffect::isGrabbed(EffectWindow* w, ScriptedEffect::DataRole grabRol
}
}
void ScriptedEffect::reconfigure(ReconfigureFlags flags)
{
AnimationEffect::reconfigure(flags);
emit configChanged();
}
QString ScriptedEffect::activeConfig() const
{
return m_currentConfig;
}
void ScriptedEffect::setActiveConfig(const QString &name)
{
if (name.isEmpty()) {
m_currentConfig = "main";
return;
}
Plasma::ConfigLoader *loader = m_configs.value(name, 0);
if (!loader) {
if (!loadConfig(name)) {
return;
}
}
m_currentConfig = name;
}
QVariant ScriptedEffect::readConfig(const QString& key)
{
Plasma::ConfigLoader *config = m_configs.value(m_currentConfig);
QVariant result;
if (config) {
result = config->property(key);
}
return result;
}
bool ScriptedEffect::loadConfig(const QString& name)
{
const QString path = KStandardDirs::locateLocal("data", "kwin/effects/" + m_effectName + "/config/" + name + ".xml");
if (path.isEmpty()) {
return false;
}
QFile f(path);
KConfigGroup cg = effects->effectConfig(m_effectName);
Plasma::ConfigLoader *loader = new Plasma::ConfigLoader(&cg, &f, this);
m_configs.insert(name, loader);
return true;
}
} // namespace

View file

@ -26,6 +26,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
class QScriptEngine;
class QScriptValue;
namespace Plasma {
class ConfigLoader;
}
namespace KWin
{
@ -33,6 +37,12 @@ class ScriptedEffect : public KWin::AnimationEffect
{
Q_OBJECT
Q_ENUMS(DataRole)
/**
* The current active configuration description. For instance, setting it to "foo" would cause the
* Effect to try and reference the contents/config/foo.xml KConfigXT file. Setting this to an empty
* string will switch to the main.xml file.
**/
Q_PROPERTY(QString activeConfig READ activeConfig WRITE setActiveConfig)
public:
// copied from kwineffects.h
enum DataRole {
@ -49,7 +59,10 @@ public:
const QString &scriptFile() const {
return m_scriptFile;
}
static ScriptedEffect *create(const QString &pathToScript);
virtual void reconfigure(ReconfigureFlags flags);
QString activeConfig() const;
void setActiveConfig(const QString &name);
static ScriptedEffect *create(const QString &effectName, const QString &pathToScript);
/**
* Whether another effect has grabbed the @p w with the given @p grabRole.
* @param w The window to check
@ -57,17 +70,32 @@ public:
* @returns @c true if another window has grabbed the effect, @c false otherwise
**/
Q_SCRIPTABLE bool isGrabbed(KWin::EffectWindow *w, DataRole grabRole);
/**
* Reads the value from the configuration data for the given key as defined by the currently active configuration.
* @returns The config value if present
**/
Q_SCRIPTABLE QVariant readConfig(const QString &key);
public Q_SLOTS:
void animate(KWin::EffectWindow *w, Attribute a, int ms, KWin::FPx2 to, KWin::FPx2 from = KWin::FPx2(), uint meta = 0, QEasingCurve curve = QEasingCurve(), int delay = 0);
Q_SIGNALS:
/**
* Signal emitted whenever the effect's config changed.
**/
void configChanged();
private Q_SLOTS:
void signalHandlerException(const QScriptValue &value);
private:
ScriptedEffect();
bool init(const QString &pathToScript);
bool init(const QString &effectName, const QString &pathToScript);
bool loadConfig(const QString &name);
QScriptEngine *m_engine;
QString m_effectName;
QString m_scriptFile;
QString m_currentConfig;
QMap<QString, Plasma::ConfigLoader*> m_configs;
};
}