Skip to content

Commit

Permalink
Add a pressed property to TouchPoint.
Browse files Browse the repository at this point in the history
Remove the valid property, and replace it with pressed. The semantics
have changed slightly for a release -- pressed will immediately become
false, whereas valid remained true until the next touch event.

Also make sure touch information is correctly updated on release.

Change-Id: Ic61e1b6884c67f19100a6f8fc218b8b05b291ff0
Reviewed-by: Alan Alpert <alan.alpert@nokia.com>
  • Loading branch information
Michael Brasser authored and Qt by Nokia committed Dec 22, 2011
1 parent 5c22d95 commit 9e61464
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 139 deletions.
37 changes: 19 additions & 18 deletions src/quick/items/qquickmultipointtoucharea.cpp
Expand Up @@ -124,19 +124,16 @@ void QQuickTouchPoint::setArea(const QRectF &area)
}

/*!
\qmlproperty bool QtQuick2::TouchPoint::valid
\qmlproperty bool QtQuick2::TouchPoint::pressed
This property holds whether the touch point is valid.
An invalid touch point is one that has not yet been pressed,
or has already been released.
This property holds whether the touch point is currently pressed.
*/
void QQuickTouchPoint::setValid(bool valid)
void QQuickTouchPoint::setPressed(bool pressed)
{
if (_valid == valid)
if (_pressed == pressed)
return;
_valid = valid;
emit validityChanged();
_pressed = pressed;
emit pressedChanged();
}

