summaryrefslogtreecommitdiffstats
path: root/src/declarative/fx/qfxflickable.cpp
blob: 52b142b3593a1b71e16028809f02681f0e5e8095 (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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (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 either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** 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.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qfxflickable.h"
#include "qfxflickable_p.h"

#include <QGraphicsSceneMouseEvent>
#include <QPointer>
#include <QTimer>

QT_BEGIN_NAMESPACE

ElasticValue::ElasticValue(QmlTimeLineValue &val)
    : _value(val)
{
    _to = _value.value();
    _myValue = _to;
    _velocity = 0;
}

void ElasticValue::setValue(qreal to)
{
    if (_to != to) {
        _to = to;
        _startTime.start();
        if (state() != Running)
            start();
    }
}

void ElasticValue::clear()
{
    stop();
    _velocity = 0.0;
    _myValue = _value.value();
}

void ElasticValue::updateCurrentTime(int)
{
    const qreal Tension = 0.1;
    int elapsed = _startTime.restart();
    if (!elapsed)
        return;
    qreal dist = _to - _value.value();
    qreal move = Tension * dist * qAbs(dist);
    if (elapsed < 100 && _velocity != 0.0)
        move = (elapsed * move + (100 - elapsed) * _velocity) / 100;
    _myValue += move * elapsed / 1000;
    _value.setValue(qRound(_myValue)); // moving sub-pixel can be ugly.
//    _value.setValue(_myValue);
    _velocity = move;
    if (qAbs(_velocity) < 5.0)
        clear();
    emit updated();
}

QFxFlickablePrivate::QFxFlickablePrivate()
  : _flick(new QFxItem), _moveX(_flick, &QFxItem::setX), _moveY(_flick, &QFxItem::setY)
    , vWidth(-1), vHeight(-1), overShoot(true), flicked(false), moving(false), stealMouse(false)
    , pressed(false), maxVelocity(-1), locked(false), dragMode(QFxFlickable::Hard)
    , elasticY(_moveY), elasticX(_moveX), velocityDecay(100), xVelocity(this), yVelocity(this)
    , vTime(0), atXEnd(false), atXBeginning(true), pageXPosition(0.), pageWidth(0.)
    , atYEnd(false), atYBeginning(true), pageYPosition(0.), pageHeight(0.)
{
    fixupXEvent = QmlTimeLineEvent::timeLineEvent<QFxFlickablePrivate, &QFxFlickablePrivate::fixupX>(&_moveX, this);
    fixupYEvent = QmlTimeLineEvent::timeLineEvent<QFxFlickablePrivate, &QFxFlickablePrivate::fixupY>(&_moveY, this);
}

void QFxFlickablePrivate::init()
{
    Q_Q(QFxFlickable);
    _flick->setParent(q);
    QObject::connect(&_tl, SIGNAL(updated()), q, SLOT(ticked()));
    QObject::connect(&_tl, SIGNAL(completed()), q, SLOT(movementEnding()));
    q->setAcceptedMouseButtons(Qt::LeftButton);
    q->setOptions(QSimpleCanvasItem::ChildMouseFilter | QSimpleCanvasItem::MouseEvents);
    QObject::connect(_flick, SIGNAL(leftChanged()), q, SIGNAL(positionChanged()));
    QObject::connect(_flick, SIGNAL(topChanged()), q, SIGNAL(positionChanged()));
    QObject::connect(&elasticX, SIGNAL(updated()), q, SLOT(ticked()));
    QObject::connect(&elasticY, SIGNAL(updated()), q, SLOT(ticked()));
}

void QFxFlickablePrivate::fixupX()
{
    Q_Q(QFxFlickable);
    if (!q->xflick() || _moveX.timeLine())
        return;

    vTime = _tl.time();

    if (_moveX.value() > q->minXExtent() || q->maxXExtent() > 0) {
        _tl.move(_moveX, q->minXExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200);
        flicked = false;
        //emit flickingChanged();
    } else if (_moveX.value() < q->maxXExtent()) {
        _tl.move(_moveX,  q->maxXExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200);
        flicked = false;
        //emit flickingChanged();
    }
}

void QFxFlickablePrivate::fixupY()
{
    Q_Q(QFxFlickable);
    if (!q->yflick() || _moveY.timeLine())
        return;

    vTime = _tl.time();

    if (_moveY.value() > q->minYExtent() || (q->maxYExtent() > q->minYExtent())) {
        _tl.move(_moveY, q->minYExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200);
        //emit flickingChanged();
    } else if (_moveY.value() < q->maxYExtent()) {
        _tl.move(_moveY,  q->maxYExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200);
        //emit flickingChanged();
    } else {
        flicked = false;
    }
}

void QFxFlickablePrivate::updateBeginningEnd()
{
    Q_Q(QFxFlickable);
    bool pageChange = false;
    bool atBoundaryChange = false;

    // Vertical
    const int viewheight = q->height();
    const int maxyextent = int(-q->maxYExtent());
    const qreal ypos = -_moveY.value();
    qreal pagePos = ((ypos * 100.0) / (maxyextent + viewheight)) / 100.0;
    qreal pageSize = ((viewheight * 100.0) / (maxyextent + viewheight)) / 100.0;
    bool atBeginning = (ypos <= 0.0);
    bool atEnd = (maxyextent <= ypos);

    if (pageSize != pageHeight) {
        pageHeight = pageSize;
        pageChange = true;
    }
    if (pagePos != pageYPosition) {
        pageYPosition = pagePos;
        pageChange = true;
    }
    if (atBeginning != atYBeginning) {
        atYBeginning = atBeginning;
        atBoundaryChange = true;
    }
    if (atEnd != atYEnd) {
        atYEnd = atEnd;
        atBoundaryChange = true;
    }

    // Horizontal
    const int viewwidth = q->width();
    const int maxxextent = int(-q->maxXExtent());
    const qreal xpos = -_moveX.value();
    pagePos = ((xpos * 100.0) / (maxxextent + viewwidth)) / 100.0;
    pageSize = ((viewwidth * 100.0) / (maxxextent + viewwidth)) / 100.0;
    atBeginning = (xpos <= 0.0);
    atEnd = (maxxextent <= xpos);

    if (pageSize != pageWidth) {
        pageWidth = pageSize;
        pageChange = true;
    }
    if (pagePos != pageXPosition) {
        pageXPosition = pagePos;
        pageChange = true;
    }
    if (atBeginning != atXBeginning) {
        atXBeginning = atBeginning;
        atBoundaryChange = true;
    }
    if (atEnd != atXEnd) {
        atXEnd = atEnd;
        atBoundaryChange = true;
    }

    if (pageChange)
        emit q->pageChanged();
    if (atBoundaryChange)
        emit q->isAtBoundaryChanged();
}

static const int FlickThreshold = 5;

QML_DEFINE_TYPE(QFxFlickable,Flickable);

/*!
    \qmlclass Flickable
    \brief The Flickable item provides a surface that can be "flicked".
    \inherits Item

    Flickable places its children on a surface that can be dragged and flicked.

    \code
    Flickable {
        width: 200; height: 200; viewportWidth: image.width; viewportHeight: image.height
        Image { id: image; source: "bigimage.png" }
    }
    \endcode

    \image flickable.gif

    \note Flickable does not automatically clip its contents. If
    it is not full-screen it is likely that \c clip should be set
    to true.

    \note Due to an implementation detail items placed inside a flickable cannot anchor to it by
    id, use 'parent' instead.
*/

/*!
    \internal
    \class QFxFlickable
    \brief The QFxFlickable class provides a view that can be "flicked".

    \ingroup group_widgets

    QFxFlickable allows its children to be dragged and flicked.
    
\code
Flickable {
    width: 320; height: 480; viewportWidth: image.width; viewportHeight: image.height
    Image { id: image; source: "bigimage.png" }
}
\endcode

    Note that QFxFlickable does not automatically clip its contents. If
    it is not full-screen it is likely that QFxItem::clip should be set
    to true.

*/

QFxFlickable::QFxFlickable(QFxItem *parent)
  : QFxItem(*(new QFxFlickablePrivate), parent)
{
    Q_D(QFxFlickable);
    d->init();
}

QFxFlickable::QFxFlickable(QFxFlickablePrivate &dd, QFxItem *parent)
  : QFxItem(dd, parent)
{
    Q_D(QFxFlickable);
    d->init();
}

QFxFlickable::~QFxFlickable()
{
}

/*!
    \qmlproperty int Flickable::xPosition
    \qmlproperty int Flickable::yPosition

    These properties hold the surface coordinate currently at the top-left
    corner of the Flickable. For example, if you flick an image up 100 pixels,
    \c yPosition will be 100.
*/

/*!
    \property QFxFlickable::xPosition
    \brief the x position of the view.

    The xPosition represents the left-most visible coordinate in the view.
*/
qreal QFxFlickable::xPosition() const
{
    Q_D(const QFxFlickable);
    return -d->_moveX.value();
}

void QFxFlickable::setXPosition(qreal pos)
{
    Q_D(QFxFlickable);
    pos = qRound(pos);
    if (-pos != d->_moveX.value()) {
        d->_tl.reset(d->_moveX);
        d->_moveX.setValue(-pos);
        viewportMoved();
    }
}

/*!
    \property QFxFlickable::yPosition
    \brief the y position of the view.

    The yPosition represents the top-most visible coordinate in the view.
*/
qreal QFxFlickable::yPosition() const
{
    Q_D(const QFxFlickable);
    return -d->_moveY.value();
}

void QFxFlickable::setYPosition(qreal pos)
{
    Q_D(QFxFlickable);
    pos = qRound(pos);
    if (-pos != d->_moveY.value()) {
        d->_tl.reset(d->_moveY);
        d->_moveY.setValue(-pos);
        viewportMoved();
    }
}

/*!
    \qmlproperty bool Flickable::locked

    A user cannot drag or flick a Flickable that is locked.

    This property is useful for temporarily disabling flicking. This allows
    special interaction with Flickable's children: for example, you might want to 
    freeze a flickable map while viewing detailed information on a location popup that is a child of the Flickable.
*/

/*!
    \property QFxFlickable::locked
    \brief determines whether the user can move the view.

    If the Flickable is locked, the user cannot move the view.
*/
bool QFxFlickable::isLocked() const
{
    Q_D(const QFxFlickable);
    return d->locked;
}

void QFxFlickable::setLocked(bool lock)
{
    Q_D(QFxFlickable);
    d->locked = lock;
}

/*!
    \qmlproperty enumeration Flickable::dragMode
    This property contains the kind of 'physics' applied when dragging the surface.

    Two modes are supported:
    \list 
    \i Hard - the view follows the user's input exactly.
    \i Elastic - the view moves elastically in response to the user's input.
    \endlist
*/

/*!
    \property QFxFlickable::dragMode
    \brief sets the kind of 'physics' applied when dragging the view.

    Two modes are supported:
    \list
    \i Hard - the view follows the user's input exactly.
    \i Elastic - the view moves elastically in response to the user's input.
    \endlist
*/
QFxFlickable::DragMode QFxFlickable::dragMode() const
{
    Q_D(const QFxFlickable);
    return d->dragMode;
}

void QFxFlickable::setDragMode(DragMode mode)
{
    Q_D(QFxFlickable);
    d->dragMode = mode;
}

/*!
    \qmlproperty real Flickable::xVelocity
    \qmlproperty real Flickable::yVelocity

    The instantaneous velocity of movement along the x and y axes, in pixels/sec.
*/

/*!
    \property QFxFlickable::xVelocity
    \brief provides the instantaneous velocity of movement in the x-axis (pixels/sec).
*/
qreal QFxFlickable::xVelocity() const
{
    Q_D(const QFxFlickable);
    return d->xVelocity.value();
}

/*!
    \property QFxFlickable::yVelocity
    \brief provides the instantaneous velocity of movement in the y-axis (pixels/sec).
*/
qreal QFxFlickable::yVelocity() const
{
    Q_D(const QFxFlickable);
    return d->yVelocity.value();
}

/*!
    \qmlproperty bool Flickable::atXBeginning
    \qmlproperty bool Flickable::atXEnd
    \qmlproperty bool Flickable::atYBeginning
    \qmlproperty bool Flickable::atYEnd

    These properties are true if the flickable view is positioned at the beginning,
    or end respecively.
*/
bool QFxFlickable::isAtXEnd() const
{
    Q_D(const QFxFlickable);
    return d->atXEnd;
}

bool QFxFlickable::isAtXBeginning() const
{
    Q_D(const QFxFlickable);
    return d->atXBeginning;
}

bool QFxFlickable::isAtYEnd() const
{
    Q_D(const QFxFlickable);
    return d->atYEnd;
}

bool QFxFlickable::isAtYBeginning() const
{
    Q_D(const QFxFlickable);
    return d->atYBeginning;
}

/*!
    \qmlproperty real Flickable::pageXPosition
    \qmlproperty real Flickable::pageWidth
    \qmlproperty real Flickable::pageYPosition
    \qmlproperty real Flickable::pageHeight

    These properties describe the position and size of the currently viewed page.
    The page size is defined as the percentage of the full view currently visible,
    scaled to 0.0 - 1.0.  The page position is also in the range 0.0 (beginning) to
    1.0 (end).

    These properties are typically used to draw a scrollbar, for example:
    \code
    Rect {
        opacity: 0.5; anchors.right: MyListView.right-2; width: 6
        y: MyListView.pageYPosition * MyListView.height
        height: MyListView.pageHeight * MyListView.height
    }
    \endcode
*/
qreal QFxFlickable::pageWidth() const
{
    Q_D(const QFxFlickable);
    return d->pageWidth;
}

qreal QFxFlickable::pageXPosition() const
{
    Q_D(const QFxFlickable);
    return d->pageXPosition;
}

qreal QFxFlickable::pageHeight() const
{
    Q_D(const QFxFlickable);
    return d->pageHeight;
}

qreal QFxFlickable::pageYPosition() const
{
    Q_D(const QFxFlickable);
    return d->pageYPosition;
}

void QFxFlickable::ticked()
{
    viewportMoved();
}

QFxItem *QFxFlickable::viewport()
{
    Q_D(QFxFlickable);
    return d->_flick;
}

qreal QFxFlickable::visibleX() const
{
    Q_D(const QFxFlickable);
    return -d->_moveX.value();
}

qreal QFxFlickable::visibleY() const
{
    Q_D(const QFxFlickable);
    return -d->_moveY.value();
}

void QFxFlickablePrivate::handleMousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (!locked && _tl.isActive() && (qAbs(velocityX) > 10 || qAbs(velocityY) > 10))
        stealMouse = true; // If we've been flicked then steal the click.
    else
        stealMouse = false;
    pressed = true;
    _tl.clear();
    velocityX = -1;
    velocityY = -1;
    lastPos = QPoint();
    lastPosTime.start();
    pressPos = event->pos();
    pressX = _moveX.value();
    pressY = _moveY.value();
    flicked = false;
    pressTime.start();
    if (dragMode == QFxFlickable::Elastic) {
        elasticX.clear();
        elasticY.clear();
    }
    velocityTime.start();
}

void QFxFlickablePrivate::handleMouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    Q_Q(QFxFlickable);
    if (locked || lastPosTime.isNull())
        return;
    bool rejectY = false;
    bool rejectX = false;
    bool moved = false;

    if (q->yflick()) {
        int dy = int(event->pos().y() - pressPos.y());
        if (qAbs(dy) > FlickThreshold || pressTime.elapsed() > 200) {
            qreal newY = dy + pressY;
            const qreal minY = q->minYExtent();
            const qreal maxY = q->maxYExtent();
            if (newY > minY)
                newY = minY + (newY - minY) / 2;
            if (newY < maxY && maxY - minY < 0)
                newY = maxY + (newY - maxY) / 2;
            if (q->overShoot() || (newY <= minY && newY >= maxY)) {
                if (dragMode == QFxFlickable::Hard)
                    _moveY.setValue(newY);
                else
                    elasticY.setValue(newY);
                moved = true;
            } else if (!q->overShoot())
                rejectY = true;
            if (qAbs(dy) > FlickThreshold)
                stealMouse = true;
        }
    }

    if (q->xflick()) {
        int dx = int(event->pos().x() - pressPos.x());
        if (qAbs(dx) > FlickThreshold || pressTime.elapsed() > 200) {
            qreal newX = dx + pressX;
            if (q->overShoot() || (newX <= q->minXExtent() && newX >= q->maxXExtent())) {
                if (dragMode == QFxFlickable::Hard)
                    _moveX.setValue(newX);
                else
                    elasticX.setValue(newX);
                moved = true;
            } else if (!q->overShoot())
                rejectX = true;
            if (qAbs(dx) > FlickThreshold)
                stealMouse = true;
        }
    }

    if (!lastPos.isNull()) {
        qreal elapsed = qreal(lastPosTime.restart()) / 1000.;
        if (elapsed <= 0)
            elapsed = 1;
        if (q->yflick()) {
            qreal diff = event->pos().y() - lastPos.y();
            velocityY = diff / elapsed;
        }

        if (q->xflick()) {
            qreal diff = event->pos().x() - lastPos.x();
            velocityX = diff / elapsed;
        }
    }

    if (rejectY) velocityY = 0;
    if (rejectX) velocityX = 0;

    if (moved) {
        q->viewportMoved();
        q->movementStarting();
    }

    lastPos = event->pos();
}

