Update licenseheader text in source files for qtdeclarative Qt module
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgdragtarget.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qsgdragtarget_p.h"
43 #include "qsgitem_p.h"
44 #include "qsgcanvas.h"
45
46 /*!
47     \qmlclass DragEvent QSGDragEvent
48     \brief The DragEvent object provides information about a drag event.
49
50     The position of the drag event can be obtained from the \l x and \l
51     properties, the \l keys property identifies the drag keys of the event
52     source and the \l data property contains the payload of the drag event.
53 */
54
55 /*!
56     \qmlproperty real DragEvent::x
57
58     This property holds the x coordinate of a drag event.
59 */
60
61 /*!
62     \qmlproperty real DragEvent::y
63
64     This property holds the y coordinate of a drag event.
65 */
66
67 /*!
68     \qmlproperty stringlist DragEvent::keys
69
70     This property holds a list of keys identifying the data type or source of a
71     drag event.
72 */
73
74 /*!
75     \qmlproperty variant DragEvent::data
76
77     This property holds data payload of a drag event.
78 */
79
80 /*!
81     \qmlproperty real DragEvent::accepted
82
83     This property holds whether the drag event was accepted by a handler.
84
85     The default value is true.
86 */
87
88 class QSGDragTargetPrivate : public QSGItemPrivate
89 {
90     Q_DECLARE_PUBLIC(QSGDragTarget)
91
92 public:
93     QSGDragTargetPrivate();
94     ~QSGDragTargetPrivate();
95
96     bool hasMatchingKey(const QStringList &keys) const;
97
98     QStringList keys;
99     QRegExp keyRegExp;
100     QVariant dragData;
101     QPointF dragPosition;
102     QSGItem *dropItem;
103     bool containsDrag : 1;
104 };
105
106 QSGDragTargetPrivate::QSGDragTargetPrivate()
107     : dropItem(0)
108     , containsDrag(false)
109 {
110 }
111
112 QSGDragTargetPrivate::~QSGDragTargetPrivate()
113 {
114 }
115
116 /*!
117     \qmlclass DragTarget QSGDragTarget
118     \brief The DragTarget item provides drag and drop handling.
119
120     A DragTarget is an invisible item which receives events when another item
121     is dragged over it.
122
123     A MouseArea item can be used to drag items.
124
125     The \l keys property can be used to filter drag events which don't include
126     a matching key.
127
128     The \l dropItem property is communicated to the source of a drag event as
129     the recipient of a drop on the drag target.
130
131     The \l delegate property provides a means to specify a component to be
132     instantiated for each active drag over a drag target.
133 */
134
135 QSGDragTarget::QSGDragTarget(QSGItem *parent)
136     : QSGItem(*new QSGDragTargetPrivate, parent)
137 {
138 }
139
140 QSGDragTarget::~QSGDragTarget()
141 {
142 }
143
144 /*!
145     \qmlproperty bool DragTarget::containsDrag
146
147     This property identifies whether the DragTarget currently contains any
148     dragged items.
149 */
150
151 bool QSGDragTarget::containsDrag() const
152 {
153     Q_D(const QSGDragTarget);
154     return d->containsDrag;
155 }
156
157 /*!
158     \qmlproperty stringlist DragTarget::keys
159
160     This property holds a list of drag keys a DragTarget will accept.
161 */
162
163 QStringList QSGDragTarget::keys() const
164 {
165     Q_D(const QSGDragTarget);
166     return d->keys;
167 }
168
169 void QSGDragTarget::setKeys(const QStringList &keys)
170 {
171     Q_D(QSGDragTarget);
172     if (d->keys != keys) {
173         d->keys = keys;
174
175         if (keys.isEmpty()) {
176             d->keyRegExp = QRegExp();
177         } else {
178             QString pattern = QLatin1Char('(') + QRegExp::escape(keys.first());
179             for (int i = 1; i < keys.count(); ++i)
180                 pattern += QLatin1Char('|') + QRegExp::escape(keys.at(i));
181             pattern += QLatin1Char(')');
182             d->keyRegExp = QRegExp(pattern.replace(QLatin1String("\\*"), QLatin1String(".+")));
183         }
184         emit keysChanged();
185     }
186 }
187
188 /*!
189     \qmlproperty Item DragTarget::dropItem
190
191     This property identifies an item as the recipient of a drop event within
192     a DragTarget.
193
194     \sa MouseArea::drag.dropItem
195 */
196
197 QSGItem *QSGDragTarget::dropItem() const
198 {
199     Q_D(const QSGDragTarget);
200     return d->dropItem;
201 }
202
203 void QSGDragTarget::setDropItem(QSGItem *item)
204 {
205     Q_D(QSGDragTarget);
206     if (d->dropItem != item) {
207         d->dropItem = item;
208         emit dropItemChanged();
209     }
210 }
211
212 void QSGDragTarget::resetDropItem()
213 {
214     Q_D(QSGDragTarget);
215     if (d->dropItem) {
216         d->dropItem = 0;
217         emit dropItemChanged();
218     }
219 }
220
221 qreal QSGDragTarget::dragX() const
222 {
223     Q_D(const QSGDragTarget);
224     return d->dragPosition.x();
225 }
226
227 qreal QSGDragTarget::dragY() const
228 {
229     Q_D(const QSGDragTarget);
230     return d->dragPosition.y();
231 }
232
233 QVariant QSGDragTarget::dragData() const
234 {
235     Q_D(const QSGDragTarget);
236     return d->dragData;
237 }
238
239 /*!
240     \qmlsignal DragTarget::onPositionChanged(DragEvent drag)
241     \qmlattachedsignal DragTarget::onPositionChanged(DragEvent drag)
242
243     This handler is called when the position of a drag has changed.
244 */
245
246 void QSGDragTarget::dragMoveEvent(QSGDragEvent *event)
247 {
248     Q_D(QSGDragTarget);
249     if (!d->containsDrag) {
250         event->setAccepted(false);
251         return;
252     }
253
254     event->setDropItem(d->dropItem);
255
256     d->dragPosition = event->position();
257     emit dragPositionChanged();
258
259     QSGDragTargetEvent dragTargetEvent(event);
260     emit positionChanged(&dragTargetEvent);
261 }
262
263 bool QSGDragTargetPrivate::hasMatchingKey(const QStringList &keys) const
264 {
265     if (keyRegExp.isEmpty())
266         return true;
267
268     foreach (const QString &key, keys) {
269         if (keyRegExp.exactMatch(key))
270             return true;
271     }
272     return false;
273 }
274
275 /*!
276     \qmlsignal DragTarget::onEntered(DragEvent drag)
277     \qmlattachedsignal DragTarget::onEntered(DragEvent drag)
278
279     This handler is called when a drag enters the bounds of a DragTarget.
280 */
281
282 void QSGDragTarget::dragEnterEvent(QSGDragEvent *event)
283 {
284     Q_D(QSGDragTarget);
285     if (!d->effectiveEnable || !d->hasMatchingKey(event->keys()) || d->containsDrag) {
286         event->setAccepted(false);
287         return;
288     }
289
290     event->setDropItem(d->dropItem);
291
292     QSGDragTargetEvent dragTargetEvent(event);
293     emit entered(&dragTargetEvent);
294
295     if (event->isAccepted()) {
296
297         d->dragData = event->data();
298         d->containsDrag = true;
299         if (!d->dragData.isNull())
300             emit dragDataChanged();
301         emit containsDragChanged();
302     }
303 }
304
305 /*!
306     \qmlsignal DragTarget::onExited(DragEvent drag)
307     \qmlattachedsignal DragTarget::onExited(DragEvent drag)
308
309     This handler is called when a drag exits the bounds of a DragTarget.
310 */
311
312 void QSGDragTarget::dragExitEvent(QSGDragEvent *event)
313 {
314     Q_D(QSGDragTarget);
315     if (!d->containsDrag) {
316         event->setAccepted(false);
317         return;
318     }
319
320     QSGDragTargetEvent dragTargetEvent(event);
321     emit exited(&dragTargetEvent);
322
323     d->containsDrag = false;
324     emit containsDragChanged();
325     if (!d->dragData.isNull()) {
326         d->dragData = QVariant();
327         emit dragDataChanged();
328     }
329 }
330
331 /*!
332     \qmlsignal DragTarget::onDropped(DragEvent drag)
333     \qmlattachedsignal DragTarget::onDropped(DragEvent drag)
334
335     This handler is called when a drop event occurs within the bounds of a
336     a DragTarget.
337 */
338
339 void QSGDragTarget::dragDropEvent(QSGDragEvent *event)
340 {
341     Q_D(QSGDragTarget);
342     if (!d->containsDrag) {
343         event->setAccepted(false);
344         return;
345     }
346
347     event->setDropItem(d->dropItem);
348
349     QSGDragTargetEvent dragTargetEvent(event);
350     emit dropped(&dragTargetEvent);
351
352     d->containsDrag = false;
353     emit containsDragChanged();
354     if (!d->dragData.isNull()) {
355         d->dragData = QVariant();
356         emit dragDataChanged();
357     }
358 }
359
360 QT_END_NAMESPACE
361