| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qwaylanddnd_p.h" |
| 5 | |
| 6 | #include "qwaylanddatadevice_p.h" |
| 7 | #include "qwaylanddatadevicemanager_p.h" |
| 8 | #include "qwaylanddataoffer_p.h" |
| 9 | #include "qwaylandinputdevice_p.h" |
| 10 | #include "qwaylanddisplay_p.h" |
| 11 | |
| 12 | #include <QtGui/private/qshapedpixmapdndwindow_p.h> |
| 13 | |
| 14 | #include <QDebug> |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | #if QT_CONFIG(draganddrop) |
| 18 | namespace QtWaylandClient { |
| 19 | |
| 20 | QWaylandDrag::QWaylandDrag(QWaylandDisplay *display) |
| 21 | : m_display(display) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | QWaylandDrag::~QWaylandDrag() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | void QWaylandDrag::startDrag() |
| 30 | { |
| 31 | // Some compositors do not send a pointer leave before starting a drag, some do. |
| 32 | // This is discussed upstream at: https://gitlab.freedesktop.org/wayland/wayland/-/issues/444 |
| 33 | // For consistency between compositors we emit the leave event here, upon drag start. |
| 34 | m_display->currentInputDevice()->handleStartDrag(); |
| 35 | |
| 36 | QBasicDrag::startDrag(); |
| 37 | QWaylandWindow *icon = static_cast<QWaylandWindow *>(shapedPixmapWindow()->handle()); |
| 38 | if (m_display->currentInputDevice()->dataDevice()->startDrag(mimeData: drag()->mimeData(), supportedActions: drag()->supportedActions(), icon)) { |
| 39 | icon->addAttachOffset(point: -drag()->hotSpot()); |
| 40 | } else { |
| 41 | // Cancelling immediately does not work, since the event loop for QDrag::exec is started |
| 42 | // after this function returns. |
| 43 | QMetaObject::invokeMethod(object: this, function: [this](){ cancelDrag(); }, type: Qt::QueuedConnection); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void QWaylandDrag::cancel() |
| 48 | { |
| 49 | QBasicDrag::cancel(); |
| 50 | |
| 51 | m_display->currentInputDevice()->dataDevice()->cancelDrag(); |
| 52 | |
| 53 | if (drag()) |
| 54 | drag()->deleteLater(); |
| 55 | } |
| 56 | |
| 57 | void QWaylandDrag::move(const QPoint &globalPos, Qt::MouseButtons b, Qt::KeyboardModifiers mods) |
| 58 | { |
| 59 | Q_UNUSED(globalPos); |
| 60 | Q_UNUSED(b); |
| 61 | Q_UNUSED(mods); |
| 62 | // Do nothing |
| 63 | } |
| 64 | |
| 65 | void QWaylandDrag::drop(const QPoint &globalPos, Qt::MouseButtons b, Qt::KeyboardModifiers mods) |
| 66 | { |
| 67 | QBasicDrag::drop(globalPos, b, mods); |
| 68 | } |
| 69 | |
| 70 | void QWaylandDrag::endDrag() |
| 71 | { |
| 72 | m_display->currentInputDevice()->handleEndDrag(); |
| 73 | } |
| 74 | |
| 75 | void QWaylandDrag::setResponse(bool accepted) |
| 76 | { |
| 77 | // This method is used for old DataDevices where the drag action is not communicated |
| 78 | Qt::DropAction action = defaultAction(possibleActions: drag()->supportedActions(), modifiers: m_display->currentInputDevice()->modifiers()); |
| 79 | setResponse(QPlatformDropQtResponse(accepted, action)); |
| 80 | } |
| 81 | |
| 82 | void QWaylandDrag::setResponse(const QPlatformDropQtResponse &response) |
| 83 | { |
| 84 | setCanDrop(response.isAccepted()); |
| 85 | |
| 86 | if (canDrop()) { |
| 87 | updateCursor(action: response.acceptedAction()); |
| 88 | } else { |
| 89 | updateCursor(action: Qt::IgnoreAction); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void QWaylandDrag::setDropResponse(const QPlatformDropQtResponse &response) |
| 94 | { |
| 95 | setExecutedDropAction(response.acceptedAction()); |
| 96 | } |
| 97 | |
| 98 | void QWaylandDrag::finishDrag() |
| 99 | { |
| 100 | QKeyEvent event(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier); |
| 101 | eventFilter(o: shapedPixmapWindow(), e: &event); |
| 102 | |
| 103 | if (drag()) |
| 104 | drag()->deleteLater(); |
| 105 | } |
| 106 | |
| 107 | bool QWaylandDrag::ownsDragObject() const |
| 108 | { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | #endif // draganddrop |
| 114 | QT_END_NAMESPACE |
| 115 | |