File indexing completed on 2024-04-28 16:44:39

0001 /*
0002  *   SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
0003  *   SPDX-FileCopyrightText: 2016 David Edmundson <davidedmundson@kde.org>
0004  *
0005  *   SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "groupingcontainment.h"
0009 #include "debug.h"
0010 
0011 #include <QDebug>
0012 #include <QProcess>
0013 
0014 #include <QDBusConnection>
0015 #include <QDBusConnectionInterface>
0016 #include <QDBusPendingCallWatcher>
0017 #include <QMenu>
0018 #include <QQuickItem>
0019 #include <QQuickWindow>
0020 #include <QScreen>
0021 #include <QStandardItemModel>
0022 
0023 #include <Plasma/PluginLoader>
0024 #include <Plasma/ServiceJob>
0025 
0026 #include <KActionCollection>
0027 #include <KLocalizedString>
0028 
0029 #include <plasma_version.h>
0030 
0031 GroupingContainment::GroupingContainment(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
0032     : Plasma::Containment(parent, data, args)
0033 {
0034     setHasConfigurationInterface(true);
0035     setContainmentType(Plasma::Types::CustomEmbeddedContainment);
0036 }
0037 
0038 GroupingContainment::~GroupingContainment()
0039 {
0040 }
0041 
0042 void GroupingContainment::init()
0043 {
0044     Containment::init();
0045 }
0046 
0047 void GroupingContainment::newTask(const QString &task)
0048 {
0049     createApplet(task, QVariantList() << QStringLiteral("org.kde.plasma:force-create"));
0050 }
0051 
0052 void GroupingContainment::cleanupTask(const QString &task)
0053 {
0054     const auto appletList = applets();
0055     for (Plasma::Applet *applet : appletList) {
0056         if (!applet->pluginMetaData().isValid() || task == applet->pluginMetaData().pluginId()) {
0057             applet->destroy();
0058         }
0059     }
0060 }
0061 
0062 void GroupingContainment::showPlasmoidMenu(QQuickItem *appletInterface, int x, int y)
0063 {
0064     if (!appletInterface) {
0065         return;
0066     }
0067 
0068     Plasma::Applet *applet = appletInterface->property("_plasma_applet").value<Plasma::Applet *>();
0069 
0070     QPointF pos = appletInterface->mapToScene(QPointF(x, y));
0071 
0072     if (appletInterface->window() && appletInterface->window()->screen()) {
0073         pos = appletInterface->window()->mapToGlobal(pos.toPoint());
0074     } else {
0075         pos = QPoint();
0076     }
0077 
0078     QMenu *desktopMenu = new QMenu;
0079     connect(this, &QObject::destroyed, desktopMenu, &QMenu::close);
0080     desktopMenu->setAttribute(Qt::WA_DeleteOnClose);
0081 
0082     Q_EMIT applet->contextualActionsAboutToShow();
0083     const QList<QAction *> actions = applet->contextualActions();
0084     for (QAction *action : actions) {
0085         if (action) {
0086             desktopMenu->addAction(action);
0087         }
0088     }
0089 
0090     // add run associated action/ remove / alternatives
0091     desktopMenu->addActions(applet->actions()->actions());
0092 
0093     if (desktopMenu->isEmpty()) {
0094         delete desktopMenu;
0095         return;
0096     }
0097 
0098     desktopMenu->adjustSize();
0099 
0100     if (QScreen *screen = appletInterface->window()->screen()) {
0101         const QRect geo = screen->availableGeometry();
0102 
0103         pos =
0104             QPoint(qBound(geo.left(), (int)pos.x(), geo.right() - desktopMenu->width()), qBound(geo.top(), (int)pos.y(), geo.bottom() - desktopMenu->height()));
0105     }
0106 
0107     desktopMenu->popup(pos.toPoint());
0108 }
0109 
0110 QPointF GroupingContainment::popupPosition(QQuickItem *visualParent, int x, int y)
0111 {
0112     if (!visualParent) {
0113         return QPointF(0, 0);
0114     }
0115 
0116     QPointF pos = visualParent->mapToScene(QPointF(x, y));
0117 
0118     if (visualParent->window() && visualParent->window()->screen()) {
0119         pos = visualParent->window()->mapToGlobal(pos.toPoint());
0120     } else {
0121         return QPoint();
0122     }
0123     return pos;
0124 }
0125 
0126 void GroupingContainment::reorderItemBefore(QQuickItem *before, QQuickItem *after)
0127 {
0128     if (!before || !after) {
0129         return;
0130     }
0131 
0132     before->setVisible(false);
0133     before->setParentItem(after->parentItem());
0134     before->stackBefore(after);
0135     before->setVisible(true);
0136 }
0137 
0138 void GroupingContainment::reorderItemAfter(QQuickItem *after, QQuickItem *before)
0139 {
0140     if (!before || !after) {
0141         return;
0142     }
0143 
0144     after->setVisible(false);
0145     after->setParentItem(before->parentItem());
0146     after->stackAfter(before);
0147     after->setVisible(true);
0148 }
0149 
0150 K_PLUGIN_CLASS(GroupingContainment)
0151 
0152 #include "groupingcontainment.moc"