* share the one UiLoader amongst all javascript applets

* use function ptrs to make it a lot easier to add new widget and a lot faster to make them

svn path=/trunk/KDE/kdebase/workspace/plasma/scriptengines/javascript/; revision=907298
This commit is contained in:
Aaron J. Seigo 2009-01-07 20:35:28 +00:00
parent 14a52775bb
commit 7a47d0ded7
4 changed files with 72 additions and 118 deletions

View File

@ -35,7 +35,6 @@
#include <Plasma/Package> #include <Plasma/Package>
#include "appletinterface.h" #include "appletinterface.h"
#include "uiloader.h"
using namespace Plasma; using namespace Plasma;
@ -172,6 +171,8 @@ void kConfigGroupFromScriptValue(const QScriptValue& obj, KConfigGroup &config)
} }
} }
KSharedPtr<UiLoader> SimpleJavaScriptApplet::s_widgetLoader;
SimpleJavaScriptApplet::SimpleJavaScriptApplet(QObject *parent, const QVariantList &args) SimpleJavaScriptApplet::SimpleJavaScriptApplet(QObject *parent, const QVariantList &args)
: Plasma::AppletScript(parent) : Plasma::AppletScript(parent)
{ {
@ -183,6 +184,9 @@ SimpleJavaScriptApplet::SimpleJavaScriptApplet(QObject *parent, const QVariantLi
SimpleJavaScriptApplet::~SimpleJavaScriptApplet() SimpleJavaScriptApplet::~SimpleJavaScriptApplet()
{ {
if (s_widgetLoader.count() == 1) {
s_widgetLoader.clear();
}
} }
void SimpleJavaScriptApplet::reportError() void SimpleJavaScriptApplet::reportError()
@ -631,9 +635,11 @@ QScriptValue SimpleJavaScriptApplet::newPlasmaFrameSvg(QScriptContext *context,
void SimpleJavaScriptApplet::installWidgets(QScriptEngine *engine) void SimpleJavaScriptApplet::installWidgets(QScriptEngine *engine)
{ {
QScriptValue globalObject = engine->globalObject(); QScriptValue globalObject = engine->globalObject();
UiLoader loader; if (!s_widgetLoader) {
s_widgetLoader = new UiLoader;
}
foreach (const QString &widget, loader.availableWidgets()) { foreach (const QString &widget, s_widgetLoader->availableWidgets()) {
QScriptValue fun = engine->newFunction(createWidget); QScriptValue fun = engine->newFunction(createWidget);
QScriptValue name = engine->toScriptValue(widget); QScriptValue name = engine->toScriptValue(widget);
fun.setProperty(QString("functionName"), name, fun.setProperty(QString("functionName"), name,
@ -660,8 +666,11 @@ QScriptValue SimpleJavaScriptApplet::createWidget(QScriptContext *context, QScri
} }
QString self = context->callee().property("functionName").toString(); QString self = context->callee().property("functionName").toString();
UiLoader loader; if (!s_widgetLoader) {
QGraphicsWidget *w = loader.createWidget(self, parent); s_widgetLoader = new UiLoader;
}
QGraphicsWidget *w = s_widgetLoader->createWidget(self, parent);
if (!w) { if (!w) {
return QScriptValue(); return QScriptValue();

View File

@ -24,6 +24,8 @@
#include <Plasma/AppletScript> #include <Plasma/AppletScript>
#include <Plasma/DataEngine> #include <Plasma/DataEngine>
#include "uiloader.h"
class QScriptEngine; class QScriptEngine;
class QScriptContext; class QScriptContext;
@ -71,6 +73,7 @@ private:
static QScriptValue createPrototype( QScriptEngine *engine, const QString &name ); static QScriptValue createPrototype( QScriptEngine *engine, const QString &name );
private: private:
static KSharedPtr<UiLoader> s_widgetLoader;
QScriptEngine *m_engine; QScriptEngine *m_engine;
QScriptValue m_self; QScriptValue m_self;
AppletInterface *m_interface; AppletInterface *m_interface;

View File

@ -38,6 +38,7 @@
#include <Plasma/ScrollBar> #include <Plasma/ScrollBar>
#include <Plasma/SignalPlotter> #include <Plasma/SignalPlotter>
#include <Plasma/Slider> #include <Plasma/Slider>
#include <Plasma/SpinBox>
#include <Plasma/SvgWidget> #include <Plasma/SvgWidget>
#include <Plasma/TabBar> #include <Plasma/TabBar>
#include <Plasma/TextEdit> #include <Plasma/TextEdit>
@ -45,115 +46,68 @@
#include <Plasma/TreeView> #include <Plasma/TreeView>
#include <Plasma/WebView> #include <Plasma/WebView>
class UiLoaderPrivate QGraphicsWidget *createBusyWidget(QGraphicsWidget *parent) { return new Plasma::BusyWidget(parent); }
{ QGraphicsWidget *createCheckBox(QGraphicsWidget *parent) { return new Plasma::CheckBox(parent); }
public: QGraphicsWidget *createComboBox(QGraphicsWidget *parent) { return new Plasma::ComboBox(parent); }
QStringList widgets; QGraphicsWidget *createFlashingLabel(QGraphicsWidget *parent) { return new Plasma::FlashingLabel(parent); }
QStringList layouts; QGraphicsWidget *createFrame(QGraphicsWidget *parent) { return new Plasma::Frame(parent); }
}; QGraphicsWidget *createGroupBox(QGraphicsWidget *parent) { return new Plasma::GroupBox(parent); }
QGraphicsWidget *createIconWidget(QGraphicsWidget *parent) { return new Plasma::IconWidget(parent); }
QGraphicsWidget *createLabel(QGraphicsWidget *parent) { return new Plasma::Label(parent); }
QGraphicsWidget *createLineEdit(QGraphicsWidget *parent) { return new Plasma::LineEdit(parent); }
QGraphicsWidget *createMeter(QGraphicsWidget *parent) { return new Plasma::Meter(parent); }
QGraphicsWidget *createPushButton(QGraphicsWidget *parent) { return new Plasma::PushButton(parent); }
QGraphicsWidget *createRadioButton(QGraphicsWidget *parent) { return new Plasma::RadioButton(parent); }
QGraphicsWidget *createScrollBar(QGraphicsWidget *parent) { return new Plasma::ScrollBar(parent); }
QGraphicsWidget *createSignalPlotter(QGraphicsWidget *parent) { return new Plasma::SignalPlotter(parent); }
QGraphicsWidget *createSlider(QGraphicsWidget *parent) { return new Plasma::Slider(parent); }
QGraphicsWidget *createSpinBox(QGraphicsWidget *parent) { return new Plasma::SpinBox(parent); }
QGraphicsWidget *createSvgWidget(QGraphicsWidget *parent) { return new Plasma::SvgWidget(parent); }
QGraphicsWidget *createTabBar(QGraphicsWidget *parent) { return new Plasma::TabBar(parent); }
QGraphicsWidget *createTextEdit(QGraphicsWidget *parent) { return new Plasma::TextEdit(parent); }
QGraphicsWidget *createToolButton(QGraphicsWidget *parent) { return new Plasma::ToolButton(parent); }
QGraphicsWidget *createTreeView(QGraphicsWidget *parent) { return new Plasma::TreeView(parent); }
UiLoader::UiLoader(QObject *parent) UiLoader::UiLoader()
: QObject(parent),
d(new UiLoaderPrivate())
{ {
d->widgets m_widgetCtors.insert("BusyWidget", createBusyWidget);
<< "BusyWidget" m_widgetCtors.insert("CheckBox", createCheckBox);
<< "CheckBox" m_widgetCtors.insert("ComboBox", createComboBox);
<< "ComboBox" m_widgetCtors.insert("FlashingLabel", createFlashingLabel);
<< "FlashingLabel" m_widgetCtors.insert("Frame", createFrame);
<< "Frame" m_widgetCtors.insert("GroupBox", createGroupBox);
<< "GroupBox" m_widgetCtors.insert("IconWidget", createIconWidget);
<< "IconWidget" m_widgetCtors.insert("Label", createLabel);
<< "Label" m_widgetCtors.insert("LineEdit", createLineEdit);
<< "LineEdit" m_widgetCtors.insert("Meter", createMeter);
<< "Meter" m_widgetCtors.insert("PushButton", createPushButton);
<< "PushButton" m_widgetCtors.insert("RadioButton", createRadioButton);
<< "RadioButton" m_widgetCtors.insert("ScrollBar", createScrollBar);
<< "ScrollBar" m_widgetCtors.insert("SignalPlotter", createSignalPlotter);
<< "SignalPlotter" m_widgetCtors.insert("Slider", createSlider);
<< "Slider" m_widgetCtors.insert("SpinBox", createSpinBox);
<< "SvgWidget" m_widgetCtors.insert("SvgWidget", createSvgWidget);
<< "TabBar" m_widgetCtors.insert("TabBar", createTabBar);
<< "TextEdit" m_widgetCtors.insert("TextEdit", createTextEdit);
<< "ToolButton" m_widgetCtors.insert("ToolButton", createToolButton);
<< "TreeView" m_widgetCtors.insert("TreeView", createTreeView);
<< "WebView";
d->layouts
<< "GridLayout"
<< "LinearLayout";
} }
UiLoader::~UiLoader() UiLoader::~UiLoader()
{ {
delete d; kDebug();
} }
QStringList UiLoader::availableWidgets() const QStringList UiLoader::availableWidgets() const
{ {
return d->widgets; return m_widgetCtors.keys();
} }
QGraphicsWidget *UiLoader::createWidget(const QString &className, QGraphicsWidget *parent) QGraphicsWidget *UiLoader::createWidget(const QString &className, QGraphicsWidget *parent)
{ {
if (className == QString("BusyWidget")) { widgetCreator w = m_widgetCtors.value(className, 0);
return new Plasma::BusyWidget(parent); if (w) {
} else if (className == QString("CheckBox")) { return (w)(parent);
return new Plasma::CheckBox(parent);
} else if (className == QString("ComboBox")) {
return new Plasma::ComboBox(parent);
} else if (className == QString("FlashingLabel")) {
return new Plasma::FlashingLabel(parent);
} else if (className == QString("Frame")) {
return new Plasma::Frame(parent);
} else if (className == QString("GroupBox")) {
return new Plasma::GroupBox(parent);
} else if (className == QString("IconWidget")) {
return new Plasma::IconWidget(parent);
} else if (className == QString("Label")) {
return new Plasma::Label(parent);
} else if (className == QString("LineEdit")) {
return new Plasma::LineEdit(parent);
} else if (className == QString("Meter")) {
return new Plasma::Meter(parent);
} else if (className == QString("PushButton")) {
return new Plasma::PushButton(parent);
} else if (className == QString("RadioButton")) {
return new Plasma::RadioButton(parent);
} else if (className == QString("ScrollBar")) {
return new Plasma::ScrollBar(parent);
} else if (className == QString("SignalPlotter")) {
return new Plasma::SignalPlotter(parent);
} else if (className == QString("Slider")) {
return new Plasma::Slider(parent);
} else if (className == QString("SvgWidget")) {
return new Plasma::SvgWidget(parent);
} else if (className == QString("TabBar")) {
return new Plasma::TabBar(parent);
} else if (className == QString("TextEdit")) {
return new Plasma::TextEdit(parent);
} else if (className == QString("ToolButton")) {
return new Plasma::ToolButton(parent);
} else if (className == QString("TreeView")) {
return new Plasma::TreeView(parent);
} else if (className == QString("WebView")) {
return new Plasma::WebView(parent);
}
return 0;
}
QStringList UiLoader::availableLayouts() const
{
return d->layouts;
}
QGraphicsLayout *UiLoader::createLayout(const QString &className, QGraphicsLayoutItem *parent)
{
if (className == QString("GridLayout")) {
return new QGraphicsGridLayout(parent);
} else if (className == QString("LinearLayout")) {
return new QGraphicsLinearLayout(parent);
} }
return 0; return 0;

View File

@ -20,37 +20,25 @@
#ifndef PLASMA_UILOADER_H #ifndef PLASMA_UILOADER_H
#define PLASMA_UILOADER_H #define PLASMA_UILOADER_H
#include <QtCore/QObject> #include <KSharedPtr>
#include <plasma/applet.h> #include <plasma/applet.h>
class QGraphicsWidget; class QGraphicsWidget;
class UiLoaderPrivate; class UiLoader : public QSharedData
/**
* @class UiLoader plasma/uiloader.h <Plasma/UiLoader>
*
* Dynamically create plasma Widgets and Layouts.
*
* @author Richard J. Moore, <rich@kde.org>
*/
class UiLoader : public QObject
{ {
Q_OBJECT
public: public:
UiLoader(QObject *parent = 0); UiLoader();
virtual ~UiLoader(); virtual ~UiLoader();
QStringList availableWidgets() const; QStringList availableWidgets() const;
QGraphicsWidget *createWidget(const QString &className, QGraphicsWidget *parent = 0); QGraphicsWidget *createWidget(const QString &className, QGraphicsWidget *parent = 0);
QStringList availableLayouts() const;
QGraphicsLayout *createLayout(const QString &className, QGraphicsLayoutItem *parent);
private: private:
UiLoaderPrivate *const d; typedef QGraphicsWidget *(*widgetCreator)(QGraphicsWidget*);
QHash<QString, widgetCreator> m_widgetCtors;
}; };
#endif // PLASMA_UILOADER_H #endif // PLASMA_UILOADER_H