1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 "qquickdroparea_p.h"
43 #include "qquickdrag_p.h"
44 #include "qquickitem_p.h"
45 #include "qquickcanvas.h"
47 #include <private/qdeclarativeengine_p.h>
51 QQuickDropAreaDrag::QQuickDropAreaDrag(QQuickDropAreaPrivate *d, QObject *parent)
57 QQuickDropAreaDrag::~QQuickDropAreaDrag()
61 class QQuickDropAreaPrivate : public QQuickItemPrivate
63 Q_DECLARE_PUBLIC(QQuickDropArea)
66 QQuickDropAreaPrivate();
67 ~QQuickDropAreaPrivate();
69 bool hasMatchingKey(const QStringList &keys) const;
71 QStringList getKeys(const QMimeData *mimeData) const;
76 QQuickDropAreaDrag *drag;
77 QDeclarativeGuard<QObject> source;
78 QDeclarativeGuard<QMimeData> mimeData;
81 QQuickDropAreaPrivate::QQuickDropAreaPrivate()
86 QQuickDropAreaPrivate::~QQuickDropAreaPrivate()
92 \qmlclass DropArea QQuickDropArea
93 \inqmlmodule QtQuick 2
94 \brief The DropArea item provides drag and drop handling.
96 A DropArea is an invisible item which receives events when other items are
99 The Drag attached property can be used to notify the DropArea when an Item is
102 The \l keys property can be used to filter drag events which don't include
105 The \l dropItem property is communicated to the source of a drag event as
106 the recipient of a drop on the drag target.
108 The \l delegate property provides a means to specify a component to be
109 instantiated for each active drag over a drag target.
112 QQuickDropArea::QQuickDropArea(QQuickItem *parent)
113 : QQuickItem(*new QQuickDropAreaPrivate, parent)
115 setFlags(ItemAcceptsDrops);
118 QQuickDropArea::~QQuickDropArea()
123 \qmlproperty bool QtQuick2::DropArea::containsDrag
125 This property identifies whether the DropArea currently contains any
129 bool QQuickDropArea::containsDrag() const
131 Q_D(const QQuickDropArea);
136 \qmlproperty stringlist QtQuick2::DropArea::keys
138 This property holds a list of drag keys a DropArea will accept.
140 If no keys are listed the DropArea will accept events from any drag source,
141 otherwise the drag source must have at least one compatible key.
143 \sa QtQuick2::Drag::keys
146 QStringList QQuickDropArea::keys() const
148 Q_D(const QQuickDropArea);
152 void QQuickDropArea::setKeys(const QStringList &keys)
155 if (d->keys != keys) {
158 if (keys.isEmpty()) {
159 d->keyRegExp = QRegExp();
161 QString pattern = QLatin1Char('(') + QRegExp::escape(keys.first());
162 for (int i = 1; i < keys.count(); ++i)
163 pattern += QLatin1Char('|') + QRegExp::escape(keys.at(i));
164 pattern += QLatin1Char(')');
165 d->keyRegExp = QRegExp(pattern.replace(QLatin1String("\\*"), QLatin1String(".+")));
171 QQuickDropAreaDrag *QQuickDropArea::drag()
175 d->drag = new QQuickDropAreaDrag(d);
180 \qmlproperty Object QtQuick2::DropArea::drag.source
182 This property holds the source of a drag.
185 QObject *QQuickDropAreaDrag::source() const
191 \qmlproperty qreal QtQuick2::DropArea::drag.x
192 \qmlproperty qreal QtQuick2::DropArea::drag.y
194 These properties hold the coordinates of the last drag event.
197 qreal QQuickDropAreaDrag::x() const
199 return d->dragPosition.x();
202 qreal QQuickDropAreaDrag::y() const
204 return d->dragPosition.y();
208 \qmlsignal QtQuick2::DropArea::onPositionChanged(DragEvent drag)
210 This handler is called when the position of a drag has changed.
213 void QQuickDropArea::dragMoveEvent(QDragMoveEvent *event)
219 d->dragPosition = event->pos();
221 emit d->drag->positionChanged();
224 QQuickDropEvent dragTargetEvent(d, event);
225 emit positionChanged(&dragTargetEvent);
228 bool QQuickDropAreaPrivate::hasMatchingKey(const QStringList &keys) const
230 if (keyRegExp.isEmpty())
233 foreach (const QString &key, keys) {
234 if (keyRegExp.exactMatch(key))
240 QStringList QQuickDropAreaPrivate::getKeys(const QMimeData *mimeData) const
242 if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(mimeData))
243 return dragMime->keys();
244 return mimeData->formats();
248 \qmlsignal QtQuick2::DropArea::onEntered(DragEvent drag)
250 This handler is called when a \a drag enters the bounds of a DropArea.
253 void QQuickDropArea::dragEnterEvent(QDragEnterEvent *event)
256 const QMimeData *mimeData = event->mimeData();
257 if (!d->effectiveEnable || d->mimeData || !mimeData || !d->hasMatchingKey(d->getKeys(mimeData)))
260 d->dragPosition = event->pos();
263 QQuickDropEvent dragTargetEvent(d, event);
264 emit entered(&dragTargetEvent);
266 if (event->isAccepted()) {
267 d->mimeData = const_cast<QMimeData *>(mimeData);
268 if (QQuickDragMimeData *dragMime = qobject_cast<QQuickDragMimeData *>(d->mimeData))
269 d->source = dragMime->source();
271 d->source = event->source();
272 d->dragPosition = event->pos();
274 emit d->drag->positionChanged();
275 emit d->drag->sourceChanged();
277 emit containsDragChanged();
282 \qmlsignal QtQuick2::DropArea::onExited()
284 This handler is called when a drag exits the bounds of a DropArea.
287 void QQuickDropArea::dragLeaveEvent(QDragLeaveEvent *)
297 emit containsDragChanged();
299 emit d->drag->sourceChanged();
303 \qmlsignal QtQuick2::DropArea::onDropped(DragEvent drop)
305 This handler is called when a drop event occurs within the bounds of a
309 void QQuickDropArea::dropEvent(QDropEvent *event)
315 QQuickDropEvent dragTargetEvent(d, event);
316 emit dropped(&dragTargetEvent);
320 emit containsDragChanged();
322 emit d->drag->sourceChanged();
326 \qmlclass DragEvent QQuickDragEvent
327 \inqmlmodule QtQuick 2
328 \brief The DragEvent object provides information about a drag event.
330 The position of the drag event can be obtained from the \l x and \l y
331 properties, and the \l keys property identifies the drag keys of the event
336 \qmlproperty real QtQuick2::DragEvent::x
338 This property holds the x coordinate of a drag event.
342 \qmlproperty real QtQuick2::DragEvent::y
344 This property holds the y coordinate of a drag event.
348 \qmlproperty Object QtQuick2::DragEvent::drag.source
350 This property holds the source of a drag event.
353 QObject *QQuickDropEvent::source()
355 if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(event->mimeData()))
356 return dragMime->source();
358 return event->source();
362 \qmlproperty stringlist QtQuick2::DragEvent::keys
364 This property holds a list of keys identifying the data type or source of a
368 QStringList QQuickDropEvent::keys() const
370 return d->getKeys(event->mimeData());
374 \qmlproperty enum QtQuick2::DragEvent::action
376 This property holds the action that the \l source is to perform on an accepted drop.
378 The drop action may be one of:
381 \o Qt.CopyAction Copy the data to the target
382 \o Qt.MoveAction Move the data from the source to the target
383 \o Qt.LinkAction Create a link from the source to the target.
384 \o Qt.IgnoreAction Ignore the action (do nothing with the data).
389 \qmlproperty flags QtQuick2::DragEvent::supportedActions
391 This property holds the set of \l {action}{actions} supported by the
396 \qmlproperty real QtQuick2::DragEvent::accepted
398 This property holds whether the drag event was accepted by a handler.
400 The default value is true.
404 \qmlmethod void QtQuick2::DragEvent::accept()
405 \qmlmethod void QtQuick2::DragEvent::accept(enum action)
407 Accepts the drag event.
409 If an \a action is specified it will overwrite the value of the \l action property.
412 void QQuickDropEvent::accept(QDeclarativeV8Function *args)
414 Qt::DropAction action = event->dropAction();
416 if (args->Length() >= 1) {
417 v8::Local<v8::Value> v = (*args)[0];
419 action = Qt::DropAction(v->Int32Value());
421 // get action from arguments.
422 event->setDropAction(action);