/src/qt/qtbase/src/gui/kernel/qsimpledrag.cpp
Line | Count | Source |
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 | | // Qt-Security score:significant reason:default |
4 | | |
5 | | #include "qsimpledrag_p.h" |
6 | | |
7 | | #include "qbitmap.h" |
8 | | #include "qdrag.h" |
9 | | #include "qpixmap.h" |
10 | | #include "qevent.h" |
11 | | #include "qfile.h" |
12 | | #include "qguiapplication.h" |
13 | | #include "qpoint.h" |
14 | | #include "qbuffer.h" |
15 | | #include "qimage.h" |
16 | | #include "qdir.h" |
17 | | #include "qimagereader.h" |
18 | | #include "qimagewriter.h" |
19 | | #include "qplatformscreen.h" |
20 | | #include "qplatformwindow.h" |
21 | | |
22 | | #include <QtCore/QEventLoop> |
23 | | #include <QtCore/QDebug> |
24 | | #include <QtCore/QLoggingCategory> |
25 | | |
26 | | #include <private/qguiapplication_p.h> |
27 | | #include <private/qdnd_p.h> |
28 | | |
29 | | #include <private/qshapedpixmapdndwindow_p.h> |
30 | | #include <private/qhighdpiscaling_p.h> |
31 | | |
32 | | QT_BEGIN_NAMESPACE |
33 | | |
34 | | Q_STATIC_LOGGING_CATEGORY(lcDnd, "qt.gui.dnd") |
35 | | |
36 | | static QWindow* topLevelAt(const QPoint &pos) |
37 | 0 | { |
38 | 0 | const QWindowList list = QGuiApplication::topLevelWindows(); |
39 | 0 | const auto crend = list.crend(); |
40 | 0 | for (auto it = list.crbegin(); it != crend; ++it) { |
41 | 0 | QWindow *w = *it; |
42 | 0 | if (w->isVisible() && w->handle() && w->geometry().contains(pos) && !qobject_cast<QShapedPixmapWindow*>(w)) |
43 | 0 | return w; |
44 | 0 | } |
45 | 0 | return nullptr; |
46 | 0 | } |
47 | | |
48 | | /*! |
49 | | \class QBasicDrag |
50 | | \brief QBasicDrag is a base class for implementing platform drag and drop. |
51 | | \since 5.0 |
52 | | \internal |
53 | | \ingroup qpa |
54 | | |
55 | | QBasicDrag implements QPlatformDrag::drag() by running a local event loop in which |
56 | | it tracks mouse movements and moves the drag icon (QShapedPixmapWindow) accordingly. |
57 | | It provides new virtuals allowing for querying whether the receiving window |
58 | | (within the Qt application or outside) accepts the drag and sets the state accordingly. |
59 | | */ |
60 | | |
61 | | QBasicDrag::QBasicDrag() |
62 | 0 | { |
63 | 0 | } |
64 | | |
65 | | QBasicDrag::~QBasicDrag() |
66 | 0 | { |
67 | 0 | delete m_drag_icon_window; |
68 | 0 | } |
69 | | |
70 | | void QBasicDrag::enableEventFilter() |
71 | 0 | { |
72 | 0 | qApp->installEventFilter(this); |
73 | 0 | } |
74 | | |
75 | | void QBasicDrag::disableEventFilter() |
76 | 0 | { |
77 | 0 | qApp->removeEventFilter(this); |
78 | 0 | } |
79 | | |
80 | | |
81 | | static inline QPoint getNativeMousePos(QEvent *e, QWindow *window) |
82 | 0 | { |
83 | 0 | return QHighDpi::toNativePixels(static_cast<QMouseEvent *>(e)->globalPosition().toPoint(), window); |
84 | 0 | } |
85 | | |
86 | | bool QBasicDrag::eventFilter(QObject *o, QEvent *e) |
87 | 0 | { |
88 | 0 | Q_UNUSED(o); |
89 | |
|
90 | 0 | if (!m_drag) { |
91 | 0 | if (e->type() == QEvent::KeyRelease && static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) { |
92 | 0 | disableEventFilter(); |
93 | 0 | exitDndEventLoop(); |
94 | 0 | return true; // block the key release |
95 | 0 | } |
96 | 0 | return false; |
97 | 0 | } |
98 | | |
99 | 0 | switch (e->type()) { |
100 | 0 | case QEvent::ShortcutOverride: |
101 | | // prevent accelerators from firing while dragging |
102 | 0 | e->accept(); |
103 | 0 | return true; |
104 | | |
105 | 0 | case QEvent::KeyPress: |
106 | 0 | case QEvent::KeyRelease: |
107 | 0 | { |
108 | 0 | QKeyEvent *ke = static_cast<QKeyEvent *>(e); |
109 | 0 | if (ke->key() == Qt::Key_Escape && e->type() == QEvent::KeyPress) { |
110 | 0 | cancel(); |
111 | 0 | disableEventFilter(); |
112 | 0 | exitDndEventLoop(); |
113 | |
|
114 | 0 | } else if (ke->modifiers() != QGuiApplication::keyboardModifiers()) { |
115 | 0 | move(m_lastPos, QGuiApplication::mouseButtons(), ke->modifiers()); |
116 | 0 | } |
117 | 0 | return true; // Eat all key events |
118 | 0 | } |
119 | | |
120 | 0 | case QEvent::MouseMove: |
121 | 0 | { |
122 | 0 | m_lastPos = getNativeMousePos(e, m_drag_icon_window); |
123 | 0 | auto mouseMove = static_cast<QMouseEvent *>(e); |
124 | 0 | move(m_lastPos, mouseMove->buttons(), mouseMove->modifiers()); |
125 | 0 | return true; // Eat all mouse move events |
126 | 0 | } |
127 | 0 | case QEvent::MouseButtonRelease: |
128 | 0 | { |
129 | 0 | QPointer<QObject> objGuard(o); |
130 | 0 | disableEventFilter(); |
131 | 0 | if (canDrop()) { |
132 | 0 | QPoint nativePosition = getNativeMousePos(e, m_drag_icon_window); |
133 | 0 | auto mouseRelease = static_cast<QMouseEvent *>(e); |
134 | 0 | drop(nativePosition, mouseRelease->buttons(), mouseRelease->modifiers()); |
135 | 0 | } else { |
136 | 0 | cancel(); |
137 | 0 | } |
138 | 0 | exitDndEventLoop(); |
139 | 0 | if (!objGuard) |
140 | 0 | return true; |
141 | | |
142 | | // If a QShapedPixmapWindow (drag feedback) is being dragged along, the |
143 | | // mouse event's localPos() will be relative to that, which is useless. |
144 | | // We want a position relative to the window where the drag ends, if possible (?). |
145 | | // If there is no such window (belonging to this Qt application), |
146 | | // make the event relative to the window where the drag started. (QTBUG-66103) |
147 | 0 | const QMouseEvent *release = static_cast<QMouseEvent *>(e); |
148 | 0 | const QWindow *releaseWindow = topLevelAt(release->globalPosition().toPoint()); |
149 | 0 | qCDebug(lcDnd) << "mouse released over" << releaseWindow << "after drag from" << m_sourceWindow << "globalPos" << release->globalPosition().toPoint(); |
150 | 0 | if (!releaseWindow) |
151 | 0 | releaseWindow = m_sourceWindow; |
152 | 0 | QPointF releaseWindowPos = (releaseWindow ? releaseWindow->mapFromGlobal(release->globalPosition()) : release->globalPosition()); |
153 | 0 | QMouseEvent *newRelease = new QMouseEvent(release->type(), |
154 | 0 | releaseWindowPos, releaseWindowPos, release->globalPosition(), |
155 | 0 | release->button(), release->buttons(), |
156 | 0 | release->modifiers(), release->source(), release->pointingDevice()); |
157 | 0 | QCoreApplication::postEvent(o, newRelease); |
158 | 0 | return true; // defer mouse release events until drag event loop has returned |
159 | 0 | } |
160 | 0 | case QEvent::MouseButtonDblClick: |
161 | 0 | case QEvent::Wheel: |
162 | 0 | return true; |
163 | 0 | default: |
164 | 0 | break; |
165 | 0 | } |
166 | 0 | return false; |
167 | 0 | } |
168 | | |
169 | | Qt::DropAction QBasicDrag::drag(QDrag *o) |
170 | 0 | { |
171 | 0 | m_drag = o; |
172 | 0 | m_executed_drop_action = Qt::IgnoreAction; |
173 | 0 | m_can_drop = false; |
174 | | |
175 | | // Create the loop before starting the drag. startDrag() installs the event filter |
176 | | // and delivers the first drag events, so the drag can already be cancelled, |
177 | | // dropped or destroyed before we get here, and exitDndEventLoop() and cancelDrag() |
178 | | // both no-op while m_eventLoop is null. QEventLoop::exec() clears any pending exit |
179 | | // when it starts, so we need a flag to avoid losing track of an early exit. |
180 | 0 | m_dragEndRequested = false; |
181 | 0 | m_eventLoop = new QEventLoop; |
182 | 0 | startDrag(); |
183 | 0 | qCDebug(lcDnd) << "entering drag event loop; drag" << m_drag |
184 | 0 | << "already ended?" << m_dragEndRequested; |
185 | 0 | if (m_drag && !m_dragEndRequested) |
186 | 0 | m_eventLoop->exec(); |
187 | 0 | qCDebug(lcDnd) << "left drag event loop; action" << m_executed_drop_action; |
188 | 0 | delete m_eventLoop; |
189 | 0 | m_eventLoop = nullptr; |
190 | 0 | m_drag = nullptr; |
191 | 0 | endDrag(); |
192 | |
|
193 | 0 | return m_executed_drop_action; |
194 | 0 | } |
195 | | |
196 | | void QBasicDrag::cancelDrag() |
197 | 0 | { |
198 | 0 | qCDebug(lcDnd) << "cancelling drag"; |
199 | 0 | if (m_eventLoop) { |
200 | 0 | cancel(); |
201 | 0 | m_dragEndRequested = true; |
202 | 0 | m_eventLoop->quit(); |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | void QBasicDrag::startDrag() |
207 | 0 | { |
208 | 0 | QPoint pos; |
209 | 0 | #ifndef QT_NO_CURSOR |
210 | 0 | pos = QCursor::pos(); |
211 | 0 | static constexpr QGuiApplicationPrivate::QLastCursorPosition uninitializedCursorPosition; |
212 | 0 | if (pos == uninitializedCursorPosition) { |
213 | | // ### fixme: no mouse pos registered. Get pos from touch... |
214 | 0 | pos = QPoint(); |
215 | 0 | } |
216 | 0 | #endif |
217 | 0 | m_lastPos = pos; |
218 | 0 | recreateShapedPixmapWindow(m_screen, pos); |
219 | 0 | enableEventFilter(); |
220 | 0 | } |
221 | | |
222 | | void QBasicDrag::endDrag() |
223 | 0 | { |
224 | 0 | } |
225 | | |
226 | | void QBasicDrag::recreateShapedPixmapWindow(QScreen *screen, const QPoint &pos) |
227 | 0 | { |
228 | 0 | delete m_drag_icon_window; |
229 | | // ### TODO Check if its really necessary to have m_drag_icon_window |
230 | | // when QDrag is used without a pixmap - QDrag::setPixmap() |
231 | 0 | m_drag_icon_window = new QShapedPixmapWindow(screen); |
232 | |
|
233 | 0 | m_drag_icon_window->setUseCompositing(m_useCompositing); |
234 | 0 | m_drag_icon_window->setPixmap(m_drag->pixmap()); |
235 | 0 | m_drag_icon_window->setHotspot(m_drag->hotSpot()); |
236 | 0 | m_drag_icon_window->updateGeometry(pos); |
237 | 0 | m_drag_icon_window->setVisible(true); |
238 | 0 | } |
239 | | |
240 | | void QBasicDrag::cancel() |
241 | 0 | { |
242 | 0 | disableEventFilter(); |
243 | 0 | restoreCursor(); |
244 | | // Can be reached before startDrag() has created the icon window, now that the event |
245 | | // loop exists for the duration of startDrag() too. |
246 | 0 | if (m_drag_icon_window) |
247 | 0 | m_drag_icon_window->setVisible(false); |
248 | 0 | } |
249 | | |
250 | | /*! |
251 | | Move the drag label to \a globalPos, which is |
252 | | interpreted in device independent coordinates. Typically called from reimplementations of move(). |
253 | | */ |
254 | | |
255 | | void QBasicDrag::moveShapedPixmapWindow(const QPoint &globalPos) |
256 | 0 | { |
257 | 0 | if (m_drag) |
258 | 0 | m_drag_icon_window->updateGeometry(globalPos); |
259 | 0 | } |
260 | | |
261 | | void QBasicDrag::drop(const QPoint &, Qt::MouseButtons, Qt::KeyboardModifiers) |
262 | 0 | { |
263 | 0 | disableEventFilter(); |
264 | 0 | restoreCursor(); |
265 | 0 | m_drag_icon_window->setVisible(false); |
266 | 0 | } |
267 | | |
268 | | void QBasicDrag::exitDndEventLoop() |
269 | 0 | { |
270 | 0 | m_dragEndRequested = true; |
271 | 0 | qCDebug(lcDnd) << "ending drag event loop; running?" |
272 | 0 | << (m_eventLoop && m_eventLoop->isRunning()); |
273 | 0 | if (m_eventLoop && m_eventLoop->isRunning()) |
274 | 0 | m_eventLoop->exit(); |
275 | 0 | } |
276 | | |
277 | | void QBasicDrag::updateCursor(Qt::DropAction action) |
278 | 0 | { |
279 | | // In case QDrag is destroyed from a drag event handler, cancel() has |
280 | | // already restored the cursor: nothing left to do. |
281 | 0 | if (!m_drag) |
282 | 0 | return; |
283 | | |
284 | 0 | #ifndef QT_NO_CURSOR |
285 | 0 | Qt::CursorShape cursorShape = Qt::ForbiddenCursor; |
286 | 0 | if (canDrop()) { |
287 | 0 | switch (action) { |
288 | 0 | case Qt::CopyAction: |
289 | 0 | cursorShape = Qt::DragCopyCursor; |
290 | 0 | break; |
291 | 0 | case Qt::LinkAction: |
292 | 0 | cursorShape = Qt::DragLinkCursor; |
293 | 0 | break; |
294 | 0 | default: |
295 | 0 | cursorShape = Qt::DragMoveCursor; |
296 | 0 | break; |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | 0 | QPixmap pixmap = m_drag->dragCursor(action); |
301 | |
|
302 | 0 | if (!m_dndHasSetOverrideCursor) { |
303 | 0 | QCursor newCursor = !pixmap.isNull() ? QCursor(pixmap) : QCursor(cursorShape); |
304 | 0 | QGuiApplication::setOverrideCursor(newCursor); |
305 | 0 | m_dndHasSetOverrideCursor = true; |
306 | 0 | } else { |
307 | 0 | QCursor *cursor = QGuiApplication::overrideCursor(); |
308 | 0 | if (!cursor) { |
309 | 0 | QGuiApplication::changeOverrideCursor(pixmap.isNull() ? QCursor(cursorShape) : QCursor(pixmap)); |
310 | 0 | } else { |
311 | 0 | if (!pixmap.isNull()) { |
312 | 0 | if (cursor->pixmap().cacheKey() != pixmap.cacheKey()) |
313 | 0 | QGuiApplication::changeOverrideCursor(QCursor(pixmap)); |
314 | 0 | } else if (cursorShape != cursor->shape()) { |
315 | 0 | QGuiApplication::changeOverrideCursor(QCursor(cursorShape)); |
316 | 0 | } |
317 | 0 | } |
318 | 0 | } |
319 | 0 | #endif |
320 | 0 | updateAction(action); |
321 | 0 | } |
322 | | |
323 | | void QBasicDrag::restoreCursor() |
324 | 0 | { |
325 | 0 | #ifndef QT_NO_CURSOR |
326 | 0 | if (m_dndHasSetOverrideCursor) { |
327 | 0 | QGuiApplication::restoreOverrideCursor(); |
328 | 0 | m_dndHasSetOverrideCursor = false; |
329 | 0 | } |
330 | 0 | #endif |
331 | 0 | } |
332 | | |
333 | | static inline QPoint fromNativeGlobalPixels(const QPoint &point) |
334 | 0 | { |
335 | 0 | #ifndef QT_NO_HIGHDPISCALING |
336 | 0 | QPoint res = point; |
337 | 0 | if (QHighDpiScaling::isActive()) { |
338 | 0 | for (const QScreen *s : std::as_const(QGuiApplicationPrivate::screen_list)) { |
339 | 0 | if (s->handle()->geometry().contains(point)) { |
340 | 0 | res = QHighDpi::fromNativePixels(point, s); |
341 | 0 | break; |
342 | 0 | } |
343 | 0 | } |
344 | 0 | } |
345 | 0 | return res; |
346 | | #else |
347 | | return point; |
348 | | #endif |
349 | 0 | } |
350 | | |
351 | | /*! |
352 | | \class QSimpleDrag |
353 | | \brief QSimpleDrag implements QBasicDrag for Drag and Drop operations within the Qt Application itself. |
354 | | \since 5.0 |
355 | | \internal |
356 | | \ingroup qpa |
357 | | |
358 | | The class checks whether the receiving window is a window of the Qt application |
359 | | and sets the state accordingly. It does not take windows of other applications |
360 | | into account. |
361 | | */ |
362 | | |
363 | | QSimpleDrag::QSimpleDrag() |
364 | 0 | { |
365 | 0 | } |
366 | | |
367 | | void QSimpleDrag::startDrag() |
368 | 0 | { |
369 | 0 | setExecutedDropAction(Qt::IgnoreAction); |
370 | |
|
371 | 0 | QBasicDrag::startDrag(); |
372 | | // Here we can be fairly sure that QGuiApplication::mouseButtons/keyboardModifiers() will |
373 | | // contain sensible values as startDrag() normally is called from mouse event handlers |
374 | | // by QDrag::exec(). A better API would be if we could pass something like "input device |
375 | | // pointer" to QDrag::exec(). My guess is that something like that might be required for |
376 | | // QTBUG-52430. |
377 | 0 | m_sourceWindow = topLevelAt(QCursor::pos()); |
378 | 0 | m_windowUnderCursor = m_sourceWindow; |
379 | 0 | if (m_sourceWindow) { |
380 | 0 | auto nativePixelPos = QHighDpi::toNativePixels(QCursor::pos(), m_sourceWindow); |
381 | 0 | move(nativePixelPos, QGuiApplication::mouseButtons(), QGuiApplication::keyboardModifiers()); |
382 | 0 | } else { |
383 | 0 | setCanDrop(false); |
384 | 0 | updateCursor(Qt::IgnoreAction); |
385 | 0 | } |
386 | |
|
387 | 0 | qCDebug(lcDnd) << "drag began from" << m_sourceWindow << "cursor pos" << QCursor::pos() << "can drop?" << canDrop(); |
388 | 0 | } |
389 | | |
390 | | static void sendDragLeave(QWindow *window) |
391 | 0 | { |
392 | 0 | QWindowSystemInterface::handleDrag(window, nullptr, QPoint(), Qt::IgnoreAction, { }, { }); |
393 | 0 | } |
394 | | |
395 | | void QSimpleDrag::cancel() |
396 | 0 | { |
397 | 0 | QBasicDrag::cancel(); |
398 | 0 | if (drag()) { |
399 | | // Leave the window the drag is currently over, which is only the window it |
400 | | // started from until the first move() onto another window. Leaving |
401 | | // m_sourceWindow instead would send it a second QDragLeaveEvent and leave the |
402 | | // window actually under the cursor believing a drag is still in progress. |
403 | 0 | if (m_windowUnderCursor) |
404 | 0 | sendDragLeave(m_windowUnderCursor); |
405 | 0 | m_windowUnderCursor = nullptr; |
406 | 0 | m_sourceWindow = nullptr; |
407 | 0 | } |
408 | 0 | } |
409 | | |
410 | | void QSimpleDrag::move(const QPoint &nativeGlobalPos, Qt::MouseButtons buttons, |
411 | | Qt::KeyboardModifiers modifiers) |
412 | 0 | { |
413 | 0 | if (!drag()) |
414 | 0 | return; |
415 | | |
416 | 0 | QPoint globalPos = fromNativeGlobalPixels(nativeGlobalPos); |
417 | 0 | moveShapedPixmapWindow(globalPos); |
418 | 0 | QWindow *window = topLevelAt(globalPos); |
419 | |
|
420 | 0 | if (!window || window != m_windowUnderCursor) { |
421 | 0 | if (m_windowUnderCursor) |
422 | 0 | sendDragLeave(m_windowUnderCursor); |
423 | 0 | m_windowUnderCursor = window; |
424 | 0 | if (!window) { |
425 | | // QSimpleDrag supports only in-process dnd, we can't drop anywhere else. |
426 | 0 | setCanDrop(false); |
427 | 0 | updateCursor(Qt::IgnoreAction); |
428 | 0 | return; |
429 | 0 | } |
430 | 0 | } |
431 | | |
432 | 0 | const QPoint pos = nativeGlobalPos - window->handle()->geometry().topLeft(); |
433 | 0 | const QPlatformDragQtResponse qt_response = QWindowSystemInterface::handleDrag( |
434 | 0 | window, drag()->mimeData(), pos, drag()->supportedActions(), |
435 | 0 | buttons, modifiers); |
436 | |
|
437 | 0 | setCanDrop(qt_response.isAccepted()); |
438 | 0 | updateCursor(qt_response.acceptedAction()); |
439 | 0 | } |
440 | | |
441 | | void QSimpleDrag::drop(const QPoint &nativeGlobalPos, Qt::MouseButtons buttons, |
442 | | Qt::KeyboardModifiers modifiers) |
443 | 0 | { |
444 | 0 | if (!drag()) |
445 | 0 | return; |
446 | | |
447 | 0 | QPoint globalPos = fromNativeGlobalPixels(nativeGlobalPos); |
448 | |
|
449 | 0 | QBasicDrag::drop(nativeGlobalPos, buttons, modifiers); |
450 | 0 | QWindow *window = topLevelAt(globalPos); |
451 | 0 | if (!window) |
452 | 0 | return; |
453 | | |
454 | 0 | const QPoint pos = nativeGlobalPos - window->handle()->geometry().topLeft(); |
455 | 0 | const QPlatformDropQtResponse response = QWindowSystemInterface::handleDrop( |
456 | 0 | window, drag()->mimeData(), pos, drag()->supportedActions(), |
457 | 0 | buttons, modifiers); |
458 | 0 | if (response.isAccepted()) { |
459 | 0 | setExecutedDropAction(response.acceptedAction()); |
460 | 0 | } else { |
461 | 0 | setExecutedDropAction(Qt::IgnoreAction); |
462 | 0 | } |
463 | 0 | } |
464 | | |
465 | | QT_END_NAMESPACE |