File indexing completed on 2023-09-24 04:14:55

0001 /*
0002     SPDX-FileCopyrightText: 2009 Chani Armitage <chani@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "containmentactions.h"
0008 #include "containment.h"
0009 
0010 #include "private/containment_p.h"
0011 #include "private/containmentactions_p.h"
0012 
0013 #include <QContextMenuEvent>
0014 #include <QDebug>
0015 #include <QMetaEnum>
0016 #include <QMouseEvent>
0017 #include <QWheelEvent>
0018 
0019 #include <KLocalizedString>
0020 
0021 #include "version.h"
0022 
0023 namespace Plasma
0024 {
0025 ContainmentActions::ContainmentActions(QObject *parentObject)
0026     : d(new ContainmentActionsPrivate({}, this))
0027 {
0028     setParent(parentObject);
0029 }
0030 
0031 ContainmentActions::ContainmentActions(QObject *parentObject, const QVariantList &args)
0032     : d(new ContainmentActionsPrivate(args.value(0), this))
0033 {
0034     setParent(parentObject);
0035 
0036     // now remove first item since those are managed by Wallpaper and subclasses shouldn't
0037     // need to worry about them. yes, it violates the constness of this var, but it lets us add
0038     // or remove items later while applets can just pretend that their args always start at 0
0039     QVariantList &mutableArgs = const_cast<QVariantList &>(args);
0040     if (!mutableArgs.isEmpty()) {
0041         mutableArgs.removeFirst();
0042     }
0043 }
0044 
0045 ContainmentActions::~ContainmentActions()
0046 {
0047     delete d;
0048 }
0049 
0050 #if PLASMA_BUILD_DEPRECATED_SINCE(5, 67)
0051 KPluginInfo ContainmentActions::pluginInfo() const
0052 {
0053     return KPluginInfo(d->containmentActionsDescription);
0054 }
0055 #endif
0056 
0057 KPluginMetaData ContainmentActions::metadata() const
0058 {
0059     return d->containmentActionsDescription;
0060 }
0061 
0062 Containment *ContainmentActions::containment()
0063 {
0064     if (d->containment) {
0065         return d->containment;
0066     }
0067     return qobject_cast<Containment *>(parent());
0068 }
0069 
0070 void ContainmentActions::restore(const KConfigGroup &config)
0071 {
0072     Q_UNUSED(config);
0073 }
0074 
0075 void ContainmentActions::save(KConfigGroup &config)
0076 {
0077     Q_UNUSED(config);
0078 }
0079 
0080 QWidget *ContainmentActions::createConfigurationInterface(QWidget *parent)
0081 {
0082     Q_UNUSED(parent);
0083     return nullptr;
0084 }
0085 
0086 void ContainmentActions::configurationAccepted()
0087 {
0088     // do nothing by default
0089 }
0090 
0091 void ContainmentActions::performNextAction()
0092 {
0093     // do nothing by default, implement in subclasses
0094 }
0095 
0096 void ContainmentActions::performPreviousAction()
0097 {
0098     // do nothing by default, implement in subclasses
0099 }
0100 
0101 QList<QAction *> ContainmentActions::contextualActions()
0102 {
0103     return QList<QAction *>();
0104 }
0105 
0106 QString ContainmentActions::eventToString(QEvent *event)
0107 {
0108     QString trigger;
0109     Qt::KeyboardModifiers modifiers;
0110 
0111 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0112     const auto &mo = QObject::staticQtMetaObject;
0113 #else
0114     const auto &mo = Qt::staticMetaObject;
0115 #endif
0116 
0117     switch (event->type()) {
0118     case QEvent::MouseButtonPress:
0119     case QEvent::MouseButtonRelease:
0120     case QEvent::MouseButtonDblClick: {
0121         QMouseEvent *e = static_cast<QMouseEvent *>(event);
0122         int m = mo.indexOfEnumerator("MouseButtons");
0123         QMetaEnum mouse = mo.enumerator(m);
0124         trigger += QString::fromLatin1(mouse.valueToKey(e->button()));
0125         modifiers = e->modifiers();
0126         break;
0127     }
0128     case QEvent::Wheel: {
0129         QWheelEvent *e = static_cast<QWheelEvent *>(event);
0130         trigger = QStringLiteral("wheel:%1")
0131                       .arg(std::abs(e->angleDelta().x()) > std::abs(e->angleDelta().y()) ? QStringLiteral("Horizontal") : QStringLiteral("Vertical"));
0132         modifiers = e->modifiers();
0133         break;
0134     }
0135     case QEvent::ContextMenu: {
0136         int m = mo.indexOfEnumerator("MouseButtons");
0137         QMetaEnum mouse = mo.enumerator(m);
0138         trigger = QString::fromLatin1(mouse.valueToKey(Qt::RightButton));
0139         modifiers = Qt::NoModifier;
0140         break;
0141     }
0142     default:
0143         return QString();
0144     }
0145 
0146     int k = mo.indexOfEnumerator("KeyboardModifiers");
0147     QMetaEnum kbd = mo.enumerator(k);
0148     trigger += QLatin1Char(';') + QString::fromLatin1(kbd.valueToKeys(modifiers));
0149 
0150     return trigger;
0151 }
0152 
0153 void ContainmentActions::setContainment(Containment *newContainment)
0154 {
0155     d->containment = newContainment;
0156 }
0157 
0158 } // Plasma namespace
0159 
0160 #include "moc_containmentactions.cpp"