Fix uses of various qml doc commands
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickdroparea.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickdroparea_p.h"
43 #include "qquickdrag_p.h"
44 #include "qquickitem_p.h"
45 #include "qquickwindow.h"
46
47 #include <private/qqmlengine_p.h>
48
49 #ifndef QT_NO_DRAGANDDROP
50
51 QT_BEGIN_NAMESPACE
52
53 QQuickDropAreaDrag::QQuickDropAreaDrag(QQuickDropAreaPrivate *d, QObject *parent)
54     : QObject(parent)
55     , d(d)
56 {
57 }
58
59 QQuickDropAreaDrag::~QQuickDropAreaDrag()
60 {
61 }
62
63 class QQuickDropAreaPrivate : public QQuickItemPrivate
64 {
65     Q_DECLARE_PUBLIC(QQuickDropArea)
66
67 public:
68     QQuickDropAreaPrivate();
69     ~QQuickDropAreaPrivate();
70
71     bool hasMatchingKey(const QStringList &keys) const;
72
73     QStringList getKeys(const QMimeData *mimeData) const;
74
75     QStringList keys;
76     QRegExp keyRegExp;
77     QPointF dragPosition;
78     QQuickDropAreaDrag *drag;
79     QQmlGuard<QObject> source;
80     QQmlGuard<QMimeData> mimeData;
81 };
82
83 QQuickDropAreaPrivate::QQuickDropAreaPrivate()
84     : drag(0)
85 {
86 }
87
88 QQuickDropAreaPrivate::~QQuickDropAreaPrivate()
89 {
90     delete drag;
91 }
92
93 /*!
94     \qmltype DropArea
95     \instantiates QQuickDropArea
96     \inqmlmodule QtQuick 2
97     \ingroup qtquick-input
98     \brief For specifying drag and drop handling in an area
99
100     A DropArea is an invisible item which receives events when other items are
101     dragged over it.
102
103     The \l Drag attached property can be used to notify the DropArea when an Item is
104     dragged over it.
105
106     The \l keys property can be used to filter drag events which don't include
107     a matching key.
108
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.
111
112     The \l delegate property provides a means to specify a component to be
113     instantiated for each active drag over a drag target.
114 */
115
116 QQuickDropArea::QQuickDropArea(QQuickItem *parent)
117     : QQuickItem(*new QQuickDropAreaPrivate, parent)
118 {
119     setFlags(ItemAcceptsDrops);
120 }
121
122 QQuickDropArea::~QQuickDropArea()
123 {
124 }
125
126 /*!
127     \qmlproperty bool QtQuick2::DropArea::containsDrag
128
129     This property identifies whether the DropArea currently contains any
130     dragged items.
131 */
132
133 bool QQuickDropArea::containsDrag() const
134 {
135     Q_D(const QQuickDropArea);
136     return d->mimeData;
137 }
138
139 /*!
140     \qmlproperty stringlist QtQuick2::DropArea::keys
141
142     This property holds a list of drag keys a DropArea will accept.
143
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.
146
147     \sa QtQuick2::Drag::keys
148 */
149
150 QStringList QQuickDropArea::keys() const
151 {
152     Q_D(const QQuickDropArea);
153     return d->keys;
154 }
155
156 void QQuickDropArea::setKeys(const QStringList &keys)
157 {
158     Q_D(QQuickDropArea);
159     if (d->keys != keys) {
160         d->keys = keys;
161
162         if (keys.isEmpty()) {
163             d->keyRegExp = QRegExp();
164         } else {
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(".+")));
170         }
171         emit keysChanged();
172     }
173 }
174
175 QQuickDropAreaDrag *QQuickDropArea::drag()
176 {
177     Q_D(QQuickDropArea);
178     if (!d->drag)
179         d->drag = new QQuickDropAreaDrag(d);
180     return d->drag;
181 }
182
183 /*!
184     \qmlproperty Object QtQuick2::DropArea::drag.source
185
186     This property holds the source of a drag.
187 */
188
189 QObject *QQuickDropAreaDrag::source() const
190 {
191     return d->source;
192 }
193
194 /*!
195     \qmlproperty qreal QtQuick2::DropArea::drag.x
196     \qmlproperty qreal QtQuick2::DropArea::drag.y
197
198     These properties hold the coordinates of the last drag event.
199 */
200
201 qreal QQuickDropAreaDrag::x() const
202 {
203     return d->dragPosition.x();
204 }
205
206 qreal QQuickDropAreaDrag::y() const
207 {
208     return d->dragPosition.y();
209 }
210
211 /*!
212     \qmlsignal QtQuick2::DropArea::onPositionChanged(DragEvent drag)
213
214     This handler is called when the position of a drag has changed.
215 */
216
217 void QQuickDropArea::dragMoveEvent(QDragMoveEvent *event)
218 {
219     Q_D(QQuickDropArea);
220     if (!d->mimeData)
221         return;
222
223     d->dragPosition = event->pos();
224     if (d->drag)
225         emit d->drag->positionChanged();
226
227     event->accept();
228     QQuickDropEvent dragTargetEvent(d, event);
229     emit positionChanged(&dragTargetEvent);
230 }
231
232 bool QQuickDropAreaPrivate::hasMatchingKey(const QStringList &keys) const
233 {
234     if (keyRegExp.isEmpty())
235         return true;
236
237     QRegExp copy = keyRegExp;
238     foreach (const QString &key, keys) {
239         if (copy.exactMatch(key))
240             return true;
241     }
242     return false;
243 }
244
245 QStringList QQuickDropAreaPrivate::getKeys(const QMimeData *mimeData) const
246 {
247     if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(mimeData))
248         return dragMime->keys();
249     return mimeData->formats();
250 }
251
252 /*!
253     \qmlsignal QtQuick2::DropArea::onEntered(DragEvent drag)
254
255     This handler is called when a \a drag enters the bounds of a DropArea.
256 */
257
258 void QQuickDropArea::dragEnterEvent(QDragEnterEvent *event)
259 {
260     Q_D(QQuickDropArea);
261     const QMimeData *mimeData = event->mimeData();
262     if (!d->effectiveEnable || d->mimeData || !mimeData || !d->hasMatchingKey(d->getKeys(mimeData)))
263         return;
264
265     d->dragPosition = event->pos();
266
267     event->accept();
268     QQuickDropEvent dragTargetEvent(d, event);
269     emit entered(&dragTargetEvent);
270
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();
275         else
276             d->source = event->source();
277         d->dragPosition = event->pos();
278         if (d->drag) {
279             emit d->drag->positionChanged();
280             emit d->drag->sourceChanged();
281         }
282         emit containsDragChanged();
283     }
284 }
285
286 /*!
287     \qmlsignal QtQuick2::DropArea::onExited()
288
289     This handler is called when a drag exits the bounds of a DropArea.
290 */
291
292 void QQuickDropArea::dragLeaveEvent(QDragLeaveEvent *)
293 {
294     Q_D(QQuickDropArea);
295     if (!d->mimeData)
296         return;
297
298     emit exited();
299
300     d->mimeData = 0;
301     d->source = 0;
302     emit containsDragChanged();
303     if (d->drag)
304         emit d->drag->sourceChanged();
305 }
306
307 /*!
308     \qmlsignal QtQuick2::DropArea::onDropped(DragEvent drop)
309
310     This handler is called when a drop event occurs within the bounds of a
311     a DropArea.
312 */
313
314 void QQuickDropArea::dropEvent(QDropEvent *event)
315 {
316     Q_D(QQuickDropArea);
317     if (!d->mimeData)
318         return;
319
320     QQuickDropEvent dragTargetEvent(d, event);
321     emit dropped(&dragTargetEvent);
322
323     d->mimeData = 0;
324     d->source = 0;
325     emit containsDragChanged();
326     if (d->drag)
327         emit d->drag->sourceChanged();
328 }
329
330 /*!
331     \qmltype DragEvent
332     \instantiates QQuickDropEvent
333     \inqmlmodule QtQuick 2
334     \ingroup qtquick-input-events
335     \brief Provides information about a drag event
336
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
339     \l source.
340 */
341
342 /*!
343     \qmlproperty real QtQuick2::DragEvent::x
344
345     This property holds the x coordinate of a drag event.
346 */
347
348 /*!
349     \qmlproperty real QtQuick2::DragEvent::y
350
351     This property holds the y coordinate of a drag event.
352 */
353
354 /*!
355     \qmlproperty Object QtQuick2::DragEvent::drag.source
356
357     This property holds the source of a drag event.
358 */
359
360 QObject *QQuickDropEvent::source()
361 {
362     if (const QQuickDragMimeData *dragMime = qobject_cast<const QQuickDragMimeData *>(event->mimeData()))
363         return dragMime->source();
364     else
365         return event->source();
366 }
367
368 /*!
369     \qmlproperty stringlist QtQuick2::DragEvent::keys
370
371     This property holds a list of keys identifying the data type or source of a
372     drag event.
373 */
374
375 QStringList QQuickDropEvent::keys() const
376 {
377     return d->getKeys(event->mimeData());
378 }
379
380 /*!
381     \qmlproperty enumeration QtQuick2::DragEvent::action
382
383     This property holds the action that the \l source is to perform on an accepted drop.
384
385     The drop action may be one of:
386
387     \list
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).
392     \endlist
393 */
394
395 /*!
396     \qmlproperty flags QtQuick2::DragEvent::supportedActions
397
398     This property holds the set of \l {action}{actions} supported by the
399     drag source.
400 */
401
402 /*!
403     \qmlproperty real QtQuick2::DragEvent::accepted
404
405     This property holds whether the drag event was accepted by a handler.
406
407     The default value is true.
408 */
409
410 /*!
411     \qmlmethod QtQuick2::DragEvent::accept()
412     \qmlmethod QtQuick2::DragEvent::accept(enumeration action)
413
414     Accepts the drag event.
415
416     If an \a action is specified it will overwrite the value of the \l action property.
417 */
418
419 void QQuickDropEvent::accept(QQmlV8Function *args)
420 {
421     Qt::DropAction action = event->dropAction();
422
423     if (args->Length() >= 1) {
424         v8::Local<v8::Value> v = (*args)[0];
425         if (v->IsInt32())
426             action = Qt::DropAction(v->Int32Value());
427     }
428     // get action from arguments.
429     event->setDropAction(action);
430     event->accept();
431 }
432
433
434 QT_END_NAMESPACE
435
436 #endif // QT_NO_DRAGANDDROP