void QFxFlickablePrivate::handleMouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
    Q_Q(QFxFlickable);

    pressed = false;
    if (lastPosTime.isNull())
        return;

    if (dragMode == QFxFlickable::Elastic) {
        elasticY.clear();
        elasticX.clear();
    }

    vTime = _tl.time();
    if (qAbs(velocityY) > 10) {
        qreal maxDistance = -1;
        // -ve velocity means list is moving up
        if (velocityY > 0) {
            if (_moveY.value() < q->minYExtent())
                maxDistance = qAbs(q->minYExtent() -_moveY.value() + (overShoot?30:0));
        } else {
            if (_moveY.value() > q->maxYExtent())
                maxDistance = qAbs(q->maxYExtent() - _moveY.value()) + (overShoot?30:0);
        }
        if (maxDistance > 0) {
            qreal v = velocityY;
            if (maxVelocity != -1 && maxVelocity < qAbs(v)) {
                if (v < 0)
                    v = -maxVelocity;
                else
                    v = maxVelocity;
            }
            _tl.accel(_moveY, v, 500, maxDistance);
            _tl.execute(fixupYEvent);
            flicked = true;
            emit q->flickingChanged();
            emit q->flickStarted();
        } else {
            fixupY();
        }
    } else {
        fixupY();
    }
    if (qAbs(velocityX) > 10) {
        qreal maxDistance = -1;
        // -ve velocity means list is moving up
        if (velocityX > 0) {
            if (_moveX.value() < q->minXExtent())
                maxDistance = qAbs(q->minXExtent()) -_moveX.value() + (overShoot?30:0);
        } else {
            if (_moveX.value() > q->maxXExtent())
                maxDistance = qAbs(q->maxXExtent() - _moveX.value()) + (overShoot?30:0);
        }
        if (maxDistance > 0) {
            qreal v = velocityX;
            if (maxVelocity != -1 && maxVelocity < qAbs(v)) {
                if (v < 0)
                    v = -maxVelocity;
                else
                    v = maxVelocity;
            }
            _tl.accel(_moveX, v, 500, maxDistance);
            _tl.execute(fixupXEvent);
            flicked = true;
            emit q->flickingChanged();
            emit q->flickStarted();
        } else {
            fixupX();
        }
    } else {
        fixupX();
    }
    stealMouse = false;
    lastPosTime = QTime();

    if (!_tl.isActive())
        q->movementEnding();
}

