summaryrefslogtreecommitdiffstats
path: root/src/declarative/graphicsitems/qdeclarativepincharea.cpp
blob: 9bc3d8d6f73261271538043ee1ba8c09a8a5d00d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qdeclarativepincharea_p.h"
#include "qdeclarativepincharea_p_p.h"

#include <QApplication>
#include <QGraphicsScene>

#include <float.h>
#include <math.h>

QT_BEGIN_NAMESPACE


/*!
    \qmlclass PinchEvent QDeclarativePinchEvent
    \ingroup qml-event-elements
    \brief The PinchEvent object provides information about a pinch event.

    \bold {The PinchEvent element was added in QtQuick 1.1}

    The \c center, \c startCenter, \c previousCenter properties provide the center position between the two touch points.

    The \c scale and \c previousScale properties provide the scale factor.

    The \c angle, \c previousAngle and \c rotation properties provide the angle between the two points and the amount of rotation.

    The \c point1, \c point2, \c startPoint1, \c startPoint2 properties provide the positions of the touch points.

    The \c accepted property may be set to false in the \c onPinchStarted handler if the gesture should not
    be handled.

    \sa PinchArea
*/

/*!
    \qmlproperty QPointF PinchEvent::center
    \qmlproperty QPointF PinchEvent::startCenter
    \qmlproperty QPointF PinchEvent::previousCenter

    These properties hold the position of the center point between the two touch points.

    \list
    \o \c center is the current center point
    \o \c previousCenter is the center point of the previous event.
    \o \c startCenter is the center point when the gesture began
    \endlist
*/

/*!
    \qmlproperty real PinchEvent::scale
    \qmlproperty real PinchEvent::previousScale

    These properties hold the scale factor determined by the change in distance between the two touch points.

    \list
    \o \c scale is the current scale factor.
    \o \c previousScale is the scale factor of the previous event.
    \endlist

    When a pinch gesture is started, the scale is 1.0.
*/

/*!
    \qmlproperty real PinchEvent::angle
    \qmlproperty real PinchEvent::previousAngle
    \qmlproperty real PinchEvent::rotation

    These properties hold the angle between the two touch points.

    \list
    \o \c angle is the current angle between the two points in the range -180 to 180.
    \o \c previousAngle is the angle of the previous event.
    \o \c rotation is the total rotation since the pinch gesture started.
    \endlist

    When a pinch gesture is started, the rotation is 0.0.
*/

/*!
    \qmlproperty QPointF PinchEvent::point1
    \qmlproperty QPointF PinchEvent::startPoint1
    \qmlproperty QPointF PinchEvent::point2
    \qmlproperty QPointF PinchEvent::startPoint2

    These properties provide the actual touch points generating the pinch.

    \list
    \o \c point1 and \c point2 hold the current positions of the points.
    \o \c startPoint1 and \c startPoint2 hold the positions of the points when the second point was touched.
    \endlist
*/

/*!
    \qmlproperty bool PinchEvent::accepted

    Setting this property to false in the \c PinchArea::onPinchStarted handler
    will result in no further pinch events being generated, and the gesture
    ignored.
*/

QDeclarativePinch::QDeclarativePinch()
    : m_target(0), m_minScale(1.0), m_maxScale(1.0)
    , m_minRotation(0.0), m_maxRotation(0.0)
    , m_axis(NoDrag), m_xmin(-FLT_MAX), m_xmax(FLT_MAX)
    , m_ymin(-FLT_MAX), m_ymax(FLT_MAX), m_active(false)
{
}

QDeclarativePinchAreaPrivate::~QDeclarativePinchAreaPrivate()
{
    delete pinch;
}

/*!
    \qmlclass PinchArea QDeclarativePinchArea
    \brief The PinchArea item enables simple pinch gesture handling.
    \inherits Item

    \bold {The PinchArea element was added in QtQuick 1.1}

    A PinchArea is an invisible item that is typically used in conjunction with
    a visible item in order to provide pinch gesture handling for that item.

    The \l enabled property is used to enable and disable pinch handling for
    the proxied item. When disabled, the pinch area becomes transparent to
    mouse/touch events.

    PinchArea can be used in two ways:

    \list
    \o setting a \c pinch.target to provide automatic interaction with an element
    \o using the onPinchStarted, onPinchUpdated and onPinchFinished handlers
    \endlist

    \sa PinchEvent
*/

