Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/kernel/qdrag.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include <qdrag.h> | - | ||||||
41 | #include "private/qguiapplication_p.h" | - | ||||||
42 | #include "qpa/qplatformintegration.h" | - | ||||||
43 | #include "qpa/qplatformdrag.h" | - | ||||||
44 | #include <qpixmap.h> | - | ||||||
45 | #include <qpoint.h> | - | ||||||
46 | #include "qdnd_p.h" | - | ||||||
47 | - | |||||||
48 | #ifndef QT_NO_DRAGANDDROP | - | ||||||
49 | - | |||||||
50 | QT_BEGIN_NAMESPACE | - | ||||||
51 | - | |||||||
52 | /*! | - | ||||||
53 | \class QDrag | - | ||||||
54 | \inmodule QtGui | - | ||||||
55 | \ingroup draganddrop | - | ||||||
56 | \brief The QDrag class provides support for MIME-based drag and drop data | - | ||||||
57 | transfer. | - | ||||||
58 | - | |||||||
59 | Drag and drop is an intuitive way for users to copy or move data around in an | - | ||||||
60 | application, and is used in many desktop environments as a mechanism for copying | - | ||||||
61 | data between applications. Drag and drop support in Qt is centered around the | - | ||||||
62 | QDrag class that handles most of the details of a drag and drop operation. | - | ||||||
63 | - | |||||||
64 | The data to be transferred by the drag and drop operation is contained in a | - | ||||||
65 | QMimeData object. This is specified with the setMimeData() function in the | - | ||||||
66 | following way: | - | ||||||
67 | - | |||||||
68 | \snippet dragging/mainwindow.cpp 1 | - | ||||||
69 | - | |||||||
70 | Note that setMimeData() assigns ownership of the QMimeData object to the | - | ||||||
71 | QDrag object. The QDrag must be constructed on the heap with a parent QObject | - | ||||||
72 | to ensure that Qt can clean up after the drag and drop operation has been | - | ||||||
73 | completed. | - | ||||||
74 | - | |||||||
75 | A pixmap can be used to represent the data while the drag is in | - | ||||||
76 | progress, and will move with the cursor to the drop target. This | - | ||||||
77 | pixmap typically shows an icon that represents the MIME type of | - | ||||||
78 | the data being transferred, but any pixmap can be set with | - | ||||||
79 | setPixmap(). The cursor's hot spot can be given a position | - | ||||||
80 | relative to the top-left corner of the pixmap with the | - | ||||||
81 | setHotSpot() function. The following code positions the pixmap so | - | ||||||
82 | that the cursor's hot spot points to the center of its bottom | - | ||||||
83 | edge: | - | ||||||
84 | - | |||||||
85 | \snippet separations/finalwidget.cpp 2 | - | ||||||
86 | - | |||||||
87 | \note On X11, the pixmap may not be able to keep up with the mouse | - | ||||||
88 | movements if the hot spot causes the pixmap to be displayed | - | ||||||
89 | directly under the cursor. | - | ||||||
90 | - | |||||||
91 | The source and target widgets can be found with source() and target(). | - | ||||||
92 | These functions are often used to determine whether drag and drop operations | - | ||||||
93 | started and finished at the same widget, so that special behavior can be | - | ||||||
94 | implemented. | - | ||||||
95 | - | |||||||
96 | QDrag only deals with the drag and drop operation itself. It is up to the | - | ||||||
97 | developer to decide when a drag operation begins, and how a QDrag object should | - | ||||||
98 | be constructed and used. For a given widget, it is often necessary to | - | ||||||
99 | reimplement \l{QWidget::mousePressEvent()}{mousePressEvent()} to determine | - | ||||||
100 | whether the user has pressed a mouse button, and reimplement | - | ||||||
101 | \l{QWidget::mouseMoveEvent()}{mouseMoveEvent()} to check whether a QDrag is | - | ||||||
102 | required. | - | ||||||
103 | - | |||||||
104 | \sa {Drag and Drop}, QClipboard, QMimeData, QMacPasteboardMime, | - | ||||||
105 | {Draggable Icons Example}, {Draggable Text Example}, {Drop Site Example}, | - | ||||||
106 | {Fridge Magnets Example} | - | ||||||
107 | */ | - | ||||||
108 | - | |||||||
109 | /*! | - | ||||||
110 | Constructs a new drag object for the widget specified by \a dragSource. | - | ||||||
111 | */ | - | ||||||
112 | QDrag::QDrag(QObject *dragSource) | - | ||||||
113 | : QObject(*new QDragPrivate, dragSource) | - | ||||||
114 | { | - | ||||||
115 | Q_D(QDrag); | - | ||||||
116 | d->source = dragSource; | - | ||||||
117 | d->target = 0; | - | ||||||
118 | d->data = 0; | - | ||||||
119 | d->hotspot = QPoint(-10, -10); | - | ||||||
120 | d->executed_action = Qt::IgnoreAction; | - | ||||||
121 | d->supported_actions = Qt::IgnoreAction; | - | ||||||
122 | d->default_action = Qt::IgnoreAction; | - | ||||||
123 | } | - | ||||||
124 | - | |||||||
125 | /*! | - | ||||||
126 | Destroys the drag object. | - | ||||||
127 | */ | - | ||||||
128 | QDrag::~QDrag() | - | ||||||
129 | { | - | ||||||
130 | Q_D(QDrag); | - | ||||||
131 | delete d->data; | - | ||||||
132 | } | - | ||||||
133 | - | |||||||
134 | /*! | - | ||||||
135 | Sets the data to be sent to the given MIME \a data. Ownership of the data is | - | ||||||
136 | transferred to the QDrag object. | - | ||||||
137 | */ | - | ||||||
138 | void QDrag::setMimeData(QMimeData *data) | - | ||||||
139 | { | - | ||||||
140 | Q_D(QDrag); | - | ||||||
141 | if (d->data == data) | - | ||||||
142 | return; | - | ||||||
143 | if (d->data != 0) | - | ||||||
144 | delete d->data; | - | ||||||
145 | d->data = data; | - | ||||||
146 | } | - | ||||||
147 | - | |||||||
148 | /*! | - | ||||||
149 | Returns the MIME data that is encapsulated by the drag object. | - | ||||||
150 | */ | - | ||||||
151 | QMimeData *QDrag::mimeData() const | - | ||||||
152 | { | - | ||||||
153 | Q_D(const QDrag); | - | ||||||
154 | return d->data; | - | ||||||
155 | } | - | ||||||
156 | - | |||||||
157 | /*! | - | ||||||
158 | Sets \a pixmap as the pixmap used to represent the data in a drag | - | ||||||
159 | and drop operation. You can only set a pixmap before the drag is | - | ||||||
160 | started. | - | ||||||
161 | */ | - | ||||||
162 | void QDrag::setPixmap(const QPixmap &pixmap) | - | ||||||
163 | { | - | ||||||
164 | Q_D(QDrag); | - | ||||||
165 | d->pixmap = pixmap; | - | ||||||
166 | } | - | ||||||
167 | - | |||||||
168 | /*! | - | ||||||
169 | Returns the pixmap used to represent the data in a drag and drop operation. | - | ||||||
170 | */ | - | ||||||
171 | QPixmap QDrag::pixmap() const | - | ||||||
172 | { | - | ||||||
173 | Q_D(const QDrag); | - | ||||||
174 | return d->pixmap; | - | ||||||
175 | } | - | ||||||
176 | - | |||||||
177 | /*! | - | ||||||
178 | Sets the position of the hot spot relative to the top-left corner of the | - | ||||||
179 | pixmap used to the point specified by \a hotspot. | - | ||||||
180 | - | |||||||
181 | \b{Note:} on X11, the pixmap may not be able to keep up with the mouse | - | ||||||
182 | movements if the hot spot causes the pixmap to be displayed | - | ||||||
183 | directly under the cursor. | - | ||||||
184 | */ | - | ||||||
185 | void QDrag::setHotSpot(const QPoint& hotspot) | - | ||||||
186 | { | - | ||||||
187 | Q_D(QDrag); | - | ||||||
188 | d->hotspot = hotspot; | - | ||||||
189 | } | - | ||||||
190 | - | |||||||
191 | /*! | - | ||||||
192 | Returns the position of the hot spot relative to the top-left corner of the | - | ||||||
193 | cursor. | - | ||||||
194 | */ | - | ||||||
195 | QPoint QDrag::hotSpot() const | - | ||||||
196 | { | - | ||||||
197 | Q_D(const QDrag); | - | ||||||
198 | return d->hotspot; | - | ||||||
199 | } | - | ||||||
200 | - | |||||||
201 | /*! | - | ||||||
202 | Returns the source of the drag object. This is the widget where the drag | - | ||||||
203 | and drop operation originated. | - | ||||||
204 | */ | - | ||||||
205 | QObject *QDrag::source() const | - | ||||||
206 | { | - | ||||||
207 | Q_D(const QDrag); | - | ||||||
208 | return d->source; | - | ||||||
209 | } | - | ||||||
210 | - | |||||||
211 | /*! | - | ||||||
212 | Returns the target of the drag and drop operation. This is the widget where | - | ||||||
213 | the drag object was dropped. | - | ||||||
214 | */ | - | ||||||
215 | QObject *QDrag::target() const | - | ||||||
216 | { | - | ||||||
217 | Q_D(const QDrag); | - | ||||||
218 | return d->target; | - | ||||||
219 | } | - | ||||||
220 | - | |||||||
221 | /*! | - | ||||||
222 | \since 4.3 | - | ||||||
223 | - | |||||||
224 | Starts the drag and drop operation and returns a value indicating the requested | - | ||||||
225 | drop action when it is completed. The drop actions that the user can choose | - | ||||||
226 | from are specified in \a supportedActions. The default proposed action will be selected | - | ||||||
227 | among the allowed actions in the following order: Move, Copy and Link. | - | ||||||
228 | - | |||||||
229 | \b{Note:} On Linux and \macos, the drag and drop operation | - | ||||||
230 | can take some time, but this function does not block the event | - | ||||||
231 | loop. Other events are still delivered to the application while | - | ||||||
232 | the operation is performed. On Windows, the Qt event loop is | - | ||||||
233 | blocked during the operation. | - | ||||||
234 | - | |||||||
235 | \sa cancel() | - | ||||||
236 | */ | - | ||||||
237 | - | |||||||
238 | Qt::DropAction QDrag::exec(Qt::DropActions supportedActions) | - | ||||||
239 | { | - | ||||||
240 | return exec(supportedActions, Qt::IgnoreAction); | - | ||||||
241 | } | - | ||||||
242 | - | |||||||
243 | /*! | - | ||||||
244 | \since 4.3 | - | ||||||
245 | - | |||||||
246 | Starts the drag and drop operation and returns a value indicating the requested | - | ||||||
247 | drop action when it is completed. The drop actions that the user can choose | - | ||||||
248 | from are specified in \a supportedActions. | - | ||||||
249 | - | |||||||
250 | The \a defaultDropAction determines which action will be proposed when the user performs a | - | ||||||
251 | drag without using modifier keys. | - | ||||||
252 | - | |||||||
253 | \b{Note:} On Linux and \macos, the drag and drop operation | - | ||||||
254 | can take some time, but this function does not block the event | - | ||||||
255 | loop. Other events are still delivered to the application while | - | ||||||
256 | the operation is performed. On Windows, the Qt event loop is | - | ||||||
257 | blocked during the operation. However, QDrag::exec() on | - | ||||||
258 | Windows causes processEvents() to be called frequently to keep the GUI responsive. | - | ||||||
259 | If any loops or operations are called while a drag operation is active, it will block the drag operation. | - | ||||||
260 | */ | - | ||||||
261 | - | |||||||
262 | Qt::DropAction QDrag::exec(Qt::DropActions supportedActions, Qt::DropAction defaultDropAction) | - | ||||||
263 | { | - | ||||||
264 | Q_D(QDrag); | - | ||||||
265 | if (!d->data) { | - | ||||||
266 | qWarning("QDrag: No mimedata set before starting the drag"); | - | ||||||
267 | return d->executed_action; | - | ||||||
268 | } | - | ||||||
269 | Qt::DropAction transformedDefaultDropAction = Qt::IgnoreAction; | - | ||||||
270 | - | |||||||
271 | if (defaultDropAction == Qt::IgnoreAction) { | - | ||||||
272 | if (supportedActions & Qt::MoveAction) { | - | ||||||
273 | transformedDefaultDropAction = Qt::MoveAction; | - | ||||||
274 | } else if (supportedActions & Qt::CopyAction) { | - | ||||||
275 | transformedDefaultDropAction = Qt::CopyAction; | - | ||||||
276 | } else if (supportedActions & Qt::LinkAction) { | - | ||||||
277 | transformedDefaultDropAction = Qt::LinkAction; | - | ||||||
278 | } | - | ||||||
279 | } else { | - | ||||||
280 | transformedDefaultDropAction = defaultDropAction; | - | ||||||
281 | } | - | ||||||
282 | d->supported_actions = supportedActions; | - | ||||||
283 | d->default_action = transformedDefaultDropAction; | - | ||||||
284 | d->executed_action = QDragManager::self()->drag(this); | - | ||||||
285 | - | |||||||
286 | return d->executed_action; | - | ||||||
287 | } | - | ||||||
288 | - | |||||||
289 | /*! | - | ||||||
290 | \obsolete | - | ||||||
291 | - | |||||||
292 | \b{Note:} It is recommended to use exec() instead of this function. | - | ||||||
293 | - | |||||||
294 | Starts the drag and drop operation and returns a value indicating the requested | - | ||||||
295 | drop action when it is completed. The drop actions that the user can choose | - | ||||||
296 | from are specified in \a request. Qt::CopyAction is always allowed. | - | ||||||
297 | - | |||||||
298 | \b{Note:} Although the drag and drop operation can take some time, this function | - | ||||||
299 | does not block the event loop. Other events are still delivered to the application | - | ||||||
300 | while the operation is performed. | - | ||||||
301 | - | |||||||
302 | \sa exec() | - | ||||||
303 | */ | - | ||||||
304 | Qt::DropAction QDrag::start(Qt::DropActions request) | - | ||||||
305 | { | - | ||||||
306 | Q_D(QDrag); | - | ||||||
307 | if (!d->data) { | - | ||||||
308 | qWarning("QDrag: No mimedata set before starting the drag"); | - | ||||||
309 | return d->executed_action; | - | ||||||
310 | } | - | ||||||
311 | d->supported_actions = request | Qt::CopyAction; | - | ||||||
312 | d->default_action = Qt::IgnoreAction; | - | ||||||
313 | d->executed_action = QDragManager::self()->drag(this); | - | ||||||
314 | return d->executed_action; | - | ||||||
315 | } | - | ||||||
316 | - | |||||||
317 | /*! | - | ||||||
318 | Sets the drag \a cursor for the \a action. This allows you | - | ||||||
319 | to override the default native cursors. To revert to using the | - | ||||||
320 | native cursor for \a action pass in a null QPixmap as \a cursor. | - | ||||||
321 | - | |||||||
322 | The \a action can only be CopyAction, MoveAction or LinkAction. | - | ||||||
323 | All other values of DropAction are ignored. | - | ||||||
324 | */ | - | ||||||
325 | void QDrag::setDragCursor(const QPixmap &cursor, Qt::DropAction action) | - | ||||||
326 | { | - | ||||||
327 | Q_D(QDrag); | - | ||||||
328 | if (action != Qt::CopyAction && action != Qt::MoveAction && action != Qt::LinkAction) | - | ||||||
329 | return; | - | ||||||
330 | if (cursor.isNull()) | - | ||||||
331 | d->customCursors.remove(action); | - | ||||||
332 | else | - | ||||||
333 | d->customCursors[action] = cursor; | - | ||||||
334 | } | - | ||||||
335 | - | |||||||
336 | /*! | - | ||||||
337 | Returns the drag cursor for the \a action. | - | ||||||
338 | - | |||||||
339 | \since 5.0 | - | ||||||
340 | */ | - | ||||||
341 | - | |||||||
342 | QPixmap QDrag::dragCursor(Qt::DropAction action) const | - | ||||||
343 | { | - | ||||||
344 | typedef QMap<Qt::DropAction, QPixmap>::const_iterator Iterator; | - | ||||||
345 | - | |||||||
346 | Q_D(const QDrag); | - | ||||||
347 | const Iterator it = d->customCursors.constFind(action); | - | ||||||
348 | if (it != d->customCursors.constEnd()) | - | ||||||
349 | return it.value(); | - | ||||||
350 | - | |||||||
351 | Qt::CursorShape shape = Qt::ForbiddenCursor; | - | ||||||
352 | switch (action) { | - | ||||||
353 | case Qt::MoveAction: | - | ||||||
354 | shape = Qt::DragMoveCursor; | - | ||||||
355 | break; | - | ||||||
356 | case Qt::CopyAction: | - | ||||||
357 | shape = Qt::DragCopyCursor; | - | ||||||
358 | break; | - | ||||||
359 | case Qt::LinkAction: | - | ||||||
360 | shape = Qt::DragLinkCursor; | - | ||||||
361 | break; | - | ||||||
362 | default: | - | ||||||
363 | shape = Qt::ForbiddenCursor; | - | ||||||
364 | } | - | ||||||
365 | return QGuiApplicationPrivate::instance()->getPixmapCursor(shape); | - | ||||||
366 | } | - | ||||||
367 | - | |||||||
368 | /*! | - | ||||||
369 | Returns the set of possible drop actions for this drag operation. | - | ||||||
370 | - | |||||||
371 | \sa exec(), defaultAction() | - | ||||||
372 | */ | - | ||||||
373 | Qt::DropActions QDrag::supportedActions() const | - | ||||||
374 | { | - | ||||||
375 | Q_D(const QDrag); | - | ||||||
376 | return d->supported_actions; | - | ||||||
377 | } | - | ||||||
378 | - | |||||||
379 | - | |||||||
380 | /*! | - | ||||||
381 | Returns the default proposed drop action for this drag operation. | - | ||||||
382 | - | |||||||
383 | \sa exec(), supportedActions() | - | ||||||
384 | */ | - | ||||||
385 | Qt::DropAction QDrag::defaultAction() const | - | ||||||
386 | { | - | ||||||
387 | Q_D(const QDrag); | - | ||||||
388 | return d->default_action; | - | ||||||
389 | } | - | ||||||
390 | - | |||||||
391 | /*! | - | ||||||
392 | Cancels a drag operation initiated by Qt. | - | ||||||
393 | - | |||||||
394 | \note This is currently implemented on Windows and X11. | - | ||||||
395 | - | |||||||
396 | \since 5.6 | - | ||||||
397 | \sa exec() | - | ||||||
398 | */ | - | ||||||
399 | void QDrag::cancel() | - | ||||||
400 | { | - | ||||||
401 | if (QPlatformDrag *platformDrag = QGuiApplicationPrivate::platformIntegration()->drag())
| 0 | ||||||
402 | platformDrag->cancelDrag(); never executed: platformDrag->cancelDrag(); | 0 | ||||||
403 | } never executed: end of block | 0 | ||||||
404 | - | |||||||
405 | /*! | - | ||||||
406 | \fn void QDrag::actionChanged(Qt::DropAction action) | - | ||||||
407 | - | |||||||
408 | This signal is emitted when the \a action associated with the | - | ||||||
409 | drag changes. | - | ||||||
410 | - | |||||||
411 | \sa targetChanged() | - | ||||||
412 | */ | - | ||||||
413 | - | |||||||
414 | /*! | - | ||||||
415 | \fn void QDrag::targetChanged(QObject *newTarget) | - | ||||||
416 | - | |||||||
417 | This signal is emitted when the target of the drag and drop | - | ||||||
418 | operation changes, with \a newTarget the new target. | - | ||||||
419 | - | |||||||
420 | \sa target(), actionChanged() | - | ||||||
421 | */ | - | ||||||
422 | - | |||||||
423 | QT_END_NAMESPACE | - | ||||||
424 | - | |||||||
425 | #endif // QT_NO_DRAGANDDROP | - | ||||||
Source code | Switch to Preprocessed file |