[plasma/plasma-workspace] shell/scripting: Scripting: restore Applet::index property

Marco Martin <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 6f2cbab5ca795d8ac2a05a4f6b9e2904829d931c by Marco Martin.
Committed on 06/08/2026 at 13:17.
Pushed by mart into branch 'master'.

Scripting: restore Applet::index property

on panels use the config to the index order
and use the reorderApplets() function that the panel containment exposes to apply a different index
Depends from https://invent.kde.org/plasma/plasma-desktop/-/merge_requests/3926

BUG:523675

M  +37   -21   shell/scripting/widget.cpp

https://invent.kde.org/plasma/plasma-workspace/-/commit/6f2cbab5ca795d8ac2a05a4f6b9e2904829d931c

diff --git a/shell/scripting/widget.cpp b/shell/scripting/widget.cpp
index 3db5fcddcd..19224d9846 100644
--- a/shell/scripting/widget.cpp
+++ b/shell/scripting/widget.cpp
@@ -94,43 +94,59 @@ int Widget::index() const
     }
 
     Plasma::Containment *c = d->applet->containment();
-    if (!c) {
+    if (!c || (c->containmentType() != Plasma::Containment::Panel && c->containmentType() != Plasma::Containment::CustomPanel)) {
         return -1;
     }
 
-    /*QGraphicsLayout *layout = c->layout();
-    if (!layout) {
-        return - 1;
-    }
-
-    for (int i = 0; i < layout->count(); ++i) {
-        if (layout->itemAt(i) == applet) {
-            return i;
-        }
-    }*/
+    KConfigGroup cg = c->config();
+    cg = KConfigGroup(&cg, QStringLiteral("General"));
+    const QString orderString = cg.readEntry("AppletOrder", QString());
+    auto parts = orderString.split(u';');
+    QList<int> order;
+    std::ranges::transform(parts, std::back_inserter(order), [](const QString &s) {
+        return s.toInt();
+    });
 
-    return -1;
+    return order.indexOf(d->applet->id());
 }
 
 void Widget::setIndex(int index)
 {
-    Q_UNUSED(index)
-    /*
     if (!d->applet) {
         return;
     }
 
     Plasma::Containment *c = d->applet->containment();
-    if (!c) {
-        return;
-    }
-    //FIXME: this is hackish. would be nice to define this for gridlayouts too
-    QGraphicsLinearLayout *layout = dynamic_cast<QGraphicsLinearLayout *>(c->layout());
-    if (!layout) {
+    if (!c || (c->containmentType() != Plasma::Containment::Panel && c->containmentType() != Plasma::Containment::CustomPanel)) {
         return;
     }
 
-    layout->insertItem(index, applet);*/
+    KConfigGroup cg = c->config();
+    cg = KConfigGroup(&cg, QStringLiteral("General"));
+    const QString orderString = cg.readEntry("AppletOrder", QString());
+    auto parts = orderString.split(u';');
+    QList<int> order;
+    order.reserve(parts.size());
+    std::ranges::transform(std::as_const(parts), std::back_inserter(order), [](const QString &s) {
+        return s.toInt();
+    });
+
+    order.removeAll(d->applet->id());
+
+    order.insert(std::min(std::max(0, index), int(order.length()) - 1), d->applet->id());
+
+    parts.clear();
+    std::ranges::transform(std::as_const(order), std::back_inserter(parts), [](int val) {
+        return QString::number(val);
+    });
+
+    QQuickItem *containmentItem = PlasmaQuick::AppletQuickItem::itemForApplet(c);
+
+    bool success = QMetaObject::invokeMethod(containmentItem, "reorderApplets", Q_ARG(QVariant, QVariant::fromValue(order)));
+
+    if (!success) {
+        qWarning() << "Impossible to invoke reorderApplets() on the panel";
+    }
 }
 
 QJSValue Widget::geometry() const
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.