diff options
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativegridview.cpp')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativegridview.cpp | 221 |
1 files changed, 155 insertions, 66 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp index 60ffbe2..93f8d06 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -81,6 +81,10 @@ public: item->setPos(QPointF(row, col)); } } + bool contains(int x, int y) const { + return (x >= item->x() && x < item->x() + view->cellWidth() && + y >= item->y() && y < item->y() + view->cellHeight()); + } QDeclarativeItem *item; QDeclarativeGridView *view; @@ -97,7 +101,7 @@ class QDeclarativeGridViewPrivate : public QDeclarativeFlickablePrivate public: QDeclarativeGridViewPrivate() : currentItem(0), flow(QDeclarativeGridView::LeftToRight) - , visiblePos(0), visibleIndex(0) , currentIndex(-1) + , 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) @@ -115,7 +119,7 @@ public: void updateGrid(); void scheduleLayout(); - void layout(bool removed=false); + void layout(); void updateUnrequestedIndexes(); void updateUnrequestedPositions(); void updateTrackedItem(); @@ -274,7 +278,7 @@ public: || newGeometry.width() != oldGeometry.width()) { if (q->isComponentComplete()) { updateGrid(); - layout(); + scheduleLayout(); } } } @@ -305,7 +309,6 @@ public: QHash<QDeclarativeItem*,int> unrequestedItems; FxGridItem *currentItem; QDeclarativeGridView::Flow flow; - int visiblePos; int visibleIndex; int currentIndex; int cellWidth; @@ -350,7 +353,6 @@ void QDeclarativeGridViewPrivate::clear() for (int i = 0; i < visibleItems.count(); ++i) releaseItem(visibleItems.at(i)); visibleItems.clear(); - visiblePos = 0; visibleIndex = 0; releaseItem(currentItem); currentItem = 0; @@ -536,11 +538,11 @@ void QDeclarativeGridViewPrivate::scheduleLayout() Q_Q(QDeclarativeGridView); if (!layoutScheduled) { layoutScheduled = true; - QMetaObject::invokeMethod(q, "layout", Qt::QueuedConnection); + QCoreApplication::postEvent(q, new QEvent(QEvent::User), Qt::HighEventPriority); } } -void QDeclarativeGridViewPrivate::layout(bool removed) +void QDeclarativeGridViewPrivate::layout() { Q_Q(QDeclarativeGridView); layoutScheduled = false; @@ -549,8 +551,6 @@ void QDeclarativeGridViewPrivate::layout(bool removed) qreal colPos = visibleItems.first()->colPos(); int col = visibleIndex % columns; if (colPos != col * colSize()) { - if (removed) - rowPos -= rowSize(); colPos = col * colSize(); visibleItems.first()->setPosition(colPos, rowPos); } @@ -748,21 +748,26 @@ void QDeclarativeGridViewPrivate::fixup(AxisData &data, qreal minExtent, qreal m if (currentItem && currentItem->rowPos() - position() != highlightRangeStart) { qreal pos = currentItem->rowPos() - highlightRangeStart; timeline.reset(data.move); - if (fixupDuration) + if (fixupDuration) { timeline.move(data.move, -pos, QEasingCurve(QEasingCurve::InOutQuad), fixupDuration/2); - else + } else { data.move.setValue(-pos); + q->viewportMoved(); + } vTime = timeline.time(); } } else if (snapMode != QDeclarativeGridView::NoSnap) { - qreal pos = qMax(qMin(snapPosAt(position()) - highlightRangeStart, -maxExtent), -minExtent); - qreal dist = qAbs(data.move + pos); + qreal pos = -snapPosAt(-(data.move.value() - highlightRangeStart)) + highlightRangeStart; + pos = qMin(qMax(pos, maxExtent), minExtent); + qreal dist = qAbs(data.move.value() - 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); + if (fixupDuration) { + timeline.move(data.move, pos, QEasingCurve(QEasingCurve::InOutQuad), fixupDuration/2); + } else { + data.move.setValue(pos); + q->viewportMoved(); + } vTime = timeline.time(); } } else { @@ -788,7 +793,7 @@ void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal m 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)); + maxDistance = qAbs(minExtent - data.move.value()); } if (snapMode != QDeclarativeGridView::SnapToRow && highlightRange != QDeclarativeGridView::StrictlyEnforceRange) data.flickTarget = minExtent; @@ -797,7 +802,7 @@ void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal m 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); + maxDistance = qAbs(maxExtent - data.move.value()); } if (snapMode != QDeclarativeGridView::SnapToRow && highlightRange != QDeclarativeGridView::StrictlyEnforceRange) data.flickTarget = maxExtent; @@ -816,7 +821,8 @@ void QDeclarativeGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal m qreal maxAccel = v2 / (2.0f * maxDistance); qreal overshootDist = 0.0; if (maxAccel < accel) { - qreal dist = v2 / (accel * 2.0); + // + rowSize()/4 to encourage moving at least one item in the flick direction + qreal dist = v2 / (accel * 2.0) + rowSize()/4; if (v > 0) dist = -dist; data.flickTarget = -snapPosAt(-(data.move.value() - highlightRangeStart) + dist) + highlightRangeStart; @@ -1421,6 +1427,17 @@ void QDeclarativeGridView::setSnapMode(SnapMode mode) } } +bool QDeclarativeGridView::event(QEvent *event) +{ + Q_D(QDeclarativeGridView); + if (event->type() == QEvent::User) { + d->layout(); + return true; + } + + return QDeclarativeFlickable::event(event); +} + void QDeclarativeGridView::viewportMoved() { Q_D(QDeclarativeGridView); @@ -1488,7 +1505,7 @@ qreal QDeclarativeGridView::maxYExtent() const return QDeclarativeFlickable::maxYExtent(); qreal extent; if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) - extent = -(d->rowPosAt(count()-1) - d->highlightRangeEnd); + extent = -(d->endPosition() - d->highlightRangeEnd); else extent = -(d->endPosition() - height()); const qreal minY = minYExtent(); @@ -1515,7 +1532,7 @@ qreal QDeclarativeGridView::maxXExtent() const return QDeclarativeFlickable::maxXExtent(); qreal extent; if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange) - extent = -(d->rowPosAt(count()-1) - d->highlightRangeEnd); + extent = -(d->endPosition() - d->highlightRangeEnd); else extent = -(d->endPosition() - height()); const qreal minX = minXExtent(); @@ -1651,46 +1668,105 @@ void QDeclarativeGridView::moveCurrentIndexRight() } /*! - \qmlmethod GridView::positionViewAtIndex(int index) + \qmlmethod GridView::positionViewAtIndex(int index, PositionMode mode) + + Positions the view such that the \a index is at the position specified by + \a mode: + + \list + \o Beginning - position item at the top (or left for TopToBottom flow) of the view. + \o Center- position item in the center of the view. + \o End - position item at bottom (or right for horizontal orientation) of the view. + \o Visible - if any part of the item is visible then take no action, otherwise + bring the item into view. + \o Contain - ensure the entire item is visible. If the item is larger than + the view the item is positioned at the top (or left for TopToBottom flow) of the view. + \endlist - Positions the view such that the \a index is at the top (or left for horizontal orientation) of the view. If positioning the view at the index would cause empty space to be displayed at - the end of the view, the view will be positioned at the end. + the beginning or end of the view, the view will be positioned at the boundary. It is not recommended to use contentX or contentY to position the view at a particular index. This is unreliable since removing items from the start - of the list does not cause all other items to be repositioned. + of the view does not cause all other items to be repositioned. The correct way to bring an item into view is with positionViewAtIndex. */ -void QDeclarativeGridView::positionViewAtIndex(int index) +void QDeclarativeGridView::positionViewAtIndex(int index, int mode) { Q_D(QDeclarativeGridView); if (!d->isValid() || index < 0 || index >= d->model->count()) return; + if (mode < Beginning || mode > Contain) + return; - qreal maxExtent = d->flow == QDeclarativeGridView::LeftToRight ? -maxYExtent() : -maxXExtent(); + qreal pos = d->position(); FxGridItem *item = d->visibleItem(index); - if (item) { - // Already created - just move to top of view - int pos = qMin(item->rowPos(), maxExtent); - d->setPosition(pos); - } else { - int pos = d->rowPosAt(index); + if (!item) { + int itemPos = d->rowPosAt(index); // save the currently visible items in case any of them end up visible again QList<FxGridItem*> oldVisible = d->visibleItems; d->visibleItems.clear(); d->visibleIndex = index - index % d->columns; - d->setPosition(pos); - // setPosition() will cause refill. Adjust if we have moved beyond range - if (d->position() > maxExtent) - d->setPosition(maxExtent); + d->setPosition(itemPos); // now release the reference to all the old visible items. for (int i = 0; i < oldVisible.count(); ++i) d->releaseItem(oldVisible.at(i)); + item = d->visibleItem(index); + } + if (item) { + qreal itemPos = item->rowPos(); + switch (mode) { + case Beginning: + pos = itemPos; + break; + case Center: + pos = itemPos - (d->size() - d->rowSize())/2; + break; + case End: + pos = itemPos - d->size() + d->rowSize(); + break; + case Visible: + if (itemPos > pos + d->size()) + pos = itemPos - d->size() + d->rowSize(); + else if (item->endRowPos() < pos) + pos = itemPos; + break; + case Contain: + if (item->endRowPos() > pos + d->size()) + pos = itemPos - d->size() + d->rowSize(); + if (itemPos < pos) + pos = itemPos; + } + qreal maxExtent = d->flow == QDeclarativeGridView::LeftToRight ? -maxYExtent() : -maxXExtent(); + pos = qMin(pos, maxExtent); + qreal minExtent = d->flow == QDeclarativeGridView::LeftToRight ? -minYExtent() : -minXExtent(); + pos = qMax(pos, minExtent); + d->setPosition(pos); } d->fixupPosition(); } +/*! + \qmlmethod int GridView::indexAt(int x, int y) + + Returns the index of the visible item containing the point \a x, \a y in content + coordinates. If there is no item at the point specified, or the item is + not visible -1 is returned. + + If the item is outside the visible area, -1 is returned, regardless of + whether an item will exist at that point when scrolled into view. +*/ +int QDeclarativeGridView::indexAt(int x, int y) const +{ + Q_D(const QDeclarativeGridView); + for (int i = 0; i < d->visibleItems.count(); ++i) { + const FxGridItem *listItem = d->visibleItems.at(i); + if(listItem->contains(x, y)) + return listItem->index; + } + + return -1; +} void QDeclarativeGridView::componentComplete() { @@ -1763,7 +1839,7 @@ void QDeclarativeGridView::itemsInserted(int modelIndex, int count) { Q_D(QDeclarativeGridView); if (!d->visibleItems.count() || d->model->count() <= 1) { - refill(); + d->scheduleLayout(); d->updateCurrent(qMax(0, qMin(d->currentIndex, d->model->count()-1))); emit countChanged(); return; @@ -1792,8 +1868,9 @@ void QDeclarativeGridView::itemsInserted(int modelIndex, int count) d->currentIndex += count; if (d->currentItem) d->currentItem->index = d->currentIndex; + emit currentIndexChanged(); } - d->layout(); + d->scheduleLayout(); emit countChanged(); return; } @@ -1823,9 +1900,21 @@ void QDeclarativeGridView::itemsInserted(int modelIndex, int count) } } + // Update the indexes of the following visible items. + for (int i = 0; i < d->visibleItems.count(); ++i) { + FxGridItem *listItem = d->visibleItems.at(i); + if (listItem->index != -1 && listItem->index >= modelIndex) + listItem->index += count; + } + + bool addedVisible = false; QList<FxGridItem*> added; int i = 0; while (i < insertCount && rowPos <= to + d->rowSize()*(d->columns - (colPos/d->colSize()))/qreal(d->columns)) { + if (!addedVisible) { + d->scheduleLayout(); + addedVisible = true; + } FxGridItem *item = d->createItem(modelIndex + i); d->visibleItems.insert(index, item); item->setPosition(colPos, rowPos); @@ -1846,6 +1935,15 @@ void QDeclarativeGridView::itemsInserted(int modelIndex, int count) } } + // update visibleIndex + d->visibleIndex = 0; + for (QList<FxGridItem*>::Iterator it = d->visibleItems.begin(); it != d->visibleItems.end(); ++it) { + if ((*it)->index != -1) { + d->visibleIndex = (*it)->index; + break; + } + } + if (d->currentIndex >= modelIndex) { // adjust current item index d->currentIndex += count; @@ -1853,18 +1951,13 @@ void QDeclarativeGridView::itemsInserted(int modelIndex, int count) d->currentItem->index = d->currentIndex; d->currentItem->setPosition(d->colPosAt(d->currentIndex), d->rowPosAt(d->currentIndex)); } - } - // Update the indexes of the following visible items. - for (; index < d->visibleItems.count(); ++index) { - FxGridItem *listItem = d->visibleItems.at(index); - if (listItem->index != -1) - listItem->index += count; + emit currentIndexChanged(); } // everything is in order now - emit add() signal for (int j = 0; j < added.count(); ++j) added.at(j)->attached->emitAdd(); - d->layout(); + emit countChanged(); } @@ -1880,8 +1973,10 @@ void QDeclarativeGridView::itemsRemoved(int modelIndex, int count) FxGridItem *item = *it; if (item->index == -1 || item->index < modelIndex) { // already removed, or before removed items - if (item->index < modelIndex) + if (item->index < modelIndex && !removedVisible) { + d->scheduleLayout(); removedVisible = true; + } ++it; } else if (item->index >= modelIndex + count) { // after removed items @@ -1889,7 +1984,10 @@ void QDeclarativeGridView::itemsRemoved(int modelIndex, int count) ++it; } else { // removed item - removedVisible = true; + if (!removedVisible) { + d->scheduleLayout(); + removedVisible = true; + } item->attached->emitRemove(); if (item->attached->delayRemove()) { item->index = -1; @@ -1907,6 +2005,7 @@ void QDeclarativeGridView::itemsRemoved(int modelIndex, int count) d->currentIndex -= count; if (d->currentItem) d->currentItem->index -= count; + emit currentIndexChanged(); } else if (currentRemoved) { // current item has been removed. d->releaseItem(d->currentItem); @@ -1916,6 +2015,7 @@ void QDeclarativeGridView::itemsRemoved(int modelIndex, int count) } // update visibleIndex + d->visibleIndex = 0; for (it = d->visibleItems.begin(); it != d->visibleItems.end(); ++it) { if ((*it)->index != -1) { d->visibleIndex = (*it)->index; @@ -1923,27 +2023,16 @@ void QDeclarativeGridView::itemsRemoved(int modelIndex, int count) } } - if (removedVisible) { - if (d->visibleItems.isEmpty()) { - d->visibleIndex = 0; - d->setPosition(0); - refill(); - } else { - // Correct the positioning of the items - d->scheduleLayout(); - } + if (removedVisible && d->visibleItems.isEmpty()) { + d->timeline.clear(); + d->setPosition(0); + if (d->model->count() == 0) + update(); } emit countChanged(); } -void QDeclarativeGridView::layout() -{ - Q_D(QDeclarativeGridView); - if (d->layoutScheduled) - d->layout(); -} - void QDeclarativeGridView::destroyRemoved() { Q_D(QDeclarativeGridView); @@ -2055,7 +2144,7 @@ void QDeclarativeGridView::itemsMoved(int from, int to, int count) d->releaseItem(item); } - d->layout(removedBeforeVisible); + d->layout(); } void QDeclarativeGridView::modelReset() |