Qt 4.8
Public Functions | List of all members
QSwipeGestureRecognizer Class Reference

#include <qstandardgestures_p.h>

Inheritance diagram for QSwipeGestureRecognizer:
QGestureRecognizer

Public Functions

QGesturecreate (QObject *target)
 This function is called by Qt to create a new QGesture object for the given target (QWidget or QGraphicsObject). More...
 
 QSwipeGestureRecognizer ()
 
QGestureRecognizer::Result recognize (QGesture *state, QObject *watched, QEvent *event)
 Handles the given event for the watched object, updating the state of the gesture object as required, and returns a suitable result for the current recognition step. More...
 
void reset (QGesture *state)
 This function is called by the framework to reset a given gesture. More...
 
- Public Functions inherited from QGestureRecognizer
 QGestureRecognizer ()
 Constructs a new gesture recognizer object. More...
 
virtual ~QGestureRecognizer ()
 Destroys the gesture recognizer. More...
 

Additional Inherited Members

- Public Types inherited from QGestureRecognizer
enum  ResultFlag {
  Ignore = 0x0001, MayBeGesture = 0x0002, TriggerGesture = 0x0004, FinishGesture = 0x0008,
  CancelGesture = 0x0010, ResultState_Mask = 0x00ff, ConsumeEventHint = 0x0100, ResultHint_Mask = 0xff00
}
 This enum describes the result of the current event filtering step in a gesture recognizer state machine. More...
 
- Static Public Functions inherited from QGestureRecognizer
static Qt::GestureType registerRecognizer (QGestureRecognizer *recognizer)
 Registers the given recognizer in the gesture framework and returns a gesture ID for it. More...
 
static void unregisterRecognizer (Qt::GestureType type)
 Unregisters all gesture recognizers of the specified type. More...
 

Detailed Description

Definition at line 83 of file qstandardgestures_p.h.

Constructors and Destructors

◆ QSwipeGestureRecognizer()

QSwipeGestureRecognizer::QSwipeGestureRecognizer ( )

Definition at line 281 of file qstandardgestures.cpp.

282 {
283 }

Functions

◆ create()

QGesture * QSwipeGestureRecognizer::create ( QObject target)
virtual

This function is called by Qt to create a new QGesture object for the given target (QWidget or QGraphicsObject).

Reimplement this function to create a custom QGesture-derived gesture object if necessary.

The application takes ownership of the created gesture object.

Reimplemented from QGestureRecognizer.

Definition at line 285 of file qstandardgestures.cpp.

286 {
287  if (target && target->isWidgetType()) {
288  static_cast<QWidget *>(target)->setAttribute(Qt::WA_AcceptTouchEvents);
289  }
290  return new QSwipeGesture;
291 }
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
bool isWidgetType() const
Returns true if the object is a widget; otherwise returns false.
Definition: qobject.h:146
The QSwipeGesture class describes a swipe gesture made by the user.
Definition: qgesture.h:207

◆ recognize()

QGestureRecognizer::Result QSwipeGestureRecognizer::recognize ( QGesture gesture,
QObject watched,
QEvent event 
)
virtual

Handles the given event for the watched object, updating the state of the gesture object as required, and returns a suitable result for the current recognition step.

This function is called by the framework to allow the recognizer to filter input events dispatched to QWidget or QGraphicsObject instances that it is monitoring.

The result reflects how much of the gesture has been recognized. The state of the gesture object is set depending on the result.

See also
QGestureRecognizer::Result

Implements QGestureRecognizer.

Definition at line 293 of file qstandardgestures.cpp.

