Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qdrag.cpp
Go to the documentation of this file.
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 <qdrag.h>
5#include "private/qguiapplication_p.h"
6#include "qpa/qplatformintegration.h"
7#include "qpa/qplatformdrag.h"
8#include <qpixmap.h>
9#include <qpoint.h>
10#include "qdnd_p.h"
11
13
74 : QObject(*new QDragPrivate, dragSource)
75{
76 Q_D(QDrag);
77 d->source = dragSource;
78 d->target = nullptr;
79 d->data = nullptr;
80 d->hotspot = QPoint(-10, -10);
81 d->executed_action = Qt::IgnoreAction;
82 d->supported_actions = Qt::IgnoreAction;
83 d->default_action = Qt::IgnoreAction;
84}
85
90{
91 Q_D(QDrag);
92 delete d->data;
93}
94
100{
101 Q_D(QDrag);
102 if (d->data == data)
103 return;
104 if (d->data != nullptr)
105 delete d->data;
106 d->data = data;
107}
108
113{
114 Q_D(const QDrag);
115 return d->data;
116}
117
124{
125 Q_D(QDrag);
126 d->pixmap = pixmap;
127}
128
133{
134 Q_D(const QDrag);
135 return d->pixmap;
136}
137
146void QDrag::setHotSpot(const QPoint& hotspot)
147{
148 Q_D(QDrag);
149 d->hotspot = hotspot;
150}
151
157{
158 Q_D(const QDrag);
159 return d->hotspot;
160}
161
167{
168 Q_D(const QDrag);
169 return d->source;
170}
171
177{
178 Q_D(const QDrag);
179 return d->target;
180}
181
199Qt::DropAction QDrag::exec(Qt::DropActions supportedActions)
200{
202}
203
223Qt::DropAction QDrag::exec(Qt::DropActions supportedActions, Qt::DropAction defaultDropAction)
224{
225 Q_D(QDrag);
226 if (!d->data) {
227 qWarning("QDrag: No mimedata set before starting the drag");
228 return d->executed_action;
229 }
230 Qt::DropAction transformedDefaultDropAction = Qt::IgnoreAction;
231
232 if (defaultDropAction == Qt::IgnoreAction) {
234 transformedDefaultDropAction = Qt::MoveAction;
235 } else if (supportedActions & Qt::CopyAction) {
236 transformedDefaultDropAction = Qt::CopyAction;
237 } else if (supportedActions & Qt::LinkAction) {
238 transformedDefaultDropAction = Qt::LinkAction;
239 }
240 } else {
241 transformedDefaultDropAction = defaultDropAction;
242 }
243 d->supported_actions = supportedActions;
244 d->default_action = transformedDefaultDropAction;
245 QPointer<QDrag> self = this;
246 auto executed_action = QDragManager::self()->drag(self.data());
247 if (self.isNull())
248 return Qt::IgnoreAction;
249 d->executed_action = executed_action;
250 return d->executed_action;
251}
252
263{
264 Q_D(QDrag);
265 if (cursor.isNull())
266 d->customCursors.remove(action);
267 else
268 d->customCursors[action] = cursor;
269}
270
278{
280
281 Q_D(const QDrag);
282 const Iterator it = d->customCursors.constFind(action);
283 if (it != d->customCursors.constEnd())
284 return it.value();
285
287 switch (action) {
288 case Qt::MoveAction:
289 shape = Qt::DragMoveCursor;
290 break;
291 case Qt::CopyAction:
292 shape = Qt::DragCopyCursor;
293 break;
294 case Qt::LinkAction:
295 shape = Qt::DragLinkCursor;
296 break;
297 default:
298 shape = Qt::ForbiddenCursor;
299 }
301}
302
308Qt::DropActions QDrag::supportedActions() const
309{
310 Q_D(const QDrag);
311 return d->supported_actions;
312}
313
314
321{
322 Q_D(const QDrag);
323 return d->default_action;
324}
325
335{
337 platformDrag->cancelDrag();
338}
339
359
360#include "moc_qdrag.cpp"
Qt::DropAction drag(QDrag *)
Definition qdnd.cpp:61
static QDragManager * self()
Definition qdnd.cpp:30
\inmodule QtGui
Definition qdrag.h:22
void setHotSpot(const QPoint &hotspot)
Sets the position of the hot spot relative to the top-left corner of the pixmap used to the point spe...
Definition qdrag.cpp:146
Qt::DropAction exec(Qt::DropActions supportedActions=Qt::MoveAction)
Definition qdrag.cpp:199
QMimeData * mimeData() const
Returns the MIME data that is encapsulated by the drag object.
Definition qdrag.cpp:112
void setDragCursor(const QPixmap &cursor, Qt::DropAction action)
Sets the drag cursor for the action.
Definition qdrag.cpp:262
void setMimeData(QMimeData *data)
Sets the data to be sent to the given MIME data.
Definition qdrag.cpp:99
QDrag(QObject *dragSource)
Constructs a new drag object for the widget specified by dragSource.
Definition qdrag.cpp:73
QObject * source() const
Returns the source of the drag object.
Definition qdrag.cpp:166
QPixmap pixmap() const
Returns the pixmap used to represent the data in a drag and drop operation.
Definition qdrag.cpp:132
Qt::DropAction defaultAction() const
Returns the default proposed drop action for this drag operation.
Definition qdrag.cpp:320
void setPixmap(const QPixmap &)
Sets pixmap as the pixmap used to represent the data in a drag and drop operation.
Definition qdrag.cpp:123
QPixmap dragCursor(Qt::DropAction action) const
Returns the drag cursor for the action.
Definition qdrag.cpp:277
QPoint hotSpot() const
Returns the position of the hot spot relative to the top-left corner of the cursor.
Definition qdrag.cpp:156
~QDrag()
Destroys the drag object.
Definition qdrag.cpp:89
QObject * target() const
Returns the target of the drag and drop operation.
Definition qdrag.cpp:176
static void cancel()
Cancels a drag operation initiated by Qt.
Definition qdrag.cpp:334
Qt::DropActions supportedActions() const
Returns the set of possible drop actions for this drag operation.
Definition qdrag.cpp:308
static QPlatformIntegration * platformIntegration()
static QGuiApplicationPrivate * instance()
QPixmap getPixmapCursor(Qt::CursorShape cshape)
\inmodule QtCore
Definition qmimedata.h:16
\inmodule QtCore
Definition qobject.h:90
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
The QPlatformDrag class provides an abstraction for drag.
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore
Definition qpointer.h:18
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition qstring.h:898
QChar * data()
Returns a pointer to the data stored in the QString.
Definition qstring.h:1095
QCursor cursor
QSet< QString >::iterator it
Combined button and popup list for selecting options.
CursorShape
@ DragCopyCursor
@ DragLinkCursor
@ DragMoveCursor
@ ForbiddenCursor
DropAction
@ CopyAction
@ IgnoreAction
@ MoveAction
@ LinkAction
#define qWarning
Definition qlogging.h:162
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
dialog exec()
widget render & pixmap