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
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
|
/****************************************************************************
**
** 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
// These are highly device dependant.
// DragThreshold determines how far the "mouse" must move before
// we begin a drag.
// FlickThreshold determines how far the "mouse" must have moved
// before we perform a flick.
static const int DragThreshold = 8;
static const int FlickThreshold = 20;
class QFxFlickableVisibleArea : public QObject
{
Q_OBJECT
Q_PROPERTY(qreal xPosition READ xPosition NOTIFY pageChanged)
Q_PROPERTY(qreal yPosition READ yPosition NOTIFY pageChanged)
Q_PROPERTY(qreal widthRatio READ widthRatio NOTIFY pageChanged)
Q_PROPERTY(qreal heightRatio READ heightRatio NOTIFY pageChanged)
public:
QFxFlickableVisibleArea(QFxFlickable *parent=0);
qreal xPosition() const;
qreal widthRatio() const;
qreal yPosition() const;
qreal heightRatio() const;
void updateVisible();
signals:
void pageChanged();
private:
QFxFlickable *flickable;
qreal m_xPosition;
qreal m_widthRatio;
qreal m_yPosition;
qreal m_heightRatio;
};
QFxFlickableVisibleArea::QFxFlickableVisibleArea(QFxFlickable *parent)
: QObject(parent), flickable(parent), m_xPosition(0.), m_widthRatio(0.)
, m_yPosition(0.), m_heightRatio(0.)
{
}
qreal QFxFlickableVisibleArea::widthRatio() const
{
return m_widthRatio;
}
qreal QFxFlickableVisibleArea::xPosition() const
{
return m_xPosition;
}
qreal QFxFlickableVisibleArea::heightRatio() const
{
return m_heightRatio;
}
qreal QFxFlickableVisibleArea::yPosition() const
{
return m_yPosition;
}
void QFxFlickableVisibleArea::updateVisible()
{
QFxFlickablePrivate *p = static_cast<QFxFlickablePrivate *>(QGraphicsItemPrivate::get(flickable));
bool pageChange = false;
// Vertical
const qreal viewheight = flickable->height();
const qreal maxyextent = -flickable->maxYExtent();
qreal pagePos = -p->_moveY.value() / (maxyextent + viewheight);
qreal pageSize = viewheight / (maxyextent + viewheight);
if (pageSize != m_heightRatio) {
m_heightRatio = pageSize;
pageChange = true;
}
if (pagePos != m_yPosition) {
m_yPosition = pagePos;
pageChange = true;
}
// Horizontal
const qreal viewwidth = flickable->width();
const qreal maxxextent = -flickable->maxXExtent();
pagePos = -p->_moveX.value() / (maxxextent + viewwidth);
pageSize = viewwidth / (maxxextent + viewwidth);
if (pageSize != m_widthRatio) {
m_widthRatio = pageSize;
pageChange = true;
}
if (pagePos != m_xPosition) {
m_xPosition = pagePos;
pageChange = true;
}
if (pageChange)
emit pageChanged();
}
QFxFlickablePrivate::QFxFlickablePrivate()
: viewport(new QFxItem), _moveX(viewport, &QFxItem::setX), _moveY(viewport, &QFxItem::setY)
, vWidth(-1), vHeight(-1), overShoot(true), flicked(false), moving(false), stealMouse(false)
, pressed(false), atXEnd(false), atXBeginning(true), atYEnd(false), atYBeginning(true)
, interactive(true), maxVelocity(5000), reportedVelocitySmoothing(100)
, delayedPressEvent(0), delayedPressTarget(0), pressDelay(0)
, horizontalVelocity(this), verticalVelocity(this), vTime(0), visibleArea(0)
{
fixupXEvent = QmlTimeLineEvent::timeLineEvent<QFxFlickablePrivate, &QFxFlickablePrivate::fixupX>(&_moveX, this);
fixupYEvent = QmlTimeLineEvent::timeLineEvent<QFxFlickablePrivate, &QFxFlickablePrivate::fixupY>(&_moveY, this);
}
void QFxFlickablePrivate::init()
{
Q_Q(QFxFlickable);
viewport->setParent(q);
QObject::connect(&timeline, SIGNAL(updated()), q, SLOT(ticked()));
QObject::connect(&timeline, SIGNAL(completed()), q, SLOT(movementEnding()));
q->setAcceptedMouseButtons(Qt::LeftButton);
q->setFiltersChildEvents(true);
QObject::connect(viewport, SIGNAL(xChanged()), q, SIGNAL(positionXChanged()));
QObject::connect(viewport, SIGNAL(yChanged()), q, SIGNAL(positionYChanged()));
QObject::connect(q, SIGNAL(heightChanged()), q, SLOT(heightChange()));
QObject::connect(q, SIGNAL(widthChanged()), q, SLOT(widthChange()));
}
void QFxFlickablePrivate::flickX(qreal velocity)
{
Q_Q(QFxFlickable);
qreal maxDistance = -1;
// -ve velocity means list is moving up
if (velocity > 0) {
if (_moveX.value() < q->minXExtent())
maxDistance = qAbs(q->minXExtent() -_moveX.value() + (overShoot?30:0));
flickTargetX = q->minXExtent();
} else {
if (_moveX.value() > q->maxXExtent())
maxDistance = qAbs(q->maxXExtent() - _moveX.value()) + (overShoot?30:0);
flickTargetX = q->maxXExtent();
}
if (maxDistance > 0) {
qreal v = velocity;
if (maxVelocity != -1 && maxVelocity < qAbs(v)) {
if (v < 0)
v = -maxVelocity;
else
v = maxVelocity;
}
timeline.reset(_moveX);
timeline.accel(_moveX, v, 500, maxDistance);
timeline.execute(fixupXEvent);
if (!flicked) {
flicked = true;
emit q->flickingChanged();
emit q->flickStarted();
}
} else {
timeline.reset(_moveX);
fixupX();
}
}
void QFxFlickablePrivate::flickY(qreal velocity)
{
Q_Q(QFxFlickable);
qreal maxDistance = -1;
// -ve velocity means list is moving up
if (velocity > 0) {
if (_moveY.value() < q->minYExtent())
maxDistance = qAbs(q->minYExtent() -_moveY.value() + (overShoot?30:0));
flickTargetY = q->minYExtent();
} else {
if (_moveY.value() > q->maxYExtent())
maxDistance = qAbs(q->maxYExtent() - _moveY.value()) + (overShoot?30:0);
flickTargetY = q->maxYExtent();
}
if (maxDistance > 0) {
qreal v = velocity;
if (maxVelocity != -1 && maxVelocity < qAbs(v)) {
if (v < 0)
v = -maxVelocity;
else
v = maxVelocity;
}
timeline.reset(_moveY);
timeline.accel(_moveY, v, 500, maxDistance);
timeline.execute(fixupYEvent);
if (!flicked) {
flicked = true;
emit q->flickingChanged();
emit q->flickStarted();
}
} else {
timeline.reset(_moveY);
fixupY();
}
}
void QFxFlickablePrivate::fixupX()
{
Q_Q(QFxFlickable);
if (!q->xflick() || _moveX.timeLine())
return;
vTime = timeline.time();
if (_moveX.value() > q->minXExtent() || (q->maxXExtent() > q->minXExtent())) {
timeline.reset(_moveX);
if (_moveX.value() != q->minXExtent())
timeline.move(_moveX, q->minXExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200);
//emit flickingChanged();
} else if (_moveX.value() < q->maxXExtent()) {
timeline.reset(_moveX);
timeline.move(_moveX, q->maxXExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200);
//emit flickingChanged();
} else {
flicked = false;
}
}
void QFxFlickablePrivate::fixupY()
{
Q_Q(QFxFlickable);
if (!q->yflick() || _moveY.timeLine())
return;
vTime = timeline.time();
if (_moveY.value() > q->minYExtent() || (q->maxYExtent() > q->minYExtent())) {
timeline.reset(_moveY);
if (_moveY.value() != q->minYExtent())
timeline.move(_moveY, q->minYExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200);
//emit flickingChanged();
} else if (_moveY.value() < q->maxYExtent()) {
timeline.reset(_moveY);
timeline.move(_moveY, q->maxYExtent(), QEasingCurve(QEasingCurve::InOutQuad), 200);
//emit flickingChanged();
} else {
flicked = false;
}
}
void QFxFlickablePrivate::updateBeginningEnd()
{
Q_Q(QFxFlickable);
bool atBoundaryChange = false;
// Vertical
const int maxyextent = int(-q->maxYExtent());
const qreal ypos = -_moveY.value();
bool atBeginning = (ypos <= 0.0);
bool atEnd = (maxyextent <= ypos);
if (atBeginning != atYBeginning) {
atYBeginning = atBeginning;
atBoundaryChange = true;
}
if (atEnd != atYEnd) {
atYEnd = atEnd;
atBoundaryChange = true;
}
// Horizontal
const int maxxextent = int(-q->maxXExtent());
const qreal xpos = -_moveX.value();
atBeginning = (xpos <= 0.0);
atEnd = (maxxextent <= xpos);
if (atBeginning != atXBeginning) {
atXBeginning = atBeginning;
atBoundaryChange = true;
}
if (atEnd != atXEnd) {
atXEnd = atEnd;
atBoundaryChange = true;
}
if (atBoundaryChange)
emit q->isAtBoundaryChanged();
if (visibleArea)
visibleArea->updateVisible();
}
QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Flickable,QFxFlickable)
/*!
\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.
*/
/*!
\qmlproperty real Flickable::visibleArea.xPosition
\qmlproperty real Flickable::visibleArea.widthRatio
\qmlproperty real Flickable::visibleArea.yPosition
\qmlproperty real Flickable::visibleArea.heightRatio
These properties describe the position and size of the currently viewed area.
The size is defined as the percentage of the full view currently visible,
scaled to 0.0 - 1.0. The page position is in the range 0.0 (beginning) to
size ratio (end), i.e. yPosition is in the range 0.0 - heightRatio.
These properties are typically used to draw a scrollbar, for example:
\code
Rectangle {
opacity: 0.5; anchors.right: MyListView.right-2; width: 6
y: MyListView.visibleArea.yPosition * MyListView.height
height: MyListView.visibleArea.heightRatio * MyListView.height
}
\endcode
*/
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::viewportX
\qmlproperty int Flickable::viewportY
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.
*/
qreal QFxFlickable::viewportX() const
{
Q_D(const QFxFlickable);
return -d->_moveX.value();
}
void QFxFlickable::setViewportX(qreal pos)
{
Q_D(QFxFlickable);
pos = qRound(pos);
d->timeline.reset(d->_moveX);
if (-pos != d->_moveX.value()) {
d->_moveX.setValue(-pos);
viewportMoved();
}
}
qreal QFxFlickable::viewportY() const
{
Q_D(const QFxFlickable);
return -d->_moveY.value();
}
void QFxFlickable::setViewportY(qreal pos)
{
Q_D(QFxFlickable);
pos = qRound(pos);
d->timeline.reset(d->_moveY);
if (-pos != d->_moveY.value()) {
d->_moveY.setValue(-pos);
viewportMoved();
}
}
/*!
\qmlproperty bool Flickable::interactive
A user cannot drag or flick a Flickable that is not interactive.
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.
*/
bool QFxFlickable::isInteractive() const
{
Q_D(const QFxFlickable);
return d->interactive;
}
void QFxFlickable::setInteractive(bool interactive)
{
Q_D(QFxFlickable);
d->interactive = interactive;
if (!interactive && d->flicked) {
d->timeline.clear();
d->flicked = false;
emit flickingChanged();
emit flickEnded();
}
}
/*!
\qmlproperty real Flickable::horizontalVelocity
\qmlproperty real Flickable::verticalVelocity
\qmlproperty real Flickable::reportedVelocitySmoothing
The instantaneous velocity of movement along the x and y axes, in pixels/sec.
The reported velocity is smoothed to avoid erratic output.
reportedVelocitySmoothing determines how much smoothing is applied.
*/
qreal QFxFlickable::horizontalVelocity() const
{
Q_D(const QFxFlickable);
return d->horizontalVelocity.value();
}
qreal QFxFlickable::verticalVelocity() const
{
Q_D(const QFxFlickable);
return d->verticalVelocity.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;
}
void QFxFlickable::ticked()
{
viewportMoved();
}
QFxItem *QFxFlickable::viewport()
{
Q_D(QFxFlickable);
return d->viewport;
}
qreal QFxFlickable::visibleX() const
{
Q_D(const QFxFlickable);
return -d->_moveX.value();
}
qreal QFxFlickable::visibleY() const
{
Q_D(const QFxFlickable);
return -d->_moveY.value();
}
QFxFlickableVisibleArea *QFxFlickable::visibleArea()
{
Q_D(QFxFlickable);
if (!d->visibleArea)
d->visibleArea = new QFxFlickableVisibleArea(this);
return d->visibleArea;
}
void QFxFlickablePrivate::handleMousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (interactive && timeline.isActive() && (qAbs(velocityX) > 10 || qAbs(velocityY) > 10))
stealMouse = true; // If we've been flicked then steal the click.
else
stealMouse = false;
pressed = true;
timeline.clear();
velocityX = -1;
velocityY = -1;
lastPos = QPoint();
lastPosTime.start();
pressPos = event->pos();
pressX = _moveX.value();
pressY = _moveY.value();
flicked = false;
pressTime.start();
velocityTime.start();
}
void QFxFlickablePrivate::handleMouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
Q_Q(QFxFlickable);
if (!interactive || 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) > DragThreshold || 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)) {
_moveY.setValue(newY);
moved = true;
} else if (!q->overShoot())
rejectY = true;
if (qAbs(dy) > DragThreshold)
stealMouse = true;
}
}
if (q->xflick()) {
int dx = int(event->pos().x() - pressPos.x());
if (qAbs(dx) > DragThreshold || pressTime.elapsed() > 200) {
qreal newX = dx + pressX;
const qreal minX = q->minXExtent();
const qreal maxX = q->maxXExtent();
if (newX > minX)
newX = minX + (newX - minX) / 2;
if (newX < maxX && maxX - minX < 0)
newX = maxX + (newX - maxX) / 2;
if (q->overShoot() || (newX <= minX && newX >= maxX)) {
_moveX.setValue(newX);
moved = true;
} else if (!q->overShoot())
rejectX = true;
if (qAbs(dx) > DragThreshold)
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 *event)
{
Q_Q(QFxFlickable);
pressed = false;
if (lastPosTime.isNull())
return;
vTime = timeline.time();
if (qAbs(velocityY) > 10 && qAbs(event->pos().y() - pressPos.y()) > FlickThreshold)
flickY(velocityY);
else
fixupY();
if (qAbs(velocityX) > 10 && qAbs(event->pos().x() - pressPos.x()) > FlickThreshold)
flickX(velocityX);
else
fixupX();
stealMouse = false;
lastPosTime = QTime();
if (!timeline.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->clearDelayedPress();
d->handleMouseReleaseEvent(event);
event->accept();
ungrabMouse();
}
void QFxFlickablePrivate::captureDelayedPress(QGraphicsSceneMouseEvent *event)
{
Q_Q(QFxFlickable);
if (!q->scene() || pressDelay <= 0)
return;
delayedPressTarget = q->scene()->mouseGrabberItem();
delayedPressEvent = new QGraphicsSceneMouseEvent(event->type());
delayedPressEvent->setAccepted(false);
for (int i = 0x1; i <= 0x10; i <<= 1) {
if (event->buttons() & i) {
Qt::MouseButton button = Qt::MouseButton(i);
delayedPressEvent->setButtonDownPos(button, event->buttonDownPos(button));
}
}
delayedPressEvent->setScenePos(event->scenePos());
delayedPressEvent->setLastScenePos(event->lastScenePos());
delayedPressEvent->setPos(event->pos());
delayedPressEvent->setLastPos(event->lastPos());
delayedPressTimer.start(pressDelay, q);
}
void QFxFlickablePrivate::clearDelayedPress()
{
if (delayedPressEvent) {
delayedPressTimer.stop();
delete delayedPressEvent;
delayedPressEvent = 0;
}
}
void QFxFlickable::timerEvent(QTimerEvent *event)
{
Q_D(QFxFlickable);
if (event->timerId() == d->delayedPressTimer.timerId()) {
d->delayedPressTimer.stop();
if (d->delayedPressEvent) {
QFxItem *grabber = scene() ? qobject_cast<QFxItem*>(scene()->mouseGrabberItem()) : 0;
if (!grabber || grabber != this)
scene()->sendEvent(d->delayedPressTarget, d->delayedPressEvent);
delete d->delayedPressEvent;
d->delayedPressEvent = 0;
}
}
}
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);
int elapsed = d->velocityTime.elapsed();
if (elapsed) {
qreal prevY = d->lastFlickablePosition.x();
qreal prevX = d->lastFlickablePosition.y();
d->velocityTimeline.clear();
if (d->pressed) {
qreal horizontalVelocity = (prevX - d->_moveX.value()) * 1000 / elapsed;
qreal verticalVelocity = (prevY - d->_moveY.value()) * 1000 / elapsed;
d->velocityTimeline.move(d->horizontalVelocity, horizontalVelocity, d->reportedVelocitySmoothing);
d->velocityTimeline.move(d->horizontalVelocity, 0, d->reportedVelocitySmoothing);
d->velocityTimeline.move(d->verticalVelocity, verticalVelocity, d->reportedVelocitySmoothing);
d->velocityTimeline.move(d->verticalVelocity, 0, d->reportedVelocitySmoothing);
} else {
if (d->timeline.time() != d->vTime) {
qreal horizontalVelocity = (prevX - d->_moveX.value()) * 1000 / (d->timeline.time() - d->vTime);
qreal verticalVelocity = (prevY - d->_moveY.value()) * 1000 / (d->timeline.time() - d->vTime);
d->horizontalVelocity.setValue(horizontalVelocity);
d->verticalVelocity.setValue(verticalVelocity);
}
d->vTime = d->timeline.time();
}
}
d->lastFlickablePosition = QPointF(d->_moveY.value(), d->_moveX.value());
d->velocityTime.restart();
d->updateBeginningEnd();
if (d->flicked) {
// Near an end and it seems that the extent has changed?
// Recalculate the flick so that we don't end up in an odd position.
if (d->velocityY > 0) {
const qreal minY = minYExtent();
if (minY - d->_moveY.value() < height()/3 && minY != d->flickTargetY)
d->flickY(-d->verticalVelocity.value());
} else {
const qreal maxY = maxYExtent();
if (d->_moveY.value() - maxY < height()/3 && maxY != d->flickTargetY)
d->flickY(-d->verticalVelocity.value());
}
if (d->velocityX > 0) {
const qreal minX = minXExtent();
if (minX - d->_moveX.value() < height()/3 && minX != d->flickTargetX)
d->flickX(-d->horizontalVelocity.value());
} else {
const qreal maxX = maxXExtent();
if (d->_moveX.value() - maxX < height()/3 && maxX != d->flickTargetX)
d->flickX(-d->horizontalVelocity.value());
}
}
}
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)
viewport->fxChildren()->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->viewport->fxChildren();
}
/*!
\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.
*/
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
*/
qreal QFxFlickable::viewportWidth() const
{
Q_D(const QFxFlickable);
return d->vWidth;
}
void QFxFlickable::setViewportWidth(qreal w)
{
Q_D(QFxFlickable);
if (d->vWidth == w)
return;
d->vWidth = w;
if (w < 0)
d->viewport->setWidth(width());
else
d->viewport->setWidth(w);
// Make sure that we're entirely in view.
if (!d->pressed)
d->fixupX();
emit viewportWidthChanged();
d->updateBeginningEnd();
}
void QFxFlickable::widthChange()
{
Q_D(QFxFlickable);
if (d->vWidth < 0) {
d->viewport->setWidth(width());
emit viewportWidthChanged();
}
d->updateBeginningEnd();
}
void QFxFlickable::heightChange()
{
Q_D(QFxFlickable);
if (d->vHeight < 0) {
d->viewport->setHeight(height());
emit viewportHeightChanged();
}
d->updateBeginningEnd();
}
qreal QFxFlickable::viewportHeight() const
{
Q_D(const QFxFlickable);
return d->vHeight;
}
void QFxFlickable::setViewportHeight(qreal h)
{
Q_D(QFxFlickable);
if (d->vHeight == h)
return;
d->vHeight = h;
if (h < 0)
d->viewport->setHeight(height());
else
d->viewport->setHeight(h);
// Make sure that we're entirely in view.
if (!d->pressed)
d->fixupY();
emit viewportHeightChanged();
d->updateBeginningEnd();
}
qreal QFxFlickable::vWidth() const
{
Q_D(const QFxFlickable);
if (d->vWidth < 0)
return width();
else
return d->vWidth;
}
qreal 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())).boundingRect();
QGraphicsScene *s = scene();
QFxItem *grabber = s ? qobject_cast<QFxItem*>(s->mouseGrabberItem()) : 0;
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:
if (d->delayedPressEvent)
return false;
d->handleMousePressEvent(&mouseEvent);
d->captureDelayedPress(event);
break;
case QEvent::GraphicsSceneMouseRelease:
if (d->delayedPressEvent) {
scene()->sendEvent(d->delayedPressTarget, d->delayedPressEvent);
d->clearDelayedPress();
}
d->handleMouseReleaseEvent(&mouseEvent);
break;
default:
break;
}
grabber = qobject_cast<QFxItem*>(s->mouseGrabberItem());
if (grabber && d->stealMouse && !grabber->keepMouseGrab() && grabber != this) {
d->clearDelayedPress();
grabMouse();
}
return d->stealMouse || d->delayedPressEvent;
} else if (!d->lastPosTime.isNull()) {
d->lastPosTime = QTime();
}
if (mouseEvent.type() == QEvent::GraphicsSceneMouseRelease)
d->clearDelayedPress();
return false;
}
bool QFxFlickable::sceneEventFilter(QGraphicsItem *i, QEvent *e)
{
if (!isVisible())
return QFxItem::sceneEventFilter(i, e);
switch (e->type()) {
case QEvent::GraphicsSceneMousePress:
case QEvent::GraphicsSceneMouseMove:
case QEvent::GraphicsSceneMouseRelease:
return sendMouseEvent(static_cast<QGraphicsSceneMouseEvent *>(e));
default:
break;
}
return QFxItem::sceneEventFilter(i, e);
}
/*!
\qmlproperty int Flickable::maximumFlickVelocity
This property holds the maximum velocity that the user can flick the view in pixels/second.
The default is 5000 pixels/s
*/
qreal QFxFlickable::maximumFlickVelocity() const
{
Q_D(const QFxFlickable);
return d->maxVelocity;
}
void QFxFlickable::setMaximumFlickVelocity(qreal v)
{
Q_D(QFxFlickable);
if (v == d->maxVelocity)
return;
d->maxVelocity = v;
}
bool QFxFlickable::isFlicking() const
{
Q_D(const QFxFlickable);
return d->flicked;
}
/*!
\qmlproperty int Flickable::pressDelay
This property holds the time to delay (ms) delivering a press to
children of the Flickable. This can be useful where reacting
to a press before a flicking action has undesireable effects.
If the flickable is dragged/flicked before the delay times out
the press event will not be delivered. If the button is released
within the timeout, both the press and release will be delivered.
*/
int QFxFlickable::pressDelay() const
{
Q_D(const QFxFlickable);
return d->pressDelay;
}
void QFxFlickable::setPressDelay(int delay)
{
Q_D(QFxFlickable);
if (d->pressDelay == delay)
return;
d->pressDelay = delay;
}
qreal QFxFlickable::reportedVelocitySmoothing() const
{
Q_D(const QFxFlickable);
return d->reportedVelocitySmoothing;
}
void QFxFlickable::setReportedVelocitySmoothing(qreal reportedVelocitySmoothing)
{
Q_D(QFxFlickable);
Q_ASSERT(reportedVelocitySmoothing >= 0);
if (reportedVelocitySmoothing == d->reportedVelocitySmoothing)
return;
d->reportedVelocitySmoothing = reportedVelocitySmoothing;
emit reportedVelocitySmoothingChanged(reportedVelocitySmoothing);
}
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->horizontalVelocity.setValue(0);
}
void QFxFlickablePrivate::updateVelocity()
{
Q_Q(QFxFlickable);
emit q->velocityChanged(q->horizontalVelocity(), q->verticalVelocity());
}
QT_END_NAMESPACE
QML_DECLARE_TYPE(QFxFlickableVisibleArea)
QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,VisibleArea,QFxFlickableVisibleArea)
#include "qfxflickable.moc"
|