void QFxFlickable::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QFxFlickable);
    d->handleMousePressEvent(event);
    event->accept();
}

void QFxFlickable::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QFxFlickable);
    d->handleMouseMoveEvent(event);
    event->accept();
}

void QFxFlickable::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QFxFlickable);
    d->handleMouseReleaseEvent(event);
    event->accept();
}

qreal QFxFlickable::minYExtent() const
{
    return 0.0;
}

qreal QFxFlickable::minXExtent() const
{
    return 0.0;
}

/* returns -ve */
qreal QFxFlickable::maxXExtent() const
{
    return width() - vWidth();
}
/* returns -ve */
qreal QFxFlickable::maxYExtent() const
{
    return height() - vHeight();
}

void QFxFlickable::viewportMoved()
{
    Q_D(QFxFlickable);
    //XXX should look at moveX here as well
    if (d->flicked && (d->_moveY.value() > minYExtent() + (d->overShoot?30:0)
                || d->_moveY.value() < maxYExtent() - (d->overShoot?30:0))){
        d->flicked = false;
        emit flickingChanged();
        emit flickEnded();
        d->_tl.reset(d->_moveY);
        d->fixupY();
    }

    int elapsed = d->velocityTime.elapsed();

    if (elapsed) {
        qreal prevY = d->lastFlickablePosition.x();
        qreal prevX = d->lastFlickablePosition.y();
        d->velocityTimeline.clear();
        if (d->pressed) {
            qreal xVelocity = (prevX - d->_moveX.value()) * 1000 / elapsed;
            qreal yVelocity = (prevY - d->_moveY.value()) * 1000 / elapsed;
            d->velocityTimeline.move(d->xVelocity, xVelocity, d->velocityDecay);
            d->velocityTimeline.move(d->xVelocity, 0, d->velocityDecay);
            d->velocityTimeline.move(d->yVelocity, yVelocity, d->velocityDecay);
            d->velocityTimeline.move(d->yVelocity, 0, d->velocityDecay);
        } else {
            if (d->_tl.time() != d->vTime) {
                qreal xVelocity = (prevX - d->_moveX.value()) * 1000 / (d->_tl.time() - d->vTime);
                qreal yVelocity = (prevY - d->_moveY.value()) * 1000 / (d->_tl.time() - d->vTime);
                d->xVelocity.setValue(xVelocity);
                d->yVelocity.setValue(yVelocity);
            }
            d->vTime = d->_tl.time();
        }
    }

    d->lastFlickablePosition = QPointF(d->_moveY.value(), d->_moveX.value());
    d->velocityTime.restart();
    d->updateBeginningEnd();
}

