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:
parent
3d8bedb45a
commit
948fa2720f
50
scriptengines/javascript/javascriptaddonpackagestructure.h
Normal file
50
scriptengines/javascript/javascriptaddonpackagestructure.h
Normal 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
|
||||
|
@ -25,18 +25,25 @@
|
||||
#include <QMetaEnum>
|
||||
|
||||
#include <KDebug>
|
||||
#include <KDesktopFile>
|
||||
#include <KIO/Job>
|
||||
#include <KLocale>
|
||||
#include <KMimeType>
|
||||
#include <KPluginInfo>
|
||||
#include <KService>
|
||||
#include <KServiceTypeTrader>
|
||||
#include <KShell>
|
||||
#include <KStandardDirs>
|
||||
#include <KRun>
|
||||
|
||||
#include <Plasma/Package>
|
||||
|
||||
#ifdef USEGUI
|
||||
#include "simplebindings/filedialogproxy.h"
|
||||
#endif
|
||||
|
||||
#include "javascriptaddonpackagestructure.h"
|
||||
|
||||
Q_DECLARE_METATYPE(ScriptEnv*)
|
||||
|
||||
ScriptEnv::ScriptEnv(QObject *parent, QScriptEngine *engine)
|
||||
@ -49,6 +56,8 @@ ScriptEnv::ScriptEnv(QObject *parent, QScriptEngine *engine)
|
||||
// Add utility functions
|
||||
global.setProperty("print", m_engine->newFunction(ScriptEnv::print));
|
||||
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
|
||||
// property is hidden from scripts.
|
||||
@ -346,6 +355,155 @@ QScriptValue ScriptEnv::print(QScriptContext *context, QScriptEngine *engine)
|
||||
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"
|
||||
#endif
|
||||
#include "javascriptaddonpackagestructure.moc"
|
||||
|
||||
|
@ -54,6 +54,13 @@ public:
|
||||
|
||||
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:
|
||||
void reportError(ScriptEnv *engine, bool fatal);
|
||||
|
||||
@ -67,7 +74,9 @@ private:
|
||||
static QScriptValue runCommand(QScriptContext *context, QScriptEngine *engine);
|
||||
static QScriptValue openUrl(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:
|
||||
void signalException();
|
||||
@ -76,6 +85,7 @@ private:
|
||||
QSet<QString> m_extensions;
|
||||
AllowedUrls m_allowedUrls;
|
||||
QScriptEngine *m_engine;
|
||||
QHash<QString, QScriptValueList> m_eventListeners;
|
||||
};
|
||||
|
||||
Q_DECLARE_OPERATORS_FOR_FLAGS(ScriptEnv::AllowedUrls)
|
||||
|
@ -18,4 +18,3 @@
|
||||
|
||||
#define USEGUI
|
||||
#include "scriptenv.cpp"
|
||||
#include "scriptenv.moc"
|
||||
|
@ -148,9 +148,8 @@ void SimpleJavaScriptApplet::reportError(ScriptEnv *env, bool fatal)
|
||||
|
||||
void SimpleJavaScriptApplet::configChanged()
|
||||
{
|
||||
if (m_eventListeners.contains("configchanged")) {
|
||||
callEventListeners("configchanged");
|
||||
} else {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (!env || !env->callEventListeners("configchanged")) {
|
||||
callPlasmoidFunction("configChanged");
|
||||
}
|
||||
}
|
||||
@ -159,9 +158,8 @@ void SimpleJavaScriptApplet::dataUpdated(const QString &name, const DataEngine::
|
||||
{
|
||||
QScriptValueList args;
|
||||
args << m_engine->toScriptValue(name) << m_engine->toScriptValue(data);
|
||||
if (m_eventListeners.contains("dataupdated")) {
|
||||
callEventListeners("dataupdated");
|
||||
} else {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (!env || !env->callEventListeners("dataUpdated")) {
|
||||
callPlasmoidFunction("dataUpdated", args);
|
||||
}
|
||||
}
|
||||
@ -170,18 +168,16 @@ void SimpleJavaScriptApplet::extenderItemRestored(Plasma::ExtenderItem* item)
|
||||
{
|
||||
QScriptValueList args;
|
||||
args << m_engine->newQObject(item, QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
|
||||
if (m_eventListeners.contains("initextenderitem")) {
|
||||
callEventListeners("initextenderitem");
|
||||
} else {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (!env || !env->callEventListeners("initExtenderItem")) {
|
||||
callPlasmoidFunction("initExtenderItem", args);
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleJavaScriptApplet::activate()
|
||||
{
|
||||
if (m_eventListeners.contains("activate")) {
|
||||
callEventListeners("activate");
|
||||
} else {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (!env || !env->callEventListeners("activate")) {
|
||||
callPlasmoidFunction("activate");
|
||||
}
|
||||
}
|
||||
@ -190,19 +186,18 @@ void SimpleJavaScriptApplet::popupEvent(bool popped)
|
||||
{
|
||||
QScriptValueList args;
|
||||
args << popped;
|
||||
if (m_eventListeners.contains("popupevent")) {
|
||||
callEventListeners("popupevent");
|
||||
} else {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (!env || !env->callEventListeners("popupEvent", args)) {
|
||||
callPlasmoidFunction("popupEvent", args);
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleJavaScriptApplet::executeAction(const QString &name)
|
||||
{
|
||||
if (m_eventListeners.contains("action_" + name)) {
|
||||
callEventListeners("action_" + name);
|
||||
} else {
|
||||
callPlasmoidFunction("action_" + name);
|
||||
const QString func("action_" + name);
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (!env || !env->callEventListeners(func)) {
|
||||
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(QRectF(contentsRect));
|
||||
|
||||
if (m_eventListeners.contains("paintinterface")) {
|
||||
callEventListeners("paintinterface", args);
|
||||
} else {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (!env || !env->callEventListeners("paintInterface")) {
|
||||
callPlasmoidFunction("paintInterface", args);
|
||||
}
|
||||
}
|
||||
@ -228,75 +222,58 @@ QList<QAction*> SimpleJavaScriptApplet::contextualActions()
|
||||
void SimpleJavaScriptApplet::callPlasmoidFunction(const QString &functionName, const QScriptValueList &args)
|
||||
{
|
||||
QScriptValue func = m_self.property(functionName);
|
||||
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();
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env) {
|
||||
env->callFunction(func, args, m_self);
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleJavaScriptApplet::callEventListeners(const QString &event, const QScriptValueList &args)
|
||||
void SimpleJavaScriptApplet::addEventListener(const QString &event, const QScriptValue &func)
|
||||
{
|
||||
QScriptValueList funcs = m_eventListeners.value(event);
|
||||
QMutableListIterator<QScriptValue> it(funcs);
|
||||
while (it.hasNext()) {
|
||||
callFunction(it.next(), args);
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env) {
|
||||
env->addEventListener(event, func);
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
QString functionName;
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
|
||||
if (constraints & Plasma::FormFactorConstraint) {
|
||||
if (m_eventListeners.contains("formfactorchanged")) {
|
||||
callEventListeners("formfactorchanged");
|
||||
} else {
|
||||
if (!env || !env->callEventListeners("formFactorChanged")) {
|
||||
callPlasmoidFunction("formFactorChanged");
|
||||
}
|
||||
}
|
||||
|
||||
if (constraints & Plasma::LocationConstraint) {
|
||||
if (m_eventListeners.contains("locationchanged")) {
|
||||
callEventListeners("locationchanged");
|
||||
} else {
|
||||
if (!env || !env->callEventListeners("locationChanged")) {
|
||||
callPlasmoidFunction("locationChanged");
|
||||
}
|
||||
}
|
||||
|
||||
if (constraints & Plasma::ContextConstraint) {
|
||||
if (m_eventListeners.contains("currentactivitychanged")) {
|
||||
callEventListeners("currentactivitychanged");
|
||||
} else {
|
||||
if (!env || !env->callEventListeners("currentActivityChanged")) {
|
||||
callPlasmoidFunction("currentActivityChanged");
|
||||
}
|
||||
}
|
||||
|
||||
if (constraints & Plasma::SizeConstraint) {
|
||||
if (m_eventListeners.contains("sizechanged")) {
|
||||
callEventListeners("sizechanged");
|
||||
} else {
|
||||
if (!env || !env->callEventListeners("sizeChanged")) {
|
||||
callPlasmoidFunction("sizeChanged");
|
||||
}
|
||||
}
|
||||
|
||||
if (constraints & Plasma::ImmutableConstraint) {
|
||||
if (m_eventListeners.contains("immutabilitychanged")) {
|
||||
callEventListeners("immutabilitychanged");
|
||||
} else {
|
||||
if (!env || !env->callEventListeners("immutabilityChanged")) {
|
||||
callPlasmoidFunction("immutabilityChanged");
|
||||
}
|
||||
}
|
||||
@ -398,99 +375,109 @@ bool SimpleJavaScriptApplet::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
switch (event->type()) {
|
||||
case QEvent::KeyPress: {
|
||||
if (m_eventListeners.contains("keypress")) {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env && !env->hasEventListeners("keypress")) {
|
||||
QScriptValueList args;
|
||||
args << createKeyEventObject(static_cast<QKeyEvent *>(event));
|
||||
callEventListeners("keypress", args);
|
||||
env->callEventListeners("keypress", args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
case QEvent::KeyRelease: {
|
||||
if (m_eventListeners.contains("keyrelease")) {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env && !env->hasEventListeners("keyrelease")) {
|
||||
QScriptValueList args;
|
||||
args << createKeyEventObject(static_cast<QKeyEvent *>(event));
|
||||
callEventListeners("keyrelease", args);
|
||||
env->callEventListeners("keyrelease", args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case QEvent::GraphicsSceneHoverEnter: {
|
||||
if (m_eventListeners.contains("hoverenter")) {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env && !env->hasEventListeners("hoverenter")) {
|
||||
QScriptValueList args;
|
||||
args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event));
|
||||
callEventListeners("hoverenter", args);
|
||||
env->callEventListeners("hoverenter", args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case QEvent::GraphicsSceneHoverLeave: {
|
||||
if (m_eventListeners.contains("hoverleave")) {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env && !env->hasEventListeners("hoverleave")) {
|
||||
QScriptValueList args;
|
||||
args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event));
|
||||
callEventListeners("hoverleave", args);
|
||||
env->callEventListeners("hoverleave", args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case QEvent::GraphicsSceneHoverMove: {
|
||||
if (m_eventListeners.contains("hovermove")) {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env && !env->hasEventListeners("hovermove")) {
|
||||
QScriptValueList args;
|
||||
args << createHoverEventObject(static_cast<QGraphicsSceneHoverEvent *>(event));
|
||||
callEventListeners("hovermove", args);
|
||||
env->callEventListeners("hovermove", args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case QEvent::GraphicsSceneMousePress: {
|
||||
if (m_eventListeners.contains("mousepress")) {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env && !env->hasEventListeners("mousepress")) {
|
||||
QScriptValueList args;
|
||||
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
|
||||
callEventListeners("mousepress", args);
|
||||
env->callEventListeners("mousepress", args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case QEvent::GraphicsSceneMouseRelease: {
|
||||
if (m_eventListeners.contains("mouserelease")) {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env && !env->hasEventListeners("mouserelease")) {
|
||||
QScriptValueList args;
|
||||
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
|
||||
callEventListeners("mouserelease", args);
|
||||
env->callEventListeners("mouserelease", args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case QEvent::GraphicsSceneMouseMove: {
|
||||
if (m_eventListeners.contains("mousemove")) {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env && !env->hasEventListeners("mousemove")) {
|
||||
QScriptValueList args;
|
||||
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
|
||||
callEventListeners("mousemove", args);
|
||||
env->callEventListeners("mousemove", args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case QEvent::GraphicsSceneMouseDoubleClick: {
|
||||
if (m_eventListeners.contains("mousedoubleclick")) {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env && !env->hasEventListeners("mousedoubleclick")) {
|
||||
QScriptValueList args;
|
||||
args << createMouseEventObject(static_cast<QGraphicsSceneMouseEvent *>(event));
|
||||
callEventListeners("mousedoubleclick", args);
|
||||
env->callEventListeners("mousedoubleclick", args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case QEvent::GraphicsSceneWheel: {
|
||||
if (m_eventListeners.contains("wheel")) {
|
||||
ScriptEnv *env = ScriptEnv::findScriptEnv(m_engine);
|
||||
if (env && !env->hasEventListeners("wheel")) {
|
||||
QScriptValueList args;
|
||||
args << createWheelEventObject(static_cast<QGraphicsSceneWheelEvent *>(event));
|
||||
callEventListeners("wheel", args);
|
||||
env->callEventListeners("wheel", args);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -503,32 +490,6 @@ bool SimpleJavaScriptApplet::eventFilter(QObject *watched, QEvent *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()
|
||||
{
|
||||
QScriptValue global = m_engine->globalObject();
|
||||
|
@ -55,11 +55,11 @@ public:
|
||||
bool include(const QString &path);
|
||||
QSet<QString> loadedExtensions() const;
|
||||
QScriptValue variantToScriptValue(QVariant var);
|
||||
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
void addEventListener(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);
|
||||
|
||||
public Q_SLOTS:
|
||||
@ -79,8 +79,6 @@ private:
|
||||
bool importBuiltinExtension(const QString &extension);
|
||||
void setupObjects();
|
||||
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 createHoverEventObject(QGraphicsSceneHoverEvent *event);
|
||||
QScriptValue createMouseEventObject(QGraphicsSceneMouseEvent *event);
|
||||
@ -120,7 +118,6 @@ private:
|
||||
QScriptValue m_self;
|
||||
QVariantList m_args;
|
||||
AppletInterface *m_interface;
|
||||
QHash<QString, QScriptValueList> m_eventListeners;
|
||||
friend class AppletInterface;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user