/
qquickflickable.cpp
2137 lines (1874 loc) · 66.1 KB
1
2
/****************************************************************************
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/
5
**
6
** This file is part of the QtQml module of the Qt Toolkit.
7
8
9
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
10
11
12
13
14
15
** 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.
16
17
**
** In addition, as a special exception, Nokia gives you certain additional
18
** rights. These rights are described in the Nokia Qt LGPL Exception
19
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
21
22
23
24
25
26
27
28
29
30
31
** 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
32
33
34
35
36
**
**
**
**
**
37
**
38
39
40
41
** $QT_END_LICENSE$
**
****************************************************************************/
42
43
44
45
#include "qquickflickable_p.h"
#include "qquickflickable_p_p.h"
#include "qquickcanvas.h"
#include "qquickcanvas_p.h"
46
#include "qquickevents_p_p.h"
47
48
49
50
#include <private/qqmlglobal_p.h>
#include <QtQml/qqmlinfo.h>
51
#include <QtGui/qevent.h>
52
53
#include <QtGui/qguiapplication.h>
#include <QtGui/qstylehints.h>
54
#include "qplatformdefs.h"
55
56
57
QT_BEGIN_NAMESPACE
58
59
60
61
62
63
64
65
66
67
68
69
70
// The maximum number of pixels a flick can overshoot
#ifndef QML_FLICK_OVERSHOOT
#define QML_FLICK_OVERSHOOT 200
#endif
// The number of samples to use in calculating the velocity of a flick
#ifndef QML_FLICK_SAMPLEBUFFER
#define QML_FLICK_SAMPLEBUFFER 3
#endif
// The number of samples to discard when calculating the flick velocity.
// Touch panels often produce inaccurate results as the finger is lifted.
#ifndef QML_FLICK_DISCARDSAMPLES
71
#define QML_FLICK_DISCARDSAMPLES 0
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#endif
// The default maximum velocity of a flick.
#ifndef QML_FLICK_DEFAULTMAXVELOCITY
#define QML_FLICK_DEFAULTMAXVELOCITY 2500
#endif
// The default deceleration of a flick.
#ifndef QML_FLICK_DEFAULTDECELERATION
#define QML_FLICK_DEFAULTDECELERATION 1500
#endif
// How much faster to decelerate when overshooting
#ifndef QML_FLICK_OVERSHOOTFRICTION
#define QML_FLICK_OVERSHOOTFRICTION 8
#endif
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Multiflick acceleration minimum flick velocity threshold
#ifndef QML_FLICK_MULTIFLICK_THRESHOLD
#define QML_FLICK_MULTIFLICK_THRESHOLD 1250
#endif
// Multiflick acceleration minimum contentSize/viewSize ratio
#ifndef QML_FLICK_MULTIFLICK_RATIO
#define QML_FLICK_MULTIFLICK_RATIO 10
#endif
// Multiflick acceleration maximum velocity multiplier
#ifndef QML_FLICK_MULTIFLICK_MAXBOOST
#define QML_FLICK_MULTIFLICK_MAXBOOST 3.0
#endif
104
105
// FlickThreshold determines how far the "mouse" must have moved
// before we perform a flick.
106
static const int FlickThreshold = 15;
107
108
109
// RetainGrabVelocity is the maxmimum instantaneous velocity that
// will ensure the Flickable retains the grab on consecutive flicks.
110
static const int RetainGrabVelocity = 100;
111
112
QQuickFlickableVisibleArea::QQuickFlickableVisibleArea(QQuickFlickable *parent)
113
114
115
116
117
: QObject(parent), flickable(parent), m_xPosition(0.), m_widthRatio(0.)
, m_yPosition(0.), m_heightRatio(0.)
{
}
118
qreal QQuickFlickableVisibleArea::widthRatio() const
119
120
121
122
{
return m_widthRatio;
}
123
qreal QQuickFlickableVisibleArea::xPosition() const
124
125
126
127
{
return m_xPosition;
}
128
qreal QQuickFlickableVisibleArea::heightRatio() const
129
130
131
132
{
return m_heightRatio;
}
133
qreal QQuickFlickableVisibleArea::yPosition() const
134
135
136
137
{
return m_yPosition;
}
138
void QQuickFlickableVisibleArea::updateVisible()
139
{
140
QQuickFlickablePrivate *p = QQuickFlickablePrivate::get(flickable);
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
bool changeX = false;
bool changeY = false;
bool changeWidth = false;
bool changeHeight = false;
// Vertical
const qreal viewheight = flickable->height();
const qreal maxyextent = -flickable->maxYExtent() + flickable->minYExtent();
qreal pagePos = (-p->vData.move.value() + flickable->minYExtent()) / (maxyextent + viewheight);
qreal pageSize = viewheight / (maxyextent + viewheight);
if (pageSize != m_heightRatio) {
m_heightRatio = pageSize;
changeHeight = true;
}
if (pagePos != m_yPosition) {
m_yPosition = pagePos;
changeY = true;
}
// Horizontal
const qreal viewwidth = flickable->width();
const qreal maxxextent = -flickable->maxXExtent() + flickable->minXExtent();
pagePos = (-p->hData.move.value() + flickable->minXExtent()) / (maxxextent + viewwidth);
pageSize = viewwidth / (maxxextent + viewwidth);
if (pageSize != m_widthRatio) {
m_widthRatio = pageSize;
changeWidth = true;
}
if (pagePos != m_xPosition) {
m_xPosition = pagePos;
changeX = true;
}
if (changeX)
emit xPositionChanged(m_xPosition);
if (changeY)
emit yPositionChanged(m_yPosition);
if (changeWidth)
emit widthRatioChanged(m_widthRatio);
if (changeHeight)
emit heightRatioChanged(m_heightRatio);
}
188
189
190
191
QQuickFlickablePrivate::QQuickFlickablePrivate()
: contentItem(new QQuickItem)
, hData(this, &QQuickFlickablePrivate::setViewportX)
, vData(this, &QQuickFlickablePrivate::setViewportY)
192
193
, hMoved(false), vMoved(false)
, stealMouse(false), pressed(false), interactive(true), calcVelocity(false)
194
, pixelAligned(false)
195
196
, lastPosTime(-1)
, lastPressTime(0)
197
198
199
, deceleration(QML_FLICK_DEFAULTDECELERATION)
, maxVelocity(QML_FLICK_DEFAULTMAXVELOCITY), reportedVelocitySmoothing(100)
, delayedPressEvent(0), delayedPressTarget(0), pressDelay(0), fixupDuration(400)
200
, flickBoost(1.0), fixupMode(Normal), vTime(0), visibleArea(0)
201
202
, flickableDirection(QQuickFlickable::AutoFlickDirection)
, boundsBehavior(QQuickFlickable::DragAndOvershootBounds)
203
204
205
{
}
206
void QQuickFlickablePrivate::init()
207
{
208
Q_Q(QQuickFlickable);
209
QQml_setParent_noEvent(contentItem, q);
210
contentItem->setParentItem(q);
211
FAST_CONNECT(&timeline, SIGNAL(completed()), q, SLOT(movementEnding()))
212
213
q->setAcceptedMouseButtons(Qt::LeftButton);
q->setFiltersChildMouseEvents(true);
214
215
QQuickItemPrivate *viewportPrivate = QQuickItemPrivate::get(contentItem);
viewportPrivate->addItemChangeListener(this, QQuickItemPrivate::Geometry);
216
217
218
219
220
221
}
/*
Returns the amount to overshoot by given a velocity.
Will be roughly in range 0 - size/4
*/
222
qreal QQuickFlickablePrivate::overShootDistance(qreal size)
223
224
225
226
{
if (maxVelocity <= 0)
return 0.0;
227
228
229
return qMin(qreal(QML_FLICK_OVERSHOOT), size/3);
}
230
void QQuickFlickablePrivate::AxisData::addVelocitySample(qreal v, qreal maxVelocity)
231
232
233
234
235
236
237
238
239
240
{
if (v > maxVelocity)
v = maxVelocity;
else if (v < -maxVelocity)
v = -maxVelocity;
velocityBuffer.append(v);
if (velocityBuffer.count() > QML_FLICK_SAMPLEBUFFER)
velocityBuffer.remove(0);
}
241
void QQuickFlickablePrivate::AxisData::updateVelocity()
242
{
243
velocity = 0;
244
245
246
247
248
249
250
251
if (velocityBuffer.count() > QML_FLICK_DISCARDSAMPLES) {
int count = velocityBuffer.count()-QML_FLICK_DISCARDSAMPLES;
for (int i = 0; i < count; ++i) {
qreal v = velocityBuffer.at(i);
velocity += v;
}
velocity /= count;
}
252
253
}
254
void QQuickFlickablePrivate::itemGeometryChanged(QQuickItem *item, const QRectF &newGeom, const QRectF &oldGeom)
255
{
256
Q_Q(QQuickFlickable);
257
if (item == contentItem) {
258
259
260
261
262
bool xChanged = newGeom.x() != oldGeom.x();
bool yChanged = newGeom.y() != oldGeom.y();
if (xChanged || yChanged)
q->viewportMoved();
if (xChanged)
263
emit q->contentXChanged();
264
if (yChanged)
265
266
267
268
emit q->contentYChanged();
}
}
269
void QQuickFlickablePrivate::flickX(qreal velocity)
270
{
271
Q_Q(QQuickFlickable);
272
273
274
flick(hData, q->minXExtent(), q->maxXExtent(), q->width(), fixupX_callback, velocity);
}
275
void QQuickFlickablePrivate::flickY(qreal velocity)
276
{
277
Q_Q(QQuickFlickable);
278
279
280
flick(vData, q->minYExtent(), q->maxYExtent(), q->height(), fixupY_callback, velocity);
}
281
void QQuickFlickablePrivate::flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal,
282
QQuickTimeLineCallback::Callback fixupCallback, qreal velocity)
283
{
284
Q_Q(QQuickFlickable);
285
286
287
288
qreal maxDistance = -1;
data.fixingUp = false;
// -ve velocity means list is moving up
if (velocity > 0) {
289
maxDistance = qAbs(minExtent - data.move.value());
290
291
data.flickTarget = minExtent;
} else {
292
maxDistance = qAbs(maxExtent - data.move.value());
293
294
295
296
297
298
299
300
301
302
303
data.flickTarget = maxExtent;
}
if (maxDistance > 0) {
qreal v = velocity;
if (maxVelocity != -1 && maxVelocity < qAbs(v)) {
if (v < 0)
v = -maxVelocity;
else
v = maxVelocity;
}
timeline.reset(data.move);
304
if (boundsBehavior == QQuickFlickable::DragAndOvershootBounds)
305
306
307
timeline.accel(data.move, v, deceleration);
else
timeline.accel(data.move, v, deceleration, maxDistance);
308
timeline.callback(QQuickTimeLineCallback(&data.move, fixupCallback, this));
309
if (!hData.flicking && q->xflick() && (&data == &hData)) {
310
hData.flicking = true;
311
312
emit q->flickingChanged();
emit q->flickingHorizontallyChanged();
313
if (!vData.flicking)
314
315
emit q->flickStarted();
}
316
if (!vData.flicking && q->yflick() && (&data == &vData)) {
317
vData.flicking = true;
318
319
emit q->flickingChanged();
emit q->flickingVerticallyChanged();
320
if (!hData.flicking)
321
322
323
324
325
326
327
328
emit q->flickStarted();
}
} else {
timeline.reset(data.move);
fixup(data, minExtent, maxExtent);
}
}
329
void QQuickFlickablePrivate::fixupY_callback(void *data)
330
{
331
((QQuickFlickablePrivate *)data)->fixupY();
332
333
}
334
void QQuickFlickablePrivate::fixupX_callback(void *data)
335
{
336
((QQuickFlickablePrivate *)data)->fixupX();
337
338
}
339
void QQuickFlickablePrivate::fixupX()
340
{
341
Q_Q(QQuickFlickable);
342
343
344
fixup(hData, q->minXExtent(), q->maxXExtent());
}
345
void QQuickFlickablePrivate::fixupY()
346
{
347
Q_Q(QQuickFlickable);
348
349
350
fixup(vData, q->minYExtent(), q->maxYExtent());
}
351
void QQuickFlickablePrivate::fixup(AxisData &data, qreal minExtent, qreal maxExtent)
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
{
if (data.move.value() > minExtent || maxExtent > minExtent) {
timeline.reset(data.move);
if (data.move.value() != minExtent) {
switch (fixupMode) {
case Immediate:
timeline.set(data.move, minExtent);
break;
case ExtentChanged:
// The target has changed. Don't start from the beginning; just complete the
// second half of the animation using the new extent.
timeline.move(data.move, minExtent, QEasingCurve(QEasingCurve::OutExpo), 3*fixupDuration/4);
data.fixingUp = true;
break;
default: {
qreal dist = minExtent - data.move;
timeline.move(data.move, minExtent - dist/2, QEasingCurve(QEasingCurve::InQuad), fixupDuration/4);
timeline.move(data.move, minExtent, QEasingCurve(QEasingCurve::OutExpo), 3*fixupDuration/4);
data.fixingUp = true;
}
}
}
} else if (data.move.value() < maxExtent) {
timeline.reset(data.move);
switch (fixupMode) {
case Immediate:
timeline.set(data.move, maxExtent);
break;
case ExtentChanged:
// The target has changed. Don't start from the beginning; just complete the
// second half of the animation using the new extent.
timeline.move(data.move, maxExtent, QEasingCurve(QEasingCurve::OutExpo), 3*fixupDuration/4);
data.fixingUp = true;
break;
default: {
qreal dist = maxExtent - data.move;
timeline.move(data.move, maxExtent - dist/2, QEasingCurve(QEasingCurve::InQuad), fixupDuration/4);
timeline.move(data.move, maxExtent, QEasingCurve(QEasingCurve::OutExpo), 3*fixupDuration/4);
data.fixingUp = true;
}
}
}
394
data.inOvershoot = false;
395
396
397
398
fixupMode = Normal;
vTime = timeline.time();
}
399
void QQuickFlickablePrivate::updateBeginningEnd()
400
{
401
Q_Q(QQuickFlickable);
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
bool atBoundaryChange = false;
// Vertical
const int maxyextent = int(-q->maxYExtent());
const qreal ypos = -vData.move.value();
bool atBeginning = (ypos <= -q->minYExtent());
bool atEnd = (maxyextent <= ypos);
if (atBeginning != vData.atBeginning) {
vData.atBeginning = atBeginning;
atBoundaryChange = true;
}
if (atEnd != vData.atEnd) {
vData.atEnd = atEnd;
atBoundaryChange = true;
}
// Horizontal
const int maxxextent = int(-q->maxXExtent());
const qreal xpos = -hData.move.value();
atBeginning = (xpos <= -q->minXExtent());
atEnd = (maxxextent <= xpos);
if (atBeginning != hData.atBeginning) {
hData.atBeginning = atBeginning;
atBoundaryChange = true;
}
if (atEnd != hData.atEnd) {
hData.atEnd = atEnd;
atBoundaryChange = true;
}
434
435
436
437
438
439
440
441
442
443
if (vData.extentsChanged) {
vData.extentsChanged = false;
emit q->yOriginChanged();
}
if (hData.extentsChanged) {
hData.extentsChanged = false;
emit q->xOriginChanged();
}
444
445
446
447
448
449
450
if (atBoundaryChange)
emit q->isAtBoundaryChanged();
if (visibleArea)
visibleArea->updateVisible();
}
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
/*
XXXTODO add docs describing moving, dragging, flicking properties, e.g.
When the user starts dragging the Flickable, the dragging and moving properties
will be true.
If the velocity is sufficient when the drag is ended, flicking may begin.
The moving properties will remain true until all dragging and flicking
is finished.
*/
/*!
\qmlsignal QtQuick2::Flickable::onDragStarted()
This handler is called when the view starts to be dragged due to user
interaction.
*/
/*!
\qmlsignal QtQuick2::Flickable::onDragEnded()
This handler is called when the user stops dragging the view.
If the velocity of the drag is suffient at the time the
touch/mouse button is released then a flick will start.
*/
479
/*!
480
\qmlclass Flickable QQuickFlickable
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
\inqmlmodule QtQuick 2
\ingroup qml-basic-interaction-elements
\brief The Flickable item provides a surface that can be "flicked".
\inherits Item
The Flickable item places its children on a surface that can be dragged
and flicked, causing the view onto the child items to scroll. This
behavior forms the basis of Items that are designed to show large numbers
of child items, such as \l ListView and \l GridView.
In traditional user interfaces, views can be scrolled using standard
controls, such as scroll bars and arrow buttons. In some situations, it
is also possible to drag the view directly by pressing and holding a
mouse button while moving the cursor. In touch-based user interfaces,
this dragging action is often complemented with a flicking action, where
scrolling continues after the user has stopped touching the view.
Flickable does not automatically clip its contents. If it is not used as
a full-screen item, you should consider setting the \l{Item::}{clip} property
to true.
\section1 Example Usage
\div {class="float-right"}
\inlineimage flickable.gif
\enddiv
The following example shows a small view onto a large image in which the
user can drag or flick the image in order to view different parts of it.
512
\snippet doc/src/snippets/qml/flickable.qml document
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
\clearfloat
Items declared as children of a Flickable are automatically parented to the
Flickable's \l contentItem. This should be taken into account when
operating on the children of the Flickable; it is usually the children of
\c contentItem that are relevant. For example, the bound of Items added
to the Flickable will be available by \c contentItem.childrenRect
\section1 Limitations
\note Due to an implementation detail, items placed inside a Flickable cannot anchor to it by
\c id. Use \c parent instead.
*/
/*!
\qmlsignal QtQuick2::Flickable::onMovementStarted()
This handler is called when the view begins moving due to user
interaction.
*/
/*!
\qmlsignal QtQuick2::Flickable::onMovementEnded()
This handler is called when the view stops moving due to user
interaction. If a flick was generated, this handler will
be triggered once the flick stops. If a flick was not
generated, the handler will be triggered when the
user stops dragging - i.e. a mouse or touch release.
*/
/*!
\qmlsignal QtQuick2::Flickable::onFlickStarted()
This handler is called when the view is flicked. A flick
starts from the point that the mouse or touch is released,
while still in motion.
*/
/*!
\qmlsignal QtQuick2::Flickable::onFlickEnded()
This handler is called when the view stops moving due to a flick.
*/
/*!
\qmlproperty real QtQuick2::Flickable::visibleArea.xPosition
\qmlproperty real QtQuick2::Flickable::visibleArea.widthRatio
\qmlproperty real QtQuick2::Flickable::visibleArea.yPosition
\qmlproperty real QtQuick2::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 usually in the range 0.0 (beginning) to
1.0 minus size ratio (end), i.e. \c yPosition is in the range 0.0 to 1.0-\c heightRatio.
However, it is possible for the contents to be dragged outside of the normal
range, resulting in the page positions also being outside the normal range.
These properties are typically used to draw a scrollbar. For example:
574
\snippet doc/src/snippets/qml/flickableScrollbar.qml 0
575
\dots 8
576
\snippet doc/src/snippets/qml/flickableScrollbar.qml 1
577
578
579
\sa {declarative/ui-components/scrollbar}{scrollbar example}
*/
580
581
QQuickFlickable::QQuickFlickable(QQuickItem *parent)
: QQuickItem(*(new QQuickFlickablePrivate), parent)
582
{
583
Q_D(QQuickFlickable);
584
585
586
d->init();
}
587
588
QQuickFlickable::QQuickFlickable(QQuickFlickablePrivate &dd, QQuickItem *parent)
: QQuickItem(dd, parent)
589
{
590
Q_D(QQuickFlickable);
591
592
593
d->init();
}
594
QQuickFlickable::~QQuickFlickable()
595
596
597
{
}
598
599
600
601
602
603
604
605
/*!
\qmlproperty real QtQuick2::Flickable::contentX
\qmlproperty real QtQuick2::Flickable::contentY
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 contentY will be 100.
*/
606
qreal QQuickFlickable::contentX() const
607
{
608
Q_D(const QQuickFlickable);
609
610
611
return -d->contentItem->x();
}
612
void QQuickFlickable::setContentX(qreal pos)
613
{
614
Q_D(QQuickFlickable);
615
d->hData.explicitValue = true;
616
617
618
d->timeline.reset(d->hData.move);
d->vTime = d->timeline.time();
movementXEnding();
619
if (-pos != d->hData.move.value())
620
621
622
d->hData.move.setValue(-pos);
}
623
qreal QQuickFlickable::contentY() const
624
{
625
Q_D(const QQuickFlickable);
626
627
628
return -d->contentItem->y();
}
629
void QQuickFlickable::setContentY(qreal pos)
630
{
631
Q_D(QQuickFlickable);
632
d->vData.explicitValue = true;
633
634
635
d->timeline.reset(d->vData.move);
d->vTime = d->timeline.time();
movementYEnding();
636
if (-pos != d->vData.move.value())
637
638
639
d->vData.move.setValue(-pos);
}
640
641
642
643
644
645
646
647
648
649
650
651
652
/*!
\qmlproperty bool QtQuick2::Flickable::interactive
This property describes whether the user can interact with the Flickable.
A user cannot drag or flick a Flickable that is not interactive.
By default, this property is true.
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 scrolling through a pop-up dialog that
is a child of the Flickable.
*/
653
bool QQuickFlickable::isInteractive() const
654
{
655
Q_D(const QQuickFlickable);
656
657
658
return d->interactive;
}
659
void QQuickFlickable::setInteractive(bool interactive)
660
{
661
Q_D(QQuickFlickable);
662
663
if (interactive != d->interactive) {
d->interactive = interactive;
664
if (!interactive && (d->hData.flicking || d->vData.flicking)) {
665
666
d->timeline.clear();
d->vTime = d->timeline.time();
667
668
d->hData.flicking = false;
d->vData.flicking = false;
669
670
671
672
673
674
675
676
677
emit flickingChanged();
emit flickingHorizontallyChanged();
emit flickingVerticallyChanged();
emit flickEnded();
}
emit interactiveChanged();
}
}
678
679
680
681
682
683
684
/*!
\qmlproperty real QtQuick2::Flickable::horizontalVelocity
\qmlproperty real QtQuick2::Flickable::verticalVelocity
The instantaneous velocity of movement along the x and y axes, in pixels/sec.
The reported velocity is smoothed to avoid erratic output.
685
686
687
688
689
Note that for views with a large content size (more than 10 times the view size),
the velocity of the flick may exceed the velocity of the touch in the case
of multiple quick consecutive flicks. This allows the user to flick faster
through large content.
690
*/
691
qreal QQuickFlickable::horizontalVelocity() const
692
{
693
Q_D(const QQuickFlickable);
694
695
696
return d->hData.smoothVelocity.value();
}
697
qreal QQuickFlickable::verticalVelocity() const
698
{
699
Q_D(const QQuickFlickable);
700
701
702
return d->vData.smoothVelocity.value();
}
703
704
705
706
707
708
709
/*!
\qmlproperty bool QtQuick2::Flickable::atXBeginning
\qmlproperty bool QtQuick2::Flickable::atXEnd
\qmlproperty bool QtQuick2::Flickable::atYBeginning
\qmlproperty bool QtQuick2::Flickable::atYEnd
These properties are true if the flickable view is positioned at the beginning,
710
or end respectively.
711
*/
712
bool QQuickFlickable::isAtXEnd() const
713
{
714
Q_D(const QQuickFlickable);
715
716
717
return d->hData.atEnd;
}
718
bool QQuickFlickable::isAtXBeginning() const
719
{
720
Q_D(const QQuickFlickable);
721
722
723
return d->hData.atBeginning;
}
724
bool QQuickFlickable::isAtYEnd() const
725
{
726
Q_D(const QQuickFlickable);
727
728
729
return d->vData.atEnd;
}
730
bool QQuickFlickable::isAtYBeginning() const
731
{
732
Q_D(const QQuickFlickable);
733
734
735
return d->vData.atBeginning;
}
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*!
\qmlproperty Item QtQuick2::Flickable::contentItem
The internal item that contains the Items to be moved in the Flickable.
Items declared as children of a Flickable are automatically parented to the Flickable's contentItem.
Items created dynamically need to be explicitly parented to the \e contentItem:
\code
Flickable {
id: myFlickable
function addItem(file) {
var component = Qt.createComponent(file)
component.createObject(myFlickable.contentItem);
}
}
\endcode
*/
754
QQuickItem *QQuickFlickable::contentItem()
755
{
756
Q_D(QQuickFlickable);
757
758
759
return d->contentItem;
}
760
QQuickFlickableVisibleArea *QQuickFlickable::visibleArea()
761
{
762
Q_D(QQuickFlickable);
763
if (!d->visibleArea)
764
d->visibleArea = new QQuickFlickableVisibleArea(this);
765
766
767
return d->visibleArea;
}
768
769
770
771
772
773
/*!
\qmlproperty enumeration QtQuick2::Flickable::flickableDirection
This property determines which directions the view can be flicked.
\list
774
\li Flickable.AutoFlickDirection (default) - allows flicking vertically if the
775
776
777
\e contentHeight is not equal to the \e height of the Flickable.
Allows flicking horizontally if the \e contentWidth is not equal
to the \e width of the Flickable.
778
779
780
\li Flickable.HorizontalFlick - allows flicking horizontally.
\li Flickable.VerticalFlick - allows flicking vertically.
\li Flickable.HorizontalAndVerticalFlick - allows flicking in both directions.
781
782
\endlist
*/
783
QQuickFlickable::FlickableDirection QQuickFlickable::flickableDirection() const
784
{
785
Q_D(const QQuickFlickable);
786
787
788
return d->flickableDirection;
}
789
void QQuickFlickable::setFlickableDirection(FlickableDirection direction)
790
{
791
Q_D(QQuickFlickable);
792
793
794
795
796
797
if (direction != d->flickableDirection) {
d->flickableDirection = direction;
emit flickableDirectionChanged();
}
}
798
bool QQuickFlickable::pixelAligned() const
799
{
800
Q_D(const QQuickFlickable);
801
802
803
return d->pixelAligned;
}
804
void QQuickFlickable::setPixelAligned(bool align)
805
{
806
Q_D(QQuickFlickable);
807
808
809
810
811
812
if (align != d->pixelAligned) {
d->pixelAligned = align;
emit pixelAlignedChanged();
}
}
813
814
815
816
817
818
819
820
qint64 QQuickFlickablePrivate::computeCurrentTime(QInputEvent *event)
{
if (0 != event->timestamp() && QQuickItemPrivate::consistentTime == -1)
return event->timestamp();
return QQuickItemPrivate::elapsed(timer);
}
821
void QQuickFlickablePrivate::handleMousePressEvent(QMouseEvent *event)
822
{
823
Q_Q(QQuickFlickable);
824
QQuickItemPrivate::start(timer);
825
if (interactive && timeline.isActive()
826
827
&& ((qAbs(hData.smoothVelocity.value()) > RetainGrabVelocity && !hData.fixingUp && !hData.inOvershoot)
|| (qAbs(vData.smoothVelocity.value()) > RetainGrabVelocity && !vData.fixingUp && !vData.inOvershoot))) {
828
stealMouse = true; // If we've been flicked then steal the click.
829
830
831
832
833
834
835
836
837
838
839
840
int flickTime = timeline.time();
if (flickTime > 600) {
// too long between flicks - cancel boost
hData.continuousFlickVelocity = 0;
vData.continuousFlickVelocity = 0;
flickBoost = 1.0;
} else {
hData.continuousFlickVelocity = -hData.smoothVelocity.value();
vData.continuousFlickVelocity = -vData.smoothVelocity.value();
if (flickTime > 300) // slower flicking - reduce boost
flickBoost = qMax(1.0, flickBoost - 0.5);
}
841
842
} else {
stealMouse = false;
843
844
845
hData.continuousFlickVelocity = 0;
vData.continuousFlickVelocity = 0;
flickBoost = 1.0;
846
847
848
}
q->setKeepMouseGrab(stealMouse);
pressed = true;
849
850
851
852
if (!hData.fixingUp)
timeline.reset(hData.move);
if (!vData.fixingUp)
timeline.reset(vData.move);
853
854
hData.reset();
vData.reset();
855
856
857
858
hData.dragMinBound = q->minXExtent();
vData.dragMinBound = q->minYExtent();
hData.dragMaxBound = q->maxXExtent();
vData.dragMaxBound = q->maxYExtent();
859
fixupMode = Normal;
860
lastPos = QPointF();
861
pressPos = event->localPos();
862
863
hData.pressPos = hData.move.value();
vData.pressPos = vData.move.value();
864
865
866
867
868
869
870
871
872
873
874
bool wasFlicking = hData.flicking || vData.flicking;
if (hData.flicking) {
hData.flicking = false;
emit q->flickingHorizontallyChanged();
}
if (vData.flicking) {
vData.flicking = false;
emit q->flickingVerticallyChanged();
}
if (wasFlicking)
emit q->flickingChanged();
875
lastPosTime = lastPressTime = computeCurrentTime(event);
876
QQuickItemPrivate::start(velocityTime);
877
878
}
879
void QQuickFlickablePrivate::handleMouseMoveEvent(QMouseEvent *event)
880
{
881
Q_Q(QQuickFlickable);
882
if (!interactive || lastPosTime == -1)
883
884
885
886
887
888
889
return;
bool rejectY = false;
bool rejectX = false;
bool stealY = stealMouse;
bool stealX = stealMouse;
890
qint64 elapsedSincePress = computeCurrentTime(event) - lastPressTime;
891
892
if (q->yflick()) {
893
qreal dy = event->localPos().y() - pressPos.y();
894
if (qAbs(dy) > qApp->styleHints()->startDragDistance() || elapsedSincePress > 200) {
895
896
897
898
899
900
901
902
903
if (!vMoved)
vData.dragStartOffset = dy;
qreal newY = dy + vData.pressPos - vData.dragStartOffset;
const qreal minY = vData.dragMinBound;
const qreal maxY = vData.dragMaxBound;
if (newY > minY)
newY = minY + (newY - minY) / 2;
if (newY < maxY && maxY - minY <= 0)
newY = maxY + (newY - maxY) / 2;
904
if (boundsBehavior == QQuickFlickable::StopAtBounds && (newY > minY || newY < maxY)) {
905
906
907
908
909
910
911
912
913
914
rejectY = true;
if (newY < maxY) {
newY = maxY;
rejectY = false;
}
if (newY > minY) {
newY = minY;
rejectY = false;
}
}
915
if (!rejectY && stealMouse && dy != 0.0) {
916
timeline.clear();
917
vData.move.setValue(newY);
918
919
vMoved = true;
}
920
if (qAbs(dy) > qApp->styleHints()->startDragDistance())
921
922
923
924
925
stealY = true;
}
}
if (q->xflick()) {
926
qreal dx = event->localPos().x() - pressPos.x();
927
if (qAbs(dx) > qApp->styleHints()->startDragDistance() || elapsedSincePress > 200) {
928
929
930
931
932
933
934
935
936
if (!hMoved)
hData.dragStartOffset = dx;
qreal newX = dx + hData.pressPos - hData.dragStartOffset;
const qreal minX = hData.dragMinBound;
const qreal maxX = hData.dragMaxBound;
if (newX > minX)
newX = minX + (newX - minX) / 2;
if (newX < maxX && maxX - minX <= 0)
newX = maxX + (newX - maxX) / 2;
937
if (boundsBehavior == QQuickFlickable::StopAtBounds && (newX > minX || newX < maxX)) {
938
939
940
941
942
943
944
945
946
947
rejectX = true;
if (newX < maxX) {
newX = maxX;
rejectX = false;
}
if (newX > minX) {
newX = minX;
rejectX = false;
}
}
948
if (!rejectX && stealMouse && dx != 0.0) {
949
timeline.clear();
950
hData.move.setValue(newX);
951
952
953
hMoved = true;
}
954
if (qAbs(dx) > qApp->styleHints()->startDragDistance())
955
956
957
958
959
960
961
962
stealX = true;
}
}
stealMouse = stealX || stealY;
if (stealMouse)
q->setKeepMouseGrab(true);
963
964
965
966
967
968
969
if (rejectY) {
vData.velocityBuffer.clear();
vData.velocity = 0;
}
if (rejectX) {
hData.velocityBuffer.clear();
hData.velocity = 0;
970
971
972
}
if (hMoved || vMoved) {
973
draggingStarting();
974
975
976
q->movementStarting();
}
977
978
979
980
981
982
983
984
985
986
987
988
qint64 currentTimestamp = computeCurrentTime(event);
qreal elapsed = qreal(currentTimestamp - (lastPos.isNull() ? lastPressTime : lastPosTime)) / 1000.;
if (elapsed <= 0)
return;
lastPosTime = currentTimestamp;
QQuickMouseEventEx *extended = QQuickMouseEventEx::extended(event);
if (q->yflick() && !rejectY) {
if (extended && extended->capabilities().testFlag(QTouchDevice::Velocity)) {
vData.addVelocitySample(extended->velocity().y(), maxVelocity);
} else {
qreal dy = event->localPos().y() - (lastPos.isNull() ? pressPos.y() : lastPos.y());
vData.addVelocitySample(dy/elapsed, maxVelocity);
989
}
990
991
992
993
994
995
996
}
if (q->xflick() && !rejectX) {
if (extended && extended->capabilities().testFlag(QTouchDevice::Velocity)) {
hData.addVelocitySample(extended->velocity().x(), maxVelocity);
} else {
qreal dx = event->localPos().x() - (lastPos.isNull() ? pressPos.x() : lastPos.x());
hData.addVelocitySample(dx/elapsed, maxVelocity);
997
}
998
999
}
1000
lastPos = event->localPos();