~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/kernel/qdrag.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the gui module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <qwidget.h>
 
30
#include <qdrag.h>
 
31
#include <qpixmap.h>
 
32
#include <qpoint.h>
 
33
#include "qdnd_p.h"
 
34
 
 
35
 
 
36
/*!
 
37
    \class QDrag
 
38
    \brief The QDrag class provides support for MIME-based drag and drop data
 
39
    transfer.
 
40
 
 
41
    Drag and drop is an intuitive way for users to copy or move data around in an
 
42
    application, and is used in many desktop environments as a mechanism for copying
 
43
    data between applications. Drag and drop support in Qt is centered around the
 
44
    QDrag class that handles most of the details of a drag and drop operation.
 
45
 
 
46
    The data to be transferred by the drag and drop operation is contained in a
 
47
    QMimeData object. This is specified with the setMimeData() function in the
 
48
    following way:
 
49
 
 
50
    \quotefromfile snippets/dragging/mainwindow.cpp
 
51
    \skipto mousePressEvent
 
52
    \skipto QDrag
 
53
    \printuntil setMimeData
 
54
 
 
55
    Note that setMimeData() assigns ownership of the QMimeData object to the
 
56
    QDrag object. The QDrag must be constructed on the heap with a parent QObject
 
57
    to ensure that Qt can clean up after the drag and drop operation has been
 
58
    completed.
 
59
 
 
60
    A pixmap can be used to represent the data while the drag is in progress, and
 
61
    will move with the cursor to the drop target. This pixmap typically shows an
 
62
    icon that represents the MIME type of the data being transferred, but any
 
63
    pixmap can be set with setPixmap(). Care must be taken to ensure that the
 
64
    pixmap is not too large. The cursor's hot spot can be given a position relative
 
65
    to the top-left corner of the pixmap with the setHotSpot() function. The
 
66
    following code positions the pixmap so that the cursor's hot spot points to
 
67
    the center of its bottom edge:
 
68
 
 
69
    \quotefromfile snippets/separations/finalwidget.cpp
 
70
    \skipto setHotSpot
 
71
    \printuntil ;
 
72
 
 
73
    The source and target widgets can be found with source() and target().
 
74
    These functions are often used to determine whether drag and drop operations
 
75
    started and finished at the same widget, so that special behavior can be
 
76
    implemented.
 
77
 
 
78
    QDrag only deals with the drag and drop operation itself. It is up to the
 
79
    developer to decide when a drag operation begins, and how a QDrag object should
 
80
    be constructed and used. For a given widget, it is often necessary to
 
81
    reimplement \l{QWidget::mousePressEvent()}{mousePressEvent()} to determine
 
82
    whether the user has pressed a mouse button, and reimplement
 
83
    \l{QWidget::mouseMoveEvent()}{mouseMoveEvent()} to check whether a QDrag is
 
84
    required.
 
85
 
 
86
    \sa \link dnd.html Drag and Drop\endlink QClipboard QMimeData
 
87
*/
 
88
 
 
89
/*!
 
90
    Constructs a new drag object for the widget specified by \a dragSource.
 
91
*/
 
92
QDrag::QDrag(QWidget *dragSource)
 
93
    : QObject(*new QDragPrivate, dragSource)
 
94
{
 
95
    Q_D(QDrag);
 
96
    d->source = dragSource;
 
97
    d->target = 0;
 
98
    d->data = 0;
 
99
    d->hotspot = QPoint(-10, -10);
 
100
    d->possible_actions = Qt::CopyAction;
 
101
    d->executed_action = Qt::IgnoreAction;
 
102
}
 
103
 
 
104
/*!
 
105
    Destroys the drag object.
 
106
*/
 
107
QDrag::~QDrag()
 
108
{
 
109
    Q_D(QDrag);
 
110
    delete d->data;
 
111
    QDragManager *manager = QDragManager::self();
 
112
    if (manager && manager->object == this)
 
113
        manager->cancel(false);
 
114
}
 
115
 
 
116
/*!
 
117
    Sets the data to be sent to the given MIME \a data. Ownership of the data is
 
118
    transferred to the QDrag object. 
 
119
*/
 
120
void QDrag::setMimeData(QMimeData *data)
 
121
{
 
122
    Q_D(QDrag);
 
123
    if (d->data != 0)
 
124
        delete d->data;
 
125
    d->data = data;
 
126
}
 
