From 9275e84a995304c37d0bf456f7bf4020fad7935d Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Tue, 4 Feb 2014 17:23:22 +0100 Subject: [PATCH] add a Wallpaper attached property for wallpapers --- .../qml/plasmoid/appletinterface.cpp | 1 - .../qml/plasmoid/containmentinterface.cpp | 2 -- .../qml/plasmoid/declarativeappletscript.cpp | 4 ++++ .../qml/plasmoid/wallpaperinterface.cpp | 8 +++++++- .../qml/plasmoid/wallpaperinterface.h | 16 ++++++++++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/scriptengines/qml/plasmoid/appletinterface.cpp b/src/scriptengines/qml/plasmoid/appletinterface.cpp index 7e0ccb3e9..e105f8a0a 100644 --- a/src/scriptengines/qml/plasmoid/appletinterface.cpp +++ b/src/scriptengines/qml/plasmoid/appletinterface.cpp @@ -59,7 +59,6 @@ AppletInterface::AppletInterface(DeclarativeAppletScript *script, QQuickItem *pa m_busy(false), m_hideOnDeactivate(true) { - qmlRegisterType(); qmlRegisterType(); connect(this, &AppletInterface::configNeedsSaving, diff --git a/src/scriptengines/qml/plasmoid/containmentinterface.cpp b/src/scriptengines/qml/plasmoid/containmentinterface.cpp index b5bf2d5c1..64bf4ecc1 100644 --- a/src/scriptengines/qml/plasmoid/containmentinterface.cpp +++ b/src/scriptengines/qml/plasmoid/containmentinterface.cpp @@ -59,8 +59,6 @@ ContainmentInterface::ContainmentInterface(DeclarativeAppletScript *parent) { setAcceptedMouseButtons(Qt::AllButtons); - qmlRegisterType(); - connect(containment(), &Plasma::Containment::appletRemoved, this, &ContainmentInterface::appletRemovedForward); connect(containment(), &Plasma::Containment::appletAdded, diff --git a/src/scriptengines/qml/plasmoid/declarativeappletscript.cpp b/src/scriptengines/qml/plasmoid/declarativeappletscript.cpp index 5f8793a71..d3099f310 100644 --- a/src/scriptengines/qml/plasmoid/declarativeappletscript.cpp +++ b/src/scriptengines/qml/plasmoid/declarativeappletscript.cpp @@ -42,6 +42,7 @@ #include "plasmoid/appletinterface.h" #include "plasmoid/containmentinterface.h" +#include "plasmoid/wallpaperinterface.h" #include #include @@ -60,6 +61,9 @@ DeclarativeAppletScript::DeclarativeAppletScript(QObject *parent, const QVariant qmlRegisterUncreatableType("org.kde.plasma.plasmoid", 2, 0, "Containment", QLatin1String("Do not create objects of type Containment")); + qmlRegisterUncreatableType("org.kde.plasma.plasmoid", 2, 0, "Wallpaper", + QLatin1String("Do not create objects of type Wallpaper")); + qmlRegisterType(); Q_UNUSED(args); } diff --git a/src/scriptengines/qml/plasmoid/wallpaperinterface.cpp b/src/scriptengines/qml/plasmoid/wallpaperinterface.cpp index 1d5f80d9d..88099f185 100644 --- a/src/scriptengines/qml/plasmoid/wallpaperinterface.cpp +++ b/src/scriptengines/qml/plasmoid/wallpaperinterface.cpp @@ -36,6 +36,8 @@ #include #include +QHash WallpaperInterface::s_rootObjects = QHash(); + WallpaperInterface::WallpaperInterface(ContainmentInterface *parent) : QQuickItem(parent), m_containmentInterface(parent), @@ -55,7 +57,9 @@ WallpaperInterface::WallpaperInterface(ContainmentInterface *parent) } WallpaperInterface::~WallpaperInterface() -{} +{ + s_rootObjects.remove(m_qmlObject->engine()); +} KPluginInfo::List WallpaperInterface::listWallpaperInfoForMimetype(const QString &mimetype, const QString &formFactor) { @@ -109,6 +113,7 @@ void WallpaperInterface::syncWallpaperPackage() if (!m_qmlObject) { m_qmlObject = new KDeclarative::QmlObject(this); + s_rootObjects[m_qmlObject->engine()] = this; m_qmlObject->setInitializationDelayed(true); } @@ -142,6 +147,7 @@ void WallpaperInterface::syncWallpaperPackage() } else if (m_qmlObject->mainComponent()) { qWarning() << "Error loading the wallpaper" << m_qmlObject->mainComponent()->errors(); + s_rootObjects.remove(m_qmlObject->engine()); m_qmlObject->deleteLater(); m_qmlObject = 0; diff --git a/src/scriptengines/qml/plasmoid/wallpaperinterface.h b/src/scriptengines/qml/plasmoid/wallpaperinterface.h index 8750dc03f..8a4621f9e 100644 --- a/src/scriptengines/qml/plasmoid/wallpaperinterface.h +++ b/src/scriptengines/qml/plasmoid/wallpaperinterface.h @@ -21,6 +21,7 @@ #define WALLPAPERINTERFACE_H #include +#include #include @@ -78,6 +79,17 @@ public: Q_INVOKABLE QAction *action(QString name) const; + static WallpaperInterface *qmlAttachedProperties(QObject *object) + { + //at the moment of the attached object creation, the root item is the only one that hasn't a parent + //only way to avoid creation of this attached for everybody but the root item + if (!object->parent() && s_rootObjects.contains(QtQml::qmlEngine(object))) { + return s_rootObjects.value(QtQml::qmlEngine(object)); + } else { + return 0; + } + } + Q_SIGNALS: void packageChanged(); void configurationChanged(); @@ -95,6 +107,10 @@ private: Plasma::ConfigLoader *m_configLoader; KActionCollection *m_actions; QSignalMapper *m_actionSignals; + + static QHash s_rootObjects; }; +QML_DECLARE_TYPEINFO(WallpaperInterface, QML_HAS_ATTACHED_PROPERTIES) + #endif