move the event listener concept to the scriptenv, introduce the concept of addons for javascript components

svn path=/trunk/KDE/kdebase/runtime/; revision=1158928
This commit is contained in:
Aaron J. Seigo 2010-08-04 03:53:41 +00:00
parent 3d8bedb45a
commit 948fa2720f
6 changed files with 290 additions and 115 deletions

View File

@ -0,0 +1,50 @@
/*
* Copyright 2010 Aaron J. Seigo <aseigo@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* 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 JAVASCRIPTADDONPACKAGESTRUCTURE_H
#define JAVASCRIPTADDONPACKAGESTRUCTURE_H
#include <Plasma/PackageStructure>
class JavascriptAddonPackageStructure : public Plasma::PackageStructure
{
Q_OBJECT
public:
JavascriptAddonPackageStructure(QObject *parent = 0)
: Plasma::PackageStructure(parent)
{
setServicePrefix("plasma-layout-template");
setDefaultPackageRoot("plasma/layout-templates");
addFileDefinition("mainscript", "layout.js", i18n("Main Script File"));
setRequired("mainscript", true);
}
void pathChanged()
{
KDesktopFile config(path() + "/metadata.desktop");
KConfigGroup cg = config.desktopGroup();
QString mainScript = cg.readEntry("X-Plasma-MainScript", QString());
if (!mainScript.isEmpty()) {
addFileDefinition("mainscript", mainScript, i18n("Main Script File"));
}
}
};
#endif

View File

@ -25,18 +25,25 @@
#include <QMetaEnum> #include <QMetaEnum>
#include <KDebug> #include <KDebug>
#include <KDesktopFile>
#include <KIO/Job> #include <KIO/Job>
#include <KLocale> #include <KLocale>
#include <KMimeType> #include <KMimeType>
#include <KPluginInfo>
#include <KService> #include <KService>
#include <KServiceTypeTrader>
#include <KShell> #include <KShell>
#include <KStandardDirs> #include <KStandardDirs>
#include <KRun> #include <KRun>
#include <Plasma/Package>
#ifdef USEGUI #ifdef USEGUI
#include "simplebindings/filedialogproxy.h" #include "simplebindings/filedialogproxy.h"
#endif #endif
#include "javascriptaddonpackagestructure.h"
Q_DECLARE_METATYPE(ScriptEnv*) Q_DECLARE_METATYPE(ScriptEnv*)
ScriptEnv::ScriptEnv(QObject *parent, QScriptEngine *engine) ScriptEnv::ScriptEnv(QObject *parent, QScriptEngine *engine)
@ -49,6 +56,8 @@ ScriptEnv::ScriptEnv(QObject *parent, QScriptEngine *engine)
// Add utility functions // Add utility functions
global.setProperty("print", m_engine->newFunction(ScriptEnv::print)); global.setProperty("print", m_engine->newFunction(ScriptEnv::print));
global.setProperty("debug", m_engine->newFunction(ScriptEnv::debug)); global.setProperty("debug", m_engine->newFunction(ScriptEnv::debug));
global.setProperty("listAddons", m_engine->newFunction(ScriptEnv::listAddons));
global.setProperty("loadAddon", m_engine->newFunction(ScriptEnv::loadAddon));
// Add an accessor so we can find the scriptenv given only the engine. The // Add an accessor so we can find the scriptenv given only the engine. The
// property is hidden from scripts. // property is hidden from scripts.
@ -346,6 +355,155 @@ QScriptValue ScriptEnv::print(QScriptContext *context, QScriptEngine *engine)
return engine->undefinedValue(); return engine->undefinedValue();
} }
#ifndef USEGUI QScriptValue ScriptEnv::listAddons(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() < 1) {
return context->throwError(i18n("listAddons takes one argument: addon type"));
}
QStringList addons;
const QString type = context->argument(0).toString();
if (type.isEmpty()) {
return qScriptValueFromValue(engine, addons);
}
const QString constraint = QString("[X-KDE-PluginInfo-Category] == '%1'").arg(type);
KService::List offers = KServiceTypeTrader::self()->query("Plasma/JavascriptAddon", constraint);
foreach (KService::Ptr offer, offers) {
KPluginInfo info(offer);
addons << info.pluginName();
}
return qScriptValueFromValue(engine, addons);
}
QScriptValue ScriptEnv::loadAddon(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() < 2) {
return context->throwError(i18n("listAddons takes two arguments: addon type and addon name to load"));
}
const QString type = context->argument(0).toString();
const QString plugin = context->argument(1).toString();
if (type.isEmpty() || plugin.isEmpty()) {
return false;
}
const QString constraint = QString("[X-KDE-PluginInfo-Category] == '%1' and [X-KDE-PluginInfo-Name] == '%2'")
.arg(type, plugin);
KService::List offers = KServiceTypeTrader::self()->query("Plasma/JavascriptAddon", constraint);
if (offers.isEmpty()) {
return false;
}
Plasma::PackageStructure::Ptr structure(new JavascriptAddonPackageStructure);
const QString subPath = structure->defaultPackageRoot() + '/' + plugin + '/';
const QString path = KStandardDirs::locate("data", subPath);
Plasma::Package package(path, structure);
//FIXME include() will not work from within addons; needs a solution
QFile file(package.filePath("mainscript"));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
kError() << "failed to open script file" << path;
return false;
}
QTextStream buffer(&file);
QString code(buffer.readAll());
QScriptContext *innerContext = engine->pushContext();
innerContext->activationObject().setProperty("registerAddon", engine->newFunction(ScriptEnv::registerAddon));
engine->evaluate(code, file.fileName());
engine->popContext();
return engine->undefinedValue();
}
QScriptValue ScriptEnv::registerAddon(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() > 0) {
QScriptValue func = context->argument(0);
if (func.isFunction()) {
QScriptValue obj = func.construct();
QScriptValueList args;
args << obj;
ScriptEnv *env = ScriptEnv::findScriptEnv(engine);
if (env) {
env->callEventListeners("addonCreated", args);
}
}
}
return engine->undefinedValue();
}
void ScriptEnv::callFunction(QScriptValue &func, const QScriptValueList &args, const QScriptValue &activator)
{
if (!func.isFunction()) {
return;
}
QScriptContext *ctx = m_engine->pushContext();
ctx->setActivationObject(activator);
func.call(activator, args);
m_engine->popContext();
if (m_engine->hasUncaughtException()) {
emit reportError(this, false);
m_engine->clearExceptions();
}
}
bool ScriptEnv::hasEventListeners(const QString &event) const
{
return m_eventListeners.contains(event);
}
bool ScriptEnv::callEventListeners(const QString &event, const QScriptValueList &args)
{
if (!m_eventListeners.contains(event)) {
return false;
}
QScriptValueList funcs = m_eventListeners.value(event);
QMutableListIterator<QScriptValue> it(funcs);
while (it.hasNext()) {
callFunction(it.next(), args);
}
return true;
}
void ScriptEnv::addEventListener(const QString &event, const QScriptValue &func)
{
if (func.isFunction()) {
m_eventListeners[event.toLower()].append(func);
}
}
void ScriptEnv::removeEventListener(const QString &event, const QScriptValue &func)
{
if (func.isFunction()) {
QScriptValueList funcs = m_eventListeners.value("mousepress");
QMutableListIterator<QScriptValue> it(funcs);//m_eventListeners.value("mousepress"));
while (it.hasNext()) {
if (it.next().equals(func)) {
it.remove();
}
}
if (funcs.isEmpty()) {
m_eventListeners.remove(event.toLower());
} else {
m_eventListeners.insert(event.toLower(), funcs);
}
}
}
#include "scriptenv.moc" #include "scriptenv.moc"
#endif #include "javascriptaddonpackagestructure.moc"

View File

@ -54,6 +54,13 @@ public:
bool checkForErrors(bool fatal); bool checkForErrors(bool fatal);
void addEventListener(const QString &event, const QScriptValue &func);
void removeEventListener(const QString &event, const QScriptValue &func);
void callFunction(QScriptValue &func, const QScriptValueList &args = QScriptValueList(), const QScriptValue &activator = QScriptValue());
bool callEventListeners(const QString &event, const QScriptValueList &args = QScriptValueList());
bool hasEventListeners(const QString &event) const;
Q_SIGNALS: Q_SIGNALS:
void reportError(ScriptEnv *engine, bool fatal); void reportError(ScriptEnv *engine, bool fatal);
@ -67,7 +74,9 @@ private:
static QScriptValue runCommand(QScriptContext *context, QScriptEngine *engine); static QScriptValue runCommand(QScriptContext *context, QScriptEngine *engine);
static QScriptValue openUrl(QScriptContext *context, QScriptEngine *engine); static QScriptValue openUrl(QScriptContext *context, QScriptEngine *engine);
static QScriptValue getUrl(QScriptContext *context, QScriptEngine *engine); static QScriptValue getUrl(QScriptContext *context, QScriptEngine *engine);
static QScriptValue listAddons(QScriptContext *context, QScriptEngine *engine);
static QScriptValue loadAddon(QScriptContext *context, QScriptEngine *engine);
static QScriptValue registerAddon(QScriptContext *context, QScriptEngine *engine);
private Q_SLOTS: private Q_SLOTS:
void signalException(); void signalException();
@ -76,6 +85,7 @@ private:
QSet<QString> m_extensions; QSet<QString> m_extensions;
AllowedUrls m_allowedUrls; AllowedUrls m_allowedUrls;
QScriptEngine *m_engine; QScriptEngine *m_engine;
QHash<QString, QScriptValueList> m_eventListeners;
}; };
Q_DECLARE_OPERATORS_FOR_FLAGS(ScriptEnv::AllowedUrls) Q_DECLARE_OPERATORS_FOR_FLAGS(ScriptEnv::AllowedUrls)

View File

@ -18,4 +18,3 @@
#define USEGUI #define USEGUI
#include "scriptenv.cpp" #include "scriptenv.cpp"
#include "scriptenv.moc"

View File

@ -148,9 +148,8 @@ void SimpleJavaScriptApplet::reportError(ScriptEnv *env, bool fatal)
void SimpleJavaScriptApplet::configChanged() void SimpleJavaScriptApplet::configChanged()
{ {
if (m_eventListeners.contains("configchanged")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
callEventListeners("configchanged"); if (!env || !env->callEventListeners("configchanged")) {
} else {
callPlasmoidFunction("configChanged"); callPlasmoidFunction("configChanged");
} }
} }
@ -159,9 +158,8 @@ void SimpleJavaScriptApplet::dataUpdated(const QString &name, const DataEngine::
{ {
QScriptValueList args; QScriptValueList args;
args << m_engine->toScriptValue(name) << m_engine->toScriptValue(data); args << m_engine->toScriptValue(name) << m_engine->toScriptValue(data);
if (m_eventListeners.contains("dataupdated")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
callEventListeners("dataupdated"); if (!env || !env->callEventListeners("dataUpdated")) {
} else {
callPlasmoidFunction("dataUpdated", args); callPlasmoidFunction("dataUpdated", args);
} }
} }
@ -170,18 +168,16 @@ void SimpleJavaScriptApplet::extenderItemRestored(Plasma::ExtenderItem* item)
{ {
QScriptValueList args; QScriptValueList args;
args << m_engine->newQObject(item, QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject); args << m_engine->newQObject(item, QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
if (m_eventListeners.contains("initextenderitem")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
callEventListeners("initextenderitem"); if (!env || !env->callEventListeners("initExtenderItem")) {
} else {
callPlasmoidFunction("initExtenderItem", args); callPlasmoidFunction("initExtenderItem", args);
} }
} }
void SimpleJavaScriptApplet::activate() void SimpleJavaScriptApplet::activate()
{ {
if (m_eventListeners.contains("activate")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
callEventListeners("activate"); if (!env || !env->callEventListeners("activate")) {
} else {
callPlasmoidFunction("activate"); callPlasmoidFunction("activate");
} }
} }
@ -190,19 +186,18 @@ void SimpleJavaScriptApplet::popupEvent(bool popped)
{ {
QScriptValueList args; QScriptValueList args;
args << popped; args << popped;
if (m_eventListeners.contains("popupevent")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
callEventListeners("popupevent"); if (!env || !env->callEventListeners("popupEvent", args)) {
} else {
callPlasmoidFunction("popupEvent", args); callPlasmoidFunction("popupEvent", args);
} }
} }
void SimpleJavaScriptApplet::executeAction(const QString &name) void SimpleJavaScriptApplet::executeAction(const QString &name)
{ {
if (m_eventListeners.contains("action_" + name)) { const QString func("action_" + name);
callEventListeners("action_" + name); ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
} else { if (!env || !env->callEventListeners(func)) {
callPlasmoidFunction("action_" + name); callPlasmoidFunction(func);
} }
} }
@ -213,9 +208,8 @@ void SimpleJavaScriptApplet::paintInterface(QPainter *p, const QStyleOptionGraph
args << m_engine->toScriptValue(const_cast<QStyleOptionGraphicsItem*>(option)); args << m_engine->toScriptValue(const_cast<QStyleOptionGraphicsItem*>(option));
args << m_engine->toScriptValue(QRectF(contentsRect)); args << m_engine->toScriptValue(QRectF(contentsRect));
if (m_eventListeners.contains("paintinterface")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
callEventListeners("paintinterface", args); if (!env || !env->callEventListeners("paintInterface")) {
} else {
callPlasmoidFunction("paintInterface", args); callPlasmoidFunction("paintInterface", args);
} }
} }
@ -228,75 +222,58 @@ QList<QAction*> SimpleJavaScriptApplet::contextualActions()
void SimpleJavaScriptApplet::callPlasmoidFunction(const QString &functionName, const QScriptValueList &args) void SimpleJavaScriptApplet::callPlasmoidFunction(const QString &functionName, const QScriptValueList &args)
{ {
QScriptValue func = m_self.property(functionName); QScriptValue func = m_self.property(functionName);
callFunction(func, args, m_self); ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
} if (env) {
env->callFunction(func, args, m_self);
void SimpleJavaScriptApplet::callFunction(QScriptValue &func, const QScriptValueList &args, const QScriptValue &activator)
{
if (!func.isFunction()) {
return;
}
QScriptContext *ctx = m_engine->pushContext();
ctx->setActivationObject(activator);
func.call(activator, args);
m_engine->popContext();
if (m_engine->hasUncaughtException()) {
reportError(m_env);
m_engine->clearExceptions();
} }
} }
void SimpleJavaScriptApplet::callEventListeners(const QString &event, const QScriptValueList &args) void SimpleJavaScriptApplet::addEventListener(const QString &event, const QScriptValue &func)
{ {
QScriptValueList funcs = m_eventListeners.value(event); ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
QMutableListIterator<QScriptValue> it(funcs); if (env) {
while (it.hasNext()) { env->addEventListener(event, func);
callFunction(it.next(), args); }
}
void SimpleJavaScriptApplet::removeEventListener(const QString &event, const QScriptValue &func)
{
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env) {
env->removeEventListener(event, func);
} }
} }
void SimpleJavaScriptApplet::constraintsEvent(Plasma::Constraints constraints) void SimpleJavaScriptApplet::constraintsEvent(Plasma::Constraints constraints)
{ {
QString functionName; ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (constraints & Plasma::FormFactorConstraint) { if (constraints & Plasma::FormFactorConstraint) {
if (m_eventListeners.contains("formfactorchanged")) { if (!env || !env->callEventListeners("formFactorChanged")) {
callEventListeners("formfactorchanged");
} else {
callPlasmoidFunction("formFactorChanged"); callPlasmoidFunction("formFactorChanged");
} }
} }
if (constraints & Plasma::LocationConstraint) { if (constraints & Plasma::LocationConstraint) {
if (m_eventListeners.contains("locationchanged")) { if (!env || !env->callEventListeners("locationChanged")) {
callEventListeners("locationchanged");
} else {
callPlasmoidFunction("locationChanged"); callPlasmoidFunction("locationChanged");
} }
} }
if (constraints & Plasma::ContextConstraint) { if (constraints & Plasma::ContextConstraint) {
if (m_eventListeners.contains("currentactivitychanged")) { if (!env || !env->callEventListeners("currentActivityChanged")) {
callEventListeners("currentactivitychanged");
} else {
callPlasmoidFunction("currentActivityChanged"); callPlasmoidFunction("currentActivityChanged");
} }
} }
if (constraints & Plasma::SizeConstraint) { if (constraints & Plasma::SizeConstraint) {
if (m_eventListeners.contains("sizechanged")) { if (!env || !env->callEventListeners("sizeChanged")) {
callEventListeners("sizechanged");
} else {
callPlasmoidFunction("sizeChanged"); callPlasmoidFunction("sizeChanged");
} }
} }
if (constraints & Plasma::ImmutableConstraint) { if (constraints & Plasma::ImmutableConstraint) {
if (m_eventListeners.contains("immutabilitychanged")) { if (!env || !env->callEventListeners("immutabilityChanged")) {
callEventListeners("immutabilitychanged");
} else {
callPlasmoidFunction("immutabilityChanged"); callPlasmoidFunction("immutabilityChanged");
} }
} }
@ -398,99 +375,109 @@ bool SimpleJavaScriptApplet::eventFilter(QObject *watched, QEvent *event)
{ {
switch (event->type()) { switch (event->type()) {
case QEvent::KeyPress: { case QEvent::KeyPress: {
if (m_eventListeners.contains("keypress")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env && !env->hasEventListeners("keypress")) {
QScriptValueList args; QScriptValueList args;
args << createKeyEventObject(static_cast<QKeyEvent *>(event)); args << createKeyEventObject(static_cast<QKeyEvent *>(event));
callEventListeners("keypress", args); env->callEventListeners("keypress", args);
return true; return true;
} }
} }
case QEvent::KeyRelease: { case QEvent::KeyRelease: {
if (m_eventListeners.contains("keyrelease")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env && !env->hasEventListeners("keyrelease")) {
QScriptValueList args; QScriptValueList args;
args << createKeyEventObject(static_cast<QKeyEvent *>(event)); args << createKeyEventObject(static_cast<QKeyEvent *>(event));
callEventListeners("keyrelease", args); env->callEventListeners("keyrelease", args);
return true; return true;
} }
} }
break; break;
case QEvent::GraphicsSceneHoverEnter: { case QEvent::GraphicsSceneHoverEnter: {
if (m_eventListeners.contains("hoverenter")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env && !env->hasEventListeners("hoverenter")) {
QScriptValueList args; QScriptValueList args;
args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event)); args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event));
callEventListeners("hoverenter", args); env->callEventListeners("hoverenter", args);
return true; return true;
} }
} }
break; break;
case QEvent::GraphicsSceneHoverLeave: { case QEvent::GraphicsSceneHoverLeave: {
if (m_eventListeners.contains("hoverleave")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env && !env->hasEventListeners("hoverleave")) {
QScriptValueList args; QScriptValueList args;
args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event)); args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event));
callEventListeners("hoverleave", args); env->callEventListeners("hoverleave", args);
return true; return true;
} }
} }
break; break;
case QEvent::GraphicsSceneHoverMove: { case QEvent::GraphicsSceneHoverMove: {
if (m_eventListeners.contains("hovermove")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env && !env->hasEventListeners("hovermove")) {
QScriptValueList args; QScriptValueList args;
args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event)); args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event));
callEventListeners("hovermove", args); env->callEventListeners("hovermove", args);
return true; return true;
} }
} }
break; break;
case QEvent::GraphicsSceneMousePress: { case QEvent::GraphicsSceneMousePress: {
if (m_eventListeners.contains("mousepress")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env && !env->hasEventListeners("mousepress")) {
QScriptValueList args; QScriptValueList args;
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event)); args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
callEventListeners("mousepress", args); env->callEventListeners("mousepress", args);
return true; return true;
} }
} }
break; break;
case QEvent::GraphicsSceneMouseRelease: { case QEvent::GraphicsSceneMouseRelease: {
if (m_eventListeners.contains("mouserelease")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env && !env->hasEventListeners("mouserelease")) {
QScriptValueList args; QScriptValueList args;
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event)); args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
callEventListeners("mouserelease", args); env->callEventListeners("mouserelease", args);
return true; return true;
} }
} }
break; break;
case QEvent::GraphicsSceneMouseMove: { case QEvent::GraphicsSceneMouseMove: {
if (m_eventListeners.contains("mousemove")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env && !env->hasEventListeners("mousemove")) {
QScriptValueList args; QScriptValueList args;
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event)); args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
callEventListeners("mousemove", args); env->callEventListeners("mousemove", args);
return true; return true;
} }
} }
break; break;
case QEvent::GraphicsSceneMouseDoubleClick: { case QEvent::GraphicsSceneMouseDoubleClick: {
if (m_eventListeners.contains("mousedoubleclick")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env && !env->hasEventListeners("mousedoubleclick")) {
QScriptValueList args; QScriptValueList args;
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event)); args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
callEventListeners("mousedoubleclick", args); env->callEventListeners("mousedoubleclick", args);
return true; return true;
} }
} }
break; break;
case QEvent::GraphicsSceneWheel: { case QEvent::GraphicsSceneWheel: {
if (m_eventListeners.contains("wheel")) { ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
if (env && !env->hasEventListeners("wheel")) {
QScriptValueList args; QScriptValueList args;
args << createWheelEventObject(static_cast<QGraphicsSceneWheelEvent *>(event)); args << createWheelEventObject(static_cast<QGraphicsSceneWheelEvent *>(event));
callEventListeners("wheel", args); env->callEventListeners("wheel", args);
return true; return true;
} }
} }
@ -503,32 +490,6 @@ bool SimpleJavaScriptApplet::eventFilter(QObject *watched, QEvent *event)
return Plasma::AppletScript::eventFilter(watched, event); return Plasma::AppletScript::eventFilter(watched, event);
} }
void SimpleJavaScriptApplet::addEventListener(const QString &event, const QScriptValue &func)
{
if (func.isFunction()) {
m_eventListeners[event.toLower()].append(func);
}
}
void SimpleJavaScriptApplet::removeEventListener(const QString &event, const QScriptValue &func)
{
if (func.isFunction()) {
QScriptValueList funcs = m_eventListeners.value("mousepress");
QMutableListIterator<QScriptValue> it(funcs);//m_eventListeners.value("mousepress"));
while (it.hasNext()) {
if (it.next().equals(func)) {
it.remove();
}
}
if (funcs.isEmpty()) {
m_eventListeners.remove(event.toLower());
} else {
m_eventListeners.insert(event.toLower(), funcs);
}
}
}
void SimpleJavaScriptApplet::setupObjects() void SimpleJavaScriptApplet::setupObjects()
{ {
QScriptValue global = m_engine->globalObject(); QScriptValue global = m_engine->globalObject();

View File

@ -55,11 +55,11 @@ public:
bool include(const QString &path); bool include(const QString &path);
QSet<QString> loadedExtensions() const; QSet<QString> loadedExtensions() const;
QScriptValue variantToScriptValue(QVariant var); QScriptValue variantToScriptValue(QVariant var);
bool eventFilter(QObject *watched, QEvent *event);
void addEventListener(const QString &event, const QScriptValue &func); void addEventListener(const QString &event, const QScriptValue &func);
void removeEventListener(const QString &event, const QScriptValue &func); void removeEventListener(const QString &event, const QScriptValue &func);
bool eventFilter(QObject *watched, QEvent *event);
static QString findImageFile(QScriptEngine *engine, const QString &file); static QString findImageFile(QScriptEngine *engine, const QString &file);
public Q_SLOTS: public Q_SLOTS:
@ -79,8 +79,6 @@ private:
bool importBuiltinExtension(const QString &extension); bool importBuiltinExtension(const QString &extension);
void setupObjects(); void setupObjects();
void callPlasmoidFunction(const QString &functionName, const QScriptValueList &args = QScriptValueList()); void callPlasmoidFunction(const QString &functionName, const QScriptValueList &args = QScriptValueList());
void callFunction(QScriptValue &func, const QScriptValueList &args = QScriptValueList(), const QScriptValue &activator = QScriptValue());
void callEventListeners(const QString &event, const QScriptValueList &args = QScriptValueList());
QScriptValue createKeyEventObject(QKeyEvent *event); QScriptValue createKeyEventObject(QKeyEvent *event);
QScriptValue createHoverEventObject(QGraphicsSceneHoverEvent *event); QScriptValue createHoverEventObject(QGraphicsSceneHoverEvent *event);
QScriptValue createMouseEventObject(QGraphicsSceneMouseEvent *event); QScriptValue createMouseEventObject(QGraphicsSceneMouseEvent *event);
@ -120,7 +118,6 @@ private:
QScriptValue m_self; QScriptValue m_self;
QVariantList m_args; QVariantList m_args;
AppletInterface *m_interface; AppletInterface *m_interface;
QHash<QString, QScriptValueList> m_eventListeners;
friend class AppletInterface; friend class AppletInterface;
}; };