127
 
 
128
/*!
 
129
    Returns the MIME data that is encapsulated by the drag object.
 
130
*/
 
131
QMimeData *QDrag::mimeData() const
 
132
{
 
133
    Q_D(const QDrag);
 
134
    return d->data;
 
135
}
 
136
 
 
137
/*!
 
138
    Sets \a pixmap as the pixmap used to represent the data in a drag
 
139
    and drop operation.
 
140
*/
 
141
void QDrag::setPixmap(const QPixmap &pixmap)
 
142
{
 
143
    Q_D(QDrag);
 
144
    d->pixmap = pixmap;
 
145
}
 
146
 
 
147
/*!
 
148
    Returns the pixmap used to represent the data in a drag and drop operation.
 
149
*/
 
150
QPixmap QDrag::pixmap() const
 
151
{
 
152
    Q_D(const QDrag);
 
153
    return d->pixmap;
 
154
}
 
155
 
 
156
/*!
 
157
    Sets the position of the hot spot relative to the top-left corner of the
 
158
    pixmap used to the point specified by \a hotspot.
 
159
*/
 
160
void QDrag::setHotSpot(const QPoint& hotspot)
 
161
{
 
162
    Q_D(QDrag);
 
163
    d->hotspot = hotspot;
 
164
}
 
165
 
 
166
/*!
 
167
    Returns the position of the hot spot relative to the top-left corner of the
 
168
    cursor.
 
169
*/
 
170
QPoint QDrag::hotSpot() const
 
171
{
 
172
    Q_D(const QDrag);
 
173
    return d->hotspot;
 
174
};
 
175
 
 
176
/*!
 
177
    Returns the source of the drag object. This is the widget where the drag
 
178
    and drop operation originated.
 
179
*/
 
180
QWidget *QDrag::source() const
 
181
{
 
182
    Q_D(const QDrag);
 
183
    return d->source;
 
184
}
 
185
 
 
186
/*!
 
187
    Returns the target of the drag and drop operation. This is the widget where
 
188
    the drag object was dropped.
 
189
*/
 
190
QWidget *QDrag::target() const
 
191
{
 
192
    Q_D(const QDrag);
 
193
    return d->target;
 
194
}
 
195
 
 
196
/*!
 
197
    Starts the drag and drop operation. The actions that are available to the
 
198
    user when the drag and drop operation is completed are specified in
 
199
    \a request. Qt::CopyAction is always allowed.
 
200
*/
 
201
Qt::DropAction QDrag::start(Qt::DropActions request)
 
202
{
 
203
    Q_D(QDrag);
 
204
    Q_ASSERT_X(d->data, "QDrag", "No mimedata set before starting the drag");
 
205
    QDragManager *manager = QDragManager::self();
 
206
    d->possible_actions = request | Qt::CopyAction;
 
207
    if (manager)
 
208
        d->executed_action = manager->drag(this);
 
209
    return d->executed_action;
 
210
}
 
211
 
 
212
/*!
 
213
    Sets the drag \a cursor for the \a action. This alows you
 
214
    to overide the defualt native cursors. To revert to using the
 
215
    native cursor for \a action pass in a null QPixmap as \a cursor.
 
216
    
 
217
    The \a action can only be CopyAction, MoveAction or LinkAction.
 
218
    All other values of DropAction are ignored.
 
219
*/
 
220
void QDrag::setDragCursor(const QPixmap &cursor, Qt::DropAction action)
 
221
{
 
222
    Q_D(QDrag);
 
223
    if (action != Qt::CopyAction && action != Qt::MoveAction && action != Qt::LinkAction)
 
224
        return;
 
225
    if (cursor.isNull())
 
226
        d->customCursors.remove(action);
 
227
    else
 
228
        d->customCursors[action] = cursor;
 
229
}
 
230
 
 
231
/*!
 
232
    \fn void QDrag::actionChanged(Qt::DropAction action)
 
233
 
 
234
    This signal is emitted when the \a action associated with the
 
235
    drag changes.
 
236
 
 
237
    \sa targetChanged()
 
238
*/
 
239
 
 
240
/*!
 
241
    \fn void QDrag::targetChanged(QWidget *newTarget)
 
242
 
 
243
    This signal is emitted when the target of the drag and drop
 
244
    operation changes, with \a newTarget the new target.
 
245
 
 
246
    \sa target(), actionChanged()
 
247
*/