summaryrefslogtreecommitdiffstats
path: root/src/declarative/items/qsgdragtarget.cpp
blob: 5b6d36ee1111d6d0da477558594baa18f4260c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qsgdragtarget_p.h"
#include "qsgitem_p.h"
#include "qsgcanvas.h"

QT_BEGIN_NAMESPACE

/*!
    \qmlclass DragEvent QSGDragEvent
    \brief The DragEvent object provides information about a drag event.

    The position of the drag event can be obtained from the \l x and \l
    properties, the \l keys property identifies the drag keys of the event
    source and the \l data property contains the payload of the drag event.
*/

/*!
    \qmlproperty real DragEvent::x

    This property holds the x coordinate of a drag event.
*/

/*!
    \qmlproperty real DragEvent::y

    This property holds the y coordinate of a drag event.
*/

/*!
    \qmlproperty stringlist DragEvent::keys

    This property holds a list of keys identifying the data type or source of a
    drag event.
*/

/*!
    \qmlproperty variant DragEvent::data

    This property holds data payload of a drag event.
*/

/*!
    \qmlproperty real DragEvent::accepted

    This property holds whether the drag event was accepted by a handler.

    The default value is true.
*/

class QSGDragTargetPrivate : public QSGItemPrivate
{
    Q_DECLARE_PUBLIC(QSGDragTarget)

public:
    QSGDragTargetPrivate();
    ~QSGDragTargetPrivate();

    bool hasMatchingKey(const QStringList &keys) const;

    QStringList keys;
    QRegExp keyRegExp;
    QVariant dragData;
    QPointF dragPosition;
    QSGItem *dropItem;
    bool containsDrag : 1;
};

QSGDragTargetPrivate::QSGDragTargetPrivate()
    : dropItem(0)
    , containsDrag(false)
{
}

QSGDragTargetPrivate::~QSGDragTargetPrivate()
{
}

/*!
    \qmlclass DragTarget QSGDragTarget
    \brief The DragTarget item provides drag and drop handling.

    A DragTarget is an invisible item which receives events when another item
    is dragged over it.

    A MouseArea item can be used to drag items.

    The \l keys property can be used to filter drag events which don't include
    a matching key.

    The \l dropItem property is communicated to the source of a drag event as
    the recipient of a drop on the drag target.

    The \l delegate property provides a means to specify a component to be
    instantiated for each active drag over a drag target.
*/

QSGDragTarget::QSGDragTarget(QSGItem *parent)
    : QSGItem(*new QSGDragTargetPrivate, parent)
{
}

QSGDragTarget::~QSGDragTarget()
{
}

/*!
    \qmlproperty bool DragTarget::containsDrag

    This property identifies whether the DragTarget currently contains any
    dragged items.
*/

bool QSGDragTarget::containsDrag() const
{
    Q_D(const QSGDragTarget);
    return d->containsDrag;
}

/*!
    \qmlproperty stringlist DragTarget::keys

    This property holds a list of drag keys a DragTarget will accept.
*/

QStringList QSGDragTarget::keys() const
{
    Q_D(const QSGDragTarget);
    return d->keys;
}

void QSGDragTarget::setKeys(const QStringList &keys)
{
    Q_D(QSGDragTarget);
    if (d->keys != keys) {
        d->keys = keys;

        if (keys.isEmpty()) {
            d->keyRegExp = QRegExp();
        } else {
            QString pattern = QLatin1Char('(') + QRegExp::escape(keys.first());
            for (int i = 1; i < keys.count(); ++i)
                pattern += QLatin1Char('|') + QRegExp::escape(keys.at(i));
            pattern += QLatin1Char(')');
            d->keyRegExp = QRegExp(pattern.replace(QLatin1String("\\*"), QLatin1String(".+")));
        }
        emit keysChanged();
    }
}

/*!
    \qmlproperty Item DragTarget::dropItem

    This property identifies an item as the recipient of a drop event within
    a DragTarget.

    \sa MouseArea::drag.dropItem
*/

QSGItem *QSGDragTarget::dropItem() const
{
    Q_D(const QSGDragTarget);
    return d->dropItem;
}