void QFxFlickablePrivate::data_removeAt(int)
{
    // ###
}

int QFxFlickablePrivate::data_count() const
{
    // ###
    return 0;
}

void QFxFlickablePrivate::data_append(QObject *o)
{
    Q_Q(QFxFlickable);
    QFxItem *i = qobject_cast<QFxItem *>(o);
    if (i)
        _flick->children()->append(i);
    else
        o->setParent(q);
}

void QFxFlickablePrivate::data_insert(int, QObject *)
{
    // ###
}

QObject *QFxFlickablePrivate::data_at(int) const
{
    // ###
    return 0;
}

void QFxFlickablePrivate::data_clear()
{
    // ###
}


QmlList<QObject *> *QFxFlickable::flickableData()
{
    Q_D(QFxFlickable);
    return &d->data;
}

QmlList<QFxItem *> *QFxFlickable::flickableChildren()
{
    Q_D(QFxFlickable);
    return d->_flick->children();
}

/*!
    \qmlproperty bool Flickable::overShoot
    This property holds the number of pixels the surface may overshoot the
    Flickable's boundaries when flicked.

    If overShoot is non-zero the contents can be flicked beyond the boundary
    of the Flickable before being moved back to the boundary.  This provides
    the feeling that the edges of the view are soft, rather than a hard
    physical boundary.
*/

