1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qsgdragtarget_p.h"
43 #include "qsgitem_p.h"
44 #include "qsgcanvas.h"
47 \qmlclass DragEvent QSGDragEvent
48 \brief The DragEvent object provides information about a drag event.
50 The position of the drag event can be obtained from the \l x and \l
51 properties, the \l keys property identifies the drag keys of the event
52 source and the \l data property contains the payload of the drag event.
56 \qmlproperty real DragEvent::x
58 This property holds the x coordinate of a drag event.
62 \qmlproperty real DragEvent::y
64 This property holds the y coordinate of a drag event.
68 \qmlproperty stringlist DragEvent::keys
70 This property holds a list of keys identifying the data type or source of a
75 \qmlproperty variant DragEvent::data
77 This property holds data payload of a drag event.
81 \qmlproperty real DragEvent::accepted
83 This property holds whether the drag event was accepted by a handler.
85 The default value is true.
88 class QSGDragTargetPrivate : public QSGItemPrivate
90 Q_DECLARE_PUBLIC(QSGDragTarget)
93 QSGDragTargetPrivate();
94 ~QSGDragTargetPrivate();
96 bool hasMatchingKey(const QStringList &keys) const;
101 QPointF dragPosition;
103 bool containsDrag : 1;
106 QSGDragTargetPrivate::QSGDragTargetPrivate()
108 , containsDrag(false)
112 QSGDragTargetPrivate::~QSGDragTargetPrivate()
117 \qmlclass DragTarget QSGDragTarget
118 \brief The DragTarget item provides drag and drop handling.
120 A DragTarget is an invisible item which receives events when another item
123 A MouseArea item can be used to drag items.
125 The \l keys property can be used to filter drag events which don't include
128 The \l dropItem property is communicated to the source of a drag event as
129 the recipient of a drop on the drag target.
131 The \l delegate property provides a means to specify a component to be
132 instantiated for each active drag over a drag target.
135 QSGDragTarget::QSGDragTarget(QSGItem *parent)
136 : QSGItem(*new QSGDragTargetPrivate, parent)
140 QSGDragTarget::~QSGDragTarget()
145 \qmlproperty bool DragTarget::containsDrag
147 This property identifies whether the DragTarget currently contains any
151 bool QSGDragTarget::containsDrag() const
153 Q_D(const QSGDragTarget);
154 return d->containsDrag;
158 \qmlproperty stringlist DragTarget::keys
160 This property holds a list of drag keys a DragTarget will accept.
163 QStringList QSGDragTarget::keys() const
165 Q_D(const QSGDragTarget);
169 void QSGDragTarget::setKeys(const QStringList &keys)
172 if (d->keys != keys) {
175 if (keys.isEmpty()) {
176 d->keyRegExp = QRegExp();
178 QString pattern = QLatin1Char('(') + QRegExp::escape(keys.first());
179 for (int i = 1; i < keys.count(); ++i)
180 pattern += QLatin1Char('|') + QRegExp::escape(keys.at(i));
181 pattern += QLatin1Char(')');
182 d->keyRegExp = QRegExp(pattern.replace(QLatin1String("\\*"), QLatin1String(".+")));
189 \qmlproperty Item DragTarget::dropItem
191 This property identifies an item as the recipient of a drop event within
194 \sa MouseArea::drag.dropItem
197 QSGItem *QSGDragTarget::dropItem() const
199 Q_D(const QSGDragTarget);
203 void QSGDragTarget::setDropItem(QSGItem *item)
206 if (d->dropItem != item) {
208 emit dropItemChanged();
212 void QSGDragTarget::resetDropItem()
217 emit dropItemChanged();
221 qreal QSGDragTarget::dragX() const
223 Q_D(const QSGDragTarget);
224 return d->dragPosition.x();
227 qreal QSGDragTarget::dragY() const
229 Q_D(const QSGDragTarget);
230 return d->dragPosition.y();
233 QVariant QSGDragTarget::dragData() const
235 Q_D(const QSGDragTarget);
240 \qmlsignal DragTarget::onPositionChanged(DragEvent drag)
241 \qmlattachedsignal DragTarget::onPositionChanged(DragEvent drag)
243 This handler is called when the position of a drag has changed.
246 void QSGDragTarget::dragMoveEvent(QSGDragEvent *event)
249 if (!d->containsDrag) {
250 event->setAccepted(false);
254 event->setDropItem(d->dropItem);
256 d->dragPosition = event->position();
257 emit dragPositionChanged();
259 QSGDragTargetEvent dragTargetEvent(event);
260 emit positionChanged(&dragTargetEvent);
263 bool QSGDragTargetPrivate::hasMatchingKey(const QStringList &keys) const
265 if (keyRegExp.isEmpty())
268 foreach (const QString &key, keys) {
269 if (keyRegExp.exactMatch(key))
276 \qmlsignal DragTarget::onEntered(DragEvent drag)
277 \qmlattachedsignal DragTarget::onEntered(DragEvent drag)
279 This handler is called when a drag enters the bounds of a DragTarget.
282 void QSGDragTarget::dragEnterEvent(QSGDragEvent *event)
285 if (!d->effectiveEnable || !d->hasMatchingKey(event->keys()) || d->containsDrag) {
286 event->setAccepted(false);
290 event->setDropItem(d->dropItem);
292 QSGDragTargetEvent dragTargetEvent(event);
293 emit entered(&dragTargetEvent);
295 if (event->isAccepted()) {
297 d->dragData = event->data();
298 d->containsDrag = true;
299 if (!d->dragData.isNull())
300 emit dragDataChanged();
301 emit containsDragChanged();
306 \qmlsignal DragTarget::onExited(DragEvent drag)
307 \qmlattachedsignal DragTarget::onExited(DragEvent drag)
309 This handler is called when a drag exits the bounds of a DragTarget.
312 void QSGDragTarget::dragExitEvent(QSGDragEvent *event)
315 if (!d->containsDrag) {
316 event->setAccepted(false);
320 QSGDragTargetEvent dragTargetEvent(event);
321 emit exited(&dragTargetEvent);
323 d->containsDrag = false;
324 emit containsDragChanged();
325 if (!d->dragData.isNull()) {
326 d->dragData = QVariant();
327 emit dragDataChanged();
332 \qmlsignal DragTarget::onDropped(DragEvent drag)
333 \qmlattachedsignal DragTarget::onDropped(DragEvent drag)
335 This handler is called when a drop event occurs within the bounds of a
339 void QSGDragTarget::dragDropEvent(QSGDragEvent *event)
342 if (!d->containsDrag) {
343 event->setAccepted(false);
347 event->setDropItem(d->dropItem);
349 QSGDragTargetEvent dragTargetEvent(event);
350 emit dropped(&dragTargetEvent);
352 d->containsDrag = false;
353 emit containsDragChanged();
354 if (!d->dragData.isNull()) {
355 d->dragData = QVariant();
356 emit dragDataChanged();