Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | static QWindow* topLevelAt(const QPoint &pos) | - |
11 | { | - |
12 | QWindowList list = QGuiApplication::topLevelWindows(); | - |
13 | for (int i = list.count()-1; i >= 0; --i) { | - |
14 | QWindow *w = list.at(i); | - |
15 | if (w->isVisible() && w->geometry().contains(pos) && !qobject_cast<QShapedPixmapWindow*>(w)) | - |
16 | return w; | - |
17 | } | - |
18 | return 0; | - |
19 | } | - |
20 | QBasicDrag::QBasicDrag() : | - |
21 | m_restoreCursor(false), m_eventLoop(0), | - |
22 | m_executed_drop_action(Qt::IgnoreAction), m_can_drop(false), | - |
23 | m_drag(0), m_drag_icon_window(0), m_useCompositing(true), | - |
24 | m_screen(nullptr) | - |
25 | { | - |
26 | } | - |
27 | | - |
28 | QBasicDrag::~QBasicDrag() | - |
29 | { | - |
30 | delete m_drag_icon_window; | - |
31 | } | - |
32 | | - |
33 | void QBasicDrag::enableEventFilter() | - |
34 | { | - |
35 | (static_cast<QGuiApplication *>(QCoreApplication::instance()))->installEventFilter(this); | - |
36 | } | - |
37 | | - |
38 | void QBasicDrag::disableEventFilter() | - |
39 | { | - |
40 | (static_cast<QGuiApplication *>(QCoreApplication::instance()))->removeEventFilter(this); | - |
41 | } | - |
42 | | - |
43 | | - |
44 | static inline QPoint getNativeMousePos(QEvent *e, QObject *o) | - |
45 | { | - |
46 | return QHighDpi::toNativePixels(static_cast<QMouseEvent *>(e)->globalPos(), qobject_cast<QWindow*>(o)); | - |
47 | } | - |
48 | | - |
49 | bool QBasicDrag::eventFilter(QObject *o, QEvent *e) | - |
50 | { | - |
51 | (void)o;; | - |
52 | | - |
53 | if (!m_drag) { | - |
54 | if (e->type() == QEvent::KeyRelease && static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) { | - |
55 | disableEventFilter(); | - |
56 | exitDndEventLoop(); | - |
57 | return true; | - |
58 | } | - |
59 | return false; | - |
60 | } | - |
61 | | - |
62 | switch (e->type()) { | - |
63 | case QEvent::ShortcutOverride: | - |
64 | | - |
65 | e->accept(); | - |
66 | return true; | - |
67 | | - |
68 | case QEvent::KeyPress: | - |
69 | case QEvent::KeyRelease: | - |
70 | { | - |
71 | QKeyEvent *ke = static_cast<QKeyEvent *>(e); | - |
72 | if (ke->key() == Qt::Key_Escape && e->type() == QEvent::KeyPress) { | - |
73 | cancel(); | - |
74 | disableEventFilter(); | - |
75 | exitDndEventLoop(); | - |
76 | | - |
77 | } | - |
78 | return true; | - |
79 | } | - |
80 | | - |
81 | case QEvent::MouseMove: | - |
82 | { | - |
83 | QPoint nativePosition = getNativeMousePos(e, o); | - |
84 | move(nativePosition); | - |
85 | return true; | - |
86 | } | - |
87 | case QEvent::MouseButtonRelease: | - |
88 | disableEventFilter(); | - |
89 | if (canDrop()) { | - |
90 | QPoint nativePosition = getNativeMousePos(e, o); | - |
91 | drop(nativePosition); | - |
92 | } else { | - |
93 | cancel(); | - |
94 | } | - |
95 | exitDndEventLoop(); | - |
96 | QCoreApplication::postEvent(o, new QMouseEvent(*static_cast<QMouseEvent *>(e))); | - |
97 | return true; | - |
98 | case QEvent::MouseButtonDblClick: | - |
99 | case QEvent::Wheel: | - |
100 | return true; | - |
101 | default: | - |
102 | break; | - |
103 | } | - |
104 | return false; | - |
105 | } | - |
106 | | - |
107 | Qt::DropAction QBasicDrag::drag(QDrag *o) | - |
108 | { | - |
109 | m_drag = o; | - |
110 | m_executed_drop_action = Qt::IgnoreAction; | - |
111 | m_can_drop = false; | - |
112 | m_restoreCursor = true; | - |
113 | | - |
114 | (static_cast<QGuiApplication *>(QCoreApplication::instance()))->setOverrideCursor(Qt::DragCopyCursor); | - |
115 | updateCursor(m_executed_drop_action); | - |
116 | | - |
117 | startDrag(); | - |
118 | m_eventLoop = new QEventLoop; | - |
119 | m_eventLoop->exec(); | - |
120 | delete m_eventLoop; | - |
121 | m_eventLoop = 0; | - |
122 | m_drag = 0; | - |
123 | endDrag(); | - |
124 | return m_executed_drop_action; | - |
125 | } | - |
126 | | - |
127 | void QBasicDrag::cancelDrag() | - |
128 | { | - |
129 | if (m_eventLoopTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
130 | cancel(); | - |
131 | m_eventLoop->quit(); | - |
132 | } never executed: end of block | 0 |
133 | } never executed: end of block | 0 |
134 | | - |
135 | void QBasicDrag::restoreCursor() | - |
136 | { | - |
137 | if (m_restoreCursor) { | - |
138 | | - |
139 | QGuiApplication::restoreOverrideCursor(); | - |
140 | | - |
141 | m_restoreCursor = false; | - |
142 | } | - |
143 | } | - |
144 | | - |
145 | void QBasicDrag::startDrag() | - |
146 | { | - |
147 | QPoint pos; | - |
148 | | - |
149 | pos = QCursor::pos(); | - |
150 | if (pos.x() == int(qInf())) { | - |
151 | | - |
152 | pos = QPoint(); | - |
153 | } | - |
154 | | - |
155 | recreateShapedPixmapWindow(m_screen, pos); | - |
156 | enableEventFilter(); | - |
157 | } | - |
158 | | - |
159 | void QBasicDrag::endDrag() | - |
160 | { | - |
161 | } | - |
162 | | - |
163 | void QBasicDrag::recreateShapedPixmapWindow(QScreen *screen, const QPoint &pos) | - |
164 | { | - |
165 | delete m_drag_icon_window; | - |
166 | | - |
167 | | - |
168 | m_drag_icon_window = new QShapedPixmapWindow(screen); | - |
169 | | - |
170 | m_drag_icon_window->setUseCompositing(m_useCompositing); | - |
171 | m_drag_icon_window->setPixmap(m_drag->pixmap()); | - |
172 | m_drag_icon_window->setHotspot(m_drag->hotSpot()); | - |
173 | m_drag_icon_window->updateGeometry(pos); | - |
174 | m_drag_icon_window->setVisible(true); | - |
175 | } | - |
176 | | - |
177 | void QBasicDrag::cancel() | - |
178 | { | - |
179 | disableEventFilter(); | - |
180 | restoreCursor(); | - |
181 | m_drag_icon_window->setVisible(false); | - |
182 | } | - |
183 | | - |
184 | | - |
185 | | - |
186 | | - |
187 | | - |
188 | | - |
189 | void QBasicDrag::moveShapedPixmapWindow(const QPoint &globalPos) | - |
190 | { | - |
191 | if (m_drag) | - |
192 | m_drag_icon_window->updateGeometry(globalPos); | - |
193 | } | - |
194 | | - |
195 | void QBasicDrag::drop(const QPoint &) | - |
196 | { | - |
197 | disableEventFilter(); | - |
198 | restoreCursor(); | - |
199 | m_drag_icon_window->setVisible(false); | - |
200 | } | - |
201 | | - |
202 | void QBasicDrag::exitDndEventLoop() | - |
203 | { | - |
204 | if (m_eventLoop && m_eventLoop->isRunning()) | - |
205 | m_eventLoop->exit(); | - |
206 | } | - |
207 | | - |
208 | void QBasicDrag::updateCursor(Qt::DropAction action) | - |
209 | { | - |
210 | | - |
211 | Qt::CursorShape cursorShape = Qt::ForbiddenCursor; | - |
212 | if (canDrop()) { | - |
213 | switch (action) { | - |
214 | case Qt::CopyAction: | - |
215 | cursorShape = Qt::DragCopyCursor; | - |
216 | break; | - |
217 | case Qt::LinkAction: | - |
218 | cursorShape = Qt::DragLinkCursor; | - |
219 | break; | - |
220 | default: | - |
221 | cursorShape = Qt::DragMoveCursor; | - |
222 | break; | - |
223 | } | - |
224 | } | - |
225 | | - |
226 | QCursor *cursor = QGuiApplication::overrideCursor(); | - |
227 | QPixmap pixmap = m_drag->dragCursor(action); | - |
228 | if (!cursor) { | - |
229 | QGuiApplication::changeOverrideCursor((pixmap.isNull()) ? QCursor(cursorShape) : QCursor(pixmap)); | - |
230 | } else { | - |
231 | if (!pixmap.isNull()) { | - |
232 | if ((cursor->pixmap().cacheKey() != pixmap.cacheKey())) { | - |
233 | QGuiApplication::changeOverrideCursor(QCursor(pixmap)); | - |
234 | } | - |
235 | } else { | - |
236 | if (cursorShape != cursor->shape()) { | - |
237 | QGuiApplication::changeOverrideCursor(QCursor(cursorShape)); | - |
238 | } | - |
239 | } | - |
240 | } | - |
241 | | - |
242 | updateAction(action); | - |
243 | } | - |
244 | QSimpleDrag::QSimpleDrag() : m_current_window(0) | - |
245 | { | - |
246 | } | - |
247 | | - |
248 | QMimeData *QSimpleDrag::platformDropData() | - |
249 | { | - |
250 | if (drag()) | - |
251 | return drag()->mimeData(); | - |
252 | return 0; | - |
253 | } | - |
254 | | - |
255 | void QSimpleDrag::startDrag() | - |
256 | { | - |
257 | QBasicDrag::startDrag(); | - |
258 | m_current_window = topLevelAt(QCursor::pos()); | - |
259 | if (m_current_window) { | - |
260 | QPlatformDragQtResponse response = QWindowSystemInterface::handleDrag(m_current_window, drag()->mimeData(), QCursor::pos(), drag()->supportedActions()); | - |
261 | setCanDrop(response.isAccepted()); | - |
262 | updateCursor(response.acceptedAction()); | - |
263 | } else { | - |
264 | setCanDrop(false); | - |
265 | updateCursor(Qt::IgnoreAction); | - |
266 | } | - |
267 | setExecutedDropAction(Qt::IgnoreAction); | - |
268 | } | - |
269 | | - |
270 | void QSimpleDrag::cancel() | - |
271 | { | - |
272 | QBasicDrag::cancel(); | - |
273 | if (drag() && m_current_window) { | - |
274 | QWindowSystemInterface::handleDrag(m_current_window, 0, QPoint(), Qt::IgnoreAction); | - |
275 | m_current_window = 0; | - |
276 | } | - |
277 | } | - |
278 | | - |
279 | void QSimpleDrag::move(const QPoint &globalPos) | - |
280 | { | - |
281 | | - |
282 | moveShapedPixmapWindow(globalPos); | - |
283 | QWindow *window = topLevelAt(globalPos); | - |
284 | if (!window) | - |
285 | return; | - |
286 | | - |
287 | const QPoint pos = globalPos - window->geometry().topLeft(); | - |
288 | const QPlatformDragQtResponse qt_response = | - |
289 | QWindowSystemInterface::handleDrag(window, drag()->mimeData(), pos, drag()->supportedActions()); | - |
290 | | - |
291 | updateCursor(qt_response.acceptedAction()); | - |
292 | setCanDrop(qt_response.isAccepted()); | - |
293 | } | - |
294 | | - |
295 | void QSimpleDrag::drop(const QPoint &globalPos) | - |
296 | { | - |
297 | | - |
298 | | - |
299 | QBasicDrag::drop(globalPos); | - |
300 | QWindow *window = topLevelAt(globalPos); | - |
301 | if (!window) | - |
302 | return; | - |
303 | | - |
304 | const QPoint pos = globalPos - window->geometry().topLeft(); | - |
305 | const QPlatformDropQtResponse response = | - |
306 | QWindowSystemInterface::handleDrop(window, drag()->mimeData(),pos, drag()->supportedActions()); | - |
307 | if (response.isAccepted()) { | - |
308 | setExecutedDropAction(response.acceptedAction()); | - |
309 | } else { | - |
310 | setExecutedDropAction(Qt::IgnoreAction); | - |
311 | } | - |
312 | } | - |
313 | | - |
314 | | - |
315 | | - |
316 | | - |
| | |