296 {
297  QSwipeGesture *q = static_cast<QSwipeGesture *>(state);
298  QSwipeGesturePrivate *d = q->d_func();
299 
300  const QTouchEvent *ev = static_cast<const QTouchEvent *>(event);
301 
302  QGestureRecognizer::Result result;
303 
304  switch (event->type()) {
305  case QEvent::TouchBegin: {
306  d->velocityValue = 1;
307  d->time.start();
308  d->started = true;
310  break;
311  }
312  case QEvent::TouchEnd: {
313  if (q->state() != Qt::NoGesture) {
315  } else {
317  }
318  break;
319  }
320  case QEvent::TouchUpdate: {
321  if (!d->started)
323  else if (ev->touchPoints().size() == 3) {
324  QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0);
325  QTouchEvent::TouchPoint p2 = ev->touchPoints().at(1);
326  QTouchEvent::TouchPoint p3 = ev->touchPoints().at(2);
327 
328  if (d->lastPositions[0].isNull()) {
329  d->lastPositions[0] = p1.startScreenPos().toPoint();
330  d->lastPositions[1] = p2.startScreenPos().toPoint();
331  d->lastPositions[2] = p3.startScreenPos().toPoint();
332  }
333  d->hotSpot = p1.screenPos();
334  d->isHotSpotSet = true;
335 
336  int xDistance = (p1.screenPos().x() - d->lastPositions[0].x() +
337  p2.screenPos().x() - d->lastPositions[1].x() +
338  p3.screenPos().x() - d->lastPositions[2].x()) / 3;
339  int yDistance = (p1.screenPos().y() - d->lastPositions[0].y() +
340  p2.screenPos().y() - d->lastPositions[1].y() +
341  p3.screenPos().y() - d->lastPositions[2].y()) / 3;
342 
343  const int distance = xDistance >= yDistance ? xDistance : yDistance;
344  int elapsedTime = d->time.restart();
345  if (!elapsedTime)
346  elapsedTime = 1;
347  d->velocityValue = 0.9 * d->velocityValue + distance / elapsedTime;
348  d->swipeAngle = QLineF(p1.startScreenPos(), p1.screenPos()).angle();
349 
350  static const int MoveThreshold = 50;
351  if (xDistance > MoveThreshold || yDistance > MoveThreshold) {
352  // measure the distance to check if the direction changed
353  d->lastPositions[0] = p1.screenPos().toPoint();
354  d->lastPositions[1] = p2.screenPos().toPoint();
355  d->lastPositions[2] = p3.screenPos().toPoint();
356  QSwipeGesture::SwipeDirection horizontal =
357  xDistance > 0 ? QSwipeGesture::Right : QSwipeGesture::Left;
359  yDistance > 0 ? QSwipeGesture::Down : QSwipeGesture::Up;
361  d->verticalDirection = vertical;
363  d->horizontalDirection = horizontal;
364  if (d->verticalDirection != vertical || d->horizontalDirection != horizontal) {
365  // the user has changed the direction!
367  }
369  } else {
370  if (q->state() != Qt::NoGesture)
372  else
374  }
375  } else if (ev->touchPoints().size() > 3) {
377  } else { // less than 3 touch points
378  if (d->started && (ev->touchPointStates() & Qt::TouchPointPressed))
380  else if (d->started)
382  else
384  }
385  break;
386  }
388  case QEvent::MouseMove:
391  break;
392  default:
394  break;
395  }
396  return result;
397 }
double d
Definition: qnumeric_p.h:62
EventRef event
QPointF hotSpot
Definition: qgesture_p.h:79
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:212
qint64 restart()
Restarts the timer and returns the time elapsed since the previous start.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
Qt::GestureState state() const
bool isNull() const
Returns true if both the x and y coordinates are set to 0, otherwise returns false.
Definition: qpoint.h:125
static int distance(QWidget *source, QWidget *target, QAccessible::RelationFlag relation)
Qt::TouchPointStates touchPointStates() const
Returns a bitwise OR of all the touch point states for this event.
Definition: qevent.h:819
QPointF screenPos() const
Returns the screen position of this touch point.
Definition: qevent.cpp:4498
QPointF startScreenPos() const
Returns the starting screen position of this touch point.
Definition: qevent.cpp:4547
qreal angle(const QPointF &p1, const QPointF &p2)
QSwipeGesture::SwipeDirection horizontalDirection
Definition: qgesture_p.h:156
QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition: qpoint.h:376
const QList< QTouchEvent::TouchPoint > & touchPoints() const
Returns the list of touch points contained in the touch event.
Definition: qevent.h:820
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
The TouchPoint class provides information about a touch point in a QTouchEvent.
Definition: qevent.h:744
The QTouchEvent class contains parameters that describe a touch event.
Definition: qevent.h:741
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
QPoint lastPositions[3]
Definition: qgesture_p.h:160
The QSwipeGesture class describes a swipe gesture made by the user.
Definition: qgesture.h:207
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
SwipeDirection
This enum describes the possible directions for the gesture&#39;s motion along the horizontal and vertica...
Definition: qgesture.h:219
void start()
Starts this timer.
QElapsedTimer time
Definition: qgesture_p.h:163
QSwipeGesture::SwipeDirection verticalDirection
Definition: qgesture_p.h:157

◆ reset()

void QSwipeGestureRecognizer::reset ( QGesture gesture)
virtual

This function is called by the framework to reset a given gesture.

Reimplement this function to implement additional requirements for custom QGesture objects. This may be necessary if you implement a custom QGesture whose properties need special handling when the gesture is reset.

Reimplemented from QGestureRecognizer.

Definition at line 399 of file qstandardgestures.cpp.

400 {
401  QSwipeGesture *q = static_cast<QSwipeGesture *>(state);
402  QSwipeGesturePrivate *d = q->d_func();
403 
405  d->swipeAngle = 0;
406 
407  d->lastPositions[0] = d->lastPositions[1] = d->lastPositions[2] = QPoint();
408  d->started = false;
409  d->velocityValue = 0;
410  d->time.invalidate();
411 
413 }
double d
Definition: qnumeric_p.h:62
virtual void reset(QGesture *state)
This function is called by the framework to reset a given gesture.
void invalidate()
Marks this QElapsedTimer object as invalid.
QSwipeGesture::SwipeDirection horizontalDirection
Definition: qgesture_p.h:156
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QPoint lastPositions[3]
Definition: qgesture_p.h:160
The QSwipeGesture class describes a swipe gesture made by the user.
Definition: qgesture.h:207
QElapsedTimer time
Definition: qgesture_p.h:163
QSwipeGesture::SwipeDirection verticalDirection
Definition: qgesture_p.h:157

The documentation for this class was generated from the following files: