Qt 4.8
qdeclarativegesturearea.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
43 
44 #include <qdeclarativeexpression.h>
45 #include <qdeclarativecontext.h>
46 #include <qdeclarativeinfo.h>
47 
48 #include <private/qdeclarativeproperty_p.h>
49 #include <private/qdeclarativeitem_p.h>
50 
51 #include <QtCore/qdebug.h>
52 #include <QtCore/qstringlist.h>
53 
54 #include <QtGui/qevent.h>
55 
56 #include <private/qobject_p.h>
57 
58 #ifndef QT_NO_GESTURES
59 
61 
63 {
65 public:
67 
69  Bindings bindings;
70 
72 
74 
76 
78 };
79 
153 {
155  setAcceptTouchEvents(true);
156 }
157 
159 {
160 }
161 
164 {
165  QByteArray rv;
167 
168  for(int ii = 0; ii < props.count(); ++ii)
169  {
170  QString propName = QString::fromUtf8(props.at(ii).name());
172 
173  if (propName == QLatin1String("onTap")) {
174  type = Qt::TapGesture;
175  } else if (propName == QLatin1String("onTapAndHold")) {
176  type = Qt::TapAndHoldGesture;
177  } else if (propName == QLatin1String("onPan")) {
178  type = Qt::PanGesture;
179  } else if (propName == QLatin1String("onPinch")) {
180  type = Qt::PinchGesture;
181  } else if (propName == QLatin1String("onSwipe")) {
182  type = Qt::SwipeGesture;
183  } else if (propName == QLatin1String("onGesture")) {
184  type = Qt::CustomGesture;
185  } else {
186  error(props.at(ii), QDeclarativeGestureArea::tr("Cannot assign to non-existent property \"%1\"").arg(propName));
187  return QByteArray();
188  }
189 
190  QList<QVariant> values = props.at(ii).assignedValues();
191 
192  for (int i = 0; i < values.count(); ++i) {
193  const QVariant &value = values.at(i);
194 
195  if (value.userType() == qMetaTypeId<QDeclarativeCustomParserNode>()) {
196  error(props.at(ii), QDeclarativeGestureArea::tr("GestureArea: nested objects not allowed"));
197  return QByteArray();
198  } else if (value.userType() == qMetaTypeId<QDeclarativeCustomParserProperty>()) {
199  error(props.at(ii), QDeclarativeGestureArea::tr("GestureArea: syntax error"));
200  return QByteArray();
201  } else {
203  if (v.isScript()) {
204  ds << propName;
205  ds << int(type);
206  ds << v.asScript();
207  } else {
208  error(props.at(ii), QDeclarativeGestureArea::tr("GestureArea: script expected"));
209  return QByteArray();
210  }
211  }
212  }
213  }
214 
215  return rv;
216 }
217 
219  const QByteArray &data)
220 {
221  QDeclarativeGestureArea *ga = static_cast<QDeclarativeGestureArea*>(object);
222  ga->d_func()->data = data;
223 }
224 
225 
227 {
229  if (!d->componentcomplete)
230  return;
231 
232  QDataStream ds(d->data);
233  while (!ds.atEnd()) {
234  QString propName;
235  ds >> propName;
236  int gesturetype;
237  ds >> gesturetype;
238  QString script;
239  ds >> script;
240  QDeclarativeExpression *exp = new QDeclarativeExpression(qmlContext(this), this, script);
241  d->bindings.insert(Qt::GestureType(gesturetype),exp);
242  grabGesture(Qt::GestureType(gesturetype));
243  }
244 }
245 
247 {
250  d->componentcomplete=true;
251  connectSignals();
252 }
253 
255 {
257  return d->gesture;
258 }
259 
261 {
263  if (event->type() == QEvent::Gesture)
264  return d->gestureEvent(static_cast<QGestureEvent*>(event));
265  return QDeclarativeItem::sceneEvent(event);
266 }
267 
269 {
270  bool accept = true;
271  for (Bindings::Iterator it = bindings.begin(); it != bindings.end(); ++it) {
272  if ((gesture = event->gesture(it.key()))) {
273  QDeclarativeExpression *expr = it.value();
274  expr->evaluate();
275  if (expr->hasError())
276  qmlInfo(q_func()) << expr->error();
277  event->setAccepted(true); // XXX only if value returns true?
278  }
279  }
280  return accept;
281 }
282 
284 
285 #endif // QT_NO_GESTURES
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QGraphicsItem * parent
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
QDeclarativeParserStatus ** d
#define it(className, varName)
QGesture * gesture() const
#define error(msg)
virtual bool event(QEvent *)
bool atEnd() const
Returns true if the I/O device has reached the end position (end of the stream or file) or if there i...
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QVariant evaluate(bool *valueIsUndefined=0)
Evaulates the expression, returning the result of the evaluation, or an invalid QVariant if the expre...
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual QByteArray compile(const QList< QDeclarativeCustomParserProperty > &)
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
Q_DECLARATIVE_EXPORT QDeclarativeContext * qmlContext(const QObject *)
#define Q_D(Class)
Definition: qglobal.h:2482
bool gestureEvent(QGestureEvent *event)
void grabGesture(Qt::GestureType type, Qt::GestureFlags flags=Qt::GestureFlags())
Subscribes the graphics object to the given gesture with specific flags.
void setAcceptTouchEvents(bool enabled)
If enabled is true, this item will accept touch events; otherwise, it will ignore them...
QMap< Qt::GestureType, QDeclarativeExpression * > Bindings
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QGestureEvent class provides the description of triggered gestures.
Definition: qevent.h:841
The QDeclarativeItem class provides the most basic of all visual items in QML.
QVariant data(int key) const
Returns this item&#39;s custom data for the key key as a QVariant.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
virtual int type() const
Returns the type of an item as an int.
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
quint16 values[128]
GestureType
Definition: qnamespace.h:1759
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
bool hasError() const
Returns true if the last call to evaluate() resulted in an error, otherwise false.
QDeclarativeGestureArea(QDeclarativeItem *parent=0)
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
The QGesture class represents a gesture, containing properties that describe the corresponding user i...
Definition: qgesture.h:64
virtual void componentComplete()
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
virtual bool sceneEvent(QEvent *)
void setAcceptedMouseButtons(Qt::MouseButtons buttons)
Sets the mouse buttons that this item accepts mouse events for.
QGesture * gesture(Qt::GestureType type) const
Returns a gesture object by type.
Definition: qevent.cpp:4881
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
QDeclarativeError error() const
Return any error from the last call to evaluate().
The QDeclarativeExpression class evaluates JavaScript in a QML context.
QDeclarativeInfo qmlInfo(const QObject *me)
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
virtual void setCustomData(QObject *, const QByteArray &)