/*!
    \qmlsignal PinchArea::onPinchStarted()

    This handler is called when the pinch area detects that a pinch gesture has started.

    The \l {PinchEvent}{pinch} parameter provides information about the pinch gesture,
    including the scale, center and angle of the pinch.

    To ignore this gesture set the \c pinch.accepted property to false.  The gesture
    will be cancelled and no further events will be sent.
*/

/*!
    \qmlsignal PinchArea::onPinchUpdated()

    This handler is called when the pinch area detects that a pinch gesture has changed.

    The \l {PinchEvent}{pinch} parameter provides information about the pinch gesture,
    including the scale, center and angle of the pinch.
*/

/*!
    \qmlsignal PinchArea::onPinchFinished()

    This handler is called when the pinch area detects that a pinch gesture has finished.

    The \l {PinchEvent}{pinch} parameter provides information about the pinch gesture,
    including the scale, center and angle of the pinch.
*/


/*!
    \qmlproperty Item PinchArea::pinch.target
    \qmlproperty bool PinchArea::pinch.active
    \qmlproperty real PinchArea::pinch.minimumScale
    \qmlproperty real PinchArea::pinch.maximumScale
    \qmlproperty real PinchArea::pinch.minimumRotation
    \qmlproperty real PinchArea::pinch.maximumRotation
    \qmlproperty enumeration PinchArea::pinch.dragAxis
    \qmlproperty real PinchArea::pinch.minimumX
    \qmlproperty real PinchArea::pinch.maximumX
    \qmlproperty real PinchArea::pinch.minimumY
    \qmlproperty real PinchArea::pinch.maximumY

    \c pinch provides a convenient way to make an item react to pinch gestures.

    \list
    \i \c pinch.target specifies the id of the item to drag.
    \i \c pinch.active specifies if the target item is currently being dragged.
    \i \c pinch.minimumScale and \c pinch.maximumScale limit the range of the Item::scale property.
    \i \c pinch.minimumRotation and \c pinch.maximumRotation limit the range of the Item::rotation property.
    \i \c pinch.dragAxis specifies whether dragging in not allowed (\c Pinch.NoDrag), can be done horizontally (\c Pinch.XAxis), vertically (\c Pinch.YAxis), or both (\c Pinch.XandYAxis)
    \i \c pinch.minimum and \c pinch.maximum limit how far the target can be dragged along the corresponding axes.
    \endlist
*/

QDeclarativePinchArea::QDeclarativePinchArea(QDeclarativeItem *parent)
  : QDeclarativeItem(*(new QDeclarativePinchAreaPrivate), parent)
{
    Q_D(QDeclarativePinchArea);
    d->init();
}

QDeclarativePinchArea::~QDeclarativePinchArea()
{
}

/*!
    \qmlproperty bool PinchArea::enabled
    This property holds whether the item accepts pinch gestures.

    This property defaults to true.
*/
bool QDeclarativePinchArea::isEnabled() const
{
    Q_D(const QDeclarativePinchArea);
    return d->absorb;
}

void QDeclarativePinchArea::setEnabled(bool a)
{
    Q_D(QDeclarativePinchArea);
    if (a != d->absorb) {
        d->absorb = a;
        emit enabledChanged();
    }
}

bool QDeclarativePinchArea::event(QEvent *event)
{
    Q_D(QDeclarativePinchArea);
    if (!d->absorb || !isVisible())
        return QDeclarativeItem::event(event);
    switch (event->type()) {
    case QEvent::TouchBegin:
    case QEvent::TouchUpdate: {
            QTouchEvent *touch = static_cast<QTouchEvent*>(event);
            d->touchPoints.clear();
            for (int i = 0; i < touch->touchPoints().count(); ++i) {
                if (!(touch->touchPoints().at(i).state() & Qt::TouchPointReleased)) {
                    d->touchPoints << touch->touchPoints().at(i);
                }
            }
            updatePinch();
        }
        return true;
    case QEvent::TouchEnd:
        d->touchPoints.clear();
        updatePinch();
        break;
    default:
        return QDeclarativeItem::event(event);
    }

    return QDeclarativeItem::event(event);
}