/*!
    \property QFxFlickable::overShoot
    \brief the number of pixels the view may overshoot the boundaries when flicked.

    If overShoot is non-zero the contents can be flicked beyond the boundary
    of the view before being moved back to the boundary.  This provides
    the feeling that the edges of the view are soft, rather than a hard
    physical boundary.
*/
bool QFxFlickable::overShoot() const
{
    Q_D(const QFxFlickable);
    return d->overShoot;
}

void QFxFlickable::setOverShoot(bool o)
{
    Q_D(QFxFlickable);
    d->overShoot = o;
}

/*!
    \qmlproperty int Flickable::viewportWidth
    \qmlproperty int Flickable::viewportHeight

    The dimensions of the viewport (the surface controlled by Flickable). Typically this
    should be set to the combined size of the items placed in the Flickable.

    \code
    Flickable {
        width: 320; height: 480; viewportWidth: image.width; viewportHeight: image.height
        Image { id: image; source: "bigimage.png" }
    }
    \endcode
*/

/*!
    \property QFxFlickable::viewportWidth
    \brief the width of the view.
*/
int QFxFlickable::viewportWidth() const
{
    Q_D(const QFxFlickable);
    return d->vWidth;
}

void QFxFlickable::setViewportWidth(int w)
{
    Q_D(QFxFlickable);
    if (d->vWidth == w)
        return;
    d->vWidth = w;
    if (w < 0)
        d->_flick->setWidth(width());
    else
        d->_flick->setWidth(w);
    emit viewportWidthChanged();
    d->updateBeginningEnd();
}

