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 "qdnd_p.h" |
5 | |
6 | #include "qguiapplication.h" |
7 | #include <ctype.h> |
8 | #include <qpa/qplatformdrag.h> |
9 | #include <qpa/qplatformintegration.h> |
10 | #include <private/qguiapplication_p.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | // the universe's only drag manager |
15 | QDragManager *QDragManager::m_instance = nullptr; |
16 | |
17 | QDragManager::QDragManager() |
18 | : QObject(qApp), m_currentDropTarget(nullptr), |
19 | m_platformDrag(QGuiApplicationPrivate::platformIntegration()->drag()), |
20 | m_object(nullptr) |
21 | { |
22 | Q_ASSERT(!m_instance); |
23 | } |
24 | |
25 | QDragManager::~QDragManager() |
26 | { |
27 | m_instance = nullptr; |
28 | } |
29 | |
30 | QDragManager *QDragManager::self() |
31 | { |
32 | if (!m_instance && !QGuiApplication::closingDown()) |
33 | m_instance = new QDragManager; |
34 | return m_instance; |
35 | } |
36 | |
37 | QObject *QDragManager::source() const |
38 | { |
39 | if (m_object) |
40 | return m_object->source(); |
41 | return nullptr; |
42 | } |
43 | |
44 | void QDragManager::setCurrentTarget(QObject *target, bool dropped) |
45 | { |
46 | if (m_currentDropTarget == target) |
47 | return; |
48 | |
49 | m_currentDropTarget = target; |
50 | if (!dropped && m_object) { |
51 | m_object->d_func()->target = target; |
52 | emit m_object->targetChanged(newTarget: target); |
53 | } |
54 | } |
55 | |
56 | QObject *QDragManager::currentTarget() const |
57 | { |
58 | return m_currentDropTarget; |
59 | } |
60 | |
61 | Qt::DropAction QDragManager::drag(QDrag *o) |
62 | { |
63 | if (!o || m_object == o) |
64 | return Qt::IgnoreAction; |
65 | |
66 | if (!m_platformDrag || !o->source()) { |
67 | o->deleteLater(); |
68 | return Qt::IgnoreAction; |
69 | } |
70 | |
71 | if (m_object) { |
72 | qWarning(msg: "QDragManager::drag in possibly invalid state"); |
73 | return Qt::IgnoreAction; |
74 | } |
75 | |
76 | m_object = o; |
77 | |
78 | m_object->d_func()->target = nullptr; |
79 | |
80 | QGuiApplicationPrivate::instance()->notifyDragStarted(m_object.data()); |
81 | const Qt::DropAction result = m_platformDrag->drag(m_drag: m_object); |
82 | if (!m_object.isNull() && !m_platformDrag->ownsDragObject()) |
83 | m_object->deleteLater(); |
84 | |
85 | m_object.clear(); |
86 | return result; |
87 | } |
88 | |
89 | QT_END_NAMESPACE |
90 | |
91 | #include "moc_qdnd_p.cpp" |
92 |