From 866fe4312045aac6dada8bb3894e24c82fe9d417 Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Tue, 28 Jan 2014 18:26:46 +0100 Subject: [PATCH] experiment with AppletInterface to be attached Plasmoid.title etc would be attached properties to the root object instead of being a component itself --- .../applets/widgetgallery/contents/ui/main.qml | 4 ++++ src/scriptengines/qml/plasmoid/appletinterface.cpp | 5 +++++ src/scriptengines/qml/plasmoid/appletinterface.h | 14 +++++++++++++- .../qml/plasmoid/declarativeappletscript.cpp | 4 +++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/examples/applets/widgetgallery/contents/ui/main.qml b/examples/applets/widgetgallery/contents/ui/main.qml index 723c7240c..f586741fc 100644 --- a/examples/applets/widgetgallery/contents/ui/main.qml +++ b/examples/applets/widgetgallery/contents/ui/main.qml @@ -19,14 +19,18 @@ import QtQuick 2.0 import org.kde.plasma.components 2.0 +import org.kde.shell 2.0 Item { property int minimumWidth: 300 property int minimumHeight: 400 + AppletInterface.title: "bah" + ToolBar { id: toolBar z: 10 + //AppletInterface.title: "bah" anchors { top: parent.top left: parent.left diff --git a/src/scriptengines/qml/plasmoid/appletinterface.cpp b/src/scriptengines/qml/plasmoid/appletinterface.cpp index eac58b51c..5376aa46b 100644 --- a/src/scriptengines/qml/plasmoid/appletinterface.cpp +++ b/src/scriptengines/qml/plasmoid/appletinterface.cpp @@ -52,6 +52,8 @@ Q_DECLARE_METATYPE(AppletInterface*) +QHash AppletInterface::s_rootObjects = QHash(); + AppletInterface::AppletInterface(DeclarativeAppletScript *script, QQuickItem *parent) : QQuickItem(parent), m_appletScriptEngine(script), @@ -96,6 +98,7 @@ AppletInterface::AppletInterface(DeclarativeAppletScript *script, QQuickItem *pa AppletInterface::~AppletInterface() { + s_rootObjects.remove(m_qmlObject->engine()); } void AppletInterface::init() @@ -109,6 +112,8 @@ void AppletInterface::init() //use our own custom network access manager that will access Plasma packages and to manage security (i.e. deny access to remote stuff when the proper extension isn't enabled QQmlEngine *engine = m_qmlObject->engine(); + s_rootObjects[m_qmlObject->engine()] = this; + //Hook generic url resolution to the applet package as well //TODO: same thing will have to be done for every qqmlengine: PackageUrlInterceptor is material for plasmaquick? PackageUrlInterceptor *interceptor = new PackageUrlInterceptor(engine, m_appletScriptEngine->package()); diff --git a/src/scriptengines/qml/plasmoid/appletinterface.h b/src/scriptengines/qml/plasmoid/appletinterface.h index 7714689fa..466a9dcdd 100644 --- a/src/scriptengines/qml/plasmoid/appletinterface.h +++ b/src/scriptengines/qml/plasmoid/appletinterface.h @@ -237,7 +237,15 @@ public: * DEPRECATED: use plasmoid.configuration instead */ Q_INVOKABLE void writeConfig(const QString &entry, const QVariant &value); - + + static AppletInterface *qmlAttachedProperties(QObject *object) + { + if (!object->parent() && s_rootObjects.contains(QtQml::qmlEngine(object))) { + return s_rootObjects.value(QtQml::qmlEngine(object)); + } else { + return 0; + } + } //PROPERTY ACCESSORS------------------------------------------------------------------- QString icon() const; @@ -361,6 +369,10 @@ private: bool m_expanded : 1; bool m_hideOnDeactivate : 1; friend class ContainmentInterface; + + static QHash s_rootObjects; }; +QML_DECLARE_TYPEINFO(AppletInterface, QML_HAS_ATTACHED_PROPERTIES) + #endif diff --git a/src/scriptengines/qml/plasmoid/declarativeappletscript.cpp b/src/scriptengines/qml/plasmoid/declarativeappletscript.cpp index 9876e8c7c..4c64df507 100644 --- a/src/scriptengines/qml/plasmoid/declarativeappletscript.cpp +++ b/src/scriptengines/qml/plasmoid/declarativeappletscript.cpp @@ -54,7 +54,9 @@ DeclarativeAppletScript::DeclarativeAppletScript(QObject *parent, const QVariant : Plasma::AppletScript(parent), m_interface(0) { - qmlRegisterType(); + //qmlRegisterType(); + qmlRegisterUncreatableType("org.kde.shell", 2, 0, "AppletInterface", + QLatin1String("Do not create objects of type AppletInterface")); qmlRegisterType(); Q_UNUSED(args); }