a simple binding for scripted containments

This commit is contained in:
Marco Martin 2011-02-20 19:45:12 +01:00
parent 145bcf4045
commit ea0b394a55
7 changed files with 81 additions and 5 deletions

View File

@ -32,6 +32,7 @@ public:
AbstractJsAppletScript(QObject *parent, const QVariantList &args = QVariantList());
~AbstractJsAppletScript();
virtual QScriptEngine *engine() const = 0;
virtual bool include(const QString &path) = 0;
virtual QString filePath(const QString &type, const QString &file) const = 0;
virtual QScriptValue variantToScriptValue(QVariant var) = 0;

View File

@ -375,7 +375,7 @@ void AppletInterface::gc()
PopupAppletInterface::PopupAppletInterface(AbstractJsAppletScript *parent)
: POPUPAPPLETSUPERCLASS(parent)
: APPLETSUPERCLASS(parent)
{
}
@ -429,6 +429,35 @@ QGraphicsWidget *PopupAppletInterface::popupWidget()
return popupApplet()->graphicsWidget();
}
ContainmentInterface::ContainmentInterface(AbstractJsAppletScript *parent)
: APPLETSUPERCLASS(parent)
{
connect(containment(), SIGNAL(appletRemoved(Plasma::Applet *)), this, SLOT(appletRemovedForward(Plasma::Applet *)));
connect(containment(), SIGNAL(appletAdded(Plasma::Applet *, const QPointF &)), this, SLOT(appletAddedForward(Plasma::Applet *, const QPointF &)));
}
QScriptValue ContainmentInterface::applets()
{
QScriptValue list = m_appletScriptEngine->engine()->newArray(containment()->applets().size());
int i = 0;
foreach (Plasma::Applet *applet, containment()->applets()) {
list.setProperty(i, m_appletScriptEngine->engine()->newQObject(applet));
++i;
}
return list;
}
void ContainmentInterface::appletAddedForward(Plasma::Applet *applet, const QPointF &pos)
{
emit appletAdded(applet, pos);
}
void ContainmentInterface::appletRemovedForward(Plasma::Applet *applet)
{
emit appletRemoved(applet);
}
#ifndef USE_JS_SCRIPTENGINE
#include "appletinterface.moc"
#endif

View File

@ -28,6 +28,7 @@
#include <QScriptValue>
#include <Plasma/Applet>
#include <Plasma/Containment>
#include <Plasma/PopupApplet>
#include <Plasma/DataEngine>
#include <Plasma/Theme>
@ -319,11 +320,11 @@ public:
};
#ifdef USE_JS_SCRIPTENGINE
#define POPUPAPPLETSUPERCLASS JsAppletInterface
#define APPLETSUPERCLASS JsAppletInterface
#else
#define POPUPAPPLETSUPERCLASS AppletInterface
#define APPLETSUPERCLASS AppletInterface
#endif
class PopupAppletInterface : public POPUPAPPLETSUPERCLASS
class PopupAppletInterface : public APPLETSUPERCLASS
{
Q_OBJECT
Q_PROPERTY(QIcon popupIcon READ popupIcon WRITE setPopupIcon)
@ -351,4 +352,26 @@ public Q_SLOTS:
void showPopup();
};
class ContainmentInterface : public APPLETSUPERCLASS
{
Q_OBJECT
Q_PROPERTY(QScriptValue applets READ applets)
public:
ContainmentInterface(AbstractJsAppletScript *parent);
inline Plasma::Containment *containment() const { return static_cast<Plasma::Containment *>(m_appletScriptEngine->applet()); }
QScriptValue applets() ;
Q_SIGNALS:
void appletAdded(QGraphicsWidget *applet, const QPointF &pos);
void appletRemoved(QGraphicsWidget *applet);
protected Q_SLOTS:
void appletAddedForward(Plasma::Applet *applet, const QPointF &pos);
void appletRemovedForward(Plasma::Applet *applet);
};
#endif

View File

@ -102,6 +102,7 @@ bool DeclarativeAppletScript::init()
Plasma::Applet *a = applet();
Plasma::PopupApplet *pa = qobject_cast<Plasma::PopupApplet *>(a);
Plasma::Containment *cont = qobject_cast<Plasma::Containment *>(a);
if (pa) {
pa->setPopupIcon(a->icon());
@ -112,7 +113,15 @@ bool DeclarativeAppletScript::init()
lay->addItem(m_declarativeWidget);
}
m_interface = pa ? new PopupAppletInterface(this) : new AppletInterface(this);
m_interface = 0;
if (pa) {
m_interface = new PopupAppletInterface(this);
} else if (cont) {
m_interface = new ContainmentInterface(this);
//fail? so it's a normal Applet
} else {
m_interface = new AppletInterface(this);
}
m_engineAccess = new EngineAccess(this);
m_declarativeWidget->engine()->rootContext()->setContextProperty("__engineAccess", m_engineAccess);
@ -443,6 +452,11 @@ QScriptValue DeclarativeAppletScript::loadService(QScriptContext *context, QScri
return engine->newQObject(service, QScriptEngine::AutoOwnership);
}
QScriptEngine *DeclarativeAppletScript::engine() const
{
return m_engine;
}
void DeclarativeAppletScript::setEngine(QScriptValue &val)
{
if (val.engine() == m_engine) {

View File

@ -59,6 +59,8 @@ public:
ScriptEnv *scriptEnv();
QScriptEngine *engine() const;
QScriptValue variantToScriptValue(QVariant var);
static QScriptValue loadui(QScriptContext *context, QScriptEngine *engine);

View File

@ -808,6 +808,11 @@ void SimpleJavaScriptApplet::installWidgets(QScriptEngine *engine)
}
}
QScriptEngine *SimpleJavaScriptApplet::engine() const
{
return m_engine;
}
QGraphicsWidget *SimpleJavaScriptApplet::extractParent(QScriptContext *context, QScriptEngine *engine,
int argIndex, bool *parentedToApplet)
{

View File

@ -58,6 +58,8 @@ public:
bool eventFilter(QObject *watched, QEvent *event);
QScriptEngine *engine() const;
public Q_SLOTS:
void dataUpdated(const QString &name, const Plasma::DataEngine::Data &data);
void configChanged();