diff --git a/effects.cpp b/effects.cpp index 5acd02e9c3..4da5753e3c 100644 --- a/effects.cpp +++ b/effects.cpp @@ -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; } diff --git a/scripting/scriptedeffect.cpp b/scripting/scriptedeffect.cpp index 057342909e..0608012823 100644 --- a/scripting/scriptedeffect.cpp +++ b/scripting/scriptedeffect.cpp @@ -22,6 +22,8 @@ along with this program. If not, see . #include "meta.h" // KDE #include +#include +#include // Qt #include #include @@ -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 diff --git a/scripting/scriptedeffect.h b/scripting/scriptedeffect.h index cf21e75a5d..00012c1729 100644 --- a/scripting/scriptedeffect.h +++ b/scripting/scriptedeffect.h @@ -26,6 +26,10 @@ along with this program. If not, see . 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 m_configs; }; }