File indexing completed on 2024-05-12 05:38:25

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