summaryrefslogtreecommitdiff
path: root/src/declarative/graphicsitems/qdeclarativeflickable.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativeflickable.cpp')
-rw-r--r--src/declarative/graphicsitems/qdeclarativeflickable.cpp286
1 files changed, 180 insertions, 106 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativeflickable.cpp b/src/declarative/graphicsitems/qdeclarativeflickable.cpp
index b4624436d4..6dfd4d9e58 100644
--- a/src/declarative/graphicsitems/qdeclarativeflickable.cpp
+++ b/src/declarative/graphicsitems/qdeclarativeflickable.cpp
@@ -125,12 +125,14 @@ QDeclarativeFlickablePrivate::QDeclarativeFlickablePrivate()
: viewport(new QDeclarativeItem)
, hData(this, &QDeclarativeFlickablePrivate::setRoundedViewportX)
, vData(this, &QDeclarativeFlickablePrivate::setRoundedViewportY)
- , flicked(false), moving(false), stealMouse(false)
- , pressed(false)
+ , flickingHorizontally(false), flickingVertically(false)
+ , hMoved(false), vMoved(false)
+ , movingHorizontally(false), movingVertically(false)
+ , stealMouse(false), pressed(false)
, interactive(true), deceleration(500), maxVelocity(2000), reportedVelocitySmoothing(100)
, delayedPressEvent(0), delayedPressTarget(0), pressDelay(0), fixupDuration(600)
, vTime(0), visibleArea(0)
- , flickDirection(QDeclarativeFlickable::AutoFlickDirection)
+ , flickableDirection(QDeclarativeFlickable::AutoFlickDirection)
, boundsBehavior(QDeclarativeFlickable::DragAndOvershootBounds)
{
}
@@ -158,6 +160,7 @@ void QDeclarativeFlickablePrivate::init()
q->setFiltersChildEvents(true);
QDeclarativeItemPrivate *viewportPrivate = static_cast<QDeclarativeItemPrivate*>(QGraphicsItemPrivate::get(viewport));
viewportPrivate->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry);
+ lastPosTime.invalidate();
}
/*
@@ -226,10 +229,19 @@ void QDeclarativeFlickablePrivate::flick(AxisData &data, qreal minExtent, qreal
timeline.reset(data.move);
timeline.accel(data.move, v, deceleration, maxDistance);
timeline.callback(QDeclarativeTimeLineCallback(&data.move, fixupCallback, this));
- if (!flicked) {
- flicked = true;
+ if (!flickingHorizontally && q->xflick()) {
+ flickingHorizontally = true;
emit q->flickingChanged();
- emit q->flickStarted();
+ emit q->flickingHorizontallyChanged();
+ if (!flickingVertically)
+ emit q->flickStarted();
+ }
+ if (!flickingVertically && q->yflick()) {
+ flickingVertically = true;
+ emit q->flickingChanged();
+ emit q->flickingVerticallyChanged();
+ if (!flickingHorizontally)
+ emit q->flickStarted();
}
} else {
timeline.reset(data.move);
@@ -274,7 +286,6 @@ void QDeclarativeFlickablePrivate::fixup(AxisData &data, qreal minExtent, qreal
q->viewportMoved();
}
}
- //emit flickingChanged();
} else if (data.move.value() < maxExtent) {
timeline.reset(data.move);
if (fixupDuration) {
@@ -285,11 +296,7 @@ void QDeclarativeFlickablePrivate::fixup(AxisData &data, qreal minExtent, qreal
data.move.setValue(maxExtent);
q->viewportMoved();
}
- //emit flickingChanged();
- } else {
- flicked = false;
}
-
vTime = timeline.time();
}
@@ -344,20 +351,24 @@ void QDeclarativeFlickablePrivate::updateBeginningEnd()
Flickable places its children on a surface that can be dragged and flicked.
\code
+ import Qt 4.7
+
Flickable {
- width: 200; height: 200; contentWidth: image.width; contentHeight: image.height
- Image { id: image; source: "bigimage.png" }
+ width: 200; height: 200
+ contentWidth: image.width; contentHeight: 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.
+ Flickable does not automatically clip its contents. If
+ it is not full-screen it is likely that \l {Item::clip}{clip} should be set
+ to \c true.
- \note Due to an implementation detail items placed inside a flickable cannot anchor to it by
- id, use 'parent' instead.
+ \note Due to an implementation detail, items placed inside a Flickable cannot anchor to it by
+ \c id. Use \c parent instead.
*/
/*!
@@ -400,18 +411,17 @@ void QDeclarativeFlickablePrivate::updateBeginningEnd()
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. yPosition is in the range 0.0 to 1.0-heightRatio.
+ 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:
- \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
+ These properties are typically used to draw a scrollbar. For example:
+
+ \snippet doc/src/snippets/declarative/flickableScrollbar.qml 0
+ \dots 8
+ \snippet doc/src/snippets/declarative/flickableScrollbar.qml 1
+
+ \sa {declarative/ui-components/scrollbar}{scrollbar example}
*/
QDeclarativeFlickable::QDeclarativeFlickable(QDeclarativeItem *parent)
@@ -479,11 +489,12 @@ void QDeclarativeFlickable::setContentY(qreal pos)
/*!
\qmlproperty bool Flickable::interactive
- A user cannot drag or flick a Flickable that is not interactive.
+ This property holds whether the user can interact with the Flickable. 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.
+ freeze a flickable map while scrolling through a pop-up dialog that is a child of the Flickable.
*/
bool QDeclarativeFlickable::isInteractive() const
{
@@ -496,11 +507,14 @@ void QDeclarativeFlickable::setInteractive(bool interactive)
Q_D(QDeclarativeFlickable);
if (interactive != d->interactive) {
d->interactive = interactive;
- if (!interactive && d->flicked) {
+ if (!interactive && (d->flickingHorizontally || d->flickingVertically)) {
d->timeline.clear();
d->vTime = d->timeline.time();
- d->flicked = false;
+ d->flickingHorizontally = false;
+ d->flickingVertically = false;
emit flickingChanged();
+ emit flickingHorizontallyChanged();
+ emit flickingVerticallyChanged();
emit flickEnded();
}
emit interactiveChanged();
@@ -580,35 +594,47 @@ QDeclarativeFlickableVisibleArea *QDeclarativeFlickable::visibleArea()
}
/*!
- \qmlproperty enumeration Flickable::flickDirection
+ \qmlproperty enumeration Flickable::flickableDirection
This property determines which directions the view can be flicked.
\list
- \o AutoFlickDirection (default) - allows flicking vertically if the
+ \o Flickable.AutoFlickDirection (default) - allows flicking vertically if the
\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.
- \o HorizontalFlick - allows flicking horizontally.
- \o VerticalFlick - allows flicking vertically.
- \o HorizontalAndVerticalFlick - allows flicking in both directions.
+ \o Flickable.HorizontalFlick - allows flicking horizontally.
+ \o Flickable.VerticalFlick - allows flicking vertically.
+ \o Flickable.HorizontalAndVerticalFlick - allows flicking in both directions.
\endlist
*/
-QDeclarativeFlickable::FlickDirection QDeclarativeFlickable::flickDirection() const
+QDeclarativeFlickable::FlickableDirection QDeclarativeFlickable::flickableDirection() const
{
Q_D(const QDeclarativeFlickable);
- return d->flickDirection;
+ return d->flickableDirection;
}
-void QDeclarativeFlickable::setFlickDirection(FlickDirection direction)
+void QDeclarativeFlickable::setFlickableDirection(FlickableDirection direction)
{
Q_D(QDeclarativeFlickable);
- if (direction != d->flickDirection) {
- d->flickDirection = direction;
- emit flickDirectionChanged();
+ if (direction != d->flickableDirection) {
+ d->flickableDirection = direction;
+ emit flickableDirectionChanged();
}
}
+QDeclarativeFlickable::FlickableDirection QDeclarativeFlickable::flickDirection() const
+{
+ qmlInfo(this) << "'flickDirection' is deprecated. Please use 'flickableDirection' instead.";
+ return flickableDirection();
+}
+
+void QDeclarativeFlickable::setFlickDirection(FlickableDirection direction)
+{
+ qmlInfo(this) << "'flickDirection' is deprecated. Please use 'flickableDirection' instead.";
+ setFlickableDirection(direction);
+}
+
void QDeclarativeFlickablePrivate::handleMousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (interactive && timeline.isActive() && (qAbs(hData.velocity) > 10 || qAbs(vData.velocity) > 10))
@@ -624,7 +650,8 @@ void QDeclarativeFlickablePrivate::handleMousePressEvent(QGraphicsSceneMouseEven
pressPos = event->pos();
hData.pressPos = hData.move.value();
vData.pressPos = vData.move.value();
- flicked = false;
+ flickingHorizontally = false;
+ flickingVertically = false;
QDeclarativeItemPrivate::start(pressTime);
QDeclarativeItemPrivate::start(velocityTime);
}
@@ -632,11 +659,10 @@ void QDeclarativeFlickablePrivate::handleMousePressEvent(QGraphicsSceneMouseEven
void QDeclarativeFlickablePrivate::handleMouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
Q_Q(QDeclarativeFlickable);
- if (!interactive || lastPosTime.isNull())
+ if (!interactive || !lastPosTime.isValid())
return;
bool rejectY = false;
bool rejectX = false;
- bool moved = false;
if (q->yflick()) {
int dy = int(event->pos().y() - pressPos.y());
@@ -658,7 +684,7 @@ void QDeclarativeFlickablePrivate::handleMouseMoveEvent(QGraphicsSceneMouseEvent
}
if (!rejectY && stealMouse) {
vData.move.setValue(qRound(newY));
- moved = true;
+ vMoved = true;
}
if (qAbs(dy) > QApplication::startDragDistance())
stealMouse = true;
@@ -685,7 +711,7 @@ void QDeclarativeFlickablePrivate::handleMouseMoveEvent(QGraphicsSceneMouseEvent
}
if (!rejectX && stealMouse) {
hData.move.setValue(qRound(newX));
- moved = true;
+ hMoved = true;
}
if (qAbs(dx) > QApplication::startDragDistance())
@@ -715,7 +741,7 @@ void QDeclarativeFlickablePrivate::handleMouseMoveEvent(QGraphicsSceneMouseEvent
if (rejectY) vData.velocity = 0;
if (rejectX) hData.velocity = 0;
- if (moved) {
+ if (hMoved || vMoved) {
q->movementStarting();
q->viewportMoved();
}
@@ -729,7 +755,7 @@ void QDeclarativeFlickablePrivate::handleMouseReleaseEvent(QGraphicsSceneMouseEv
stealMouse = false;
q->setKeepMouseGrab(false);
pressed = false;
- if (lastPosTime.isNull())
+ if (!lastPosTime.isValid())
return;
if (QDeclarativeItemPrivate::elapsed(lastPosTime) > 100) {
@@ -757,7 +783,7 @@ void QDeclarativeFlickablePrivate::handleMouseReleaseEvent(QGraphicsSceneMouseEv
fixupX();
}
- lastPosTime = QTime();
+ lastPosTime.invalidate();
if (!timeline.isActive())
q->movementEnding();
@@ -810,20 +836,24 @@ void QDeclarativeFlickable::wheelEvent(QGraphicsSceneWheelEvent *event)
d->vData.velocity = qMax(event->delta() - d->vData.smoothVelocity.value(), qreal(250.0));
else
d->vData.velocity = qMin(event->delta() - d->vData.smoothVelocity.value(), qreal(-250.0));
- d->flicked = false;
+ d->flickingVertically = false;
d->flickY(d->vData.velocity);
- if (d->flicked)
+ if (d->flickingVertically) {
+ d->vMoved = true;
movementStarting();
+ }
event->accept();
} else if (xflick()) {
if (event->delta() > 0)
d->hData.velocity = qMax(event->delta() - d->hData.smoothVelocity.value(), qreal(250.0));
else
d->hData.velocity = qMin(event->delta() - d->hData.smoothVelocity.value(), qreal(-250.0));
- d->flicked = false;
+ d->flickingHorizontally = false;
d->flickX(d->hData.velocity);
- if (d->flicked)
+ if (d->flickingHorizontally) {
+ d->hMoved = true;
movementStarting();
+ }
event->accept();
} else {
QDeclarativeItem::wheelEvent(event);
@@ -999,24 +1029,6 @@ QDeclarativeListProperty<QGraphicsObject> QDeclarativeFlickable::flickableChildr
return QGraphicsItemPrivate::get(d->viewport)->childrenList();
}
-bool QDeclarativeFlickable::overShoot() const
-{
- Q_D(const QDeclarativeFlickable);
- return d->boundsBehavior == DragAndOvershootBounds;
-}
-
-void QDeclarativeFlickable::setOverShoot(bool o)
-{
- Q_D(QDeclarativeFlickable);
- if ((o && d->boundsBehavior == DragAndOvershootBounds)
- || (!o && d->boundsBehavior == StopAtBounds))
- return;
- qmlInfo(this) << "overshoot is deprecated and will be removed imminently - use boundsBehavior.";
- d->boundsBehavior = o ? DragAndOvershootBounds : StopAtBounds;
- emit boundsBehaviorChanged();
- emit overShootChanged();
-}
-
/*!
\qmlproperty enumeration Flickable::boundsBehavior
This property holds whether the surface may be dragged
@@ -1026,14 +1038,14 @@ void QDeclarativeFlickable::setOverShoot(bool o)
This enables the feeling that the edges of the view are soft,
rather than a hard physical boundary.
- boundsBehavior can be one of:
+ The \c boundsBehavior can be one of:
\list
- \o \e StopAtBounds - the contents can not be dragged beyond the boundary
+ \o Flickable.StopAtBounds - the contents can not be dragged beyond the boundary
of the flickable, and flicks will not overshoot.
- \o \e DragOverBounds - the contents can be dragged beyond the boundary
+ \o Flickable.DragOverBounds - the contents can be dragged beyond the boundary
of the Flickable, but flicks will not overshoot.
- \o \e DragAndOvershootBounds (default) - the contents can be dragged
+ \o Flickable.DragAndOvershootBounds (default) - the contents can be dragged
beyond the boundary of the Flickable, and can overshoot the
boundary when flicked.
\endlist
@@ -1051,7 +1063,6 @@ void QDeclarativeFlickable::setBoundsBehavior(BoundsBehavior b)
return;
d->boundsBehavior = b;
emit boundsBehaviorChanged();
- emit overShootChanged();
}
/*!
@@ -1059,12 +1070,16 @@ void QDeclarativeFlickable::setBoundsBehavior(BoundsBehavior b)
\qmlproperty int Flickable::contentHeight
The dimensions of the content (the surface controlled by Flickable). Typically this
- should be set to the combined size of the items placed in the Flickable.
+ should be set to the combined size of the items placed in the Flickable. Note this
+ can be set automatically using \l {Item::childrenRect.width}{childrenRect.width}
+ and \l {Item::childrenRect.height}{childrenRect.height}. For example:
\code
Flickable {
- width: 320; height: 480; contentWidth: image.width; contentHeight: image.height
- Image { id: image; source: "bigimage.png" }
+ width: 320; height: 480
+ contentWidth: childrenRect.width; contentHeight: childrenRect.height
+
+ Image { id: image; source: "bigImage.png" }
}
\endcode
*/
@@ -1085,7 +1100,7 @@ void QDeclarativeFlickable::setContentWidth(qreal w)
else
d->viewport->setWidth(w);
// Make sure that we're entirely in view.
- if (!d->pressed) {
+ if (!d->pressed && !d->movingHorizontally && !d->movingVertically) {
int oldDuration = d->fixupDuration;
d->fixupDuration = 0;
d->fixupX();
@@ -1112,7 +1127,7 @@ void QDeclarativeFlickable::setContentHeight(qreal h)
else
d->viewport->setHeight(h);
// Make sure that we're entirely in view.
- if (!d->pressed) {
+ if (!d->pressed && !d->movingHorizontally && !d->movingVertically) {
int oldDuration = d->fixupDuration;
d->fixupDuration = 0;
d->fixupY();
@@ -1143,17 +1158,17 @@ qreal QDeclarativeFlickable::vHeight() const
bool QDeclarativeFlickable::xflick() const
{
Q_D(const QDeclarativeFlickable);
- if (d->flickDirection == QDeclarativeFlickable::AutoFlickDirection)
+ if (d->flickableDirection == QDeclarativeFlickable::AutoFlickDirection)
return vWidth() != width();
- return d->flickDirection & QDeclarativeFlickable::HorizontalFlick;
+ return d->flickableDirection & QDeclarativeFlickable::HorizontalFlick;
}
bool QDeclarativeFlickable::yflick() const
{
Q_D(const QDeclarativeFlickable);
- if (d->flickDirection == QDeclarativeFlickable::AutoFlickDirection)
+ if (d->flickableDirection == QDeclarativeFlickable::AutoFlickDirection)
return vHeight() != height();
- return d->flickDirection & QDeclarativeFlickable::VerticalFlick;
+ return d->flickableDirection & QDeclarativeFlickable::VerticalFlick;
}
bool QDeclarativeFlickable::sendMouseEvent(QGraphicsSceneMouseEvent *event)
@@ -1199,6 +1214,7 @@ bool QDeclarativeFlickable::sendMouseEvent(QGraphicsSceneMouseEvent *event)
default:
break;
}
+ stealThisEvent = d->stealMouse; // Update stealThisEvent and grabber in case changed by function calls above
grabber = qobject_cast<QDeclarativeItem*>(s->mouseGrabberItem());
if (grabber && stealThisEvent && !grabber->keepMouseGrab() && grabber != this) {
d->clearDelayedPress();
@@ -1206,8 +1222,8 @@ bool QDeclarativeFlickable::sendMouseEvent(QGraphicsSceneMouseEvent *event)
}
return stealThisEvent || d->delayedPressEvent;
- } else if (!d->lastPosTime.isNull()) {
- d->lastPosTime = QTime();
+ } else if (d->lastPosTime.isValid()) {
+ d->lastPosTime.invalidate();
}
if (mouseEvent.type() == QEvent::GraphicsSceneMouseRelease) {
d->clearDelayedPress();
@@ -1275,16 +1291,30 @@ void QDeclarativeFlickable::setFlickDeceleration(qreal deceleration)
emit flickDecelerationChanged();
}
+bool QDeclarativeFlickable::isFlicking() const
+{
+ Q_D(const QDeclarativeFlickable);
+ return d->flickingHorizontally || d->flickingVertically;
+}
+
/*!
\qmlproperty bool Flickable::flicking
+ \qmlproperty bool Flickable::flickingHorizontally
+ \qmlproperty bool Flickable::flickingVertically
- This property holds whether the view is currently moving due to
- the user flicking the view.
+ These properties hold whether the view is currently moving horizontally
+ or vertically due to the user flicking the view.
*/
-bool QDeclarativeFlickable::isFlicking() const
+bool QDeclarativeFlickable::isFlickingHorizontally() const
{
Q_D(const QDeclarativeFlickable);
- return d->flicked;
+ return d->flickingHorizontally;
+}
+
+bool QDeclarativeFlickable::isFlickingVertically() const
+{
+ Q_D(const QDeclarativeFlickable);
+ return d->flickingVertically;
}
/*!
@@ -1313,40 +1343,84 @@ void QDeclarativeFlickable::setPressDelay(int delay)
emit pressDelayChanged();
}
+
+bool QDeclarativeFlickable::isMoving() const
+{
+ Q_D(const QDeclarativeFlickable);
+ return d->movingHorizontally || d->movingVertically;
+}
+
/*!
\qmlproperty bool Flickable::moving
+ \qmlproperty bool Flickable::movingHorizontally
+ \qmlproperty bool Flickable::movingVertically
- This property holds whether the view is currently moving due to
- the user either dragging or flicking the view.
+ These properties hold whether the view is currently moving horizontally
+ or vertically due to the user either dragging or flicking the view.
*/
-bool QDeclarativeFlickable::isMoving() const
+bool QDeclarativeFlickable::isMovingHorizontally() const
{
Q_D(const QDeclarativeFlickable);
- return d->moving;
+ return d->movingHorizontally;
+}
+
+bool QDeclarativeFlickable::isMovingVertically() const
+{
+ Q_D(const QDeclarativeFlickable);
+ return d->movingVertically;
}
void QDeclarativeFlickable::movementStarting()
{
Q_D(QDeclarativeFlickable);
- if (!d->moving) {
- d->moving = true;
+ if (d->hMoved && !d->movingHorizontally) {
+ d->movingHorizontally = true;
+ emit movingChanged();
+ emit movingHorizontallyChanged();
+ if (!d->movingVertically)
+ emit movementStarted();
+ }
+ else if (d->vMoved && !d->movingVertically) {
+ d->movingVertically = true;
emit movingChanged();
- emit movementStarted();
+ emit movingVerticallyChanged();
+ if (!d->movingHorizontally)
+ emit movementStarted();
}
}
void QDeclarativeFlickable::movementEnding()
{
Q_D(QDeclarativeFlickable);
- if (d->moving) {
- d->moving = false;
- emit movingChanged();
- emit movementEnded();
+ if (d->flickingHorizontally) {
+ d->flickingHorizontally = false;
+ emit flickingChanged();
+ emit flickingHorizontallyChanged();
+ if (!d->flickingVertically)
+ emit flickEnded();
}
- if (d->flicked) {
- d->flicked = false;
+ if (d->flickingVertically) {
+ d->flickingVertically = false;
emit flickingChanged();
- emit flickEnded();
+ emit flickingVerticallyChanged();
+ if (!d->flickingHorizontally)
+ emit flickEnded();
+ }
+ if (d->movingHorizontally) {
+ d->movingHorizontally = false;
+ d->hMoved = false;
+ emit movingChanged();
+ emit movingHorizontallyChanged();
+ if (!d->movingVertically)
+ emit movementEnded();
+ }
+ if (d->movingVertically) {
+ d->movingVertically = false;
+ d->vMoved = false;
+ emit movingChanged();
+ emit movingVerticallyChanged();
+ if (!d->movingHorizontally)
+ emit movementEnded();
}
d->hData.smoothVelocity.setValue(0);
d->vData.smoothVelocity.setValue(0);