File indexing completed on 2024-05-12 17:08:58

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 
0016 #include <QAction>
0017 #include <QDebug>
0018 
0019 Paste::Paste(QObject *parent, const QVariantList &args)
0020     : Plasma::ContainmentActions(parent, args)
0021     , m_action(new QAction(this))
0022 {
0023     QObject::connect(m_action, &QAction::triggered, this, &Paste::doPaste);
0024 }
0025 
0026 QList<QAction *> Paste::contextualActions()
0027 {
0028     const QList<QAction *> actions{m_action};
0029 
0030     return actions;
0031 }
0032 
0033 void Paste::doPaste()
0034 {
0035     qCWarning(CONTAINMENTACTIONS_PASTE_DEBUG) << "Paste at" << m_action->data();
0036 
0037     if (!m_action->data().canConvert<QPoint>()) {
0038         return;
0039     }
0040 
0041     QPoint pos = m_action->data().value<QPoint>();
0042     Plasma::Containment *c = containment();
0043     Q_ASSERT(c);
0044 
0045     // get the actual graphic object of the containment
0046     QObject *graphicObject = c->property("_plasma_graphicObject").value<QObject *>();
0047     if (!graphicObject) {
0048         return;
0049     }
0050 
0051     QClipboard *clipboard = QGuiApplication::clipboard();
0052     // FIXME: can be the const_cast avoided?
0053     QMimeData *mimeData = const_cast<QMimeData *>(clipboard->mimeData(QClipboard::Selection));
0054     // TODO if that's not supported (ie non-linux) should we try clipboard instead of selection?
0055 
0056     graphicObject->metaObject()
0057         ->invokeMethod(graphicObject, "processMimeData", Qt::DirectConnection, Q_ARG(QMimeData *, mimeData), Q_ARG(int, pos.x()), Q_ARG(int, pos.y()));
0058 }
0059 
0060 K_PLUGIN_CLASS_WITH_JSON(Paste, "plasma-containmentactions-paste.json")
0061 
0062 #include "paste.moc"