From 59ca56a770d548c66044b353191f8ab9f582dcd5 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Wed, 20 Jan 2010 21:40:55 +0000 Subject: [PATCH] extenders svn path=/trunk/KDE/kdebase/runtime/; revision=1077776 --- .../simplebindings/appletinterface.cpp | 6 ++- .../simplebindings/appletinterface.h | 3 ++ .../simplebindings/qscriptbookkeeping.cpp | 40 ++++++++++++++----- .../javascript/simplejavascriptapplet.cpp | 36 ++++++++++++++++- .../javascript/simplejavascriptapplet.h | 7 ++++ 5 files changed, 80 insertions(+), 12 deletions(-) diff --git a/scriptengines/javascript/simplebindings/appletinterface.cpp b/scriptengines/javascript/simplebindings/appletinterface.cpp index e09f6d4e5..6e64cb803 100644 --- a/scriptengines/javascript/simplebindings/appletinterface.cpp +++ b/scriptengines/javascript/simplebindings/appletinterface.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include AppletInterface::AppletInterface(SimpleJavaScriptApplet *parent) @@ -354,6 +353,11 @@ QObject *AppletInterface::findChild(const QString &name) const return 0; } +Plasma::Extender *AppletInterface::extender() const +{ + return m_appletScriptEngine->extender(); +} + void AppletInterface::gc() { QTimer::singleShot(0, m_appletScriptEngine, SLOT(collectGarbage())); diff --git a/scriptengines/javascript/simplebindings/appletinterface.h b/scriptengines/javascript/simplebindings/appletinterface.h index 6c1cd1f11..87da7be69 100644 --- a/scriptengines/javascript/simplebindings/appletinterface.h +++ b/scriptengines/javascript/simplebindings/appletinterface.h @@ -41,6 +41,7 @@ class QSizeF; namespace Plasma { class ConfigLoader; + class Extender; } // namespace Plasa class AppletInterface : public QObject @@ -233,6 +234,8 @@ enum AnimationDirection { Q_INVOKABLE void debug(const QString &msg); Q_INVOKABLE QObject *findChild(const QString &name) const; + Q_INVOKABLE Plasma::Extender *extender() const; + Plasma::DataEngine *dataEngine(const QString &name); const Plasma::Package *package() const; diff --git a/scriptengines/javascript/simplebindings/qscriptbookkeeping.cpp b/scriptengines/javascript/simplebindings/qscriptbookkeeping.cpp index f9a27123b..f289f8e04 100644 --- a/scriptengines/javascript/simplebindings/qscriptbookkeeping.cpp +++ b/scriptengines/javascript/simplebindings/qscriptbookkeeping.cpp @@ -20,26 +20,31 @@ #include #include -#include -#include -#include - #include #include +#include +#include +#include +#include + #include "appletinterface.h" #include "dataengine.h" #include "variant.h" //Q_DECLARE_METATYPE(SimpleJavaScriptApplet*) -Q_DECLARE_METATYPE(AppletInterface*) -Q_DECLARE_METATYPE(Plasma::Applet*) Q_DECLARE_METATYPE(QGraphicsWidget*) Q_DECLARE_METATYPE(QGraphicsLayout*) + Q_DECLARE_METATYPE(KConfigGroup) + Q_DECLARE_METATYPE(Plasma::Animation*) +Q_DECLARE_METATYPE(Plasma::Applet*) +Q_DECLARE_METATYPE(Plasma::Extender*) Q_DECLARE_METATYPE(Plasma::VideoWidget::Controls) +Q_DECLARE_METATYPE(AppletInterface*) + //Q_SCRIPT_DECLARE_QMETAOBJECT(AppletInterface, SimpleJavaScriptApplet*) @@ -168,13 +173,28 @@ void qGraphicsWidgetFromQScriptValue(const QScriptValue &scriptValue, QGraphicsW anim = static_cast(obj); } +typedef Plasma::Extender *ExtenderPtr; +QScriptValue qScriptValueFromExtender(QScriptEngine *engine, const ExtenderPtr &extender) +{ + return engine->newQObject(const_cast(extender), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject); +} + +void extenderFromQScriptValue(const QScriptValue &scriptValue, ExtenderPtr &extender) +{ + QObject *obj = scriptValue.toQObject(); + extender = static_cast(obj); +} + void registerSimpleAppletMetaTypes(QScriptEngine *engine) { - qScriptRegisterMetaType(engine, qScriptValueFromData, 0, QScriptValue()); + qScriptRegisterMetaType(engine, qScriptValueFromQGraphicsWidget, qGraphicsWidgetFromQScriptValue); + qScriptRegisterMetaType(engine, qScriptValueFromKConfigGroup, kConfigGroupFromScriptValue, QScriptValue()); - qScriptRegisterMetaType(engine, qScriptValueFromControls, controlsFromScriptValue, QScriptValue()); qScriptRegisterMetaType(engine, qScriptValueFromKJob, qKJobFromQScriptValue); qScriptRegisterMetaType(engine, qScriptValueFromKIOJob, qKIOJobFromQScriptValue); - qScriptRegisterMetaType(engine, qScriptValueFromAnimation, abstractAnimationFromQScriptValue); - qScriptRegisterMetaType(engine, qScriptValueFromQGraphicsWidget, qGraphicsWidgetFromQScriptValue); + + qScriptRegisterMetaType(engine, qScriptValueFromAnimation, abstractAnimationFromQScriptValue); + qScriptRegisterMetaType(engine, qScriptValueFromData, 0, QScriptValue()); + qScriptRegisterMetaType(engine, qScriptValueFromExtender , extenderFromQScriptValue); + qScriptRegisterMetaType(engine, qScriptValueFromControls, controlsFromScriptValue, QScriptValue()); } diff --git a/scriptengines/javascript/simplejavascriptapplet.cpp b/scriptengines/javascript/simplejavascriptapplet.cpp index 40aa23d6b..7eb2712d1 100644 --- a/scriptengines/javascript/simplejavascriptapplet.cpp +++ b/scriptengines/javascript/simplejavascriptapplet.cpp @@ -41,10 +41,12 @@ #include #include -#include +#include +#include #include #include #include +#include #include #include "appletauthorization.h" @@ -173,6 +175,13 @@ void SimpleJavaScriptApplet::dataUpdated(const QString &name, const DataEngine:: } } +void SimpleJavaScriptApplet::extenderItemRestored(Plasma::ExtenderItem* item) +{ + QScriptValueList args; + args << m_engine->newQObject(item, QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject); + callFunction("initExtenderItem", args); +} + void SimpleJavaScriptApplet::executeAction(const QString &name) { //callFunction("action_" + name); @@ -283,6 +292,8 @@ void SimpleJavaScriptApplet::populateAnimationsHash() bool SimpleJavaScriptApplet::init() { + connect(applet(), SIGNAL(extenderItemRestored(Plasma::ExtenderItem*)), + this, SLOT(extenderItemRestored(Plasma::ExtenderItem*))); setupObjects(); AppletAuthorization auth(this); @@ -368,6 +379,7 @@ void SimpleJavaScriptApplet::setupObjects() global.setProperty("PlasmaFrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg)); global.setProperty("Svg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaSvg)); global.setProperty("FrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg)); + global.setProperty("ExtenderItem", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaExtenderItem)); registerSimpleAppletMetaTypes(m_engine); installWidgets(m_engine); @@ -553,6 +565,28 @@ QScriptValue SimpleJavaScriptApplet::newPlasmaFrameSvg(QScriptContext *context, return fun; } +QScriptValue SimpleJavaScriptApplet::newPlasmaExtenderItem(QScriptContext *context, QScriptEngine *engine) +{ + Plasma::Extender *extender = 0; + if (context->argumentCount() > 0) { + extender = qobject_cast(context->argument(0).toQObject()); + } + + if (!extender) { + AppletInterface *interface = extractAppletInterface(engine); + if (!interface) { + engine->undefinedValue(); + } + + extender = interface->extender(); + } + + Plasma::ExtenderItem *extenderItem = new Plasma::ExtenderItem(extender); + QScriptValue fun = engine->newQObject(extenderItem); + static_cast(engine)->registerEnums(fun, *extenderItem->metaObject()); + return fun; +} + QScriptValue SimpleJavaScriptApplet::widgetAdjustSize(QScriptContext *context, QScriptEngine *engine) { QGraphicsWidget *widget = qobject_cast(context->thisObject().toQObject()); diff --git a/scriptengines/javascript/simplejavascriptapplet.h b/scriptengines/javascript/simplejavascriptapplet.h index 12c59646b..8f2563b61 100644 --- a/scriptengines/javascript/simplejavascriptapplet.h +++ b/scriptengines/javascript/simplejavascriptapplet.h @@ -32,6 +32,11 @@ class QScriptContext; class AppletInterface; class ScriptEnv; +namespace Plasma +{ + class ExtenderItem; +} // namespace Plasma + class SimpleJavaScriptApplet : public Plasma::AppletScript { Q_OBJECT @@ -57,6 +62,7 @@ public Q_SLOTS: void configChanged(); void executeAction(const QString &name); void collectGarbage(); + void extenderItemRestored(Plasma::ExtenderItem* item); private Q_SLOTS: void engineReportsError(ScriptEnv *engine, bool fatal); @@ -81,6 +87,7 @@ private: static QScriptValue loadui(QScriptContext *context, QScriptEngine *engine); static QScriptValue newPlasmaSvg(QScriptContext *context, QScriptEngine *engine); static QScriptValue newPlasmaFrameSvg(QScriptContext *context, QScriptEngine *engine); + static QScriptValue newPlasmaExtenderItem(QScriptContext *context, QScriptEngine *engine); void installWidgets(QScriptEngine *engine); static QScriptValue createWidget(QScriptContext *context, QScriptEngine *engine);