extenders

svn path=/branches/KDE/4.4/kdebase/runtime/; revision=1077723
This commit is contained in:
Aaron J. Seigo 2010-01-20 19:08:44 +00:00
parent 3f5f41d784
commit d8b8c51c6a
5 changed files with 80 additions and 12 deletions

View File

@ -32,7 +32,6 @@
#include <Plasma/Plasma> #include <Plasma/Plasma>
#include <Plasma/Applet> #include <Plasma/Applet>
#include <Plasma/Context> #include <Plasma/Context>
#include <Plasma/DataEngine>
#include <Plasma/Package> #include <Plasma/Package>
AppletInterface::AppletInterface(SimpleJavaScriptApplet *parent) AppletInterface::AppletInterface(SimpleJavaScriptApplet *parent)
@ -365,6 +364,11 @@ QObject *AppletInterface::findChild(const QString &name) const
return 0; return 0;
} }
Plasma::Extender *AppletInterface::extender() const
{
return m_appletScriptEngine->extender();
}
void AppletInterface::gc() void AppletInterface::gc()
{ {
QTimer::singleShot(0, m_appletScriptEngine, SLOT(collectGarbage())); QTimer::singleShot(0, m_appletScriptEngine, SLOT(collectGarbage()));

View File

@ -41,6 +41,7 @@ class QSizeF;
namespace Plasma namespace Plasma
{ {
class ConfigLoader; class ConfigLoader;
class Extender;
} // namespace Plasa } // namespace Plasa
class AppletInterface : public QObject class AppletInterface : public QObject
@ -233,6 +234,8 @@ enum AnimationDirection {
Q_INVOKABLE void debug(const QString &msg); Q_INVOKABLE void debug(const QString &msg);
Q_INVOKABLE QObject *findChild(const QString &name) const; Q_INVOKABLE QObject *findChild(const QString &name) const;
Q_INVOKABLE Plasma::Extender *extender() const;
Plasma::DataEngine *dataEngine(const QString &name); Plasma::DataEngine *dataEngine(const QString &name);
const Plasma::Package *package() const; const Plasma::Package *package() const;

View File

@ -20,26 +20,31 @@
#include <QGraphicsWidget> #include <QGraphicsWidget>
#include <QScriptEngine> #include <QScriptEngine>
#include <Plasma/Applet>
#include <Plasma/Animation>
#include <Plasma/VideoWidget>
#include <KConfigGroup> #include <KConfigGroup>
#include <KIO/Job> #include <KIO/Job>
#include <Plasma/Applet>
#include <Plasma/Animation>
#include <Plasma/Extender>
#include <Plasma/VideoWidget>
#include "appletinterface.h" #include "appletinterface.h"
#include "dataengine.h" #include "dataengine.h"
#include "variant.h" #include "variant.h"
//Q_DECLARE_METATYPE(SimpleJavaScriptApplet*) //Q_DECLARE_METATYPE(SimpleJavaScriptApplet*)
Q_DECLARE_METATYPE(AppletInterface*)
Q_DECLARE_METATYPE(Plasma::Applet*)
Q_DECLARE_METATYPE(QGraphicsWidget*) Q_DECLARE_METATYPE(QGraphicsWidget*)
Q_DECLARE_METATYPE(QGraphicsLayout*) Q_DECLARE_METATYPE(QGraphicsLayout*)
Q_DECLARE_METATYPE(KConfigGroup) Q_DECLARE_METATYPE(KConfigGroup)
Q_DECLARE_METATYPE(Plasma::Animation*) Q_DECLARE_METATYPE(Plasma::Animation*)
Q_DECLARE_METATYPE(Plasma::Applet*)
Q_DECLARE_METATYPE(Plasma::Extender*)
Q_DECLARE_METATYPE(Plasma::VideoWidget::Controls) Q_DECLARE_METATYPE(Plasma::VideoWidget::Controls)
Q_DECLARE_METATYPE(AppletInterface*)
//Q_SCRIPT_DECLARE_QMETAOBJECT(AppletInterface, SimpleJavaScriptApplet*) //Q_SCRIPT_DECLARE_QMETAOBJECT(AppletInterface, SimpleJavaScriptApplet*)
@ -168,13 +173,28 @@ void qGraphicsWidgetFromQScriptValue(const QScriptValue &scriptValue, QGraphicsW
anim = static_cast<QGraphicsWidget *>(obj); anim = static_cast<QGraphicsWidget *>(obj);
} }
typedef Plasma::Extender *ExtenderPtr;
QScriptValue qScriptValueFromExtender(QScriptEngine *engine, const ExtenderPtr &extender)
{
return engine->newQObject(const_cast<Plasma::Extender *>(extender), QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
}
void extenderFromQScriptValue(const QScriptValue &scriptValue, ExtenderPtr &extender)
{
QObject *obj = scriptValue.toQObject();
extender = static_cast<Plasma::Extender *>(obj);
}
void registerSimpleAppletMetaTypes(QScriptEngine *engine) void registerSimpleAppletMetaTypes(QScriptEngine *engine)
{ {
qScriptRegisterMetaType<Plasma::DataEngine::Data>(engine, qScriptValueFromData, 0, QScriptValue()); qScriptRegisterMetaType<QGraphicsWidget*>(engine, qScriptValueFromQGraphicsWidget, qGraphicsWidgetFromQScriptValue);
qScriptRegisterMetaType<KConfigGroup>(engine, qScriptValueFromKConfigGroup, kConfigGroupFromScriptValue, QScriptValue()); qScriptRegisterMetaType<KConfigGroup>(engine, qScriptValueFromKConfigGroup, kConfigGroupFromScriptValue, QScriptValue());
qScriptRegisterMetaType<Plasma::VideoWidget::Controls>(engine, qScriptValueFromControls, controlsFromScriptValue, QScriptValue());
qScriptRegisterMetaType<KJob *>(engine, qScriptValueFromKJob, qKJobFromQScriptValue); qScriptRegisterMetaType<KJob *>(engine, qScriptValueFromKJob, qKJobFromQScriptValue);
qScriptRegisterMetaType<KIO::Job *>(engine, qScriptValueFromKIOJob, qKIOJobFromQScriptValue); qScriptRegisterMetaType<KIO::Job *>(engine, qScriptValueFromKIOJob, qKIOJobFromQScriptValue);
qScriptRegisterMetaType<Plasma::Animation*>(engine, qScriptValueFromAnimation, abstractAnimationFromQScriptValue);
qScriptRegisterMetaType<QGraphicsWidget*>(engine, qScriptValueFromQGraphicsWidget, qGraphicsWidgetFromQScriptValue); qScriptRegisterMetaType<Plasma::Animation *>(engine, qScriptValueFromAnimation, abstractAnimationFromQScriptValue);
qScriptRegisterMetaType<Plasma::DataEngine::Data>(engine, qScriptValueFromData, 0, QScriptValue());
qScriptRegisterMetaType<Plasma::Extender *>(engine, qScriptValueFromExtender , extenderFromQScriptValue);
qScriptRegisterMetaType<Plasma::VideoWidget::Controls>(engine, qScriptValueFromControls, controlsFromScriptValue, QScriptValue());
} }

View File

@ -42,10 +42,12 @@
#include <Plasma/Animation> #include <Plasma/Animation>
#include <Plasma/Applet> #include <Plasma/Applet>
#include <Plasma/Svg> #include <Plasma/Extender>
#include <Plasma/ExtenderItem>
#include <Plasma/FrameSvg> #include <Plasma/FrameSvg>
#include <Plasma/Package> #include <Plasma/Package>
#include <Plasma/PopupApplet> #include <Plasma/PopupApplet>
#include <Plasma/Svg>
#include <Plasma/VideoWidget> #include <Plasma/VideoWidget>
#include "simplebindings/animationgroup.h" #include "simplebindings/animationgroup.h"
@ -315,6 +317,8 @@ void SimpleJavaScriptApplet::populateAnimationsHash()
bool SimpleJavaScriptApplet::init() bool SimpleJavaScriptApplet::init()
{ {
connect(applet(), SIGNAL(extenderItemRestored(Plasma::ExtenderItem*)),
this, SLOT(extenderItemRestored(Plasma::ExtenderItem*)));
setupObjects(); setupObjects();
if (!importExtensions()) { if (!importExtensions()) {
@ -327,6 +331,13 @@ bool SimpleJavaScriptApplet::init()
return include(mainScript()); return include(mainScript());
} }
void SimpleJavaScriptApplet::extenderItemRestored(Plasma::ExtenderItem* item)
{
QScriptValueList args;
args << m_engine->newQObject(item, QScriptEngine::AutoOwnership, QScriptEngine::PreferExistingWrapperObject);
callFunction("initExtenderItem", args);
}
QScriptValue SimpleJavaScriptApplet::runApplication(QScriptContext *context, QScriptEngine *engine) QScriptValue SimpleJavaScriptApplet::runApplication(QScriptContext *context, QScriptEngine *engine)
{ {
Q_UNUSED(engine) Q_UNUSED(engine)
@ -589,6 +600,7 @@ void SimpleJavaScriptApplet::setupObjects()
global.setProperty("PlasmaFrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg)); global.setProperty("PlasmaFrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg));
global.setProperty("Svg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaSvg)); global.setProperty("Svg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaSvg));
global.setProperty("FrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg)); global.setProperty("FrameSvg", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaFrameSvg));
global.setProperty("ExtenderItem", m_engine->newFunction(SimpleJavaScriptApplet::newPlasmaExtenderItem));
registerSimpleAppletMetaTypes(m_engine); registerSimpleAppletMetaTypes(m_engine);
installWidgets(m_engine); installWidgets(m_engine);
@ -774,6 +786,28 @@ QScriptValue SimpleJavaScriptApplet::newPlasmaFrameSvg(QScriptContext *context,
return fun; return fun;
} }
QScriptValue SimpleJavaScriptApplet::newPlasmaExtenderItem(QScriptContext *context, QScriptEngine *engine)
{
Plasma::Extender *extender = 0;
if (context->argumentCount() > 0) {
extender = qobject_cast<Plasma::Extender *>(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);
registerEnums(engine, fun, *extenderItem->metaObject());
return fun;
}
QScriptValue SimpleJavaScriptApplet::widgetAdjustSize(QScriptContext *context, QScriptEngine *engine) QScriptValue SimpleJavaScriptApplet::widgetAdjustSize(QScriptContext *context, QScriptEngine *engine)
{ {
QGraphicsWidget *widget = qobject_cast<QGraphicsWidget*>(context->thisObject().toQObject()); QGraphicsWidget *widget = qobject_cast<QGraphicsWidget*>(context->thisObject().toQObject());

View File

@ -32,6 +32,11 @@ class QScriptContext;
class AppletInterface; class AppletInterface;
namespace Plasma
{
class ExtenderItem;
} // namespace Plasma
class SimpleJavaScriptApplet : public Plasma::AppletScript class SimpleJavaScriptApplet : public Plasma::AppletScript
{ {
Q_OBJECT Q_OBJECT
@ -63,6 +68,7 @@ public slots:
void configChanged(); void configChanged();
void executeAction(const QString &name); void executeAction(const QString &name);
void collectGarbage(); void collectGarbage();
void extenderItemRestored(Plasma::ExtenderItem* item);
private: private:
void registerGetUrl(); void registerGetUrl();
@ -85,6 +91,7 @@ private:
static QScriptValue loadui(QScriptContext *context, QScriptEngine *engine); static QScriptValue loadui(QScriptContext *context, QScriptEngine *engine);
static QScriptValue newPlasmaSvg(QScriptContext *context, QScriptEngine *engine); static QScriptValue newPlasmaSvg(QScriptContext *context, QScriptEngine *engine);
static QScriptValue newPlasmaFrameSvg(QScriptContext *context, QScriptEngine *engine); static QScriptValue newPlasmaFrameSvg(QScriptContext *context, QScriptEngine *engine);
static QScriptValue newPlasmaExtenderItem(QScriptContext *context, QScriptEngine *engine);
void installWidgets(QScriptEngine *engine); void installWidgets(QScriptEngine *engine);
static QScriptValue createWidget(QScriptContext *context, QScriptEngine *engine); static QScriptValue createWidget(QScriptContext *context, QScriptEngine *engine);