summaryrefslogtreecommitdiffstats
path: root/src/gui/kernel/qsimpledrag.cpp
blob: 2ca2eca15be78999a44f2947c5a9bf64d7d83cfe (plain)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
// Copyright (C) 2016 The Qt Company Ltd.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only#include"qsimpledrag_p.h"#include"qbitmap.h"#include"qdrag.h"#include"qpixmap.h"#include"qevent.h"#include"qfile.h"#include"qguiapplication.h"#include"qpoint.h"#include"qbuffer.h"#include"qimage.h"#include"qdir.h"#include"qimagereader.h"#include"qimagewriter.h"#include"qplatformscreen.h"#include"qplatformwindow.h"#include <QtCore/QEventLoop>#include <QtCore/QDebug>#include <QtCore/QLoggingCategory>#include <private/qguiapplication_p.h>#include <private/qdnd_p.h>#include <private/qshapedpixmapdndwindow_p.h>#include <private/qhighdpiscaling_p.h> QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcDnd,"qt.gui.dnd")static QWindow*topLevelAt(const QPoint &pos){const QWindowList list =QGuiApplication::topLevelWindows();constauto crend = list.crend();for(auto it = list.crbegin(); it != crend; ++it) { QWindow *w = *it;if(w->isVisible() && w->handle() && w->geometry().contains(pos) && !qobject_cast<QShapedPixmapWindow*>(w))return w;}returnnullptr;}/*! \class QBasicDrag \brief QBasicDrag is a base class for implementing platform drag and drop. \since 5.0 \internal \ingroup qpa QBasicDrag implements QPlatformDrag::drag() by running a local event loop in which it tracks mouse movements and moves the drag icon (QShapedPixmapWindow) accordingly. It provides new virtuals allowing for querying whether the receiving window (within the Qt application or outside) accepts the drag and sets the state accordingly.*/QBasicDrag::QBasicDrag(){}QBasicDrag::~QBasicDrag(){delete m_drag_icon_window;}voidQBasicDrag::enableEventFilter(){ qApp->installEventFilter(this);}voidQBasicDrag::disableEventFilter(){ qApp->removeEventFilter(this);}staticinline QPoint getNativeMousePos(QEvent *e, QWindow *window){returnQHighDpi::toNativePixels(static_cast<QMouseEvent *>(e)->globalPosition().toPoint(), window);}boolQBasicDrag::eventFilter(QObject *o, QEvent *e){Q_UNUSED(o);if(!m_drag) {if(e->type() ==QEvent::KeyRelease &&static_cast<QKeyEvent*>(e)->key() ==Qt::Key_Escape) {disableEventFilter();exitDndEventLoop();return true;// block the key release}return false;}switch(e->type()) {caseQEvent::ShortcutOverride:// prevent accelerators from firing while dragging e->accept();return true;caseQEvent::KeyPress:caseQEvent::KeyRelease:{ QKeyEvent *ke =static_cast<QKeyEvent *>(e);if(ke->key() ==Qt::Key_Escape && e->type() ==QEvent::KeyPress) {cancel();disableEventFilter();exitDndEventLoop();}else if(ke->modifiers() !=QGuiApplication::keyboardModifiers()) {move(m_lastPos,QGuiApplication::mouseButtons(), ke->modifiers());}return true;// Eat all key events}caseQEvent::MouseMove:{ m_lastPos =getNativeMousePos(e, m_drag_icon_window);auto mouseMove =static_cast<QMouseEvent *>(e);move(m_lastPos, mouseMove->buttons(), mouseMove->modifiers());return true;// Eat all mouse move events}caseQEvent::MouseButtonRelease:{ QPointer<QObject>objGuard(o);disableEventFilter();if(canDrop()) { QPoint nativePosition =getNativeMousePos(e, m_drag_icon_window);auto mouseRelease =static_cast<QMouseEvent *>(e);drop(nativePosition, mouseRelease->buttons(), mouseRelease->modifiers());}else{cancel();}exitDndEventLoop();if(!objGuard)return true;// If a QShapedPixmapWindow (drag feedback) is being dragged along, the// mouse event's localPos() will be relative to that, which is useless.// We want a position relative to the window where the drag ends, if possible (?).// If there is no such window (belonging to this Qt application),// make the event relative to the window where the drag started. (QTBUG-66103)const QMouseEvent *release =static_cast<QMouseEvent *>(e);const QWindow *releaseWindow =topLevelAt(release->globalPosition().toPoint());qCDebug(lcDnd) <<"mouse released over"<< releaseWindow <<"after drag from"<< m_sourceWindow <<"globalPos"<< release->globalPosition().toPoint();if(!releaseWindow) releaseWindow = m_sourceWindow; QPointF releaseWindowPos = (releaseWindow ? releaseWindow->mapFromGlobal(release->globalPosition()) : release->globalPosition()); QMouseEvent *newRelease =newQMouseEvent(release->type(), releaseWindowPos, releaseWindowPos, release->globalPosition(), release->button(), release->buttons(), release->modifiers(), release->source(), release->pointingDevice());QCoreApplication::postEvent(o, newRelease);return true;// defer mouse release events until drag event loop has returned}caseQEvent::MouseButtonDblClick:caseQEvent::Wheel:return true;default:break;}return false;}Qt::DropAction QBasicDrag::drag(QDrag *o){ m_drag = o; m_executed_drop_action =Qt::IgnoreAction; m_can_drop =false;startDrag(); m_eventLoop =new QEventLoop; m_eventLoop->exec();delete m_eventLoop; m_eventLoop =nullptr; m_drag =nullptr;endDrag();return m_executed_drop_action;}voidQBasicDrag::cancelDrag(){if(m_eventLoop) {cancel(); m_eventLoop->quit();}}voidQBasicDrag::startDrag(){ QPoint pos;#ifndef QT_NO_CURSOR pos =QCursor::pos();staticconstexpr QGuiApplicationPrivate::QLastCursorPosition uninitializedCursorPosition;if(pos == uninitializedCursorPosition) {// ### fixme: no mouse pos registered. Get pos from touch... pos =QPoint();}#endif m_lastPos = pos;recreateShapedPixmapWindow(m_screen, pos);enableEventFilter();}voidQBasicDrag::endDrag(){}voidQBasicDrag::recreateShapedPixmapWindow(QScreen *screen,const QPoint &pos){delete m_drag_icon_window;// ### TODO Check if its really necessary to have m_drag_icon_window// when QDrag is used without a pixmap - QDrag::setPixmap() m_drag_icon_window =newQShapedPixmapWindow(screen); m_drag_icon_window->setUseCompositing(m_useCompositing); m_drag_icon_window->setPixmap(m_drag->pixmap()); m_drag_icon_window->setHotspot(m_drag->hotSpot()); m_drag_icon_window->updateGeometry(pos); m_drag_icon_window->setVisible(true);}voidQBasicDrag::cancel(){disableEventFilter();restoreCursor(); m_drag_icon_window->setVisible(false);}/*! Move the drag label to \a globalPos, which is interpreted in device independent coordinates. Typically called from reimplementations of move(). */voidQBasicDrag::moveShapedPixmapWindow(const QPoint &globalPos){if(m_drag) m_drag_icon_window->updateGeometry(globalPos);}voidQBasicDrag::drop(const QPoint &,Qt::MouseButtons,Qt::KeyboardModifiers){disableEventFilter();restoreCursor(); m_drag_icon_window->setVisible(false);}voidQBasicDrag::exitDndEventLoop(){if(m_eventLoop && m_eventLoop->isRunning()) m_eventLoop->exit();}voidQBasicDrag::updateCursor(Qt::DropAction action){#ifndef QT_NO_CURSORQt::CursorShape cursorShape =Qt::ForbiddenCursor;if(canDrop()) {switch(action) {caseQt::CopyAction: cursorShape =Qt::DragCopyCursor;break;caseQt::LinkAction: cursorShape =Qt::DragLinkCursor;break;default: cursorShape =Qt::DragMoveCursor;break;}} QPixmap pixmap = m_drag->dragCursor(action);if(!m_dndHasSetOverrideCursor) { QCursor newCursor = !pixmap.isNull() ?QCursor(pixmap) :QCursor(cursorShape);QGuiApplication::setOverrideCursor(newCursor); m_dndHasSetOverrideCursor =true;}else{ QCursor *cursor =QGuiApplication::overrideCursor();if(!cursor) {QGuiApplication::changeOverrideCursor(pixmap.isNull() ?QCursor(cursorShape) :QCursor(pixmap));}else{if(!pixmap.isNull()) {if(cursor->pixmap().cacheKey() != pixmap.cacheKey())QGuiApplication::changeOverrideCursor(QCursor(pixmap));}else if(cursorShape != cursor->shape()) {QGuiApplication::changeOverrideCursor(QCursor(cursorShape));}}}#endifupdateAction(action);}voidQBasicDrag::restoreCursor(){#ifndef QT_NO_CURSORif(m_dndHasSetOverrideCursor) {QGuiApplication::restoreOverrideCursor(); m_dndHasSetOverrideCursor =false;}#endif}staticinline QPoint fromNativeGlobalPixels(const QPoint &point){#ifndef QT_NO_HIGHDPISCALING QPoint res = point;if(QHighDpiScaling::isActive()) {for(const QScreen *s :std::as_const(QGuiApplicationPrivate::screen_list)) {if(s->handle()->geometry().contains(point)) { res =QHighDpi::fromNativePixels(point, s);break;}}}return res;#elsereturn point;#endif}/*! \class QSimpleDrag \brief QSimpleDrag implements QBasicDrag for Drag and Drop operations within the Qt Application itself. \since 5.0 \internal \ingroup qpa The class checks whether the receiving window is a window of the Qt application and sets the state accordingly. It does not take windows of other applications into account.*/QSimpleDrag::QSimpleDrag(){}voidQSimpleDrag::startDrag(){setExecutedDropAction(Qt::IgnoreAction);QBasicDrag::startDrag();// Here we can be fairly sure that QGuiApplication::mouseButtons/keyboardModifiers() will// contain sensible values as startDrag() normally is called from mouse event handlers// by QDrag::exec(). A better API would be if we could pass something like "input device// pointer" to QDrag::exec(). My guess is that something like that might be required for// QTBUG-52430. m_sourceWindow =topLevelAt(QCursor::pos()); m_windowUnderCursor = m_sourceWindow;if(m_sourceWindow) {auto nativePixelPos =QHighDpi::toNativePixels(QCursor::pos(), m_sourceWindow);move(nativePixelPos,QGuiApplication::mouseButtons(),QGuiApplication::keyboardModifiers());}else{setCanDrop(false);updateCursor(Qt::IgnoreAction);}qCDebug(lcDnd) <<"drag began from"<< m_sourceWindow <<"cursor pos"<<QCursor::pos() <<"can drop?"<<canDrop();}static voidsendDragLeave(QWindow *window){QWindowSystemInterface::handleDrag(window,nullptr,QPoint(),Qt::IgnoreAction, { }, { });}voidQSimpleDrag::cancel(){QBasicDrag::cancel();if(drag() && m_sourceWindow) {sendDragLeave(m_sourceWindow); m_sourceWindow =nullptr;}}voidQSimpleDrag::move(const QPoint &nativeGlobalPos,Qt::MouseButtons buttons,Qt::KeyboardModifiers modifiers){ QPoint globalPos =fromNativeGlobalPixels(nativeGlobalPos);moveShapedPixmapWindow(globalPos); QWindow *window =topLevelAt(globalPos);if(!window || window != m_windowUnderCursor) {if(m_windowUnderCursor)sendDragLeave(m_windowUnderCursor); m_windowUnderCursor = window;if(!window) {// QSimpleDrag supports only in-process dnd, we can't drop anywhere else.setCanDrop(false);updateCursor(Qt::IgnoreAction);return;}}const QPoint pos = nativeGlobalPos - window->handle()->geometry().topLeft();const QPlatformDragQtResponse qt_response =QWindowSystemInterface::handleDrag( window,drag()->mimeData(), pos,drag()->supportedActions(), buttons, modifiers);setCanDrop(qt_response.isAccepted());updateCursor(qt_response.acceptedAction());}voidQSimpleDrag::drop(const QPoint &nativeGlobalPos,Qt::MouseButtons buttons,Qt::KeyboardModifiers modifiers){ QPoint globalPos =fromNativeGlobalPixels(nativeGlobalPos);QBasicDrag::drop(nativeGlobalPos, buttons, modifiers); QWindow *window =topLevelAt(globalPos);if(!window)return;const QPoint pos = nativeGlobalPos - window->handle()->geometry().topLeft();const QPlatformDropQtResponse response =QWindowSystemInterface::handleDrop( window,drag()->mimeData(), pos,drag()->supportedActions(), buttons, modifiers);if(response.isAccepted()) {setExecutedDropAction(response.acceptedAction());}else{setExecutedDropAction(Qt::IgnoreAction);}} QT_END_NAMESPACE 
close