/*!
Expand Down Expand Up @@ -417,25 +414,28 @@ void QQuickMultiPointTouchArea::updateTouchData(QEvent *event)
QList<QTouchEvent::TouchPoint> touchPoints = e->touchPoints();
int numTouchPoints = touchPoints.count();
//always remove released touches, and make sure we handle all releases before adds.
foreach (QTouchEvent::TouchPoint p, touchPoints) {
foreach (const QTouchEvent::TouchPoint &p, touchPoints) {
Qt::TouchPointState touchPointState = p.state();
int id = p.id();
if (touchPointState & Qt::TouchPointReleased) {
QQuickTouchPoint* dtp = static_cast<QQuickTouchPoint*>(_touchPoints.value(id));
if (!dtp)
continue;
updateTouchPoint(dtp, &p);
dtp->setPressed(false);
_releasedTouchPoints.append(dtp);
_touchPoints.remove(id);
ended = true;
}
}
if (numTouchPoints >= _minimumTouchPoints && numTouchPoints <= _maximumTouchPoints) {
foreach (QTouchEvent::TouchPoint p, touchPoints) {
foreach (const QTouchEvent::TouchPoint &p, touchPoints) {
Qt::TouchPointState touchPointState = p.state();
int id = p.id();
if (touchPointState & Qt::TouchPointReleased) {
//handled above
} else if (!_touchPoints.contains(id)) { //could be pressed, moved, or stationary
// (we may have just obtained enough points to start tracking them -- in that case moved or stationary count as newly pressed)
addTouchPoint(&p);
started = true;
} else if (touchPointState & Qt::TouchPointMoved) {
Expand Down Expand Up @@ -491,7 +491,7 @@ void QQuickMultiPointTouchArea::clearTouchLists()
if (!dtp->isQmlDefined())
delete dtp;
else
dtp->setValid(false);
dtp->setInUse(false);
}
_releasedTouchPoints.clear();
_pressedTouchPoints.clear();
Expand All @@ -502,8 +502,8 @@ void QQuickMultiPointTouchArea::addTouchPoint(const QTouchEvent::TouchPoint *p)
{
QQuickTouchPoint *dtp = 0;
foreach (QQuickTouchPoint* tp, _touchPrototypes) {
if (!tp->isValid()) {
tp->setValid(true);
if (!tp->inUse()) {
tp->setInUse(true);
dtp = tp;
break;
}
Expand All @@ -513,10 +513,9 @@ void QQuickMultiPointTouchArea::addTouchPoint(const QTouchEvent::TouchPoint *p)
dtp = new QQuickTouchPoint(false);
dtp->setPointId(p->id());
updateTouchPoint(dtp,p);
dtp->setPressed(true);
_touchPoints.insert(p->id(),dtp);
//we may have just obtained enough points to start tracking them -- in that case moved or stationary count as newly pressed
if (p->state() & Qt::TouchPointPressed || p->state() & Qt::TouchPointMoved || p->state() & Qt::TouchPointStationary)
_pressedTouchPoints.append(dtp);
_pressedTouchPoints.append(dtp);
}

void QQuickMultiPointTouchArea::addTouchPrototype(QQuickTouchPoint *prototype)
Expand Down Expand Up @@ -586,14 +585,16 @@ void QQuickMultiPointTouchArea::ungrab()
setKeepMouseGrab(false);
}
setKeepTouchGrab(false);
foreach (QObject *obj, _touchPoints)
static_cast<QQuickTouchPoint*>(obj)->setPressed(false);
emit touchPointsCanceled(_touchPoints.values());
clearTouchLists();
foreach (QObject *obj, _touchPoints) {
QQuickTouchPoint *dtp = static_cast<QQuickTouchPoint*>(obj);
if (!dtp->isQmlDefined())
delete dtp;
else
dtp->setValid(false);
dtp->setInUse(false);
}
_touchPoints.clear();
}
Expand Down
20 changes: 12 additions & 8 deletions src/quick/items/qquickmultipointtoucharea_p.h
Expand Up @@ -58,8 +58,8 @@ class QQuickMultiPointTouchArea;
class Q_AUTOTEST_EXPORT QQuickTouchPoint : public QObject
{
Q_OBJECT
Q_PROPERTY(bool valid READ isValid NOTIFY validityChanged)
Q_PROPERTY(int pointId READ pointId NOTIFY pointIdChanged)
Q_PROPERTY(bool pressed READ pressed NOTIFY pressedChanged)
Q_PROPERTY(qreal x READ x NOTIFY xChanged)
Q_PROPERTY(qreal y READ y NOTIFY yChanged)
Q_PROPERTY(qreal pressure READ pressure NOTIFY pressureChanged)
Expand All @@ -78,7 +78,8 @@ class Q_AUTOTEST_EXPORT QQuickTouchPoint : public QObject
_x(0.0), _y(0.0),
_pressure(0.0),
_qmlDefined(qmlDefined),
_valid(!qmlDefined),
_inUse(false),
_pressed(false),
_previousX(0.0), _previousY(0.0),
_sceneX(0.0), _sceneY(0.0)
{}
Expand All @@ -98,10 +99,13 @@ class Q_AUTOTEST_EXPORT QQuickTouchPoint : public QObject
QRectF area() const { return _area; }
void setArea(const QRectF &area);

bool isQmlDefined() { return _qmlDefined; }
bool isQmlDefined() const { return _qmlDefined; }

bool isValid() { return _valid; }
void setValid(bool valid);
bool inUse() const { return _inUse; }
void setInUse(bool inUse) { _inUse = inUse; }

bool pressed() const { return _pressed; }
void setPressed(bool pressed);

qreal startX() const { return _startX; }
void setStartX(qreal startX);
Expand All @@ -121,14 +125,13 @@ class Q_AUTOTEST_EXPORT QQuickTouchPoint : public QObject
qreal sceneY() const { return _sceneY; }
void setSceneY(qreal sceneY);


Q_SIGNALS:
void pressedChanged();
void pointIdChanged();
void xChanged();
void yChanged();
void pressureChanged();
void areaChanged();
void validityChanged();
void startXChanged();
void startYChanged();
void previousXChanged();
Expand All @@ -144,7 +147,8 @@ class Q_AUTOTEST_EXPORT QQuickTouchPoint : public QObject
qreal _pressure;
QRectF _area;
bool _qmlDefined;
bool _valid;
bool _inUse; //whether the point is currently in use (only valid when _qmlDefined == true)
bool _pressed;
qreal _startX;
qreal _startY;
qreal _previousX;
Expand Down
15 changes: 15 additions & 0 deletions tests/auto/qtquick2/qquickmultipointtoucharea/data/basic.qml
@@ -0,0 +1,15 @@
import QtQuick 2.0

MultiPointTouchArea {
width: 240
height: 320

minimumTouchPoints: 1
maximumTouchPoints: 4
touchPoints: [
TouchPoint { objectName: "point1" },
TouchPoint { objectName: "point2" },
TouchPoint { objectName: "point3" },
TouchPoint { objectName: "point4" }
]
}

0 comments on commit 9e61464

Please sign in to comment.