Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
12#include <QtCore/qpointer.h>
13
15
76 : QObject(*new QDragPrivate, dragSource)
77{
78 Q_D(QDrag);
79 d->source = dragSource;
80 d->target = nullptr;
81 d->data = nullptr;
82 d->hotspot = QPoint(-10, -10);
83 d->executed_action = Qt::IgnoreAction;
84 d->supported_actions = Qt::IgnoreAction;
85 d->default_action = Qt::IgnoreAction;
86}
87
92{
93 Q_D(QDrag);
94 delete d->data;
95}
96
102{
103 Q_D(QDrag);
104 if (d->data == data)
105 return;
106 if (d->data != nullptr)
107 delete d->data;
108 d->data = data;
109}
110
115{
116 Q_D(const QDrag);
117 return d->data;
118}
119
126{
127 Q_D(QDrag);
128 d->pixmap = pixmap;
129}
130
135{
136 Q_D(const QDrag);
137 return d->pixmap;
138}
139
148void QDrag::setHotSpot(const QPoint& hotspot)
149{
150 Q_D(QDrag);
151 d->hotspot = hotspot;
152}
153
159{
160 Q_D(const QDrag);
161 return d->hotspot;
162}
163
169{
170 Q_D(const QDrag);
171 return d->source;
172}
173
179{
180 Q_D(const QDrag);
181 return d->target;
182}
183
201Qt::DropAction QDrag::exec(Qt::DropActions supportedActions)
202{
204}
205
225Qt::DropAction QDrag::exec(Qt::DropActions supportedActions, Qt::DropAction defaultDropAction)
226{
227 Q_D(QDrag);
228 if (!d->data) {
229 qWarning("QDrag: No mimedata set before starting the drag");
230 return d->executed_action;
231 }
232 Qt::DropAction transformedDefaultDropAction = Qt::IgnoreAction;
233
234 if (defaultDropAction == Qt::IgnoreAction) {
236 transformedDefaultDropAction = Qt::MoveAction;
237 } else if (supportedActions & Qt::CopyAction) {
238 transformedDefaultDropAction = Qt::CopyAction;
239 } else if (supportedActions & Qt::LinkAction) {
240 transformedDefaultDropAction = Qt::LinkAction;
241 }
242 } else {
243 transformedDefaultDropAction = defaultDropAction;
244 }
245 d->supported_actions = supportedActions;
246 d->default_action = transformedDefaultDropAction;
247 QPointer<QDrag> self = this;
248 auto executed_action = QDragManager::self()->drag(self.data());
249 if (self.isNull())
250 return Qt::IgnoreAction;
251 d->executed_action = executed_action;
252 return d->executed_action;
253}
254
265{
266 Q_D(QDrag);
267 if (cursor.isNull())
268 d->customCursors.remove(action);
269 else
270 d->customCursors[action] = cursor;
271}
272
280{
282
283 Q_D(const QDrag);
284 const Iterator it = d->customCursors.constFind(action);
285 if (it != d->customCursors.constEnd())
286 return it.value();
287
289 switch (action) {
290 case Qt::MoveAction:
291 shape = Qt::DragMoveCursor;
292 break;
293 case Qt::CopyAction:
294 shape = Qt::DragCopyCursor;
295 break;
296 case Qt::LinkAction:
297 shape = Qt::DragLinkCursor;
298 break;
299 default:
300 shape = Qt::ForbiddenCursor;
301 }
302 return QGuiApplicationPrivate::instance()->getPixmapCursor(shape);
303}
304
310Qt::DropActions QDrag::supportedActions() const
311{
312 Q_D(const QDrag);
313 return d->supported_actions;
314}
315
316
323{
324 Q_D(const QDrag);
325 return d->default_action;
326}
327
337{
339 platformDrag->cancelDrag();
340}
341
361
362#include "moc_qdrag.cpp"
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:148
Qt::DropAction exec(Qt::DropActions supportedActions=Qt::MoveAction)
Definition qdrag.cpp:201
QMimeData * mimeData() const
Returns the MIME data that is encapsulated by the drag object.
Definition qdrag.cpp:114
void setDragCursor(const QPixmap &cursor, Qt::DropAction action)
Sets the drag cursor for the action.
Definition qdrag.cpp:264
void setMimeData(QMimeData *data)
Sets the data to be sent to the given MIME data.
Definition qdrag.cpp:101
QDrag(QObject *dragSource)
Constructs a new drag object for the widget specified by dragSource.
Definition qdrag.cpp:75
QObject * source() const
Returns the source of the drag object.
Definition qdrag.cpp:168
QPixmap pixmap() const
Returns the pixmap used to represent the data in a drag and drop operation.
Definition qdrag.cpp:134
Qt::DropAction defaultAction() const
Returns the default proposed drop action for this drag operation.
Definition qdrag.cpp:322
void setPixmap(const QPixmap &)
Sets pixmap as the pixmap used to represent the data in a drag and drop operation.
Definition qdrag.cpp:125
QPixmap dragCursor(Qt::DropAction action) const
Returns the drag cursor for the action.
Definition qdrag.cpp:279
QPoint hotSpot() const
Returns the position of the hot spot relative to the top-left corner of the cursor.
Definition qdrag.cpp:158
~QDrag()
Destroys the drag object.
Definition qdrag.cpp:91
QObject * target() const
Returns the target of the drag and drop operation.
Definition qdrag.cpp:178
static void cancel()
Cancels a drag operation initiated by Qt.
Definition qdrag.cpp:336
Qt::DropActions supportedActions() const
Returns the set of possible drop actions for this drag operation.
Definition qdrag.cpp:310
static QPlatformIntegration * platformIntegration()
static QGuiApplicationPrivate * instance()
\inmodule QtCore
Definition qmimedata.h:16
\inmodule QtCore
Definition qobject.h:103
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:25
const_iterator constEnd() const noexcept
Definition qset.h:143
const_iterator constFind(const T &value) const
Definition qset.h:161
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition qstring.h:994
QChar * data()
Returns a pointer to the data stored in the QString.
Definition qstring.h:1240
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:166
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
dialog exec()
widget render & pixmap