add loadUi from the js engine

svn path=/trunk/KDE/kdebase/runtime/; revision=1188406
This commit is contained in:
Marco Martin 2010-10-22 10:12:35 +00:00
parent bc13065f9a
commit 58adcad0a2
2 changed files with 27 additions and 0 deletions

View File

@ -22,10 +22,13 @@
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QDeclarativeExpression>
#include <QFile>
#include <QGraphicsLinearLayout>
#include <QScriptEngine>
#include <QScriptValueIterator>
#include <QTimer>
#include <QUiLoader>
#include <QWidget>
#include <KConfigGroup>
#include <KDebug>
@ -152,6 +155,25 @@ void DeclarativeAppletScript::configChanged()
m_env->callEventListeners("configchanged");
}
QScriptValue DeclarativeAppletScript::loadui(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() != 1) {
return context->throwError(i18n("loadui() takes one argument"));
}
QString filename = context->argument(0).toString();
QFile f(filename);
if (!f.open(QIODevice::ReadOnly)) {
return context->throwError(i18n("Unable to open '%1'",filename));
}
QUiLoader loader;
QWidget *w = loader.load(&f);
f.close();
return engine->newQObject(w, QScriptEngine::AutoOwnership);
}
QScriptValue DeclarativeAppletScript::newPlasmaSvg(QScriptContext *context, QScriptEngine *engine)
{
if (context->argumentCount() == 0) {
@ -339,6 +361,10 @@ void DeclarativeAppletScript::setupObjects()
}
global.setProperty("startupArguments", args);
// Add a global loadui method for ui files
QScriptValue fun = m_engine->newFunction(DeclarativeAppletScript::loadui);
global.setProperty("loadui", fun);
bindI18N(m_engine);
global.setProperty("dataEngine", m_engine->newFunction(DeclarativeAppletScript::dataEngine));
global.setProperty("service", m_engine->newFunction(DeclarativeAppletScript::service));

View File

@ -61,6 +61,7 @@ public:
QScriptValue variantToScriptValue(QVariant var);
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);