Qt 4.8
qdrag.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <qwidget.h>
43 #include <qdrag.h>
44 #include <qpixmap.h>
45 #include <qpoint.h>
46 #include "qdnd_p.h"
47 
48 #ifndef QT_NO_DRAGANDDROP
49 
51 
113 QDrag::QDrag(QWidget *dragSource)
114  : QObject(*new QDragPrivate, dragSource)
115 {
116  Q_D(QDrag);
117  d->source = dragSource;
118  d->target = 0;
119  d->data = 0;
120  d->hotspot = QPoint(-10, -10);
121  d->possible_actions = Qt::CopyAction;
122  d->executed_action = Qt::IgnoreAction;
123  d->defaultDropAction = Qt::IgnoreAction;
124 }
125 
130 {
131  Q_D(QDrag);
132  delete d->data;
133  QDragManager *manager = QDragManager::self();
134  if (manager && manager->object == this)
135  manager->cancel(false);
136 }
137 
143 {
144  Q_D(QDrag);
145  if (d->data == data)
146  return;
147  if (d->data != 0)
148  delete d->data;
149  d->data = data;
150 }
151 
156 {
157  Q_D(const QDrag);
158  return d->data;
159 }
160 
167 {
168  Q_D(QDrag);
169  d->pixmap = pixmap;
170 }
171 
176 {
177  Q_D(const QDrag);
178  return d->pixmap;
179 }
180 
189 void QDrag::setHotSpot(const QPoint& hotspot)
190 {
191  Q_D(QDrag);
192  d->hotspot = hotspot;
193 }
194 
200 {
201  Q_D(const QDrag);
202  return d->hotspot;
203 }
204 
210 {
211  Q_D(const QDrag);
212  return d->source;
213 }
214 
220 {
221  Q_D(const QDrag);
222  return d->target;
223 }
224 
243 Qt::DropAction QDrag::exec(Qt::DropActions supportedActions)
244 {
245  return exec(supportedActions, Qt::IgnoreAction);
246 }
247 
270 Qt::DropAction QDrag::exec(Qt::DropActions supportedActions, Qt::DropAction defaultDropAction)
271 {
272  Q_D(QDrag);
273  if (!d->data) {
274  qWarning("QDrag: No mimedata set before starting the drag");
275  return d->executed_action;
276  }
277  QDragManager *manager = QDragManager::self();
278  d->defaultDropAction = Qt::IgnoreAction;
279  d->possible_actions = supportedActions;
280 
281  if (manager) {
282  if (defaultDropAction == Qt::IgnoreAction) {
283  if (supportedActions & Qt::MoveAction) {
284  d->defaultDropAction = Qt::MoveAction;
285  } else if (supportedActions & Qt::CopyAction) {
286  d->defaultDropAction = Qt::CopyAction;
287  } else if (supportedActions & Qt::LinkAction) {
288  d->defaultDropAction = Qt::LinkAction;
289  }
290  } else {
291  d->defaultDropAction = defaultDropAction;
292  }
293  d->executed_action = manager->drag(this);
294  }
295 
296  return d->executed_action;
297 }
298 
317 Qt::DropAction QDrag::start(Qt::DropActions request)
318 {
319  Q_D(QDrag);
320  if (!d->data) {
321  qWarning("QDrag: No mimedata set before starting the drag");
322  return d->executed_action;
323  }
324  QDragManager *manager = QDragManager::self();
325  d->defaultDropAction = Qt::IgnoreAction;
326  d->possible_actions = request | Qt::CopyAction;
327  if (manager)
328  d->executed_action = manager->drag(this);
329  return d->executed_action;
330 }
331 
340 void QDrag::setDragCursor(const QPixmap &cursor, Qt::DropAction action)
341 {
342  Q_D(QDrag);
343  if (action != Qt::CopyAction && action != Qt::MoveAction && action != Qt::LinkAction)
344  return;
345  if (cursor.isNull())
346  d->customCursors.remove(action);
347  else
348  d->customCursors[action] = cursor;
349 }
350 
373 
374 #endif // QT_NO_DRAGANDDROP
double d
Definition: qnumeric_p.h:62
void setDragCursor(const QPixmap &cursor, Qt::DropAction action)
Sets the drag cursor for the action.
Definition: qdrag.cpp:340
QWidget * source() const
Returns the source of the drag object.
Definition: qdrag.cpp:209
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setPixmap(const QPixmap &)
Sets pixmap as the pixmap used to represent the data in a drag and drop operation.
Definition: qdrag.cpp:166
The QDrag class provides support for MIME-based drag and drop data transfer.
Definition: qdrag.h:61
void setMimeData(QMimeData *data)
Sets the data to be sent to the given MIME data.
Definition: qdrag.cpp:142
QDrag * object
Definition: qdnd_p.h:238
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void cancel(bool deleteSource=true)
Definition: qdnd_mac.mm:272
Qt::DropAction exec(Qt::DropActions supportedActions=Qt::MoveAction)
Starts the drag and drop operation and returns a value indicating the requested drop action when it i...
Definition: qdrag.cpp:243
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
Qt::DropAction drag(QDrag *)
Definition: qdnd_mac.mm:530
QDrag(QWidget *dragSource)
Constructs a new drag object for the widget specified by dragSource.
Definition: qdrag.cpp:113
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
DropAction
Definition: qnamespace.h:1597
QPixmap pixmap() const
Returns the pixmap used to represent the data in a drag and drop operation.
Definition: qdrag.cpp:175
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
static QDragManager * self()
Definition: qdnd.cpp:163
QMimeData * mimeData() const
Returns the MIME data that is encapsulated by the drag object.
Definition: qdrag.cpp:155
~QDrag()
Destroys the drag object.
Definition: qdrag.cpp:129
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
Qt::DropAction start(Qt::DropActions supportedActions=Qt::CopyAction)
Starts the drag and drop operation and returns a value indicating the requested drop action when it i...
Definition: qdrag.cpp:317
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
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:189
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
QPoint hotSpot() const
Returns the position of the hot spot relative to the top-left corner of the cursor.
Definition: qdrag.cpp:199
QWidget * target() const
Returns the target of the drag and drop operation.
Definition: qdrag.cpp:219