diff options
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativegridview.cpp')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativegridview.cpp | 377 |
1 files changed, 357 insertions, 20 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp index 76ad9b7..60ffbe2 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -50,6 +50,8 @@ #include <qlistmodelinterface_p.h> #include <QKeyEvent> +#include <math.h> + QT_BEGIN_NAMESPACE @@ -97,12 +99,13 @@ public: : currentItem(0), flow(QDeclarativeGridView::LeftToRight) , visiblePos(0), visibleIndex(0) , currentIndex(-1) , cellWidth(100), cellHeight(100), columns(1), requestedIndex(-1) + , highlightRangeStart(0), highlightRangeEnd(0), highlightRange(QDeclarativeGridView::NoHighlightRange) , highlightComponent(0), highlight(0), trackedItem(0) , moveReason(Other), buffer(0), highlightXAnimator(0), highlightYAnimator(0) - , bufferMode(NoBuffer) + , bufferMode(NoBuffer), snapMode(QDeclarativeGridView::NoSnap) , ownModel(false), wrap(false), autoHighlight(true) , fixCurrentVisibility(false), lazyRelease(false), layoutScheduled(false) - , deferredRelease(false) {} + , deferredRelease(false), haveHighlightRange(false) {} void init(); void clear(); @@ -119,6 +122,7 @@ public: void createHighlight(); void updateHighlight(); void updateCurrent(int modelIndex); + void fixupPosition(); FxGridItem *visibleItem(int modelIndex) const { if (modelIndex >= visibleIndex && modelIndex < visibleIndex + visibleItems.count()) { @@ -236,7 +240,33 @@ public: return -1; // Not in visibleList } - void itemGeometryChanged(QDeclarativeItem *item, const QRectF &newGeometry, const QRectF &oldGeometry) { + qreal snapPosAt(qreal pos) { + qreal snapPos = 0; + if (!visibleItems.isEmpty()) { + pos += rowSize()/2; + snapPos = visibleItems.first()->rowPos() - visibleIndex / columns * rowSize(); + snapPos = pos - fmodf(pos - snapPos, qreal(rowSize())); + } + return snapPos; + } + + int snapIndex() { + int index = currentIndex; + for (int i = 0; i < visibleItems.count(); ++i) { + FxGridItem *item = visibleItems[i]; + if (item->index == -1) + continue; + qreal itemTop = item->rowPos(); + if (itemTop >= highlight->rowPos()-rowSize()/2 && itemTop < highlight->rowPos()+rowSize()/2) { + index = item->index; + if (item->colPos() >= highlight->colPos()-colSize()/2 && item->colPos() < highlight->colPos()+colSize()/2) + return item->index; + } + } + return index; + } + + virtual void itemGeometryChanged(QDeclarativeItem *item, const QRectF &newGeometry, const QRectF &oldGeometry) { Q_Q(const QDeclarativeGridView); QDeclarativeFlickablePrivate::itemGeometryChanged(item, newGeometry, oldGeometry); if (item == q) { @@ -249,6 +279,11 @@ public: } } } + + virtual void fixup(AxisData &data, qreal minExtent, qreal maxExtent); + virtual void flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize, + QDeclarativeTimeLineCallback::Callback fixupCallback, qreal velocity); + // for debugging only void checkVisible() const { int skip = 0; @@ -277,6 +312,9 @@ public: int cellHeight; int columns; int requestedIndex; + qreal highlightRangeStart; + qreal highlightRangeEnd; + QDeclarativeGridView::HighlightRangeMode highlightRange; QDeclarativeComponent *highlightComponent; FxGridItem *highlight; FxGridItem *trackedItem; @@ -287,6 +325,7 @@ public: QDeclarativeEaseFollow *highlightYAnimator; enum BufferMode { NoBuffer = 0x00, BufferBefore = 0x01, BufferAfter = 0x02 }; BufferMode bufferMode; + QDeclarativeGridView::SnapMode snapMode; bool ownModel : 1; bool wrap : 1; @@ -295,6 +334,7 @@ public: bool lazyRelease : 1; bool layoutScheduled : 1; bool deferredRelease : 1; + bool haveHighlightRange : 1; }; void QDeclarativeGridViewPrivate::init() @@ -683,6 +723,122 @@ void QDeclarativeGridViewPrivate::updateCurrent(int modelIndex) releaseItem(oldCurrentItem); } +void QDeclarativeGridViewPrivate::fixupPosition() +{ + moveReason = Other; + if (flow == QDeclarativeGridView::LeftToRight) + fixupY(); + else + fixupX(); +} + +void QDeclarativeGridViewPrivate::fixup(AxisData &data, qreal minExtent, qreal maxExtent) +{ + Q_Q(QDeclarativeGridView); + + if ((&data == &vData && !q->yflick()) + || (&data == &hData && !q->xflick()) + || data.move.timeLine()) + return; + + int oldDuration = fixupDuration; + fixupDuration = moveReason == Mouse ? fixupDuration : 0; + + if (haveHighlightRange && highlightRange == QDeclarativeGridView::StrictlyEnforceRange) { + if (currentItem && currentItem->rowPos() - position() != highlightRangeStart) { + qreal pos = currentItem->rowPos() - highlightRangeStart; + timeline.reset(data.move); + if (fixupDuration) + timeline.move(data.move, -pos, QEasingCurve(QEasingCurve::InOutQuad), fixupDuration/2); + else + data.move.setValue(-pos); + vTime = timeline.time(); + } + } else if (snapMode != QDeclarativeGridView::NoSnap) { + qreal pos = qMax(qMin(snapPosAt(position()) - highlightRangeStart, -maxExtent), -minExtent); + qreal dist = qAbs(data.move + pos); + if (dist > 0) { + timeline.reset(data.move); + if (fixupDuration) + timeline.move(data.move, -pos, QEasingCurve(QEasingCurve::InOutQuad), fixupDuration/2); + else + data.move.setValue(-pos); + vTime = timeline.time(); + } + } else { + QDeclarativeFlickablePrivate::fixup(data, minExtent, maxExtent); + } + fixupDuration = oldDuration; +} + +void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize, + QDeclarativeTimeLineCallback::Callback fixupCallback, qreal velocity) +{ + Q_Q(QDeclarativeGridView); + + moveReason = Mouse; + if ((!haveHighlightRange || highlightRange != QDeclarativeGridView::StrictlyEnforceRange) && snapMode == QDeclarativeGridView::NoSnap) { + QDeclarativeFlickablePrivate::flick(data, minExtent, maxExtent, vSize, fixupCallback, velocity); + return; + } + qreal maxDistance = -1; + // -ve velocity means list is moving up + if (velocity > 0) { + if (snapMode == QDeclarativeGridView::SnapOneRow) { + if (FxGridItem *item = firstVisibleItem()) + maxDistance = qAbs(item->rowPos() + data.move.value()); + } else if (data.move.value() < minExtent) { + maxDistance = qAbs(minExtent - data.move.value() + (overShoot?overShootDistance(velocity, vSize):0)); + } + if (snapMode != QDeclarativeGridView::SnapToRow && highlightRange != QDeclarativeGridView::StrictlyEnforceRange) + data.flickTarget = minExtent; + } else { + if (snapMode == QDeclarativeGridView::SnapOneRow) { + qreal pos = snapPosAt(-data.move.value()) + rowSize(); + maxDistance = qAbs(pos + data.move.value()); + } else if (data.move.value() > maxExtent) { + maxDistance = qAbs(maxExtent - data.move.value()) + (overShoot?overShootDistance(velocity, vSize):0); + } + if (snapMode != QDeclarativeGridView::SnapToRow && highlightRange != QDeclarativeGridView::StrictlyEnforceRange) + data.flickTarget = maxExtent; + } + if (maxDistance > 0 && (snapMode != QDeclarativeGridView::NoSnap || highlightRange == QDeclarativeGridView::StrictlyEnforceRange)) { + // This mode requires the grid to stop exactly on a row boundary. + qreal v = velocity; + if (maxVelocity != -1 && maxVelocity < qAbs(v)) { + if (v < 0) + v = -maxVelocity; + else + v = maxVelocity; + } + qreal accel = deceleration; + qreal v2 = v * v; + qreal maxAccel = v2 / (2.0f * maxDistance); + qreal overshootDist = 0.0; + if (maxAccel < accel) { + qreal dist = v2 / (accel * 2.0); + if (v > 0) + dist = -dist; + data.flickTarget = -snapPosAt(-(data.move.value() - highlightRangeStart) + dist) + highlightRangeStart; + dist = -data.flickTarget + data.move.value(); + accel = v2 / (2.0f * qAbs(dist)); + } else { + data.flickTarget = velocity > 0 ? minExtent : maxExtent; + overshootDist = overShoot ? overShootDistance(v, vSize) : 0; + } + timeline.reset(data.move); + timeline.accel(data.move, v, accel, maxDistance + overshootDist); + timeline.callback(QDeclarativeTimeLineCallback(&data.move, fixupCallback, this)); + flicked = true; + emit q->flickingChanged(); + emit q->flickStarted(); + } else { + timeline.reset(data.move); + fixup(data, minExtent, maxExtent); + } +} + + //---------------------------------------------------------------------------- /*! @@ -1031,6 +1187,87 @@ void QDeclarativeGridView::setHighlightFollowsCurrentItem(bool autoHighlight) } /*! + \qmlproperty real GridView::preferredHighlightBegin + \qmlproperty real GridView::preferredHighlightEnd + \qmlproperty enumeration GridView::highlightRangeMode + + These properties set the preferred range of the highlight (current item) + within the view. + + Note that this is the correct way to influence where the + current item ends up when the view scrolls. For example, if you want the + currently selected item to be in the middle of the list, then set the + highlight range to be where the middle item would go. Then, when the view scrolls, + the currently selected item will be the item at that spot. This also applies to + when the currently selected item changes - it will scroll to within the preferred + highlight range. Furthermore, the behaviour of the current item index will occur + whether or not a highlight exists. + + If highlightRangeMode is set to \e ApplyRange the view will + attempt to maintain the highlight within the range, however + the highlight can move outside of the range at the ends of the list + or due to a mouse interaction. + + If highlightRangeMode is set to \e StrictlyEnforceRange the highlight will never + move outside of the range. This means that the current item will change + if a keyboard or mouse action would cause the highlight to move + outside of the range. + + The default value is \e NoHighlightRange. + + Note that a valid range requires preferredHighlightEnd to be greater + than or equal to preferredHighlightBegin. +*/ +qreal QDeclarativeGridView::preferredHighlightBegin() const +{ + Q_D(const QDeclarativeGridView); + return d->highlightRangeStart; +} + +void QDeclarativeGridView::setPreferredHighlightBegin(qreal start) +{ + Q_D(QDeclarativeGridView); + if (d->highlightRangeStart == start) + return; + d->highlightRangeStart = start; + d->haveHighlightRange = d->highlightRange != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd; + emit preferredHighlightBeginChanged(); +} + +qreal QDeclarativeGridView::preferredHighlightEnd() const +{ + Q_D(const QDeclarativeGridView); + return d->highlightRangeEnd; +} + +void QDeclarativeGridView::setPreferredHighlightEnd(qreal end) +{ + Q_D(QDeclarativeGridView); + if (d->highlightRangeEnd == end) + return; + d->highlightRangeEnd = end; + d->haveHighlightRange = d->highlightRange != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd; + emit preferredHighlightEndChanged(); +} + +QDeclarativeGridView::HighlightRangeMode QDeclarativeGridView::highlightRangeMode() const +{ + Q_D(const QDeclarativeGridView); + return d->highlightRange; +} + +void QDeclarativeGridView::setHighlightRangeMode(HighlightRangeMode mode) +{ + Q_D(QDeclarativeGridView); + if (d->highlightRange == mode) + return; + d->highlightRange = mode; + d->haveHighlightRange = d->highlightRange != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd; + emit highlightRangeModeChanged(); +} + + +/*! \qmlproperty enumeration GridView::flow This property holds the flow of the grid. @@ -1153,6 +1390,36 @@ void QDeclarativeGridView::setCellHeight(int cellHeight) d->layout(); } } +/*! + \qmlproperty enumeration GridView::snapMode + + This property determines where the view will settle following a drag or flick. + The allowed values are: + + \list + \o NoSnap (default) - the view will stop anywhere within the visible area. + \o SnapToRow - the view will settle with a row (or column for TopToBottom flow) + aligned with the start of the view. + \o SnapOneRow - the view will settle no more than one row (or column for TopToBottom flow) + away from the first visible row at the time the mouse button is released. + This mode is particularly useful for moving one page at a time. + \endlist + +*/ +QDeclarativeGridView::SnapMode QDeclarativeGridView::snapMode() const +{ + Q_D(const QDeclarativeGridView); + return d->snapMode; +} + +void QDeclarativeGridView::setSnapMode(SnapMode mode) +{ + Q_D(QDeclarativeGridView); + if (d->snapMode != mode) { + d->snapMode = mode; + emit snapModeChanged(); + } +} void QDeclarativeGridView::viewportMoved() { @@ -1175,6 +1442,32 @@ void QDeclarativeGridView::viewportMoved() } } refill(); + if (isFlicking() || d->moving) + d->moveReason = QDeclarativeGridViewPrivate::Mouse; + if (d->moveReason != QDeclarativeGridViewPrivate::SetIndex) { + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange && d->highlight) { + // reposition highlight + qreal pos = d->highlight->rowPos(); + qreal viewPos = qRound(d->position()); + if (pos > viewPos + d->highlightRangeEnd - 1 - d->rowSize()) + pos = viewPos + d->highlightRangeEnd - 1 - d->rowSize(); + if (pos < viewPos + d->highlightRangeStart) + pos = viewPos + d->highlightRangeStart; + d->highlight->setPosition(d->highlight->colPos(), pos); + + // update current index + int idx = d->snapIndex(); + if (idx >= 0 && idx != d->currentIndex) { + d->updateCurrent(idx); + if (d->currentItem && d->currentItem->colPos() != d->highlight->colPos() && d->autoHighlight) { + if (d->flow == LeftToRight) + d->highlightXAnimator->setSourceValue(d->currentItem->item->x()); + else + d->highlightYAnimator->setSourceValue(d->currentItem->item->y()); + } + } + } + } } qreal QDeclarativeGridView::minYExtent() const @@ -1182,7 +1475,10 @@ qreal QDeclarativeGridView::minYExtent() const Q_D(const QDeclarativeGridView); if (d->flow == QDeclarativeGridView::TopToBottom) return QDeclarativeFlickable::minYExtent(); - return -d->startPosition(); + qreal extent = -d->startPosition(); + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) + extent += d->highlightRangeStart; + return extent; } qreal QDeclarativeGridView::maxYExtent() const @@ -1190,7 +1486,11 @@ qreal QDeclarativeGridView::maxYExtent() const Q_D(const QDeclarativeGridView); if (d->flow == QDeclarativeGridView::TopToBottom) return QDeclarativeFlickable::maxYExtent(); - qreal extent = -(d->endPosition() - height()); + qreal extent; + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) + extent = -(d->rowPosAt(count()-1) - d->highlightRangeEnd); + else + extent = -(d->endPosition() - height()); const qreal minY = minYExtent(); if (extent > minY) extent = minY; @@ -1202,7 +1502,10 @@ qreal QDeclarativeGridView::minXExtent() const Q_D(const QDeclarativeGridView); if (d->flow == QDeclarativeGridView::LeftToRight) return QDeclarativeFlickable::minXExtent(); - return -d->startPosition(); + qreal extent = -d->startPosition(); + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) + extent += d->highlightRangeStart; + return extent; } qreal QDeclarativeGridView::maxXExtent() const @@ -1210,7 +1513,11 @@ qreal QDeclarativeGridView::maxXExtent() const Q_D(const QDeclarativeGridView); if (d->flow == QDeclarativeGridView::LeftToRight) return QDeclarativeFlickable::maxXExtent(); - qreal extent = -(d->endPosition() - width()); + qreal extent; + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) + extent = -(d->rowPosAt(count()-1) - d->highlightRangeEnd); + else + extent = -(d->endPosition() - height()); const qreal minX = minXExtent(); if (extent > minX) extent = minX; @@ -1381,6 +1688,7 @@ void QDeclarativeGridView::positionViewAtIndex(int index) for (int i = 0; i < oldVisible.count(); ++i) d->releaseItem(oldVisible.at(i)); } + d->fixupPosition(); } @@ -1394,6 +1702,7 @@ void QDeclarativeGridView::componentComplete() d->updateCurrent(0); else d->updateCurrent(d->currentIndex); + d->fixupPosition(); } void QDeclarativeGridView::trackedPositionChanged() @@ -1402,22 +1711,50 @@ void QDeclarativeGridView::trackedPositionChanged() if (!d->trackedItem || !d->currentItem) return; if (!isFlicking() && !d->moving && d->moveReason == QDeclarativeGridViewPrivate::SetIndex) { + const qreal trackedPos = d->trackedItem->rowPos(); const qreal viewPos = d->position(); - if (d->trackedItem->rowPos() < viewPos && d->currentItem->rowPos() < viewPos) { - d->setPosition(d->currentItem->rowPos() < d->trackedItem->rowPos() ? d->trackedItem->rowPos() : d->currentItem->rowPos()); - } else if (d->trackedItem->endRowPos() > viewPos + d->size() - && d->currentItem->endRowPos() > viewPos + d->size()) { - qreal pos; - if (d->trackedItem->endRowPos() < d->currentItem->endRowPos()) { - pos = d->trackedItem->endRowPos() - d->size(); - if (d->rowSize() > d->size()) - pos = d->trackedItem->rowPos(); + if (d->haveHighlightRange) { + if (d->highlightRange == StrictlyEnforceRange) { + qreal pos = viewPos; + if (trackedPos > pos + d->highlightRangeEnd - d->rowSize()) + pos = trackedPos - d->highlightRangeEnd + d->rowSize(); + if (trackedPos < pos + d->highlightRangeStart) + pos = trackedPos - d->highlightRangeStart; + d->setPosition(pos); } else { - pos = d->currentItem->endRowPos() - d->size(); - if (d->rowSize() > d->size()) - pos = d->currentItem->rowPos(); + qreal pos = viewPos; + if (trackedPos < d->startPosition() + d->highlightRangeStart) { + pos = d->startPosition(); + } else if (d->trackedItem->endRowPos() > d->endPosition() - d->size() + d->highlightRangeEnd) { + pos = d->endPosition() - d->size(); + if (pos < d->startPosition()) + pos = d->startPosition(); + } else { + if (trackedPos < viewPos + d->highlightRangeStart) { + pos = trackedPos - d->highlightRangeStart; + } else if (trackedPos > viewPos + d->highlightRangeEnd - d->rowSize()) { + pos = trackedPos - d->highlightRangeEnd + d->rowSize(); + } + } + d->setPosition(pos); + } + } else { + if (trackedPos < viewPos && d->currentItem->rowPos() < viewPos) { + d->setPosition(d->currentItem->rowPos() < trackedPos ? trackedPos : d->currentItem->rowPos()); + } else if (d->trackedItem->endRowPos() > viewPos + d->size() + && d->currentItem->endRowPos() > viewPos + d->size()) { + qreal pos; + if (d->trackedItem->endRowPos() < d->currentItem->endRowPos()) { + pos = d->trackedItem->endRowPos() - d->size(); + if (d->rowSize() > d->size()) + pos = trackedPos; + } else { + pos = d->currentItem->endRowPos() - d->size(); + if (d->rowSize() > d->size()) + pos = d->currentItem->rowPos(); + } + d->setPosition(pos); } - d->setPosition(pos); } } } |