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 "qquickdroparea_p.h"
43 #include "qquickdrag_p.h"
44 #include "qquickitem_p.h"
45 #include "qquickcanvas.h"
47 #include <private/qdeclarativeengine_p.h>
49 QQuickDropAreaDrag::QQuickDropAreaDrag(QQuickDropAreaPrivate *d, QObject *parent)
55 QQuickDropAreaDrag::~QQuickDropAreaDrag()
59 class QQuickDropAreaPrivate : public QQuickItemPrivate
61 Q_DECLARE_PUBLIC(QQuickDropArea)
64 QQuickDropAreaPrivate();
65 ~QQuickDropAreaPrivate();
67 bool hasMatchingKey(const QStringList &keys) const;
69 QStringList getKeys(const QMimeData *mimeData) const;
74 QQuickDropAreaDrag *drag;
75 QDeclarativeGuard<QObject> source;
76 QDeclarativeGuard<QMimeData> mimeData;
79 QQuickDropAreaPrivate::QQuickDropAreaPrivate()
84 QQuickDropAreaPrivate::~QQuickDropAreaPrivate()
90 \qmlclass DropArea QQuickDropArea
91 \inqmlmodule QtQuick 2
92 \brief The DropArea item provides drag and drop handling.
94 A DropArea is an invisible item which receives events when other items are
97 The Drag attached property can be used to notify the DropArea when an Item is
100 The \l keys property can be used to filter drag events which don't include
103 The \l dropItem property is communicated to the source of a drag event as
104 the recipient of a drop on the drag target.
106 The \l delegate property provides a means to specify a component to be
107 instantiated for each active drag over a drag target.
110 QQuickDropArea::QQuickDropArea(QQuickItem *parent)
111 : QQuickItem(*new QQuickDropAreaPrivate, parent)
113 setFlags(ItemAcceptsDrops);
116 QQuickDropArea::~QQuickDropArea()
121 \qmlproperty bool QtQuick2::DropArea::containsDrag
123 This property identifies whether the DropArea currently contains any
127 bool QQuickDropArea::containsDrag() const
129 Q_D(const QQuickDropArea);
134 \qmlproperty stringlist QtQuick2::DropArea::keys
136 This property holds a list of drag keys a DropArea will accept.
138 If no keys are listed the DropArea will accept events from any drag source,
139 otherwise the drag source must have at least one compatible key.
141 \sa QtQuick2::Drag::keys
144 QStringList QQuickDropArea::keys() const
146 Q_D(const QQuickDropArea);
150 void QQuickDropArea::setKeys(const QStringList &keys)
153 if (d->keys != keys) {
156 if (keys.isEmpty()) {
157 d->keyRegExp = QRegExp();
159 QString pattern = QLatin1Char('(') + QRegExp::escape(keys.first());
160 for (int i = 1; i < keys.count(); ++i)
161 pattern += QLatin1Char('|') + QRegExp::escape(keys.at(i));
162 pattern += QLatin1Char(')');
163 d->keyRegExp = QRegExp(pattern.replace(QLatin1String("\\*"), QLatin1String(".+")));
169 QQuickDropAreaDrag *QQuickDropArea::drag()
173 d->drag = new QQuickDropAreaDrag(d);
178 \qmlproperty Object QtQuick2::DropArea::drag.source
180 This property holds the source of a drag.
183 QObject *QQuickDropAreaDrag::source() const
189 \qmlproperty qreal QtQuick2::DropArea::drag.x
190 \qmlproperty qreal QtQuick2::DropArea::drag.y
192 These properties hold the coordinates of the last drag event.
195 qreal QQuickDropAreaDrag::x() const
197 return d->dragPosition.x();
200 qreal QQuickDropAreaDrag::y() const
202 return d->dragPosition.y();
206 \qmlsignal QtQuick2::DropArea::onPositionChanged(DragEvent drag)
208 This handler is called when the position of a drag has changed.
211 void QQuickDropArea::dragMoveEvent(QDragMoveEvent *event)
217 d->dragPosition = event->pos();
219 emit d->drag->positionChanged();
222 QQuickDropEvent dragTargetEvent(d, event);
223 emit positionChanged(&dragTargetEvent);
226 bool QQuickDropAreaPrivate::hasMatchingKey(const QStringList &keys) const
228 if (keyRegExp.isEmpty())
231 foreach (const QString &key, keys) {
232 if (keyRegExp.exactMatch(key))
238 QStringList QQuickDropAreaPrivate::getKeys(const QMimeData *mimeData) const
240 if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(mimeData))
241 return dragMime->keys();
242 return mimeData->formats();
246 \qmlsignal QtQuick2::DropArea::onEntered(DragEvent drag)
248 This handler is called when a \a drag enters the bounds of a DropArea.
251 void QQuickDropArea::dragEnterEvent(QDragEnterEvent *event)
254 const QMimeData *mimeData = event->mimeData();
255 if (!d->effectiveEnable || d->mimeData || !mimeData || !d->hasMatchingKey(d->getKeys(mimeData)))
258 d->dragPosition = event->pos();
261 QQuickDropEvent dragTargetEvent(d, event);
262 emit entered(&dragTargetEvent);
264 if (event->isAccepted()) {
265 d->mimeData = const_cast<QMimeData *>(mimeData);
266 if (QQuickDragMimeData *dragMime = qobject_cast<QQuickDragMimeData *>(d->mimeData))
267 d->source = dragMime->source();
269 d->source = event->source();
270 d->dragPosition = event->pos();
272 emit d->drag->positionChanged();
273 emit d->drag->sourceChanged();
275 emit containsDragChanged();
280 \qmlsignal QtQuick2::DropArea::onExited()
282 This handler is called when a drag exits the bounds of a DropArea.
285 void QQuickDropArea::dragLeaveEvent(QDragLeaveEvent *)
295 emit containsDragChanged();
297 emit d->drag->sourceChanged();
301 \qmlsignal QtQuick2::DropArea::onDropped(DragEvent drop)
303 This handler is called when a drop event occurs within the bounds of a
307 void QQuickDropArea::dropEvent(QDropEvent *event)
313 QQuickDropEvent dragTargetEvent(d, event);
314 emit dropped(&dragTargetEvent);
318 emit containsDragChanged();
320 emit d->drag->sourceChanged();
324 \qmlclass DragEvent QQuickDragEvent
325 \inqmlmodule QtQuick 2
326 \brief The DragEvent object provides information about a drag event.
328 The position of the drag event can be obtained from the \l x and \l y
329 properties, and the \l keys property identifies the drag keys of the event
334 \qmlproperty real QtQuick2::DragEvent::x
336 This property holds the x coordinate of a drag event.
340 \qmlproperty real QtQuick2::DragEvent::y
342 This property holds the y coordinate of a drag event.
346 \qmlproperty Object QtQuick2::DragEvent::drag.source
348 This property holds the source of a drag event.
351 QObject *QQuickDropEvent::source()
353 if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(event->mimeData()))
354 return dragMime->source();
356 return event->source();
360 \qmlproperty stringlist QtQuick2::DragEvent::keys
362 This property holds a list of keys identifying the data type or source of a
366 QStringList QQuickDropEvent::keys() const
368 return d->getKeys(event->mimeData());
372 \qmlproperty enum QtQuick2::DragEvent::action
374 This property holds the action that the \l source is to perform on an accepted drop.
376 The drop action may be one of:
379 \o Qt.CopyAction Copy the data to the target
380 \o Qt.MoveAction Move the data from the source to the target
381 \o Qt.LinkAction Create a link from the source to the target.
382 \o Qt.IgnoreAction Ignore the action (do nothing with the data).
387 \qmlproperty flags QtQuick2::DragEvent::supportedActions
389 This property holds the set of \l {action}{actions} supported by the
394 \qmlproperty real QtQuick2::DragEvent::accepted
396 This property holds whether the drag event was accepted by a handler.
398 The default value is true.
402 \qmlmethod void QtQuick2::DragEvent::accept()
403 \qmlmethod void QtQuick2::DragEvent::accept(enum action)
405 Accepts the drag event.
407 If an \a action is specified it will overwrite the value of the \l action property.
410 void QQuickDropEvent::accept(QDeclarativeV8Function *args)
412 Qt::DropAction action = event->dropAction();
414 if (args->Length() >= 1) {
415 v8::Local<v8::Value> v = (*args)[0];
417 action = Qt::DropAction(v->Int32Value());
419 // get action from arguments.
420 event->setDropAction(action);