void QSGDragTarget::setDropItem(QSGItem *item)
{
    Q_D(QSGDragTarget);
    if (d->dropItem != item) {
        d->dropItem = item;
        emit dropItemChanged();
    }
}

void QSGDragTarget::resetDropItem()
{
    Q_D(QSGDragTarget);
    if (d->dropItem) {
        d->dropItem = 0;
        emit dropItemChanged();
    }
}

qreal QSGDragTarget::dragX() const
{
    Q_D(const QSGDragTarget);
    return d->dragPosition.x();
}

qreal QSGDragTarget::dragY() const
{
    Q_D(const QSGDragTarget);
    return d->dragPosition.y();
}

QVariant QSGDragTarget::dragData() const
{
    Q_D(const QSGDragTarget);
    return d->dragData;
}

/*!
    \qmlsignal DragTarget::onPositionChanged(DragEvent drag)
    \qmlattachedsignal DragTarget::onPositionChanged(DragEvent drag)

    This handler is called when the position of a drag has changed.
*/

void QSGDragTarget::dragMoveEvent(QSGDragEvent *event)
{
    Q_D(QSGDragTarget);
    if (!d->containsDrag) {
        event->setAccepted(false);
        return;
    }

    event->setDropItem(d->dropItem);

    d->dragPosition = event->position();
    emit dragPositionChanged();

    QSGDragTargetEvent dragTargetEvent(event);
    emit positionChanged(&dragTargetEvent);
}

bool QSGDragTargetPrivate::hasMatchingKey(const QStringList &keys) const
{
    if (keyRegExp.isEmpty())
        return true;

    foreach (const QString &key, keys) {
        if (keyRegExp.exactMatch(key))
            return true;
    }
    return false;
}

/*!
    \qmlsignal DragTarget::onEntered(DragEvent drag)
    \qmlattachedsignal DragTarget::onEntered(DragEvent drag)

    This handler is called when a drag enters the bounds of a DragTarget.
*/

void QSGDragTarget::dragEnterEvent(QSGDragEvent *event)
{
    Q_D(QSGDragTarget);
    if (!d->effectiveEnable || !d->hasMatchingKey(event->keys()) || d->containsDrag) {
        event->setAccepted(false);
        return;
    }

    event->setDropItem(d->dropItem);

    QSGDragTargetEvent dragTargetEvent(event);
    emit entered(&dragTargetEvent);

    if (event->isAccepted()) {

        d->dragData = event->data();
        d->containsDrag = true;
        if (!d->dragData.isNull())
            emit dragDataChanged();
        emit containsDragChanged();
    }
}

/*!
    \qmlsignal DragTarget::onExited(DragEvent drag)
    \qmlattachedsignal DragTarget::onExited(DragEvent drag)

    This handler is called when a drag exits the bounds of a DragTarget.
*/

void QSGDragTarget::dragExitEvent(QSGDragEvent *event)
{
    Q_D(QSGDragTarget);
    if (!d->containsDrag) {
        event->setAccepted(false);
        return;
    }

    QSGDragTargetEvent dragTargetEvent(event);
    emit exited(&dragTargetEvent);

    d->containsDrag = false;
    emit containsDragChanged();
    if (!d->dragData.isNull()) {
        d->dragData = QVariant();
        emit dragDataChanged();
    }
}

/*!
    \qmlsignal DragTarget::onDropped(DragEvent drag)
    \qmlattachedsignal DragTarget::onDropped(DragEvent drag)

    This handler is called when a drop event occurs within the bounds of a
    a DragTarget.
*/

void QSGDragTarget::dragDropEvent(QSGDragEvent *event)
{
    Q_D(QSGDragTarget);
    if (!d->containsDrag) {
        event->setAccepted(false);
        return;
    }

    event->setDropItem(d->dropItem);

    QSGDragTargetEvent dragTargetEvent(event);
    emit dropped(&dragTargetEvent);

    d->containsDrag = false;
    emit containsDragChanged();
    if (!d->dragData.isNull()) {
        d->dragData = QVariant();
        emit dragDataChanged();
    }
}

QT_END_NAMESPACE