File indexing completed on 2024-05-12 17:10:24

0001 /*
0002     SPDX-FileCopyrightText: 2009 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "widget.h"
0008 #include "scriptengine.h"
0009 
0010 #include <QAction>
0011 #include <QMetaEnum>
0012 #include <QQuickItem>
0013 
0014 #include <Plasma/Applet>
0015 #include <Plasma/Containment>
0016 #include <Plasma/Corona>
0017 
0018 namespace WorkspaceScripting
0019 {
0020 class Widget::Private
0021 {
0022 public:
0023     Private()
0024     {
0025     }
0026 
0027     QPointer<Plasma::Applet> applet;
0028 };
0029 
0030 Widget::Widget(Plasma::Applet *applet, ScriptEngine *parent)
0031     : Applet(parent)
0032     , d(new Widget::Private)
0033 {
0034     d->applet = applet;
0035     setCurrentConfigGroup(QStringList());
0036     setCurrentGlobalConfigGroup(QStringList());
0037 }
0038 
0039 Widget::~Widget()
0040 {
0041     reloadConfigIfNeeded();
0042     delete d;
0043 }
0044 
0045 uint Widget::id() const
0046 {
0047     if (d->applet) {
0048         return d->applet->id();
0049     }
0050 
0051     return 0;
0052 }
0053 
0054 QString Widget::type() const
0055 {
0056     if (d->applet) {
0057         return d->applet->pluginMetaData().pluginId();
0058     }
0059 
0060     return QString();
0061 }
0062 
0063 void Widget::remove()
0064 {
0065     if (d->applet) {
0066         d->applet->destroy();
0067         d->applet.clear();
0068     }
0069 }
0070 
0071 void Widget::setGlobalShortcut(const QString &shortcut)
0072 {
0073     if (d->applet) {
0074         d->applet->setGlobalShortcut(QKeySequence(shortcut));
0075     }
0076 }
0077 
0078 QString Widget::globalShorcut() const
0079 {
0080     if (d->applet) {
0081         return d->applet->globalShortcut().toString();
0082     }
0083 
0084     return QString();
0085 }
0086 
0087 Plasma::Applet *Widget::applet() const
0088 {
0089     return d->applet;
0090 }
0091 
0092 int Widget::index() const
0093 {
0094     if (!d->applet) {
0095         return -1;
0096     }
0097 
0098     Plasma::Containment *c = d->applet->containment();
0099     if (!c) {
0100         return -1;
0101     }
0102 
0103     /*QGraphicsLayout *layout = c->layout();
0104     if (!layout) {
0105         return - 1;
0106     }
0107 
0108     for (int i = 0; i < layout->count(); ++i) {
0109         if (layout->itemAt(i) == applet) {
0110             return i;
0111         }
0112     }*/
0113 
0114     return -1;
0115 }
0116 
0117 void Widget::setIndex(int index)
0118 {
0119     Q_UNUSED(index)
0120     /*
0121     if (!d->applet) {
0122         return;
0123     }
0124 
0125     Plasma::Containment *c = d->applet->containment();
0126     if (!c) {
0127         return;
0128     }
0129     //FIXME: this is hackish. would be nice to define this for gridlayouts too
0130     QGraphicsLinearLayout *layout = dynamic_cast<QGraphicsLinearLayout *>(c->layout());
0131     if (!layout) {
0132         return;
0133     }
0134 
0135     layout->insertItem(index, applet);*/
0136 }
0137 
0138 QJSValue Widget::geometry() const
0139 {
0140     QQuickItem *appletItem = d->applet->property("_plasma_graphicObject").value<QQuickItem *>();
0141 
0142     if (appletItem) {
0143         QJSValue rect = engine()->newObject();
0144         const QPointF pos = appletItem->mapToScene(QPointF(0, 0));
0145         rect.setProperty(QStringLiteral("x"), pos.x());
0146         rect.setProperty(QStringLiteral("y"), pos.y());
0147         rect.setProperty(QStringLiteral("width"), appletItem->width());
0148         rect.setProperty(QStringLiteral("height"), appletItem->height());
0149         return rect;
0150     }
0151 
0152     return QJSValue();
0153 }
0154 
0155 void Widget::setGeometry(const QJSValue &geometry)
0156 {
0157     Q_UNUSED(geometry)
0158     /*if (d->applet) {
0159         d->applet->setGeometry(geometry);
0160         KConfigGroup cg = d->applet->config().parent();
0161         if (cg.isValid()) {
0162             cg.writeEntry("geometry", geometry);
0163         }
0164     }*/
0165 }
0166 
0167 void Widget::showConfigurationInterface()
0168 {
0169     /* if (d->applet) {
0170          d->applet->showConfigurationInterface();
0171      }*/
0172 }
0173 
0174 QString Widget::userBackgroundHints() const
0175 {
0176     QMetaEnum hintEnum = QMetaEnum::fromType<Plasma::Types::BackgroundHints>();
0177     return hintEnum.valueToKey(applet()->userBackgroundHints());
0178 }
0179 
0180 void Widget::setUserBackgroundHints(const QString &hint)
0181 {
0182     QMetaEnum hintEnum = QMetaEnum::fromType<Plasma::Types::BackgroundHints>();
0183     bool ok;
0184     int value = hintEnum.keyToValue(hint.toUtf8().constData(), &ok);
0185     if (ok) {
0186         applet()->setUserBackgroundHints(Plasma::Types::BackgroundHints(value));
0187     }
0188 }
0189 
0190 }