File indexing completed on 2024-05-12 05:29:36

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 
0010 #include <QDebug>
0011 #include <QMenu>
0012 #include <QQuickItem>
0013 #include <QQuickWindow>
0014 #include <QScreen>
0015 #include <QStandardItemModel>
0016 
0017 #include <KActionCollection> // Applet::actions
0018 
0019 GroupingContainment::GroupingContainment(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
0020     : Plasma::Containment(parent, data, args)
0021 {
0022     setHasConfigurationInterface(true);
0023 }
0024 
0025 void GroupingContainment::newTask(const QString &task)
0026 {
0027     createApplet(task, QVariantList{QStringLiteral("org.kde.plasma:force-create")});
0028 }
0029 
0030 void GroupingContainment::cleanupTask(const QString &task)
0031 {
0032     const auto appletList = applets();
0033     for (Plasma::Applet *applet : appletList) {
0034         if (!applet->pluginMetaData().isValid() || task == applet->pluginMetaData().pluginId()) {
0035             applet->destroy();
0036         }
0037     }
0038 }
0039 
0040 void GroupingContainment::showPlasmoidMenu(QQuickItem *appletInterface, int x, int y)
0041 {
0042     if (!appletInterface) {
0043         return;
0044     }
0045 
0046     Plasma::Applet *applet = appletInterface->property("_plasma_applet").value<Plasma::Applet *>();
0047 
0048     QPointF pos = appletInterface->mapToScene(QPointF(x, y));
0049 
0050     if (appletInterface->window() && appletInterface->window()->screen()) {
0051         pos = appletInterface->window()->mapToGlobal(pos.toPoint());
0052     } else {
0053         pos = QPoint();
0054     }
0055 
0056     QMenu *desktopMenu = new QMenu;
0057     connect(this, &QObject::destroyed, desktopMenu, &QMenu::close);
0058     desktopMenu->setAttribute(Qt::WA_DeleteOnClose);
0059 
0060     Q_EMIT applet->contextualActionsAboutToShow();
0061     const QList<QAction *> actions = applet->contextualActions();
0062     for (QAction *action : actions) {
0063         if (action) {
0064             desktopMenu->addAction(action);
0065         }
0066     }
0067 
0068     // add run associated action/ remove / alternatives
0069     desktopMenu->addActions(applet->internalActions());
0070 
0071     if (desktopMenu->isEmpty()) {
0072         delete desktopMenu;
0073         return;
0074     }
0075 
0076     desktopMenu->adjustSize();
0077 
0078     if (QScreen *screen = appletInterface->window()->screen()) {
0079         const QRect geo = screen->availableGeometry();
0080 
0081         pos =
0082             QPoint(qBound(geo.left(), (int)pos.x(), geo.right() - desktopMenu->width()), qBound(geo.top(), (int)pos.y(), geo.bottom() - desktopMenu->height()));
0083     }
0084 
0085     desktopMenu->popup(pos.toPoint());
0086 }
0087 
0088 QPointF GroupingContainment::popupPosition(QQuickItem *visualParent, int x, int y)
0089 {
0090     if (!visualParent) {
0091         return QPointF(0, 0);
0092     }
0093 
0094     QPointF pos = visualParent->mapToScene(QPointF(x, y));
0095 
0096     if (visualParent->window() && visualParent->window()->screen()) {
0097         pos = visualParent->window()->mapToGlobal(pos.toPoint());
0098     } else {
0099         return QPoint();
0100     }
0101     return pos;
0102 }
0103 
0104 void GroupingContainment::reorderItemBefore(QQuickItem *before, QQuickItem *after)
0105 {
0106     if (!before || !after) {
0107         return;
0108     }
0109 
0110     before->setVisible(false);
0111     before->setParentItem(after->parentItem());
0112     before->stackBefore(after);
0113     before->setVisible(true);
0114 }
0115 
0116 void GroupingContainment::reorderItemAfter(QQuickItem *after, QQuickItem *before)
0117 {
0118     if (!before || !after) {
0119         return;
0120     }
0121 
0122     after->setVisible(false);
0123     after->setParentItem(before->parentItem());
0124     after->stackAfter(before);
0125     after->setVisible(true);
0126 }
0127 
0128 K_PLUGIN_CLASS(GroupingContainment)
0129 
0130 #include "groupingcontainment.moc"