void QFxFlickable::setWidth(int w)
{
    Q_D(QFxFlickable);
    QFxItem::setWidth(w);
    if (d->vWidth < 0) {
        d->_flick->setWidth(w);
        emit viewportWidthChanged();
        d->updateBeginningEnd();
    }
}

void QFxFlickable::setHeight(int h)
{
    Q_D(QFxFlickable);
    QFxItem::setHeight(h);
    if (d->vHeight < 0) {
        d->_flick->setHeight(h);
        emit viewportHeightChanged();
        d->updateBeginningEnd();
    }
}

/*!
    \property QFxFlickable::viewportHeight
    \brief the height of the view.
*/
int QFxFlickable::viewportHeight() const
{
    Q_D(const QFxFlickable);
    return d->vHeight;
}

void QFxFlickable::setViewportHeight(int h)
{
    Q_D(QFxFlickable);
    if (d->vHeight == h)
        return;
    d->vHeight = h;
    if (h < 0)
        d->_flick->setHeight(height());
    else
        d->_flick->setHeight(h);
    emit viewportHeightChanged();
    d->updateBeginningEnd();
}

int QFxFlickable::vWidth() const
{
    Q_D(const QFxFlickable);
    if (d->vWidth < 0)
        return width();
    else
        return d->vWidth;
}

