From 7a47d0ded74d4b7940003b79abbfdb6ac53faccf Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Wed, 7 Jan 2009 20:35:28 +0000 Subject: [PATCH] * 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 --- .../javascript/simplejavascriptapplet.cpp | 19 ++- .../javascript/simplejavascriptapplet.h | 3 + scriptengines/javascript/uiloader.cpp | 144 ++++++------------ scriptengines/javascript/uiloader.h | 24 +-- 4 files changed, 72 insertions(+), 118 deletions(-) diff --git a/scriptengines/javascript/simplejavascriptapplet.cpp b/scriptengines/javascript/simplejavascriptapplet.cpp index ace5bcdfe..adb0f1858 100644 --- a/scriptengines/javascript/simplejavascriptapplet.cpp +++ b/scriptengines/javascript/simplejavascriptapplet.cpp @@ -35,7 +35,6 @@ #include #include "appletinterface.h" -#include "uiloader.h" using namespace Plasma; @@ -172,6 +171,8 @@ void kConfigGroupFromScriptValue(const QScriptValue& obj, KConfigGroup &config) } } +KSharedPtr SimpleJavaScriptApplet::s_widgetLoader; + SimpleJavaScriptApplet::SimpleJavaScriptApplet(QObject *parent, const QVariantList &args) : Plasma::AppletScript(parent) { @@ -183,6 +184,9 @@ SimpleJavaScriptApplet::SimpleJavaScriptApplet(QObject *parent, const QVariantLi SimpleJavaScriptApplet::~SimpleJavaScriptApplet() { + if (s_widgetLoader.count() == 1) { + s_widgetLoader.clear(); + } } void SimpleJavaScriptApplet::reportError() @@ -631,9 +635,11 @@ QScriptValue SimpleJavaScriptApplet::newPlasmaFrameSvg(QScriptContext *context, void SimpleJavaScriptApplet::installWidgets(QScriptEngine *engine) { 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 name = engine->toScriptValue(widget); fun.setProperty(QString("functionName"), name, @@ -660,8 +666,11 @@ QScriptValue SimpleJavaScriptApplet::createWidget(QScriptContext *context, QScri } QString self = context->callee().property("functionName").toString(); - UiLoader loader; - QGraphicsWidget *w = loader.createWidget(self, parent); + if (!s_widgetLoader) { + s_widgetLoader = new UiLoader; + } + + QGraphicsWidget *w = s_widgetLoader->createWidget(self, parent); if (!w) { return QScriptValue(); diff --git a/scriptengines/javascript/simplejavascriptapplet.h b/scriptengines/javascript/simplejavascriptapplet.h index 80b9e68e0..97330bbe3 100644 --- a/scriptengines/javascript/simplejavascriptapplet.h +++ b/scriptengines/javascript/simplejavascriptapplet.h @@ -24,6 +24,8 @@ #include #include +#include "uiloader.h" + class QScriptEngine; class QScriptContext; @@ -71,6 +73,7 @@ private: static QScriptValue createPrototype( QScriptEngine *engine, const QString &name ); private: + static KSharedPtr s_widgetLoader; QScriptEngine *m_engine; QScriptValue m_self; AppletInterface *m_interface; diff --git a/scriptengines/javascript/uiloader.cpp b/scriptengines/javascript/uiloader.cpp index 775b39511..3083ada69 100644 --- a/scriptengines/javascript/uiloader.cpp +++ b/scriptengines/javascript/uiloader.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -45,115 +46,68 @@ #include #include -class UiLoaderPrivate -{ -public: - QStringList widgets; - QStringList layouts; -}; +QGraphicsWidget *createBusyWidget(QGraphicsWidget *parent) { return new Plasma::BusyWidget(parent); } +QGraphicsWidget *createCheckBox(QGraphicsWidget *parent) { return new Plasma::CheckBox(parent); } +QGraphicsWidget *createComboBox(QGraphicsWidget *parent) { return new Plasma::ComboBox(parent); } +QGraphicsWidget *createFlashingLabel(QGraphicsWidget *parent) { return new Plasma::FlashingLabel(parent); } +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) - : QObject(parent), - d(new UiLoaderPrivate()) +UiLoader::UiLoader() { - d->widgets - << "BusyWidget" - << "CheckBox" - << "ComboBox" - << "FlashingLabel" - << "Frame" - << "GroupBox" - << "IconWidget" - << "Label" - << "LineEdit" - << "Meter" - << "PushButton" - << "RadioButton" - << "ScrollBar" - << "SignalPlotter" - << "Slider" - << "SvgWidget" - << "TabBar" - << "TextEdit" - << "ToolButton" - << "TreeView" - << "WebView"; - - d->layouts - << "GridLayout" - << "LinearLayout"; + m_widgetCtors.insert("BusyWidget", createBusyWidget); + m_widgetCtors.insert("CheckBox", createCheckBox); + m_widgetCtors.insert("ComboBox", createComboBox); + m_widgetCtors.insert("FlashingLabel", createFlashingLabel); + m_widgetCtors.insert("Frame", createFrame); + m_widgetCtors.insert("GroupBox", createGroupBox); + m_widgetCtors.insert("IconWidget", createIconWidget); + m_widgetCtors.insert("Label", createLabel); + m_widgetCtors.insert("LineEdit", createLineEdit); + m_widgetCtors.insert("Meter", createMeter); + m_widgetCtors.insert("PushButton", createPushButton); + m_widgetCtors.insert("RadioButton", createRadioButton); + m_widgetCtors.insert("ScrollBar", createScrollBar); + m_widgetCtors.insert("SignalPlotter", createSignalPlotter); + m_widgetCtors.insert("Slider", createSlider); + m_widgetCtors.insert("SpinBox", createSpinBox); + m_widgetCtors.insert("SvgWidget", createSvgWidget); + m_widgetCtors.insert("TabBar", createTabBar); + m_widgetCtors.insert("TextEdit", createTextEdit); + m_widgetCtors.insert("ToolButton", createToolButton); + m_widgetCtors.insert("TreeView", createTreeView); } UiLoader::~UiLoader() { - delete d; + kDebug(); } QStringList UiLoader::availableWidgets() const { - return d->widgets; + return m_widgetCtors.keys(); } QGraphicsWidget *UiLoader::createWidget(const QString &className, QGraphicsWidget *parent) { - if (className == QString("BusyWidget")) { - return new Plasma::BusyWidget(parent); - } else if (className == QString("CheckBox")) { - 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); + widgetCreator w = m_widgetCtors.value(className, 0); + if (w) { + return (w)(parent); } return 0; diff --git a/scriptengines/javascript/uiloader.h b/scriptengines/javascript/uiloader.h index 5f868f270..396890a25 100644 --- a/scriptengines/javascript/uiloader.h +++ b/scriptengines/javascript/uiloader.h @@ -20,37 +20,25 @@ #ifndef PLASMA_UILOADER_H #define PLASMA_UILOADER_H -#include +#include #include class QGraphicsWidget; -class UiLoaderPrivate; - -/** - * @class UiLoader plasma/uiloader.h - * - * Dynamically create plasma Widgets and Layouts. - * - * @author Richard J. Moore, - */ -class UiLoader : public QObject +class UiLoader : public QSharedData { - Q_OBJECT - public: - UiLoader(QObject *parent = 0); + UiLoader(); virtual ~UiLoader(); QStringList availableWidgets() const; QGraphicsWidget *createWidget(const QString &className, QGraphicsWidget *parent = 0); - QStringList availableLayouts() const; - QGraphicsLayout *createLayout(const QString &className, QGraphicsLayoutItem *parent); - private: - UiLoaderPrivate *const d; + typedef QGraphicsWidget *(*widgetCreator)(QGraphicsWidget*); + QHash m_widgetCtors; }; + #endif // PLASMA_UILOADER_H