void QDeclarativePinchArea::updatePinch()
{
    Q_D(QDeclarativePinchArea);
    if (d->touchPoints.count() != 2) {
        if (d->inPinch) {
            d->stealMouse = false;
            setKeepMouseGrab(false);
            d->inPinch = false;
            QPointF pinchCenter = mapFromScene(d->sceneLastCenter);
            QDeclarativePinchEvent pe(pinchCenter, d->pinchLastScale, d->pinchLastAngle, d->pinchRotation);
            pe.setStartCenter(d->pinchStartCenter);
            pe.setPreviousCenter(pinchCenter);
            pe.setPreviousAngle(d->pinchLastAngle);
            pe.setPreviousScale(d->pinchLastScale);
            pe.setStartPoint1(mapFromScene(d->sceneStartPoint1));
            pe.setStartPoint2(mapFromScene(d->sceneStartPoint2));
            pe.setPoint1(d->lastPoint1);
            pe.setPoint2(d->lastPoint2);
            emit pinchFinished(&pe);
            d->pinchStartDist = 0;
            if (d->pinch && d->pinch->target())
                d->pinch->setActive(false);
        }
        return;
    }
    if (d->touchPoints.at(0).state() & Qt::TouchPointPressed
        || d->touchPoints.at(1).state() & Qt::TouchPointPressed) {
        d->sceneStartPoint1 = d->touchPoints.at(0).scenePos();
        d->sceneStartPoint2 = d->touchPoints.at(1).scenePos();
        d->inPinch = false;
        d->pinchRejected = false;
    } else if (!d->pinchRejected){
        QDeclarativeItem *grabber = scene() ? qobject_cast<QDeclarativeItem*>(scene()->mouseGrabberItem()) : 0;
        if (grabber == this || !grabber || !grabber->keepMouseGrab()) {
            const int dragThreshold = QApplication::startDragDistance();
            QPointF p1 = d->touchPoints.at(0).scenePos();
            QPointF p2 = d->touchPoints.at(1).scenePos();
            qreal dx = p1.x() - p2.x();
            qreal dy = p1.y() - p2.y();
            qreal dist = sqrt(dx*dx + dy*dy);
            QPointF sceneCenter = (p1 + p2)/2;
            qreal angle = QLineF(p1, p2).angle();
            if (angle > 180)
                angle -= 360;
            if (!d->inPinch) {
                if (qAbs(p1.x()-d->sceneStartPoint1.x()) > dragThreshold
                        || qAbs(p1.y()-d->sceneStartPoint1.y()) > dragThreshold
                        || qAbs(p2.x()-d->sceneStartPoint2.x()) > dragThreshold
                        || qAbs(p2.y()-d->sceneStartPoint2.y()) > dragThreshold) {
                    d->sceneStartCenter = sceneCenter;
                    d->sceneLastCenter = sceneCenter;
                    d->pinchStartCenter = mapFromScene(sceneCenter);
                    d->pinchStartDist = dist;
                    d->pinchStartAngle = angle;
                    d->pinchLastScale = 1.0;
                    d->pinchLastAngle = angle;
                    d->pinchRotation = 0.0;
                    d->lastPoint1 = d->touchPoints.at(0).pos();
                    d->lastPoint2 = d->touchPoints.at(1).pos();
                    QDeclarativePinchEvent pe(d->pinchStartCenter, 1.0, angle, 0.0);
                    pe.setStartCenter(d->pinchStartCenter);
                    pe.setPreviousCenter(d->pinchStartCenter);
                    pe.setPreviousAngle(d->pinchLastAngle);
                    pe.setPreviousScale(d->pinchLastScale);
                    pe.setStartPoint1(mapFromScene(d->sceneStartPoint1));
                    pe.setStartPoint2(mapFromScene(d->sceneStartPoint2));
                    pe.setPoint1(d->lastPoint1);
                    pe.setPoint2(d->lastPoint2);
                    emit pinchStarted(&pe);
                    if (pe.accepted()) {
                        d->inPinch = true;
                        d->stealMouse = true;
                        QGraphicsScene *s = scene();
                        if (s && s->mouseGrabberItem() != this)
                            grabMouse();
                        setKeepMouseGrab(true);
                        if (d->pinch && d->pinch->target()) {
                            d->pinchStartPos = pinch()->target()->pos();
                            d->pinchStartScale = d->pinch->target()->scale();
                            d->pinchStartRotation = d->pinch->target()->rotation();
                            d->pinch->setActive(true);
                        }
                    } else {
                        d->pinchRejected = true;
                    }
                }
            } else if (d->pinchStartDist > 0) {
                qreal scale = dist / d->pinchStartDist;
                qreal da = d->pinchLastAngle - angle;
                if (da > 180)
                    da -= 360;
                else if (da < -180)
                    da += 360;
                d->pinchRotation += da;
                QPointF pinchCenter = mapFromScene(sceneCenter);
                QDeclarativePinchEvent pe(pinchCenter, scale, angle, d->pinchRotation);
                pe.setStartCenter(d->pinchStartCenter);
                pe.setPreviousCenter(mapFromScene(d->sceneLastCenter));
                pe.setPreviousAngle(d->pinchLastAngle);
                pe.setPreviousScale(d->pinchLastScale);
                pe.setStartPoint1(mapFromScene(d->sceneStartPoint1));
                pe.setStartPoint2(mapFromScene(d->sceneStartPoint2));
                pe.setPoint1(d->touchPoints.at(0).pos());
                pe.setPoint2(d->touchPoints.at(1).pos());
                d->pinchLastScale = scale;
                d->sceneLastCenter = sceneCenter;
                d->pinchLastAngle = angle;
                d->lastPoint1 = d->touchPoints.at(0).pos();
                d->lastPoint2 = d->touchPoints.at(1).pos();
                emit pinchUpdated(&pe);
                if (d->pinch && d->pinch->target()) {
                    qreal s = d->pinchStartScale * scale;
                    s = qMin(qMax(pinch()->minimumScale(),s), pinch()->maximumScale());
                    pinch()->target()->setScale(s);
                    QPointF pos = sceneCenter - d->sceneStartCenter + d->pinchStartPos;
                    if (pinch()->axis() & QDeclarativePinch::XAxis) {
                        qreal x = pos.x();
                        if (x < pinch()->xmin())
                            x = pinch()->xmin();
                        else if (x > pinch()->xmax())
                            x = pinch()->xmax();
                        pinch()->target()->setX(x);
                    }
                    if (pinch()->axis() & QDeclarativePinch::YAxis) {
                        qreal y = pos.y();
                        if (y < pinch()->ymin())
                            y = pinch()->ymin();
                        else if (y > pinch()->ymax())
                            y = pinch()->ymax();
                        pinch()->target()->setY(y);
                    }
                    if (d->pinchStartRotation >= pinch()->minimumRotation()
                            && d->pinchStartRotation <= pinch()->maximumRotation()) {
                        qreal r = d->pinchRotation + d->pinchStartRotation;
                        r = qMin(qMax(pinch()->minimumRotation(),r), pinch()->maximumRotation());
                        pinch()->target()->setRotation(r);
                    }
                }
            }
        }
    }
}

