From baedccfc09c3a95a7a900318cf384fd8a03e3eeb Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Tue, 27 Oct 2009 11:47:16 +1000 Subject: Improve highlight range API and fix incorrect list end calc. --- examples/declarative/focusscope/test3.qml | 4 +- examples/declarative/listview/itemlist.qml | 4 +- examples/declarative/listview/listview.qml | 7 ++-- examples/declarative/parallax/ParallaxView.qml | 19 +-------- examples/declarative/parallax/parallax.qml | 49 +++++++++++----------- src/declarative/fx/qfxlistview.cpp | 57 +++++++++++++++----------- src/declarative/fx/qfxlistview.h | 8 ++-- 7 files changed, 70 insertions(+), 78 deletions(-) diff --git a/examples/declarative/focusscope/test3.qml b/examples/declarative/focusscope/test3.qml index e5aa7b6..1b3181b 100644 --- a/examples/declarative/focusscope/test3.qml +++ b/examples/declarative/focusscope/test3.qml @@ -40,8 +40,8 @@ Rectangle { model: model delegate: verticalDelegate preferredHighlightBegin: 100 - preferredHighlightEnd: 101 - strictlyEnforceHighlightRange: true + preferredHighlightEnd: 100 + highlightRangeMode: "StrictlyEnforceRange" } diff --git a/examples/declarative/listview/itemlist.qml b/examples/declarative/listview/itemlist.qml index 6dfc90b..77f5c2d 100644 --- a/examples/declarative/listview/itemlist.qml +++ b/examples/declarative/listview/itemlist.qml @@ -30,8 +30,8 @@ Rectangle { anchors.bottomMargin: 30 model: itemModel preferredHighlightBegin: 0 - preferredHighlightEnd: 1 - strictlyEnforceHighlightRange: true + preferredHighlightEnd: 0 + highlightRangeMode: "StrictlyEnforceRange" orientation: "Horizontal" } diff --git a/examples/declarative/listview/listview.qml b/examples/declarative/listview/listview.qml index 98974fd..b614904 100644 --- a/examples/declarative/listview/listview.qml +++ b/examples/declarative/listview/listview.qml @@ -41,7 +41,7 @@ Rectangle { // current item within the the bounds of the range, however // items will not scroll beyond the beginning or end of the view, // forcing the highlight to move outside the range at the ends. - // The third list sets strictlyEnforceHighlightRange to true + // The third list sets the highlightRangeMode to StrictlyEnforceRange // and sets a range smaller than the height of an item. This // forces the current item to change when the view is flicked, // since the highlight is unable to move. @@ -61,6 +61,7 @@ Rectangle { model: MyPetsModel; delegate: petDelegate; highlight: petHighlight preferredHighlightBegin: 80 preferredHighlightEnd: 220 + highlightRangeMode: "ApplyRange" currentIndex: list1.currentIndex } ListView { @@ -69,7 +70,7 @@ Rectangle { model: MyPetsModel; delegate: petDelegate; highlight: petHighlight currentIndex: list1.currentIndex preferredHighlightBegin: 125 - preferredHighlightEnd: 126 - strictlyEnforceHighlightRange: true + preferredHighlightEnd: 125 + highlightRangeMode: "StrictlyEnforceRange" } } diff --git a/examples/declarative/parallax/ParallaxView.qml b/examples/declarative/parallax/ParallaxView.qml index 4ddab00..38bb8c0 100644 --- a/examples/declarative/parallax/ParallaxView.qml +++ b/examples/declarative/parallax/ParallaxView.qml @@ -10,7 +10,6 @@ Item { width: Math.max(list.viewportWidth, parent.width) } - /* default property alias content: visualModel.children ListView { id: list @@ -21,21 +20,7 @@ Item { highlight: Item { height: 1; width: 1} preferredHighlightBegin: 0 - preferredHighlightEnd: 1 - strictlyEnforceHighlightRange: true - } - */ - - default property alias content: flickArea.data - Flickable { - id: list - anchors.fill: parent - overShoot: false - viewportWidth: flickArea.width - - Item { - id: flickArea - width: childrenRect.width - } + preferredHighlightEnd: 0 + highlightRangeMode: "StrictlyEnforceRange" } } diff --git a/examples/declarative/parallax/parallax.qml b/examples/declarative/parallax/parallax.qml index 9adcfd7..b2139ec 100644 --- a/examples/declarative/parallax/parallax.qml +++ b/examples/declarative/parallax/parallax.qml @@ -9,37 +9,34 @@ Rectangle { anchors.fill: parent background: "pics/background.jpg" - Row { - Item { - width: 320 - height: 480 - - Clock { - anchors.centerIn: parent - } + Item { + width: 320 + height: 480 + + Clock { + anchors.centerIn: parent } + } - Item { - width: 320 - height: 480 + Item { + width: 320 + height: 480 - Smiley {} - } + Smiley {} + } - Item { - width: 320 - height: 480 - - Loader { - anchors.centerIn: parent - width: 300; height: 460 - clip: true - resizeMode: Loader.SizeItemToLoader - - source: "../../../demos/declarative/samegame/samegame.qml" - } + Item { + width: 320 + height: 480 + + Loader { + anchors.centerIn: parent + width: 300; height: 460 + clip: true + resizeMode: Loader.SizeItemToLoader + + source: "../../../demos/declarative/samegame/samegame.qml" } - } } diff --git a/src/declarative/fx/qfxlistview.cpp b/src/declarative/fx/qfxlistview.cpp index 1a4a60c..28d2bb2 100644 --- a/src/declarative/fx/qfxlistview.cpp +++ b/src/declarative/fx/qfxlistview.cpp @@ -176,9 +176,9 @@ public: , highlightRangeStart(0), highlightRangeEnd(0) , highlightComponent(0), highlight(0), trackedItem(0) , moveReason(Other), buffer(0), highlightPosAnimator(0), highlightSizeAnimator(0), spacing(0.0) - , highlightMoveSpeed(400), highlightResizeSpeed(400) + , highlightMoveSpeed(400), highlightResizeSpeed(400), highlightRange(QFxListView::NoHighlightRange) , ownModel(false), wrap(false), autoHighlight(true) - , haveHighlightRange(false), strictHighlightRange(false) + , haveHighlightRange(false) {} void init(); @@ -392,12 +392,12 @@ public: qreal spacing; qreal highlightMoveSpeed; qreal highlightResizeSpeed; + QFxListView::HighlightRangeMode highlightRange; bool ownModel : 1; bool wrap : 1; bool autoHighlight : 1; bool haveHighlightRange : 1; - bool strictHighlightRange : 1; }; void QFxListViewPrivate::init() @@ -798,7 +798,7 @@ void QFxListViewPrivate::fixupY() if (orient == Qt::Horizontal) return; - if (haveHighlightRange && strictHighlightRange) { + if (haveHighlightRange && highlightRange == QFxListView::StrictlyEnforceRange) { if (currentItem && highlight && currentItem->position() != highlight->position()) { moveReason = Mouse; timeline.clear(); @@ -813,7 +813,7 @@ void QFxListViewPrivate::fixupX() if (orient == Qt::Vertical) return; - if (haveHighlightRange && strictHighlightRange) { + if (haveHighlightRange && highlightRange == QFxListView::StrictlyEnforceRange) { if (currentItem && highlight && currentItem->position() != highlight->position()) { moveReason = Mouse; timeline.clear(); @@ -1141,19 +1141,25 @@ void QFxListView::setHighlightFollowsCurrentItem(bool autoHighlight) /*! \qmlproperty real ListView::preferredHighlightBegin \qmlproperty real ListView::preferredHighlightEnd - \qmlproperty bool ListView::strictlyEnforceHighlightRange + \qmlproperty bool ListView::highlightRangeMode These properties set the preferred range of the highlight (current item) within the view. - If the strictlyEnforceHighlightRange property is false (default) + 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 strictlyEnforceHighlightRange is true then the highlight will never - move outside the range. This means that the current item will change + 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 QFxListView::preferredHighlightBegin() const { @@ -1165,7 +1171,7 @@ void QFxListView::setPreferredHighlightBegin(qreal start) { Q_D(QFxListView); d->highlightRangeStart = start; - d->haveHighlightRange = d->highlightRangeStart < d->highlightRangeEnd; + d->haveHighlightRange = d->highlightRange != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd; } qreal QFxListView::preferredHighlightEnd() const @@ -1178,19 +1184,20 @@ void QFxListView::setPreferredHighlightEnd(qreal end) { Q_D(QFxListView); d->highlightRangeEnd = end; - d->haveHighlightRange = d->highlightRangeStart < d->highlightRangeEnd; + d->haveHighlightRange = d->highlightRange != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd; } -bool QFxListView::strictlyEnforceHighlightRange() const +QFxListView::HighlightRangeMode QFxListView::highlightRangeMode() const { Q_D(const QFxListView); - return d->strictHighlightRange; + return d->highlightRange; } -void QFxListView::setStrictlyEnforceHighlightRange(bool strict) +void QFxListView::setHighlightRangeMode(HighlightRangeMode mode) { Q_D(QFxListView); - d->strictHighlightRange = strict; + d->highlightRange = mode; + d->haveHighlightRange = d->highlightRange != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd; } /*! @@ -1375,14 +1382,14 @@ void QFxListView::viewportMoved() if (isFlicking() || d->pressed) d->moveReason = QFxListViewPrivate::Mouse; if (d->moveReason == QFxListViewPrivate::Mouse) { - if (d->haveHighlightRange && d->strictHighlightRange && d->highlight) { + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange && d->highlight) { int idx = d->snapIndex(); if (idx >= 0 && idx != d->currentIndex) d->updateCurrent(idx); qreal pos = d->currentItem->position(); - if (pos > d->position() + d->highlightRangeEnd - d->highlight->size()) - pos = d->position() + d->highlightRangeEnd - d->highlight->size(); + if (pos > d->position() + d->highlightRangeEnd - 1 - d->highlight->size()) + pos = d->position() + d->highlightRangeEnd - 1 - d->highlight->size(); if (pos < d->position() + d->highlightRangeStart) pos = d->position() + d->highlightRangeStart; d->highlight->setPosition(pos); @@ -1396,7 +1403,7 @@ qreal QFxListView::minYExtent() const if (d->orient == Qt::Horizontal) return QFxFlickable::minYExtent(); qreal extent = -d->startPosition(); - if (d->haveHighlightRange && d->strictHighlightRange) + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) extent += d->highlightRangeStart; return extent; @@ -1408,8 +1415,8 @@ qreal QFxListView::maxYExtent() const if (d->orient == Qt::Horizontal) return QFxFlickable::maxYExtent(); qreal extent; - if (d->haveHighlightRange && d->strictHighlightRange) - extent = -(d->endPosition() - d->highlightRangeEnd); + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) + extent = -(d->positionAt(count()-1) - d->highlightRangeEnd); else extent = -(d->endPosition() - height()); qreal minY = minYExtent(); @@ -1424,7 +1431,7 @@ qreal QFxListView::minXExtent() const if (d->orient == Qt::Vertical) return QFxFlickable::minXExtent(); qreal extent = -d->startPosition(); - if (d->haveHighlightRange && d->strictHighlightRange) + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) extent += d->highlightRangeStart; return extent; @@ -1436,8 +1443,8 @@ qreal QFxListView::maxXExtent() const if (d->orient == Qt::Vertical) return QFxFlickable::maxXExtent(); qreal extent; - if (d->haveHighlightRange && d->strictHighlightRange) - extent = -(d->endPosition() - d->highlightRangeEnd); + if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) + extent = -(d->positionAt(count()-1) - d->highlightRangeEnd); else extent = -(d->endPosition() - width()); qreal minX = minXExtent(); @@ -1536,7 +1543,7 @@ void QFxListView::trackedPositionChanged() if (!isFlicking() && !d->pressed && d->moveReason != QFxListViewPrivate::Mouse) { const qreal trackedPos = d->trackedItem->position(); if (d->haveHighlightRange) { - if (d->strictHighlightRange) { + if (d->highlightRange == StrictlyEnforceRange) { qreal pos = d->position(); if (trackedPos > pos + d->highlightRangeEnd - d->trackedItem->size()) pos = trackedPos - d->highlightRangeEnd + d->trackedItem->size(); diff --git a/src/declarative/fx/qfxlistview.h b/src/declarative/fx/qfxlistview.h index 1ff0e27..0fa0fa0 100644 --- a/src/declarative/fx/qfxlistview.h +++ b/src/declarative/fx/qfxlistview.h @@ -69,7 +69,7 @@ class Q_DECLARATIVE_EXPORT QFxListView : public QFxFlickable Q_PROPERTY(qreal preferredHighlightBegin READ preferredHighlightBegin WRITE setPreferredHighlightBegin) Q_PROPERTY(qreal preferredHighlightEnd READ preferredHighlightEnd WRITE setPreferredHighlightEnd) - Q_PROPERTY(bool strictlyEnforceHighlightRange READ strictlyEnforceHighlightRange WRITE setStrictlyEnforceHighlightRange) + Q_PROPERTY(HighlightRangeMode highlightRangeMode READ highlightRangeMode WRITE setHighlightRangeMode) Q_PROPERTY(qreal spacing READ spacing WRITE setSpacing NOTIFY spacingChanged) Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged) @@ -80,6 +80,7 @@ class Q_DECLARATIVE_EXPORT QFxListView : public QFxFlickable Q_PROPERTY(qreal highlightMoveSpeed READ highlightMoveSpeed WRITE setHighlightMoveSpeed NOTIFY highlightMoveSpeedChanged) Q_PROPERTY(qreal highlightResizeSpeed READ highlightResizeSpeed WRITE setHighlightResizeSpeed NOTIFY highlightResizeSpeedChanged) + Q_ENUMS(HighlightRangeMode) Q_CLASSINFO("DefaultProperty", "data") public: @@ -104,8 +105,9 @@ public: bool highlightFollowsCurrentItem() const; void setHighlightFollowsCurrentItem(bool); - bool strictlyEnforceHighlightRange() const; - void setStrictlyEnforceHighlightRange(bool strict); + enum HighlightRangeMode { NoHighlightRange, ApplyRange, StrictlyEnforceRange }; + HighlightRangeMode highlightRangeMode() const; + void setHighlightRangeMode(HighlightRangeMode mode); qreal preferredHighlightBegin() const; void setPreferredHighlightBegin(qreal); -- cgit v0.12