qdrag.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9