void QDeclarativePinchArea::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QDeclarativePinchArea);
    d->stealMouse = false;
    if (!d->absorb)
        QDeclarativeItem::mousePressEvent(event);
    else {
        setKeepMouseGrab(false);
        event->setAccepted(true);
    }
}

void QDeclarativePinchArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QDeclarativePinchArea);
    if (!d->absorb) {
        QDeclarativeItem::mouseMoveEvent(event);
        return;
    }
}

void QDeclarativePinchArea::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QDeclarativePinchArea);
    d->stealMouse = false;
    if (!d->absorb) {
        QDeclarativeItem::mouseReleaseEvent(event);
    } else {
        QGraphicsScene *s = scene();
        if (s && s->mouseGrabberItem() == this)
            ungrabMouse();
        setKeepMouseGrab(false);
    }
}

bool QDeclarativePinchArea::sceneEvent(QEvent *event)
{
    bool rv = QDeclarativeItem::sceneEvent(event);
    if (event->type() == QEvent::UngrabMouse) {
        setKeepMouseGrab(false);
    }
    return rv;
}

bool QDeclarativePinchArea::sendMouseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QDeclarativePinchArea);
    QGraphicsSceneMouseEvent mouseEvent(event->type());
    QRectF myRect = mapToScene(QRectF(0, 0, width(), height())).boundingRect();

    QGraphicsScene *s = scene();
    QDeclarativeItem *grabber = s ? qobject_cast<QDeclarativeItem*>(s->mouseGrabberItem()) : 0;
    bool stealThisEvent = d->stealMouse;
    if ((stealThisEvent || myRect.contains(event->scenePos().toPoint())) && (!grabber || !grabber->keepMouseGrab())) {
        mouseEvent.setAccepted(false);
        for (int i = 0x1; i <= 0x10; i <<= 1) {
            if (event->buttons() & i) {
                Qt::MouseButton button = Qt::MouseButton(i);
                mouseEvent.setButtonDownPos(button, mapFromScene(event->buttonDownPos(button)));
            }
        }
        mouseEvent.setScenePos(event->scenePos());
        mouseEvent.setLastScenePos(event->lastScenePos());
        mouseEvent.setPos(mapFromScene(event->scenePos()));
        mouseEvent.setLastPos(mapFromScene(event->lastScenePos()));

        switch(mouseEvent.type()) {
        case QEvent::GraphicsSceneMouseMove:
            mouseMoveEvent(&mouseEvent);
            break;
        case QEvent::GraphicsSceneMousePress:
            mousePressEvent(&mouseEvent);
            break;
        case QEvent::GraphicsSceneMouseRelease:
            mouseReleaseEvent(&mouseEvent);
            break;
        default:
            break;
        }
        grabber = qobject_cast<QDeclarativeItem*>(s->mouseGrabberItem());
        if (grabber && stealThisEvent && !grabber->keepMouseGrab() && grabber != this)
            grabMouse();

        return stealThisEvent;
    }
    if (mouseEvent.type() == QEvent::GraphicsSceneMouseRelease) {
        d->stealMouse = false;
        if (s && s->mouseGrabberItem() == this)
            ungrabMouse();
        setKeepMouseGrab(false);
    }
    return false;
}

