1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** 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 "qquickwindow.h"
47 #include <private/qqmlengine_p.h>
49 #ifndef QT_NO_DRAGANDDROP
53 QQuickDropAreaDrag::QQuickDropAreaDrag(QQuickDropAreaPrivate *d, QObject *parent)
59 QQuickDropAreaDrag::~QQuickDropAreaDrag()
63 class QQuickDropAreaPrivate : public QQuickItemPrivate
65 Q_DECLARE_PUBLIC(QQuickDropArea)
68 QQuickDropAreaPrivate();
69 ~QQuickDropAreaPrivate();
71 bool hasMatchingKey(const QStringList &keys) const;
73 QStringList getKeys(const QMimeData *mimeData) const;
78 QQuickDropAreaDrag *drag;
79 QQmlGuard<QObject> source;
80 QQmlGuard<QMimeData> mimeData;
83 QQuickDropAreaPrivate::QQuickDropAreaPrivate()
88 QQuickDropAreaPrivate::~QQuickDropAreaPrivate()
94 \qmlclass DropArea QQuickDropArea
95 \inqmlmodule QtQuick 2
96 \ingroup qtquick-input
97 \brief For specifying drag and drop handling in an area
99 A DropArea is an invisible item which receives events when other items are
102 The Drag attached property can be used to notify the DropArea when an Item is
105 The \l keys property can be used to filter drag events which don't include
108 The \l dropItem property is communicated to the source of a drag event as
109 the recipient of a drop on the drag target.
111 The \l delegate property provides a means to specify a component to be
112 instantiated for each active drag over a drag target.
115 QQuickDropArea::QQuickDropArea(QQuickItem *parent)
116 : QQuickItem(*new QQuickDropAreaPrivate, parent)
118 setFlags(ItemAcceptsDrops);
121 QQuickDropArea::~QQuickDropArea()
126 \qmlproperty bool QtQuick2::DropArea::containsDrag
128 This property identifies whether the DropArea currently contains any
132 bool QQuickDropArea::containsDrag() const
134 Q_D(const QQuickDropArea);
139 \qmlproperty stringlist QtQuick2::DropArea::keys
141 This property holds a list of drag keys a DropArea will accept.
143 If no keys are listed the DropArea will accept events from any drag source,
144 otherwise the drag source must have at least one compatible key.
146 \sa QtQuick2::Drag::keys
149 QStringList QQuickDropArea::keys() const
151 Q_D(const QQuickDropArea);
155 void QQuickDropArea::setKeys(const QStringList &keys)
158 if (d->keys != keys) {
161 if (keys.isEmpty()) {
162 d->keyRegExp = QRegExp();
164 QString pattern = QLatin1Char('(') + QRegExp::escape(keys.first());
165 for (int i = 1; i < keys.count(); ++i)
166 pattern += QLatin1Char('|') + QRegExp::escape(keys.at(i));
167 pattern += QLatin1Char(')');
168 d->keyRegExp = QRegExp(pattern.replace(QLatin1String("\\*"), QLatin1String(".+")));
174 QQuickDropAreaDrag *QQuickDropArea::drag()
178 d->drag = new QQuickDropAreaDrag(d);
183 \qmlproperty Object QtQuick2::DropArea::drag.source
185 This property holds the source of a drag.
188 QObject *QQuickDropAreaDrag::source() const
194 \qmlproperty qreal QtQuick2::DropArea::drag.x
195 \qmlproperty qreal QtQuick2::DropArea::drag.y
197 These properties hold the coordinates of the last drag event.
200 qreal QQuickDropAreaDrag::x() const
202 return d->dragPosition.x();
205 qreal QQuickDropAreaDrag::y() const
207 return d->dragPosition.y();
211 \qmlsignal QtQuick2::DropArea::onPositionChanged(DragEvent drag)
213 This handler is called when the position of a drag has changed.
216 void QQuickDropArea::dragMoveEvent(QDragMoveEvent *event)
222 d->dragPosition = event->pos();
224 emit d->drag->positionChanged();
227 QQuickDropEvent dragTargetEvent(d, event);
228 emit positionChanged(&dragTargetEvent);
231 bool QQuickDropAreaPrivate::hasMatchingKey(const QStringList &keys) const
233 if (keyRegExp.isEmpty())
236 QRegExp copy = keyRegExp;
237 foreach (const QString &key, keys) {
238 if (copy.exactMatch(key))
244 QStringList QQuickDropAreaPrivate::getKeys(const QMimeData *mimeData) const
246 if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(mimeData))
247 return dragMime->keys();
248 return mimeData->formats();
252 \qmlsignal QtQuick2::DropArea::onEntered(DragEvent drag)
254 This handler is called when a \a drag enters the bounds of a DropArea.
257 void QQuickDropArea::dragEnterEvent(QDragEnterEvent *event)
260 const QMimeData *mimeData = event->mimeData();
261 if (!d->effectiveEnable || d->mimeData || !mimeData || !d->hasMatchingKey(d->getKeys(mimeData)))
264 d->dragPosition = event->pos();
267 QQuickDropEvent dragTargetEvent(d, event);
268 emit entered(&dragTargetEvent);
270 if (event->isAccepted()) {
271 d->mimeData = const_cast<QMimeData *>(mimeData);
272 if (QQuickDragMimeData *dragMime = qobject_cast<QQuickDragMimeData *>(d->mimeData))
273 d->source = dragMime->source();
275 d->source = event->source();
276 d->dragPosition = event->pos();
278 emit d->drag->positionChanged();
279 emit d->drag->sourceChanged();
281 emit containsDragChanged();
286 \qmlsignal QtQuick2::DropArea::onExited()
288 This handler is called when a drag exits the bounds of a DropArea.
291 void QQuickDropArea::dragLeaveEvent(QDragLeaveEvent *)
301 emit containsDragChanged();
303 emit d->drag->sourceChanged();
307 \qmlsignal QtQuick2::DropArea::onDropped(DragEvent drop)
309 This handler is called when a drop event occurs within the bounds of a
313 void QQuickDropArea::dropEvent(QDropEvent *event)
319 QQuickDropEvent dragTargetEvent(d, event);
320 emit dropped(&dragTargetEvent);
324 emit containsDragChanged();
326 emit d->drag->sourceChanged();
330 \qmlclass DragEvent QQuickDragEvent
331 \inqmlmodule QtQuick 2
332 \ingroup qtquick-input-events
333 \brief Provides information about a drag event
335 The position of the drag event can be obtained from the \l x and \l y
336 properties, and the \l keys property identifies the drag keys of the event
341 \qmlproperty real QtQuick2::DragEvent::x
343 This property holds the x coordinate of a drag event.
347 \qmlproperty real QtQuick2::DragEvent::y
349 This property holds the y coordinate of a drag event.
353 \qmlproperty Object QtQuick2::DragEvent::drag.source
355 This property holds the source of a drag event.
358 QObject *QQuickDropEvent::source()
360 if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(event->mimeData()))
361 return dragMime->source();
363 return event->source();
367 \qmlproperty stringlist QtQuick2::DragEvent::keys
369 This property holds a list of keys identifying the data type or source of a
373 QStringList QQuickDropEvent::keys() const
375 return d->getKeys(event->mimeData());
379 \qmlproperty enumeration QtQuick2::DragEvent::action
381 This property holds the action that the \l source is to perform on an accepted drop.
383 The drop action may be one of:
386 \li Qt.CopyAction Copy the data to the target
387 \li Qt.MoveAction Move the data from the source to the target
388 \li Qt.LinkAction Create a link from the source to the target.
389 \li Qt.IgnoreAction Ignore the action (do nothing with the data).
394 \qmlproperty flags QtQuick2::DragEvent::supportedActions
396 This property holds the set of \l {action}{actions} supported by the
401 \qmlproperty real QtQuick2::DragEvent::accepted
403 This property holds whether the drag event was accepted by a handler.
405 The default value is true.
409 \qmlmethod void QtQuick2::DragEvent::accept()
410 \qmlmethod void QtQuick2::DragEvent::accept(enumeration action)
412 Accepts the drag event.
414 If an \a action is specified it will overwrite the value of the \l action property.
417 void QQuickDropEvent::accept(QQmlV8Function *args)
419 Qt::DropAction action = event->dropAction();
421 if (args->Length() >= 1) {
422 v8::Local<v8::Value> v = (*args)[0];
424 action = Qt::DropAction(v->Int32Value());
426 // get action from arguments.
427 event->setDropAction(action);
434 #endif // QT_NO_DRAGANDDROP