int QFxFlickable::vHeight() const
{
    Q_D(const QFxFlickable);
    if (d->vHeight < 0)
        return height();
    else
        return d->vHeight;
}

bool QFxFlickable::xflick() const
{
    return vWidth() != width();
}

bool QFxFlickable::yflick() const
{
    return vHeight() !=  height();
}

bool QFxFlickable::sendMouseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QFxFlickable);
    QGraphicsSceneMouseEvent mouseEvent(event->type());
    QRectF myRect = mapToScene(QRectF(0, 0, width(), height()));
    QFxItem *grabber = static_cast<QFxItem*>(mouseGrabberItem());
    if ((d->stealMouse || 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:
            d->handleMouseMoveEvent(&mouseEvent);
            break;
        case QEvent::GraphicsSceneMousePress:
            d->handleMousePressEvent(&mouseEvent);
            break;
        case QEvent::GraphicsSceneMouseRelease:
            d->handleMouseReleaseEvent(&mouseEvent);
            break;
        default:
            break;
        }
        grabber = static_cast<QFxItem*>(mouseGrabberItem());
        if (grabber && d->stealMouse && !grabber->keepMouseGrab())
            grabMouse();

        return d->stealMouse;
    } else if (!d->lastPosTime.isNull()) {
        d->lastPosTime = QTime();
    }
    return false;
}

bool QFxFlickable::mouseFilter(QGraphicsSceneMouseEvent *e)
{
    if (!isVisible())
        return false;
    switch (e->type()) {
    case QEvent::GraphicsSceneMousePress:
    case QEvent::GraphicsSceneMouseMove:
    case QEvent::GraphicsSceneMouseRelease: 
        return sendMouseEvent(e);
    default:
        break;
    }

    return false;
}

/*!
    \qmlproperty int Flickable::maximumFlickVelocity
    This property holds the maximum velocity that the user can flick the view.
*/

/*!
    \property QFxFlickable::maximumFlickVelocity
    \brief the maximum velocity that the user can flick the view.
*/
int QFxFlickable::maximumFlickVelocity() const
{
    Q_D(const QFxFlickable);
    return d->maxVelocity;
}

void QFxFlickable::setMaximumFlickVelocity(int v)
{
    Q_D(QFxFlickable);
    if (v == d->maxVelocity)
        return;
    d->maxVelocity = v;
}

bool QFxFlickable::isFlicking() const
{
    Q_D(const QFxFlickable);
    return d->flicked;
}

int QFxFlickable::velocityDecay() const
{
    Q_D(const QFxFlickable);
    return d->velocityDecay;
}

void QFxFlickable::setVelocityDecay(int decay)
{
    Q_D(QFxFlickable);
    Q_ASSERT(decay >= 0);
    if (decay == d->velocityDecay)
        return;
    d->velocityDecay = decay;
    emit velocityDecayChanged(decay);
}

bool QFxFlickable::isMoving() const
{
    Q_D(const QFxFlickable);
    return d->moving;
}

void QFxFlickable::movementStarting()
{
    Q_D(QFxFlickable);
    if (!d->moving) {
        d->moving = true;
        emit movingChanged();
        emit movementStarted();
    }
}

void QFxFlickable::movementEnding()
{
    Q_D(QFxFlickable);
    if (d->moving) {
        d->moving = false;
        emit movingChanged();
        emit movementEnded();
    }
    if (d->flicked) {
        d->flicked = false;
        emit flickingChanged();
        emit flickEnded();
    }
    d->xVelocity.setValue(0);
}

void QFxFlickablePrivate::updateVelocity()
{
    Q_Q(QFxFlickable);
    emit q->velocityChanged(q->xVelocity(), q->yVelocity());
}

QT_END_NAMESPACE