From 790333991ee773326bb7643d0a03828f6d992b1e Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Wed, 10 Jun 2020 09:09:03 +0000 Subject: [PATCH] support accessing applet geometry the geometry property remained broken for all plasma5 lifetime. reading it makes sense, writing it not so much (ad it depends ffrom containment to containment) reading works, writing stays noop (for max compatibility) --- shell/scripting/applet.cpp | 10 +++++++++- shell/scripting/applet.h | 6 +++++- shell/scripting/containment.cpp | 20 +++++++++----------- shell/scripting/scriptengine.cpp | 2 +- shell/scripting/widget.cpp | 18 ++++++++++++++---- shell/scripting/widget.h | 2 +- 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/shell/scripting/applet.cpp b/shell/scripting/applet.cpp index 878f99194..fb97f43fe 100644 --- a/shell/scripting/applet.cpp +++ b/shell/scripting/applet.cpp @@ -18,6 +18,7 @@ */ #include "applet.h" +#include "scriptengine.h" #include @@ -41,6 +42,7 @@ public: { } + QPointer engine = nullptr; KConfigGroup configGroup; QStringList configGroupPath; KConfigGroup globalConfigGroup; @@ -49,10 +51,11 @@ public: bool inWallpaperConfig : 1; }; -Applet::Applet(QObject *parent) +Applet::Applet(ScriptEngine *parent) : QObject(parent), d(new Applet::Private) { + d->engine = parent; } Applet::~Applet() @@ -268,6 +271,11 @@ Plasma::Applet *Applet::applet() const return nullptr; } +ScriptEngine *Applet::engine() const +{ + return d->engine; +} + } diff --git a/shell/scripting/applet.h b/shell/scripting/applet.h index 6064e0624..e720ebbc3 100644 --- a/shell/scripting/applet.h +++ b/shell/scripting/applet.h @@ -35,13 +35,15 @@ namespace Plasma namespace WorkspaceScripting { +class ScriptEngine; + class Applet : public QObject { Q_OBJECT Q_PROPERTY(QStringList currentConfigGroup WRITE setCurrentConfigGroup READ currentConfigGroup) public: - explicit Applet(QObject *parent = nullptr); + explicit Applet(ScriptEngine *parent); ~Applet() override; QStringList configKeys() const; @@ -63,6 +65,8 @@ public: virtual Plasma::Applet *applet() const; + ScriptEngine *engine() const; + protected: void reloadConfigIfNeeded(); diff --git a/shell/scripting/containment.cpp b/shell/scripting/containment.cpp index 1669f4e83..9a34512a5 100644 --- a/shell/scripting/containment.cpp +++ b/shell/scripting/containment.cpp @@ -40,7 +40,6 @@ namespace WorkspaceScripting class Containment::Private { public: - QPointer engine; QPointer containment; ShellCorona *corona; QString oldWallpaperPlugin; @@ -56,7 +55,6 @@ Containment::Containment(Plasma::Containment *containment, ScriptEngine *engine) : Applet(engine), d(new Containment::Private) { - d->engine = engine; d->containment = containment; d->corona = qobject_cast(containment->corona()); @@ -157,7 +155,7 @@ QList Containment::widgetIds() const QJSValue Containment::widgetById(const QJSValue ¶mId) const { if (!paramId.isNumber()) { - return d->engine->newError(i18n("widgetById requires an id")); + return engine()->newError(i18n("widgetById requires an id")); } const uint id = paramId.toInt(); @@ -165,7 +163,7 @@ QJSValue Containment::widgetById(const QJSValue ¶mId) const if (d->containment) { foreach (Plasma::Applet *w, d->containment.data()->applets()) { if (w->id() == id) { - return d->engine->wrap(w); + return engine()->wrap(w); } } } @@ -176,7 +174,7 @@ QJSValue Containment::widgetById(const QJSValue ¶mId) const QJSValue Containment::addWidget(const QJSValue &v, qreal x, qreal y, qreal w, qreal h, const QVariantList &args) { if (!v.isString() && !v.isQObject()) { - return d->engine->newError(i18n("addWidget requires a name of a widget or a widget object")); + return engine()->newError(i18n("addWidget requires a name of a widget or a widget object")); } if (!d->containment) { @@ -197,9 +195,9 @@ QJSValue Containment::addWidget(const QJSValue &v, qreal x, qreal y, qreal w, qr QMetaObject::invokeMethod(containmentItem , "createApplet", Qt::DirectConnection, Q_RETURN_ARG(Plasma::Applet *, applet), Q_ARG(QString, v.toString()), Q_ARG(QVariantList, args), Q_ARG(QRectF, geometry)); } if (applet) { - return d->engine->wrap(applet); + return engine()->wrap(applet); } - return d->engine->newError(i18n("Could not create the %1 widget!", v.toString())); + return engine()->newError(i18n("Could not create the %1 widget!", v.toString())); } //Case in which either: @@ -208,10 +206,10 @@ QJSValue Containment::addWidget(const QJSValue &v, qreal x, qreal y, qreal w, qr applet = d->containment.data()->createApplet(v.toString(), args); if (applet) { - return d->engine->wrap(applet); + return engine()->wrap(applet); } - return d->engine->newError(i18n("Could not create the %1 widget!", v.toString())); + return engine()->newError(i18n("Could not create the %1 widget!", v.toString())); } else if (Widget *widget = qobject_cast(v.toQObject())) { applet = widget->applet(); d->containment.data()->addApplet(applet); @@ -227,12 +225,12 @@ QJSValue Containment::widgets(const QString &widgetType) const return QJSValue(); } - QJSValue widgets = d->engine->newArray(); + QJSValue widgets = engine()->newArray(); int count = 0; foreach (Plasma::Applet *widget, d->containment.data()->applets()) { if (widgetType.isEmpty() || widget->pluginMetaData().pluginId() == widgetType) { - widgets.setProperty(count, d->engine->wrap(widget)); + widgets.setProperty(count, engine()->wrap(widget)); ++count; } } diff --git a/shell/scripting/scriptengine.cpp b/shell/scripting/scriptengine.cpp index e4c7d9c47..41982c845 100644 --- a/shell/scripting/scriptengine.cpp +++ b/shell/scripting/scriptengine.cpp @@ -81,7 +81,7 @@ QString ScriptEngine::errorString() const QJSValue ScriptEngine::wrap(Plasma::Applet *w) { - Widget *wrapper = new Widget(w); + Widget *wrapper = new Widget(w, this); return newQObject(wrapper); } diff --git a/shell/scripting/widget.cpp b/shell/scripting/widget.cpp index daf02d24f..f97649c37 100644 --- a/shell/scripting/widget.cpp +++ b/shell/scripting/widget.cpp @@ -18,9 +18,11 @@ */ #include "widget.h" +#include "scriptengine.h" #include #include +#include #include #include @@ -39,7 +41,7 @@ public: QPointer applet; }; -Widget::Widget(Plasma::Applet *applet, QObject *parent) +Widget::Widget(Plasma::Applet *applet, ScriptEngine *parent) : Applet(parent), d(new Widget::Private) { @@ -151,10 +153,18 @@ void Widget::setIndex(int index) QJSValue Widget::geometry() const { - /*if (d->applet) { - return d->applet.data()->geometry(); + QQuickItem *appletItem = d->applet.data()->property("_plasma_graphicObject").value(); + + if (appletItem) { + QJSValue rect = engine()->newObject(); + const QPointF pos = appletItem->mapToScene(QPointF(0,0)); + rect.setProperty(QStringLiteral("x"), pos.x()); + rect.setProperty(QStringLiteral("y"), pos.y()); + rect.setProperty(QStringLiteral("width"), appletItem->width()); + rect.setProperty(QStringLiteral("height"), appletItem->height()); + return rect; } -*/ + return QJSValue(); } diff --git a/shell/scripting/widget.h b/shell/scripting/widget.h index d1401d9d0..32a8d3a5d 100644 --- a/shell/scripting/widget.h +++ b/shell/scripting/widget.h @@ -52,7 +52,7 @@ class Widget : public Applet Q_PROPERTY(QString userBackgroundHints WRITE setUserBackgroundHints READ userBackgroundHints) public: - explicit Widget(Plasma::Applet *applet, QObject *parent = nullptr); + explicit Widget(Plasma::Applet *applet, ScriptEngine *parent = nullptr); ~Widget() override; uint id() const;