bool QDeclarativePinchArea::sceneEventFilter(QGraphicsItem *i, QEvent *e)
{
    Q_D(QDeclarativePinchArea);
    if (!d->absorb || !isVisible())
        return QDeclarativeItem::sceneEventFilter(i, e);
    switch (e->type()) {
    case QEvent::GraphicsSceneMousePress:
    case QEvent::GraphicsSceneMouseMove:
    case QEvent::GraphicsSceneMouseRelease:
        return sendMouseEvent(static_cast<QGraphicsSceneMouseEvent *>(e));
        break;
    case QEvent::TouchBegin:
    case QEvent::TouchUpdate: {
            QTouchEvent *touch = static_cast<QTouchEvent*>(e);
            d->touchPoints.clear();
            for (int i = 0; i < touch->touchPoints().count(); ++i)
                if (!(touch->touchPoints().at(i).state() & Qt::TouchPointReleased))
                    d->touchPoints << touch->touchPoints().at(i);
            updatePinch();
        }
        return d->inPinch;
    case QEvent::TouchEnd:
        d->touchPoints.clear();
        updatePinch();
        break;
    default:
        break;
    }

    return QDeclarativeItem::sceneEventFilter(i, e);
}

void QDeclarativePinchArea::geometryChanged(const QRectF &newGeometry,
                                            const QRectF &oldGeometry)
{
    QDeclarativeItem::geometryChanged(newGeometry, oldGeometry);
}

QVariant QDeclarativePinchArea::itemChange(GraphicsItemChange change,
                                       const QVariant &value)
{
    return QDeclarativeItem::itemChange(change, value);
}

QDeclarativePinch *QDeclarativePinchArea::pinch()
{
    Q_D(QDeclarativePinchArea);
    if (!d->pinch)
        d->pinch = new QDeclarativePinch;
    return d->pinch;
}


QT_END_NAMESPACE