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()
95 \instantiates QQuickDropArea
96 \inqmlmodule QtQuick 2
97 \ingroup qtquick-input
98 \brief For specifying drag and drop handling in an area
100 A DropArea is an invisible item which receives events when other items are
103 The \l Drag attached property can be used to notify the DropArea when an Item is
106 The \l keys property can be used to filter drag events which don't include
109 The \l source property is communicated to the source of a drag event as
110 the recipient of a drop on the drag target.
112 The \l delegate property provides a means to specify a component to be
113 instantiated for each active drag over a drag target.
116 QQuickDropArea::QQuickDropArea(QQuickItem *parent)
117 : QQuickItem(*new QQuickDropAreaPrivate, parent)
119 setFlags(ItemAcceptsDrops);
122 QQuickDropArea::~QQuickDropArea()
127 \qmlproperty bool QtQuick2::DropArea::containsDrag
129 This property identifies whether the DropArea currently contains any
133 bool QQuickDropArea::containsDrag() const
135 Q_D(const QQuickDropArea);
140 \qmlproperty stringlist QtQuick2::DropArea::keys
142 This property holds a list of drag keys a DropArea will accept.
144 If no keys are listed the DropArea will accept events from any drag source,
145 otherwise the drag source must have at least one compatible key.
147 \sa QtQuick2::Drag::keys
150 QStringList QQuickDropArea::keys() const
152 Q_D(const QQuickDropArea);
156 void QQuickDropArea::setKeys(const QStringList &keys)
159 if (d->keys != keys) {
162 if (keys.isEmpty()) {
163 d->keyRegExp = QRegExp();
165 QString pattern = QLatin1Char('(') + QRegExp::escape(keys.first());
166 for (int i = 1; i < keys.count(); ++i)
167 pattern += QLatin1Char('|') + QRegExp::escape(keys.at(i));
168 pattern += QLatin1Char(')');
169 d->keyRegExp = QRegExp(pattern.replace(QLatin1String("\\*"), QLatin1String(".+")));
175 QQuickDropAreaDrag *QQuickDropArea::drag()
179 d->drag = new QQuickDropAreaDrag(d);
184 \qmlproperty Object QtQuick2::DropArea::drag.source
186 This property holds the source of a drag.
189 QObject *QQuickDropAreaDrag::source() const
195 \qmlproperty qreal QtQuick2::DropArea::drag.x
196 \qmlproperty qreal QtQuick2::DropArea::drag.y
198 These properties hold the coordinates of the last drag event.
201 qreal QQuickDropAreaDrag::x() const
203 return d->dragPosition.x();
206 qreal QQuickDropAreaDrag::y() const
208 return d->dragPosition.y();
212 \qmlsignal QtQuick2::DropArea::onPositionChanged(DragEvent drag)
214 This handler is called when the position of a drag has changed.
217 void QQuickDropArea::dragMoveEvent(QDragMoveEvent *event)
223 d->dragPosition = event->pos();
225 emit d->drag->positionChanged();
228 QQuickDropEvent dragTargetEvent(d, event);
229 emit positionChanged(&dragTargetEvent);
232 bool QQuickDropAreaPrivate::hasMatchingKey(const QStringList &keys) const
234 if (keyRegExp.isEmpty())
237 QRegExp copy = keyRegExp;
238 foreach (const QString &key, keys) {
239 if (copy.exactMatch(key))
245 QStringList QQuickDropAreaPrivate::getKeys(const QMimeData *mimeData) const
247 if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(mimeData))
248 return dragMime->keys();
249 return mimeData->formats();
253 \qmlsignal QtQuick2::DropArea::onEntered(DragEvent drag)
255 This handler is called when a \a drag enters the bounds of a DropArea.
258 void QQuickDropArea::dragEnterEvent(QDragEnterEvent *event)
261 const QMimeData *mimeData = event->mimeData();
262 if (!d->effectiveEnable || d->mimeData || !mimeData || !d->hasMatchingKey(d->getKeys(mimeData)))
265 d->dragPosition = event->pos();
268 QQuickDropEvent dragTargetEvent(d, event);
269 emit entered(&dragTargetEvent);
271 if (event->isAccepted()) {
272 d->mimeData = const_cast<QMimeData *>(mimeData);
273 if (QQuickDragMimeData *dragMime = qobject_cast<QQuickDragMimeData *>(d->mimeData))
274 d->source = dragMime->source();
276 d->source = event->source();
277 d->dragPosition = event->pos();
279 emit d->drag->positionChanged();
280 emit d->drag->sourceChanged();
282 emit containsDragChanged();
287 \qmlsignal QtQuick2::DropArea::onExited()
289 This handler is called when a drag exits the bounds of a DropArea.
292 void QQuickDropArea::dragLeaveEvent(QDragLeaveEvent *)
302 emit containsDragChanged();
304 emit d->drag->sourceChanged();
308 \qmlsignal QtQuick2::DropArea::onDropped(DragEvent drop)
310 This handler is called when a drop event occurs within the bounds of a
314 void QQuickDropArea::dropEvent(QDropEvent *event)
320 QQuickDropEvent dragTargetEvent(d, event);
321 emit dropped(&dragTargetEvent);
325 emit containsDragChanged();
327 emit d->drag->sourceChanged();
332 \instantiates QQuickDropEvent
333 \inqmlmodule QtQuick 2
334 \ingroup qtquick-input-events
335 \brief Provides information about a drag event
337 The position of the drag event can be obtained from the \l x and \l y
338 properties, and the \l keys property identifies the drag keys of the event
343 \qmlproperty real QtQuick2::DragEvent::x
345 This property holds the x coordinate of a drag event.
349 \qmlproperty real QtQuick2::DragEvent::y
351 This property holds the y coordinate of a drag event.
355 \qmlproperty Object QtQuick2::DragEvent::drag.source
357 This property holds the source of a drag event.
360 QObject *QQuickDropEvent::source()
362 if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(event->mimeData()))
363 return dragMime->source();
365 return event->source();
369 \qmlproperty stringlist QtQuick2::DragEvent::keys
371 This property holds a list of keys identifying the data type or source of a
375 QStringList QQuickDropEvent::keys() const
377 return d->getKeys(event->mimeData());
381 \qmlproperty enumeration QtQuick2::DragEvent::action
383 This property holds the action that the \l source is to perform on an accepted drop.
385 The drop action may be one of:
388 \li Qt.CopyAction Copy the data to the target
389 \li Qt.MoveAction Move the data from the source to the target
390 \li Qt.LinkAction Create a link from the source to the target.
391 \li Qt.IgnoreAction Ignore the action (do nothing with the data).
396 \qmlproperty flags QtQuick2::DragEvent::supportedActions
398 This property holds the set of \l {action}{actions} supported by the
403 \qmlproperty real QtQuick2::DragEvent::accepted
405 This property holds whether the drag event was accepted by a handler.
407 The default value is true.
411 \qmlmethod QtQuick2::DragEvent::accept()
412 \qmlmethod QtQuick2::DragEvent::accept(enumeration action)
414 Accepts the drag event.
416 If an \a action is specified it will overwrite the value of the \l action property.
419 void QQuickDropEvent::accept(QQmlV8Function *args)
421 Qt::DropAction action = event->dropAction();
423 if (args->Length() >= 1) {
424 v8::Local<v8::Value> v = (*args)[0];
426 action = Qt::DropAction(v->Int32Value());
428 // get action from arguments.
429 event->setDropAction(action);
436 #endif // QT_NO_DRAGANDDROP