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 "qplatformdrag.h" |
5 | |
6 | #include <QtGui/private/qdnd_p.h> |
7 | #include <QtGui/QKeyEvent> |
8 | #include <QtGui/QGuiApplication> |
9 | #include <QtCore/QEventLoop> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | #ifdef QDND_DEBUG |
14 | # include <QtCore/QDebug> |
15 | #endif |
16 | |
17 | QPlatformDropQtResponse::QPlatformDropQtResponse(bool accepted, Qt::DropAction acceptedAction) |
18 | : m_accepted(accepted) |
19 | , m_accepted_action(acceptedAction) |
20 | { |
21 | } |
22 | |
23 | bool QPlatformDropQtResponse::isAccepted() const |
24 | { |
25 | return m_accepted; |
26 | } |
27 | |
28 | Qt::DropAction QPlatformDropQtResponse::acceptedAction() const |
29 | { |
30 | return m_accepted_action; |
31 | } |
32 | |
33 | QPlatformDragQtResponse::QPlatformDragQtResponse(bool accepted, Qt::DropAction acceptedAction, QRect answerRect) |
34 | : QPlatformDropQtResponse(accepted,acceptedAction) |
35 | , m_answer_rect(answerRect) |
36 | { |
37 | } |
38 | |
39 | QRect QPlatformDragQtResponse::answerRect() const |
40 | { |
41 | return m_answer_rect; |
42 | } |
43 | |
44 | class QPlatformDragPrivate { |
45 | public: |
46 | QPlatformDragPrivate() : cursor_drop_action(Qt::IgnoreAction) {} |
47 | |
48 | Qt::DropAction cursor_drop_action; |
49 | }; |
50 | |
51 | /*! |
52 | \class QPlatformDrag |
53 | \since 5.0 |
54 | \internal |
55 | \preliminary |
56 | \ingroup qpa |
57 | |
58 | \brief The QPlatformDrag class provides an abstraction for drag. |
59 | */ |
60 | QPlatformDrag::QPlatformDrag() : d_ptr(new QPlatformDragPrivate) |
61 | { |
62 | } |
63 | |
64 | QPlatformDrag::~QPlatformDrag() |
65 | { |
66 | delete d_ptr; |
67 | } |
68 | |
69 | QDrag *QPlatformDrag::currentDrag() const |
70 | { |
71 | return QDragManager::self()->object(); |
72 | } |
73 | |
74 | Qt::DropAction QPlatformDrag::defaultAction(Qt::DropActions possibleActions, |
75 | Qt::KeyboardModifiers modifiers) const |
76 | { |
77 | #ifdef QDND_DEBUG |
78 | qDebug() << "QDragManager::defaultAction(Qt::DropActions possibleActions)\nkeyboard modifiers : " << modifiers; |
79 | #endif |
80 | |
81 | Qt::DropAction default_action = Qt::IgnoreAction; |
82 | |
83 | if (currentDrag()) { |
84 | default_action = currentDrag()->defaultAction(); |
85 | } |
86 | |
87 | |
88 | if (default_action == Qt::IgnoreAction) { |
89 | //This means that the drag was initiated by QDrag::start and we need to |
90 | //preserve the old behavior |
91 | default_action = Qt::CopyAction; |
92 | } |
93 | |
94 | if (modifiers & Qt::ControlModifier && modifiers & Qt::ShiftModifier) |
95 | default_action = Qt::LinkAction; |
96 | else if (modifiers & Qt::ControlModifier) |
97 | default_action = Qt::CopyAction; |
98 | else if (modifiers & Qt::ShiftModifier) |
99 | default_action = Qt::MoveAction; |
100 | else if (modifiers & Qt::AltModifier) |
101 | default_action = Qt::LinkAction; |
102 | |
103 | #ifdef QDND_DEBUG |
104 | qDebug() << "possible actions : " << possibleActions; |
105 | #endif |
106 | |
107 | // Check if the action determined is allowed |
108 | if (!(possibleActions & default_action)) { |
109 | if (possibleActions & Qt::CopyAction) |
110 | default_action = Qt::CopyAction; |
111 | else if (possibleActions & Qt::MoveAction) |
112 | default_action = Qt::MoveAction; |
113 | else if (possibleActions & Qt::LinkAction) |
114 | default_action = Qt::LinkAction; |
115 | else |
116 | default_action = Qt::IgnoreAction; |
117 | } |
118 | |
119 | #ifdef QDND_DEBUG |
120 | qDebug() << "default action : " << default_action; |
121 | #endif |
122 | |
123 | return default_action; |
124 | } |
125 | |
126 | /*! |
127 | \brief Cancels the currently active drag (only for drags of |
128 | the current application initiated by QPlatformDrag::drag()). |
129 | |
130 | The default implementation does nothing. |
131 | |
132 | \since 5.7 |
133 | */ |
134 | |
135 | void QPlatformDrag::cancelDrag() |
136 | { |
137 | Q_UNIMPLEMENTED(); |
138 | } |
139 | |
140 | /*! |
141 | \brief Called to notify QDrag about changes of the current action. |
142 | */ |
143 | |
144 | void QPlatformDrag::updateAction(Qt::DropAction action) |
145 | { |
146 | Q_D(QPlatformDrag); |
147 | if (d->cursor_drop_action != action) { |
148 | d->cursor_drop_action = action; |
149 | emit currentDrag()->actionChanged(action); |
150 | } |
151 | } |
152 | |
153 | static const char *const default_pm[] = { |
154 | "13 9 3 1" , |
155 | ". c None" , |
156 | " c #000000" , |
157 | "X c #FFFFFF" , |
158 | "X X X X X X X" , |
159 | " X X X X X X " , |
160 | "X ......... X" , |
161 | " X.........X " , |
162 | "X ......... X" , |
163 | " X.........X " , |
164 | "X ......... X" , |
165 | " X X X X X X " , |
166 | "X X X X X X X" , |
167 | }; |
168 | |
169 | Q_GLOBAL_STATIC_WITH_ARGS(QPixmap,qt_drag_default_pixmap,(default_pm)) |
170 | |
171 | QPixmap QPlatformDrag::defaultPixmap() |
172 | { |
173 | return *qt_drag_default_pixmap(); |
174 | } |
175 | |
176 | /*! |
177 | \since 5.4 |
178 | \brief Returns bool indicating whether QPlatformDrag takes ownership |
179 | and therefore responsibility of deleting the QDrag object passed in |
180 | from QPlatformDrag::drag. This can be useful on platforms where QDrag |
181 | object has to be kept around. |
182 | */ |
183 | bool QPlatformDrag::ownsDragObject() const |
184 | { |
185 | return false; |
186 | } |
187 | |
188 | QT_END_NAMESPACE |
189 | |