File indexing completed on 2024-05-12 05:37:16

0001 /*
0002     SPDX-FileCopyrightText: 2009 Chani Armitage <chani@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "paste.h"
0008 #include "containmentactions_paste_debug.h"
0009 
0010 #include <QClipboard>
0011 #include <QGuiApplication>
0012 #include <QMimeData>
0013 
0014 #include <Plasma/Containment>
0015 #include <PlasmaQuick/AppletQuickItem>
0016 
0017 #include <QAction>
0018 #include <QDebug>
0019 
0020 Paste::Paste(QObject *parent, const QVariantList &args)
0021     : Plasma::ContainmentActions(parent, args)
0022     , m_action(new QAction(this))
0023 {
0024     QObject::connect(m_action, &QAction::triggered, this, &Paste::doPaste);
0025 }
0026 
0027 QList<QAction *> Paste::contextualActions()
0028 {
0029     const QList<QAction *> actions{m_action};
0030 
0031     return actions;
0032 }
0033 
0034 void Paste::doPaste()
0035 {
0036     qCWarning(CONTAINMENTACTIONS_PASTE_DEBUG) << "Paste at" << m_action->data();
0037 
0038     if (!m_action->data().canConvert<QPoint>()) {
0039         return;
0040     }
0041 
0042     QPoint pos = m_action->data().value<QPoint>();
0043     Plasma::Containment *c = containment();
0044     Q_ASSERT(c);
0045 
0046     // get the actual graphic object of the containment
0047     QObject *graphicObject = PlasmaQuick::AppletQuickItem::itemForApplet(c);
0048     if (!graphicObject) {
0049         return;
0050     }
0051 
0052     QClipboard *clipboard = QGuiApplication::clipboard();
0053     // FIXME: can be the const_cast avoided?
0054     QMimeData *mimeData = const_cast<QMimeData *>(clipboard->mimeData(QClipboard::Selection));
0055     // TODO if that's not supported (ie non-linux) should we try clipboard instead of selection?
0056 
0057     graphicObject->metaObject()
0058         ->invokeMethod(graphicObject, "processMimeData", Qt::DirectConnection, Q_ARG(QMimeData *, mimeData), Q_ARG(int, pos.x()), Q_ARG(int, pos.y()));
0059 }
0060 
0061 K_PLUGIN_CLASS_WITH_JSON(Paste, "plasma-containmentactions-paste.json")
0062 
0063 #include "paste.moc"