-
J-P Nurmi authored
Task-number: QTBUG-31873 Change-Id: I4230893ccb2925ed9c2429d26b411264bf7c1c65 Reviewed-by:
Andrew den Exter <andrew.den.exter@qinetic.com.au>
feaf7ca8
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qquickgridview_p.h"
#include "qquickflickable_p_p.h"
#include "qquickitemview_p_p.h"
#include <private/qqmlobjectmodel_p.h>
#include <private/qquicksmoothedanimation_p_p.h>
#include <QtGui/qevent.h>
#include <QtCore/qmath.h>
#include <QtCore/qcoreapplication.h>
#include <math.h>
#include "qplatformdefs.h"
QT_BEGIN_NAMESPACE
#ifndef QML_FLICK_SNAPONETHRESHOLD
#define QML_FLICK_SNAPONETHRESHOLD 30
#endif
//#define DEBUG_DELEGATE_LIFECYCLE
//----------------------------------------------------------------------------
class FxGridItemSG : public FxViewItem
{
public:
FxGridItemSG(QQuickItem *i, QQuickGridView *v, bool own) : FxViewItem(i, v, own), view(v) {
attached = static_cast<QQuickGridViewAttached*>(qmlAttachedPropertiesObject<QQuickGridView>(item));
}
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
qreal position() const {
return rowPos();
}
qreal endPosition() const {
return endRowPos();
}
qreal size() const {
return view->flow() == QQuickGridView::FlowLeftToRight ? view->cellHeight() : view->cellWidth();
}
qreal sectionSize() const {
return 0.0;
}
qreal rowPos() const {
if (view->flow() == QQuickGridView::FlowLeftToRight)
return (view->verticalLayoutDirection() == QQuickItemView::BottomToTop ? -view->cellHeight()-itemY() : itemY());
else
return (view->effectiveLayoutDirection() == Qt::RightToLeft ? -view->cellWidth()-itemX() : itemX());
}
qreal colPos() const {
if (view->flow() == QQuickGridView::FlowLeftToRight) {
if (view->effectiveLayoutDirection() == Qt::RightToLeft) {
qreal colSize = view->cellWidth();
int columns = view->width()/colSize;
return colSize * (columns-1) - itemX();
} else {
return itemX();
}
} else {
if (view->verticalLayoutDirection() == QQuickItemView::BottomToTop) {
return -view->cellHeight() - itemY();
} else {
return itemY();
}
}
}
qreal endRowPos() const {
if (view->flow() == QQuickGridView::FlowLeftToRight) {
if (view->verticalLayoutDirection() == QQuickItemView::BottomToTop)
return -itemY();
else
return itemY() + view->cellHeight();
} else {
if (view->effectiveLayoutDirection() == Qt::RightToLeft)
return -itemX();
else
return itemX() + view->cellWidth();
}
}
void setPosition(qreal col, qreal row, bool immediate = false) {
moveTo(pointForPosition(col, row), immediate);
}
bool contains(qreal x, qreal y) const {
return (x >= itemX() && x < itemX() + view->cellWidth() &&
y >= itemY() && y < itemY() + view->cellHeight());
}
QQuickGridView *view;
private:
QPointF pointForPosition(qreal col, qreal row) const {
qreal x;
qreal y;
if (view->flow() == QQuickGridView::FlowLeftToRight) {
x = col;
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
y = row;
if (view->effectiveLayoutDirection() == Qt::RightToLeft) {
int columns = view->width()/view->cellWidth();
x = view->cellWidth() * (columns-1) - col;
}
} else {
x = row;
y = col;
if (view->effectiveLayoutDirection() == Qt::RightToLeft)
x = -view->cellWidth() - row;
}
if (view->verticalLayoutDirection() == QQuickItemView::BottomToTop)
y = -view->cellHeight() - y;
return QPointF(x, y);
}
};
//----------------------------------------------------------------------------
class QQuickGridViewPrivate : public QQuickItemViewPrivate
{
Q_DECLARE_PUBLIC(QQuickGridView)
public:
virtual Qt::Orientation layoutOrientation() const;
virtual bool isContentFlowReversed() const;
virtual qreal positionAt(int index) const;
virtual qreal endPositionAt(int index) const;
virtual qreal originPosition() const;
virtual qreal lastPosition() const;
qreal rowSize() const;
qreal colSize() const;
qreal colPosAt(int modelIndex) const;
qreal rowPosAt(int modelIndex) const;
qreal snapPosAt(qreal pos) const;
FxViewItem *snapItemAt(qreal pos) const;
int snapIndex() const;
qreal contentXForPosition(qreal pos) const;
qreal contentYForPosition(qreal pos) const;
void resetColumns();
virtual bool addVisibleItems(qreal fillFrom, qreal fillTo, qreal bufferFrom, qreal bufferTo, bool doBuffer);
virtual bool removeNonVisibleItems(qreal bufferFrom, qreal bufferTo);
virtual FxViewItem *newViewItem(int index, QQuickItem *item);
virtual void initializeViewItem(FxViewItem *item);
virtual void repositionItemAt(FxViewItem *item, int index, qreal sizeBuffer);
virtual void repositionPackageItemAt(QQuickItem *item, int index);
virtual void resetFirstItemPosition(qreal pos = 0.0);
virtual void adjustFirstItem(qreal forwards, qreal backwards, int changeBeforeVisible);
virtual void createHighlight();
virtual void updateHighlight();
virtual void resetHighlightPosition();
virtual void setPosition(qreal pos);
virtual void layoutVisibleItems(int fromModelIndex = 0);
virtual bool applyInsertionChange(const QQmlChangeSet::Insert &insert, ChangeResult *changeResult, QList<FxViewItem *> *addedItems, QList<MovedItem> *movingIntoView);
virtual void translateAndTransitionItemsAfter(int afterModelIndex, const ChangeResult &insertionResult, const ChangeResult &removalResult);
virtual bool needsRefillForAddedOrRemovedIndex(int index) const;
virtual qreal headerSize() const;
virtual qreal footerSize() const;
virtual bool showHeaderForIndex(int index) const;
virtual bool showFooterForIndex(int index) const;
virtual void updateHeader();
virtual void updateFooter();
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
virtual void changedVisibleIndex(int newIndex);
virtual void initializeCurrentItem();
virtual void updateViewport();
virtual void fixupPosition();
virtual void fixup(AxisData &data, qreal minExtent, qreal maxExtent);
virtual bool flick(QQuickItemViewPrivate::AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize,
QQuickTimeLineCallback::Callback fixupCallback, qreal velocity);
QQuickGridView::Flow flow;
qreal cellWidth;
qreal cellHeight;
int columns;
QQuickGridView::SnapMode snapMode;
QSmoothedAnimation *highlightXAnimator;
QSmoothedAnimation *highlightYAnimator;
QQuickGridViewPrivate()
: flow(QQuickGridView::FlowLeftToRight)
, cellWidth(100), cellHeight(100), columns(1)
, snapMode(QQuickGridView::NoSnap)
, highlightXAnimator(0), highlightYAnimator(0)
{}
~QQuickGridViewPrivate()
{
delete highlightXAnimator;
delete highlightYAnimator;
}
};
Qt::Orientation QQuickGridViewPrivate::layoutOrientation() const
{
return flow == QQuickGridView::FlowLeftToRight ? Qt::Vertical : Qt::Horizontal;
}
bool QQuickGridViewPrivate::isContentFlowReversed() const
{
Q_Q(const QQuickGridView);
return (flow == QQuickGridView::FlowLeftToRight && verticalLayoutDirection == QQuickItemView::BottomToTop)
|| (flow == QQuickGridView::FlowTopToBottom && q->effectiveLayoutDirection() == Qt::RightToLeft);
}
void QQuickGridViewPrivate::changedVisibleIndex(int newIndex)
{
visibleIndex = newIndex / columns * columns;
}
void QQuickGridViewPrivate::setPosition(qreal pos)
{
Q_Q(QQuickGridView);
q->QQuickFlickable::setContentX(contentXForPosition(pos));
q->QQuickFlickable::setContentY(contentYForPosition(pos));
}
qreal QQuickGridViewPrivate::originPosition() const
{
qreal pos = 0;
if (!visibleItems.isEmpty())
pos = static_cast<FxGridItemSG*>(visibleItems.first())->rowPos() - visibleIndex / columns * rowSize();
return pos;
}
qreal QQuickGridViewPrivate::lastPosition() const
{
qreal pos = 0;
if (model && model->count()) {
// get end position of last item
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
pos = (rowPosAt(model->count() - 1) + rowSize());
}
return pos;
}
qreal QQuickGridViewPrivate::positionAt(int index) const
{
return rowPosAt(index);
}
qreal QQuickGridViewPrivate::endPositionAt(int index) const
{
return rowPosAt(index) + rowSize();
}
qreal QQuickGridViewPrivate::rowSize() const {
return flow == QQuickGridView::FlowLeftToRight ? cellHeight : cellWidth;
}
qreal QQuickGridViewPrivate::colSize() const {
return flow == QQuickGridView::FlowLeftToRight ? cellWidth : cellHeight;
}
qreal QQuickGridViewPrivate::colPosAt(int modelIndex) const
{
if (FxViewItem *item = visibleItem(modelIndex))
return static_cast<FxGridItemSG*>(item)->colPos();
if (!visibleItems.isEmpty()) {
if (modelIndex == visibleIndex) {
FxGridItemSG *firstItem = static_cast<FxGridItemSG*>(visibleItems.first());
return firstItem->colPos();
} else if (modelIndex < visibleIndex) {
int count = (visibleIndex - modelIndex) % columns;
int col = static_cast<FxGridItemSG*>(visibleItems.first())->colPos() / colSize();
col = (columns - count + col) % columns;
return col * colSize();
} else {
FxGridItemSG *lastItem = static_cast<FxGridItemSG*>(visibleItems.last());
int count = modelIndex - lastItem->index;
int col = lastItem->colPos() / colSize();
col = (col + count) % columns;
return col * colSize();
}
}
return (modelIndex % columns) * colSize();
}
qreal QQuickGridViewPrivate::rowPosAt(int modelIndex) const
{
if (FxViewItem *item = visibleItem(modelIndex))
return static_cast<FxGridItemSG*>(item)->rowPos();
if (!visibleItems.isEmpty()) {
if (modelIndex == visibleIndex) {
FxGridItemSG *firstItem = static_cast<FxGridItemSG*>(visibleItems.first());
return firstItem->rowPos();
} else if (modelIndex < visibleIndex) {
FxGridItemSG *firstItem = static_cast<FxGridItemSG*>(visibleItems.first());
int firstCol = firstItem->colPos() / colSize();
int col = visibleIndex - modelIndex + (columns - firstCol - 1);
int rows = col / columns;
return firstItem->rowPos() - rows * rowSize();
} else {
FxGridItemSG *lastItem = static_cast<FxGridItemSG*>(visibleItems.last());
int count = modelIndex - lastItem->index;
int col = lastItem->colPos() + count * colSize();
int rows = col / (columns * colSize());
return lastItem->rowPos() + rows * rowSize();
}
}
return (modelIndex / columns) * rowSize();
}
351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
qreal QQuickGridViewPrivate::snapPosAt(qreal pos) const
{
Q_Q(const QQuickGridView);
qreal snapPos = 0;
if (!visibleItems.isEmpty()) {
qreal highlightStart = highlightRangeStart;
pos += highlightStart;
pos += rowSize()/2;
snapPos = static_cast<FxGridItemSG*>(visibleItems.first())->rowPos() - visibleIndex / columns * rowSize();
snapPos = pos - fmodf(pos - snapPos, qreal(rowSize()));
snapPos -= highlightStart;
qreal maxExtent;
qreal minExtent;
if (isContentFlowReversed()) {
maxExtent = q->minXExtent()-size();
minExtent = q->maxXExtent()-size();
} else {
maxExtent = flow == QQuickGridView::FlowLeftToRight ? -q->maxYExtent() : -q->maxXExtent();
minExtent = flow == QQuickGridView::FlowLeftToRight ? -q->minYExtent() : -q->minXExtent();
}
if (snapPos > maxExtent)
snapPos = maxExtent;
if (snapPos < minExtent)
snapPos = minExtent;
}
return snapPos;
}
FxViewItem *QQuickGridViewPrivate::snapItemAt(qreal pos) const
{
for (int i = 0; i < visibleItems.count(); ++i) {
FxViewItem *item = visibleItems.at(i);
if (item->index == -1)
continue;
qreal itemTop = item->position();
if (itemTop+rowSize()/2 >= pos && itemTop - rowSize()/2 <= pos)
return item;
}
return 0;
}
int QQuickGridViewPrivate::snapIndex() const
{
int index = currentIndex;
for (int i = 0; i < visibleItems.count(); ++i) {
FxGridItemSG *item = static_cast<FxGridItemSG*>(visibleItems.at(i));
if (item->index == -1)
continue;
qreal itemTop = item->position();
FxGridItemSG *hItem = static_cast<FxGridItemSG*>(highlight);
if (itemTop >= hItem->rowPos()-rowSize()/2 && itemTop < hItem->rowPos()+rowSize()/2) {
index = item->index;
if (item->colPos() >= hItem->colPos()-colSize()/2 && item->colPos() < hItem->colPos()+colSize()/2)
return item->index;
}
}
return index;
}
qreal QQuickGridViewPrivate::contentXForPosition(qreal pos) const
{
Q_Q(const QQuickGridView);
if (flow == QQuickGridView::FlowLeftToRight) {
// vertical scroll
if (q->effectiveLayoutDirection() == Qt::LeftToRight) {
return 0;
} else {
qreal colSize = cellWidth;
421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
int columns = q->width()/colSize;
return -q->width() + (cellWidth * columns);
}
} else {
// horizontal scroll
if (q->effectiveLayoutDirection() == Qt::LeftToRight)
return pos;
else
return -pos - q->width();
}
}
qreal QQuickGridViewPrivate::contentYForPosition(qreal pos) const
{
Q_Q(const QQuickGridView);
if (flow == QQuickGridView::FlowLeftToRight) {
// vertical scroll
if (verticalLayoutDirection == QQuickItemView::TopToBottom)
return pos;
else
return -pos - q->height();
} else {
// horizontal scroll
if (verticalLayoutDirection == QQuickItemView::TopToBottom)
return 0;
else
return -q->height();
}
}
void QQuickGridViewPrivate::resetColumns()
{
Q_Q(QQuickGridView);
qreal length = flow == QQuickGridView::FlowLeftToRight ? q->width() : q->height();
columns = qMax(1, qFloor(length / colSize()));
}
FxViewItem *QQuickGridViewPrivate::newViewItem(int modelIndex, QQuickItem *item)
{
Q_Q(QQuickGridView);
Q_UNUSED(modelIndex);
return new FxGridItemSG(item, q, false);
}
void QQuickGridViewPrivate::initializeViewItem(FxViewItem *item)
{
QQuickItemViewPrivate::initializeViewItem(item);
// need to track current items that are animating
item->trackGeometry(true);
}
bool QQuickGridViewPrivate::addVisibleItems(qreal fillFrom, qreal fillTo, qreal bufferFrom, qreal bufferTo, bool doBuffer)
{
qreal colPos = colPosAt(visibleIndex);
qreal rowPos = rowPosAt(visibleIndex);
if (visibleItems.count()) {
FxGridItemSG *lastItem = static_cast<FxGridItemSG*>(visibleItems.last());
rowPos = lastItem->rowPos();
int colNum = qFloor((lastItem->colPos()+colSize()/2) / colSize());
if (++colNum >= columns) {
colNum = 0;
rowPos += rowSize();
}
colPos = colNum * colSize();
}
int modelIndex = findLastVisibleIndex();
modelIndex = modelIndex < 0 ? visibleIndex : modelIndex + 1;
491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
if (visibleItems.count() && (bufferFrom > rowPos + rowSize()*2
|| bufferTo < rowPosAt(visibleIndex) - rowSize())) {
// We've jumped more than a page. Estimate which items are now
// visible and fill from there.
int count = (fillFrom - (rowPos + rowSize())) / (rowSize()) * columns;
for (int i = 0; i < visibleItems.count(); ++i)
releaseItem(visibleItems.at(i));
visibleItems.clear();
modelIndex += count;
if (modelIndex >= model->count())
modelIndex = model->count() - 1;
else if (modelIndex < 0)
modelIndex = 0;
modelIndex = modelIndex / columns * columns;
visibleIndex = modelIndex;
colPos = colPosAt(visibleIndex);
rowPos = rowPosAt(visibleIndex);
}
int colNum = qFloor((colPos+colSize()/2) / colSize());
FxGridItemSG *item = 0;
bool changed = false;
while (modelIndex < model->count() && rowPos <= fillTo + rowSize()*(columns - colNum)/(columns+1)) {
#ifdef DEBUG_DELEGATE_LIFECYCLE
qDebug() << "refill: append item" << modelIndex << colPos << rowPos;
#endif
if (!(item = static_cast<FxGridItemSG*>(createItem(modelIndex, doBuffer))))
break;
if (!transitioner || !transitioner->canTransition(QQuickItemViewTransitioner::PopulateTransition, true)) // pos will be set by layoutVisibleItems()
item->setPosition(colPos, rowPos, true);
QQuickItemPrivate::get(item->item)->setCulled(doBuffer);
visibleItems.append(item);
if (++colNum >= columns) {
colNum = 0;
rowPos += rowSize();
}
colPos = colNum * colSize();
++modelIndex;
changed = true;
}
if (doBuffer && requestedIndex != -1) // already waiting for an item
return changed;
// Find first column
if (visibleItems.count()) {
FxGridItemSG *firstItem = static_cast<FxGridItemSG*>(visibleItems.first());
rowPos = firstItem->rowPos();
colNum = qFloor((firstItem->colPos()+colSize()/2) / colSize());
if (--colNum < 0) {
colNum = columns - 1;
rowPos -= rowSize();
}
} else {
colNum = qFloor((colPos+colSize()/2) / colSize());
}
// Prepend
colPos = colNum * colSize();
while (visibleIndex > 0 && rowPos + rowSize() - 1 >= fillFrom - rowSize()*(colNum+1)/(columns+1)){
#ifdef DEBUG_DELEGATE_LIFECYCLE
qDebug() << "refill: prepend item" << visibleIndex-1 << "top pos" << rowPos << colPos;
#endif
if (!(item = static_cast<FxGridItemSG*>(createItem(visibleIndex-1, doBuffer))))
break;
--visibleIndex;
if (!transitioner || !transitioner->canTransition(QQuickItemViewTransitioner::PopulateTransition, true)) // pos will be set by layoutVisibleItems()
item->setPosition(colPos, rowPos, true);
QQuickItemPrivate::get(item->item)->setCulled(doBuffer);
561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
visibleItems.prepend(item);
if (--colNum < 0) {
colNum = columns-1;
rowPos -= rowSize();
}
colPos = colNum * colSize();
changed = true;
}
return changed;
}
bool QQuickGridViewPrivate::removeNonVisibleItems(qreal bufferFrom, qreal bufferTo)
{
FxGridItemSG *item = 0;
bool changed = false;
while (visibleItems.count() > 1
&& (item = static_cast<FxGridItemSG*>(visibleItems.first()))
&& item->rowPos()+rowSize()-1 < bufferFrom - rowSize()*(item->colPos()/colSize()+1)/(columns+1)) {
if (item->attached->delayRemove())
break;
#ifdef DEBUG_DELEGATE_LIFECYCLE
qDebug() << "refill: remove first" << visibleIndex << "top end pos" << item->endRowPos();
#endif
if (item->index != -1)
visibleIndex++;
visibleItems.removeFirst();
if (item->transitionScheduledOrRunning()) {
#ifdef DEBUG_DELEGATE_LIFECYCLE
qDebug() << "\tnot releasing animating item:" << item->index << item->item->objectName();
#endif
item->releaseAfterTransition = true;
releasePendingTransition.append(item);
} else {
releaseItem(item);
}
changed = true;
}
while (visibleItems.count() > 1
&& (item = static_cast<FxGridItemSG*>(visibleItems.last()))
&& item->rowPos() > bufferTo + rowSize()*(columns - item->colPos()/colSize())/(columns+1)) {
if (item->attached->delayRemove())
break;
#ifdef DEBUG_DELEGATE_LIFECYCLE
qDebug() << "refill: remove last" << visibleIndex+visibleItems.count()-1;
#endif
visibleItems.removeLast();
if (item->transitionScheduledOrRunning()) {
#ifdef DEBUG_DELEGATE_LIFECYCLE
qDebug() << "\tnot releasing animating item:" << item->index << item->item->objectName();
#endif
item->releaseAfterTransition = true;
releasePendingTransition.append(item);
} else {
releaseItem(item);
}
changed = true;
}
return changed;
}
void QQuickGridViewPrivate::updateViewport()
{
resetColumns();
QQuickItemViewPrivate::updateViewport();
}
void QQuickGridViewPrivate::layoutVisibleItems(int fromModelIndex)
631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
{
if (visibleItems.count()) {
const qreal from = isContentFlowReversed() ? -position() - size() : position();
const qreal to = isContentFlowReversed() ? -position() : position() + size();
FxGridItemSG *firstItem = static_cast<FxGridItemSG*>(visibleItems.first());
qreal rowPos = firstItem->rowPos();
qreal colPos = firstItem->colPos();
int col = visibleIndex % columns;
if (colPos != col * colSize()) {
colPos = col * colSize();
firstItem->setPosition(colPos, rowPos);
firstItem->setVisible(firstItem->rowPos() + rowSize() >= from && firstItem->rowPos() <= to);
}
for (int i = 1; i < visibleItems.count(); ++i) {
FxGridItemSG *item = static_cast<FxGridItemSG*>(visibleItems.at(i));
if (++col >= columns) {
col = 0;
rowPos += rowSize();
}
colPos = col * colSize();
if (item->index >= fromModelIndex) {
item->setPosition(colPos, rowPos);
item->setVisible(item->rowPos() + rowSize() >= from && item->rowPos() <= to);
}
}
}
}
void QQuickGridViewPrivate::repositionItemAt(FxViewItem *item, int index, qreal sizeBuffer)
{
int count = sizeBuffer / rowSize();
static_cast<FxGridItemSG *>(item)->setPosition(colPosAt(index + count), rowPosAt(index + count));
}
void QQuickGridViewPrivate::repositionPackageItemAt(QQuickItem *item, int index)
{
Q_Q(QQuickGridView);
qreal pos = position();
if (flow == QQuickGridView::FlowLeftToRight) {
if (item->y() + item->height() > pos && item->y() < pos + q->height()) {
qreal y = (verticalLayoutDirection == QQuickItemView::TopToBottom)
? rowPosAt(index)
: -rowPosAt(index) - item->height();
item->setPosition(QPointF(colPosAt(index), y));
}
} else {
if (item->x() + item->width() > pos && item->x() < pos + q->width()) {
qreal y = (verticalLayoutDirection == QQuickItemView::TopToBottom)
? colPosAt(index)
: -colPosAt(index) - item->height();
if (flow == QQuickGridView::FlowTopToBottom && q->effectiveLayoutDirection() == Qt::RightToLeft)
item->setPosition(QPointF(-rowPosAt(index)-item->width(), y));
else
item->setPosition(QPointF(rowPosAt(index), y));
}
}
}
void QQuickGridViewPrivate::resetFirstItemPosition(qreal pos)
{
FxGridItemSG *item = static_cast<FxGridItemSG*>(visibleItems.first());
item->setPosition(0, pos);
}
void QQuickGridViewPrivate::adjustFirstItem(qreal forwards, qreal backwards, int changeBeforeVisible)
{
if (!visibleItems.count())
return;
701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
int moveCount = (forwards - backwards) / rowSize();
if (moveCount == 0 && changeBeforeVisible != 0)
moveCount += (changeBeforeVisible % columns) - (columns - 1);
FxGridItemSG *gridItem = static_cast<FxGridItemSG*>(visibleItems.first());
gridItem->setPosition(gridItem->colPos(), gridItem->rowPos() + ((moveCount / columns) * rowSize()));
}
void QQuickGridViewPrivate::createHighlight()
{
Q_Q(QQuickGridView);
bool changed = false;
if (highlight) {
if (trackedItem == highlight)
trackedItem = 0;
delete highlight;
highlight = 0;
delete highlightXAnimator;
delete highlightYAnimator;
highlightXAnimator = 0;
highlightYAnimator = 0;
changed = true;
}
if (currentItem) {
QQuickItem *item = createHighlightItem();
if (item) {
FxGridItemSG *newHighlight = new FxGridItemSG(item, q, true);
newHighlight->trackGeometry(true);
if (autoHighlight)
resetHighlightPosition();
highlightXAnimator = new QSmoothedAnimation;
highlightXAnimator->target = QQmlProperty(item, QLatin1String("x"));
highlightXAnimator->userDuration = highlightMoveDuration;
highlightYAnimator = new QSmoothedAnimation;
highlightYAnimator->target = QQmlProperty(item, QLatin1String("y"));
highlightYAnimator->userDuration = highlightMoveDuration;
highlight = newHighlight;
changed = true;
}
}
if (changed)
emit q->highlightItemChanged();
}
void QQuickGridViewPrivate::updateHighlight()
{
applyPendingChanges();
if ((!currentItem && highlight) || (currentItem && !highlight))
createHighlight();
bool strictHighlight = haveHighlightRange && highlightRange == QQuickGridView::StrictlyEnforceRange;
if (currentItem && autoHighlight && highlight && (!strictHighlight || !pressed)) {
// auto-update highlight
highlightXAnimator->to = currentItem->itemX();
highlightYAnimator->to = currentItem->itemY();
highlight->item->setWidth(currentItem->item->width());
highlight->item->setHeight(currentItem->item->height());
highlightXAnimator->restart();
highlightYAnimator->restart();
}
updateTrackedItem();
}
void QQuickGridViewPrivate::resetHighlightPosition()
{
771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
if (highlight && currentItem) {
FxGridItemSG *cItem = static_cast<FxGridItemSG*>(currentItem);
static_cast<FxGridItemSG*>(highlight)->setPosition(cItem->colPos(), cItem->rowPos());
}
}
qreal QQuickGridViewPrivate::headerSize() const
{
if (!header)
return 0.0;
return flow == QQuickGridView::FlowLeftToRight ? header->item->height() : header->item->width();
}
qreal QQuickGridViewPrivate::footerSize() const
{
if (!footer)
return 0.0;
return flow == QQuickGridView::FlowLeftToRight? footer->item->height() : footer->item->width();
}
bool QQuickGridViewPrivate::showHeaderForIndex(int index) const
{
return index / columns == 0;
}
bool QQuickGridViewPrivate::showFooterForIndex(int index) const
{
return index / columns == (model->count()-1) / columns;
}
void QQuickGridViewPrivate::updateFooter()
{
Q_Q(QQuickGridView);
bool created = false;
if (!footer) {
QQuickItem *item = createComponentItem(footerComponent, 1.0);
if (!item)
return;
footer = new FxGridItemSG(item, q, true);
footer->trackGeometry(true);
created = true;
}
FxGridItemSG *gridItem = static_cast<FxGridItemSG*>(footer);
qreal colOffset = 0;
qreal rowOffset = 0;
if (q->effectiveLayoutDirection() == Qt::RightToLeft) {
if (flow == QQuickGridView::FlowTopToBottom)
rowOffset += gridItem->item->width() - cellWidth;
else
colOffset += gridItem->item->width() - cellWidth;
}
if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
if (flow == QQuickGridView::FlowTopToBottom)
colOffset += gridItem->item->height() - cellHeight;
else
rowOffset += gridItem->item->height() - cellHeight;
}
if (visibleItems.count()) {
qreal endPos = lastPosition();
if (findLastVisibleIndex() == model->count()-1) {
gridItem->setPosition(colOffset, endPos + rowOffset);
} else {
qreal visiblePos = isContentFlowReversed() ? -position() : position() + size();
if (endPos <= visiblePos || gridItem->endPosition() <= endPos + rowOffset)
gridItem->setPosition(colOffset, endPos + rowOffset);
}
} else {
gridItem->setPosition(colOffset, rowOffset);
}
841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
if (created)
emit q->footerItemChanged();
}
void QQuickGridViewPrivate::updateHeader()
{
Q_Q(QQuickGridView);
bool created = false;
if (!header) {
QQuickItem *item = createComponentItem(headerComponent, 1.0);
if (!item)
return;
header = new FxGridItemSG(item, q, true);
header->trackGeometry(true);
created = true;
}
FxGridItemSG *gridItem = static_cast<FxGridItemSG*>(header);
qreal colOffset = 0;
qreal rowOffset = -headerSize();
if (q->effectiveLayoutDirection() == Qt::RightToLeft) {
if (flow == QQuickGridView::FlowTopToBottom)
rowOffset += gridItem->item->width() - cellWidth;
else
colOffset += gridItem->item->width() - cellWidth;
}
if (verticalLayoutDirection == QQuickItemView::BottomToTop) {
if (flow == QQuickGridView::FlowTopToBottom)
colOffset += gridItem->item->height() - cellHeight;
else
rowOffset += gridItem->item->height() - cellHeight;
}
if (visibleItems.count()) {
qreal startPos = originPosition();
if (visibleIndex == 0) {
gridItem->setPosition(colOffset, startPos + rowOffset);
} else {
qreal tempPos = isContentFlowReversed() ? -position()-size() : position();
qreal headerPos = isContentFlowReversed() ? gridItem->rowPos() + cellWidth - headerSize() : gridItem->rowPos();
if (tempPos <= startPos || headerPos > startPos + rowOffset)
gridItem->setPosition(colOffset, startPos + rowOffset);
}
} else {
if (isContentFlowReversed())
gridItem->setPosition(colOffset, rowOffset);
else
gridItem->setPosition(colOffset, -headerSize());
}
if (created)
emit q->headerItemChanged();
}
void QQuickGridViewPrivate::initializeCurrentItem()
{
if (currentItem && currentIndex >= 0) {
FxGridItemSG *gridItem = static_cast<FxGridItemSG*>(currentItem);
FxViewItem *actualItem = visibleItem(currentIndex);
// don't reposition the item if it's about to be transitioned to another position
if ((!actualItem || !actualItem->transitionScheduledOrRunning()))
gridItem->setPosition(colPosAt(currentIndex), rowPosAt(currentIndex));
}
}
void QQuickGridViewPrivate::fixupPosition()
{
moveReason = Other;
if (flow == QQuickGridView::FlowLeftToRight)
911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
fixupY();
else
fixupX();
}
void QQuickGridViewPrivate::fixup(AxisData &data, qreal minExtent, qreal maxExtent)
{
if ((flow == QQuickGridView::FlowTopToBottom && &data == &vData)
|| (flow == QQuickGridView::FlowLeftToRight && &data == &hData))
return;
fixupMode = moveReason == Mouse ? fixupMode : Immediate;
qreal viewPos = isContentFlowReversed() ? -position()-size() : position();
bool strictHighlightRange = haveHighlightRange && highlightRange == QQuickGridView::StrictlyEnforceRange;
if (snapMode != QQuickGridView::NoSnap) {
qreal tempPosition = isContentFlowReversed() ? -position()-size() : position();
if (snapMode == QQuickGridView::SnapOneRow && moveReason == Mouse) {
// if we've been dragged < rowSize()/2 then bias towards the next row
qreal dist = data.move.value() - (data.pressPos - data.dragStartOffset);
qreal bias = 0;
if (data.velocity > 0 && dist > QML_FLICK_SNAPONETHRESHOLD && dist < rowSize()/2)
bias = rowSize()/2;
else if (data.velocity < 0 && dist < -QML_FLICK_SNAPONETHRESHOLD && dist > -rowSize()/2)
bias = -rowSize()/2;
if (isContentFlowReversed())
bias = -bias;
tempPosition -= bias;
}
FxViewItem *topItem = snapItemAt(tempPosition+highlightRangeStart);
if (!topItem && strictHighlightRange && currentItem) {
// StrictlyEnforceRange always keeps an item in range
updateHighlight();
topItem = currentItem;
}
FxViewItem *bottomItem = snapItemAt(tempPosition+highlightRangeEnd);
if (!bottomItem && strictHighlightRange && currentItem) {
// StrictlyEnforceRange always keeps an item in range
updateHighlight();
bottomItem = currentItem;
}
qreal pos;
bool isInBounds = -position() > maxExtent && -position() <= minExtent;
if (topItem && (isInBounds || strictHighlightRange)) {
qreal headerPos = header ? static_cast<FxGridItemSG*>(header)->rowPos() : 0;
if (topItem->index == 0 && header && tempPosition+highlightRangeStart < headerPos+headerSize()/2 && !strictHighlightRange) {
pos = isContentFlowReversed() ? - headerPos + highlightRangeStart - size() : headerPos - highlightRangeStart;
} else {
if (isContentFlowReversed())
pos = qMax(qMin(-topItem->position() + highlightRangeStart - size(), -maxExtent), -minExtent);
else
pos = qMax(qMin(topItem->position() - highlightRangeStart, -maxExtent), -minExtent);
}
} else if (bottomItem && isInBounds) {
if (isContentFlowReversed())
pos = qMax(qMin(-bottomItem->position() + highlightRangeEnd - size(), -maxExtent), -minExtent);
else
pos = qMax(qMin(bottomItem->position() - highlightRangeEnd, -maxExtent), -minExtent);
} else {
QQuickItemViewPrivate::fixup(data, minExtent, maxExtent);
return;
}
qreal dist = qAbs(data.move + pos);
if (dist > 0) {
timeline.reset(data.move);
if (fixupMode != Immediate) {
timeline.move(data.move, -pos, QEasingCurve(QEasingCurve::InOutQuad), fixupDuration/2);
data.fixingUp = true;
981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
} else {
timeline.set(data.move, -pos);
}
vTime = timeline.time();
}
} else if (haveHighlightRange && highlightRange == QQuickGridView::StrictlyEnforceRange) {
if (currentItem) {
updateHighlight();
qreal pos = static_cast<FxGridItemSG*>(currentItem)->rowPos();
if (viewPos < pos + rowSize() - highlightRangeEnd)
viewPos = pos + rowSize() - highlightRangeEnd;
if (viewPos > pos - highlightRangeStart)
viewPos = pos - highlightRangeStart;
if (isContentFlowReversed())
viewPos = -viewPos-size();
timeline.reset(data.move);
if (viewPos != position()) {
if (fixupMode != Immediate) {
timeline.move(data.move, -viewPos, QEasingCurve(QEasingCurve::InOutQuad), fixupDuration/2);
data.fixingUp = true;
} else {
timeline.set(data.move, -viewPos);
}
}
vTime = timeline.time();
}
} else {
QQuickItemViewPrivate::fixup(data, minExtent, maxExtent);
}
data.inOvershoot = false;
fixupMode = Normal;
}
bool QQuickGridViewPrivate::flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize,
QQuickTimeLineCallback::Callback fixupCallback, qreal velocity)
{
data.fixingUp = false;
moveReason = Mouse;
if ((!haveHighlightRange || highlightRange != QQuickGridView::StrictlyEnforceRange)
&& snapMode == QQuickGridView::NoSnap) {
return QQuickItemViewPrivate::flick(data, minExtent, maxExtent, vSize, fixupCallback, velocity);
}
qreal maxDistance = 0;
qreal dataValue = isContentFlowReversed() ? -data.move.value()+size() : data.move.value();
// -ve velocity means list is moving up/left
if (velocity > 0) {
if (data.move.value() < minExtent) {
if (snapMode == QQuickGridView::SnapOneRow) {
// if we've been dragged < averageSize/2 then bias towards the next item
qreal dist = data.move.value() - (data.pressPos - data.dragStartOffset);
qreal bias = dist < rowSize()/2 ? rowSize()/2 : 0;
if (isContentFlowReversed())
bias = -bias;
data.flickTarget = -snapPosAt(-dataValue - bias);
maxDistance = qAbs(data.flickTarget - data.move.value());
velocity = maxVelocity;
} else {
maxDistance = qAbs(minExtent - data.move.value());
}
}
if (snapMode == QQuickGridView::NoSnap && highlightRange != QQuickGridView::StrictlyEnforceRange)
data.flickTarget = minExtent;
} else {
if (data.move.value() > maxExtent) {
if (snapMode == QQuickGridView::SnapOneRow) {
// if we've been dragged < averageSize/2 then bias towards the next item
qreal dist = data.move.value() - (data.pressPos - data.dragStartOffset);
qreal bias = -dist < rowSize()/2 ? rowSize()/2 : 0;
if (isContentFlowReversed())
bias = -bias;
1051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
data.flickTarget = -snapPosAt(-dataValue + bias);
maxDistance = qAbs(data.flickTarget - data.move.value());
velocity = -maxVelocity;
} else {
maxDistance = qAbs(maxExtent - data.move.value());
}
}
if (snapMode == QQuickGridView::NoSnap && highlightRange != QQuickGridView::StrictlyEnforceRange)
data.flickTarget = maxExtent;
}
bool overShoot = boundsBehavior == QQuickFlickable::DragAndOvershootBounds;
if (maxDistance > 0 || overShoot) {
// 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 overshootDist = 0.0;
if ((maxDistance > 0.0 && v2 / (2.0f * maxDistance) < accel) || snapMode == QQuickGridView::SnapOneRow) {
// + rowSize()/4 to encourage moving at least one item in the flick direction
qreal dist = v2 / (accel * 2.0) + rowSize()/4;
dist = qMin(dist, maxDistance);
if (v > 0)
dist = -dist;
if (snapMode != QQuickGridView::SnapOneRow) {
qreal distTemp = isContentFlowReversed() ? -dist : dist;
data.flickTarget = -snapPosAt(-dataValue + distTemp);
}
data.flickTarget = isContentFlowReversed() ? -data.flickTarget+size() : data.flickTarget;
if (overShoot) {
if (data.flickTarget >= minExtent) {
overshootDist = overShootDistance(vSize);
data.flickTarget += overshootDist;
} else if (data.flickTarget <= maxExtent) {
overshootDist = overShootDistance(vSize);
data.flickTarget -= overshootDist;
}
}
qreal adjDist = -data.flickTarget + data.move.value();
if (qAbs(adjDist) > qAbs(dist)) {
// Prevent painfully slow flicking - adjust velocity to suit flickDeceleration
qreal adjv2 = accel * 2.0f * qAbs(adjDist);
if (adjv2 > v2) {
v2 = adjv2;
v = qSqrt(v2);
if (dist > 0)
v = -v;
}
}
dist = adjDist;
accel = v2 / (2.0f * qAbs(dist));
} else {
data.flickTarget = velocity > 0 ? minExtent : maxExtent;
overshootDist = overShoot ? overShootDistance(vSize) : 0;
}
timeline.reset(data.move);
timeline.accel(data.move, v, accel, maxDistance + overshootDist);
timeline.callback(QQuickTimeLineCallback(&data.move, fixupCallback, this));
return true;
} else {
timeline.reset(data.move);
fixup(data, minExtent, maxExtent);
return false;
}
}
1121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
//----------------------------------------------------------------------------
/*!
\qmltype GridView
\instantiates QQuickGridView
\inqmlmodule QtQuick 2
\ingroup qtquick-views
\inherits Flickable
\brief For specifying a grid view of items provided by a model
A GridView displays data from models created from built-in QML types like ListModel
and XmlListModel, or custom model classes defined in C++ that inherit from
QAbstractListModel.
A GridView has a \l model, which defines the data to be displayed, and
a \l delegate, which defines how the data should be displayed. Items in a
GridView are laid out horizontally or vertically. Grid views are inherently flickable
as GridView inherits from \l Flickable.
\section1 Example Usage
The following example shows the definition of a simple list model defined
in a file called \c ContactModel.qml:
\snippet qml/gridview/ContactModel.qml 0
\div {class="float-right"}
\inlineimage gridview-simple.png
\enddiv
This model can be referenced as \c ContactModel in other QML files. See \l{QML Modules}
for more information about creating reusable components like this.
Another component can display this model data in a GridView, as in the following
example, which creates a \c ContactModel component for its model, and a \l Column
(containing \l Image and \l Text items) for its delegate.
\clearfloat
\snippet qml/gridview/gridview.qml import
\codeline
\snippet qml/gridview/gridview.qml classdocs simple
\div {class="float-right"}
\inlineimage gridview-highlight.png
\enddiv
The view will create a new delegate for each item in the model. Note that the delegate
is able to access the model's \c name and \c portrait data directly.
An improved grid view is shown below. The delegate is visually improved and is moved
into a separate \c contactDelegate component.
\clearfloat
\snippet qml/gridview/gridview.qml classdocs advanced
The currently selected item is highlighted with a blue \l Rectangle using the \l highlight property,
and \c focus is set to \c true to enable keyboard navigation for the grid view.
The grid view itself is a focus scope (see \l{Keyboard Focus in Qt Quick} for more details).
Delegates are instantiated as needed and may be destroyed at any time.
State should \e never be stored in a delegate.
GridView attaches a number of properties to the root item of the delegate, for example
\c {GridView.isCurrentItem}. In the following example, the root delegate item can access
this attached property directly as \c GridView.isCurrentItem, while the child
\c contactInfo object must refer to this property as \c wrapper.GridView.isCurrentItem.
\snippet qml/gridview/gridview.qml isCurrentItem
1191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
\note Views do not set the \l{Item::}{clip} property automatically.
If the view is not clipped by another item or the screen, it will be necessary
to set this property to true in order to clip the items that are partially or
fully outside the view.
\section1 GridView layouts
The layout of the items in a GridView can be controlled by these properties:
\list
\li \l flow - controls whether items flow from left to right (as a series of rows)
or from top to bottom (as a series of columns). This value can be either
GridView.LeftToRight or GridView.TopToBottom.
\li \l layoutDirection - controls the horizontal layout direction: that is, whether items
are laid out from the left side of the view to the right, or vice-versa. This value can
be either Qt.LeftToRight or Qt.RightToLeft.
\li \l verticalLayoutDirection - controls the vertical layout direction: that is, whether items
are laid out from the top of the view down towards the bottom of the view, or vice-versa.
This value can be either GridView.TopToBottom or GridView.BottomToTop.
\endlist
By default, a GridView flows from left to right, and items are laid out from left to right
horizontally, and from top to bottom vertically.
These properties can be combined to produce a variety of layouts, as shown in the table below.
The GridViews in the first row all have a \l flow value of GridView.LeftToRight, but use
different combinations of horizontal and vertical layout directions (specified by \l layoutDirection
and \l verticalLayoutDirection respectively). Similarly, the GridViews in the second row below
all have a \l flow value of GridView.TopToBottom, but use different combinations of horizontal and
vertical layout directions to lay out their items in different ways.
\table
\header
\li {4, 1}
\b GridViews with GridView.LeftToRight flow
\row
\li \b (H) Left to right \b (V) Top to bottom
\image gridview-layout-lefttoright-ltr-ttb.png
\li \b (H) Right to left \b (V) Top to bottom
\image gridview-layout-lefttoright-rtl-ttb.png
\li \b (H) Left to right \b (V) Bottom to top
\image gridview-layout-lefttoright-ltr-btt.png
\li \b (H) Right to left \b (V) Bottom to top
\image gridview-layout-lefttoright-rtl-btt.png
\header
\li {4, 1}
\b GridViews with GridView.TopToBottom flow
\row
\li \b (H) Left to right \b (V) Top to bottom
\image gridview-layout-toptobottom-ltr-ttb.png
\li \b (H) Right to left \b (V) Top to bottom
\image gridview-layout-toptobottom-rtl-ttb.png
\li \b (H) Left to right \b (V) Bottom to top
\image gridview-layout-toptobottom-ltr-btt.png
\li \b (H) Right to left \b (V) Bottom to top
\image gridview-layout-toptobottom-rtl-btt.png
\endtable
\sa {quick/modelviews/gridview}{GridView example}
*/
QQuickGridView::QQuickGridView(QQuickItem *parent)
: QQuickItemView(*(new QQuickGridViewPrivate), parent)
{
}
QQuickGridView::~QQuickGridView()
{
1261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
}
void QQuickGridView::setHighlightFollowsCurrentItem(bool autoHighlight)
{
Q_D(QQuickGridView);
if (d->autoHighlight != autoHighlight) {
if (!autoHighlight && d->highlightXAnimator) {
d->highlightXAnimator->stop();
d->highlightYAnimator->stop();
}
QQuickItemView::setHighlightFollowsCurrentItem(autoHighlight);
}
}
/*!
\qmlattachedproperty bool QtQuick2::GridView::isCurrentItem
This attached property is true if this delegate is the current item; otherwise false.
It is attached to each instance of the delegate.
*/
/*!
\qmlattachedproperty GridView QtQuick2::GridView::view
This attached property holds the view that manages this delegate instance.
It is attached to each instance of the delegate.
\snippet qml/gridview/gridview.qml isCurrentItem
*/
/*!
\qmlattachedproperty bool QtQuick2::GridView::delayRemove
This attached property holds whether the delegate may be destroyed. It
is attached to each instance of the delegate. The default value is false.
It is sometimes necessary to delay the destruction of an item
until an animation completes. The example delegate below ensures that the
animation completes before the item is removed from the list.
\snippet qml/gridview/gridview.qml delayRemove
If a \l remove transition has been specified, it will not be applied until
delayRemove is returned to \c false.
*/
/*!
\qmlattachedsignal QtQuick2::GridView::onAdd()
This attached handler is called immediately after an item is added to the view.
*/
/*!
\qmlattachedsignal QtQuick2::GridView::onRemove()
This attached handler is called immediately before an item is removed from the view.
If a \l remove transition has been specified, it is applied after
this signal handler is called, providing that delayRemove is false.
*/
/*!
\qmlproperty model QtQuick2::GridView::model
This property holds the model providing data for the grid.
The model provides the set of data that is used to create the items
in the view. Models can be created directly in QML using \l ListModel, \l XmlListModel
or \l VisualItemModel, or provided by C++ model classes. If a C++ model class is
used, it must be a subclass of \l QAbstractItemModel or a simple list.
\sa {qml-data-models}{Data Models}
*/
1331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
/*!
\qmlproperty Component QtQuick2::GridView::delegate
The delegate provides a template defining each item instantiated by the view.
The index is exposed as an accessible \c index property. Properties of the
model are also available depending upon the type of \l {qml-data-models}{Data Model}.
The number of objects and bindings in the delegate has a direct effect on the
flicking performance of the view. If at all possible, place functionality
that is not needed for the normal display of the delegate in a \l Loader which
can load additional components when needed.
The item size of the GridView is determined by cellHeight and cellWidth. It will not resize the items
based on the size of the root item in the delegate.
\note Delegates are instantiated as needed and may be destroyed at any time.
State should \e never be stored in a delegate.
*/
/*!
\qmlproperty int QtQuick2::GridView::currentIndex
\qmlproperty Item QtQuick2::GridView::currentItem
The \c currentIndex property holds the index of the current item, and
\c currentItem holds the current item. Setting the currentIndex to -1
will clear the highlight and set currentItem to null.
If highlightFollowsCurrentItem is \c true, setting either of these
properties will smoothly scroll the GridView so that the current
item becomes visible.
Note that the position of the current item
may only be approximate until it becomes visible in the view.
*/
/*!
\qmlproperty Item QtQuick2::GridView::highlightItem
This holds the highlight item created from the \l highlight component.
The highlightItem is managed by the view unless
\l highlightFollowsCurrentItem is set to false.
\sa highlight, highlightFollowsCurrentItem
*/
/*!
\qmlproperty int QtQuick2::GridView::count
This property holds the number of items in the view.
*/
/*!
\qmlproperty Component QtQuick2::GridView::highlight
This property holds the component to use as the highlight.
An instance of the highlight component is created for each view.
The geometry of the resulting component instance will be managed by the view
so as to stay with the current item, unless the highlightFollowsCurrentItem property is false.
\sa highlightItem, highlightFollowsCurrentItem
*/
/*!
\qmlproperty bool QtQuick2::GridView::highlightFollowsCurrentItem
This property sets whether the highlight is managed by the view.
1401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
If this property is true (the default value), the highlight is moved smoothly
to follow the current item. Otherwise, the
highlight is not moved by the view, and any movement must be implemented
by the highlight.
Here is a highlight with its motion defined by a \l {SpringAnimation} item:
\snippet qml/gridview/gridview.qml highlightFollowsCurrentItem
*/
/*!
\qmlproperty int QtQuick2::GridView::highlightMoveDuration
This property holds the move animation duration of the highlight delegate.
highlightFollowsCurrentItem must be true for this property
to have effect.
The default value for the duration is 150ms.
\sa highlightFollowsCurrentItem
*/
/*!
\qmlproperty real QtQuick2::GridView::preferredHighlightBegin
\qmlproperty real QtQuick2::GridView::preferredHighlightEnd
\qmlproperty enumeration QtQuick2::GridView::highlightRangeMode
These properties define the preferred range of the highlight (for the current item)
within the view. The \c preferredHighlightBegin value must be less than the
\c preferredHighlightEnd value.
These properties affect the position of the current item when the view is scrolled.
For example, if the currently selected item should stay in the middle of the
view when it is scrolled, set the \c preferredHighlightBegin and
\c preferredHighlightEnd values to the top and bottom coordinates of where the middle
item would be. If the \c currentItem is changed programmatically, the view will
automatically scroll so that the current item is in the middle of the view.
Furthermore, the behavior of the current item index will occur whether or not a
highlight exists.
Valid values for \c highlightRangeMode are:
\list
\li GridView.ApplyRange - the view attempts to maintain the highlight within the range.
However, the highlight can move outside of the range at the ends of the view or due
to mouse interaction.
\li GridView.StrictlyEnforceRange - the highlight never moves outside of the range.
The current item changes if a keyboard or mouse action would cause the highlight to move
outside of the range.
\li GridView.NoHighlightRange - this is the default value.
\endlist
*/
/*!
\qmlproperty enumeration QtQuick2::GridView::layoutDirection
This property holds the layout direction of the grid.
Possible values:
\list
\li Qt.LeftToRight (default) - Items will be laid out starting in the top, left corner. The flow is
dependent on the \l GridView::flow property.
\li Qt.RightToLeft - Items will be laid out starting in the top, right corner. The flow is dependent
on the \l GridView::flow property.
\endlist
\b Note: If GridView::flow is set to GridView.LeftToRight, this is not to be confused if
GridView::layoutDirection is set to Qt.RightToLeft. The GridView.LeftToRight flow value simply
1471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
indicates that the flow is horizontal.
\sa GridView::effectiveLayoutDirection, GridView::verticalLayoutDirection
*/
/*!
\qmlproperty enumeration QtQuick2::GridView::effectiveLayoutDirection
This property holds the effective layout direction of the grid.
When using the attached property \l {LayoutMirroring::enabled}{LayoutMirroring::enabled} for locale layouts,
the visual layout direction of the grid will be mirrored. However, the
property \l {GridView::layoutDirection}{layoutDirection} will remain unchanged.
\sa GridView::layoutDirection, {LayoutMirroring}{LayoutMirroring}
*/
/*!
\qmlproperty enumeration QtQuick2::GridView::verticalLayoutDirection
This property holds the vertical layout direction of the grid.
Possible values:
\list
\li GridView.TopToBottom (default) - Items are laid out from the top of the view down to the bottom of the view.
\li GridView.BottomToTop - Items are laid out from the bottom of the view up to the top of the view.
\endlist
\sa GridView::layoutDirection
*/
/*!
\qmlproperty bool QtQuick2::GridView::keyNavigationWraps
This property holds whether the grid wraps key navigation
If this is true, key navigation that would move the current item selection
past one end of the view instead wraps around and moves the selection to
the other end of the view.
By default, key navigation is not wrapped.
*/
/*!
\qmlproperty int QtQuick2::GridView::cacheBuffer
This property determines whether delegates are retained outside the
visible area of the view.
If non-zero the view may keep as many delegates
instantiated as will fit within the buffer specified. For example,
if in a vertical view the delegate is 20 pixels high, there are 3
columns and \c cacheBuffer is
set to 40, then up to 6 delegates above and 6 delegates below the visible
area may be created/retained. The buffered delegates are created asynchronously,
allowing creation to occur across multiple frames and reducing the
likelihood of skipping frames. In order to improve painting performance
delegates outside the visible area are not painted.
The default value of this property is platform dependent, but will usually
be a non-zero value.
Note that cacheBuffer is not a pixel buffer - it only maintains additional
instantiated delegates.
Setting this value can make scrolling the list smoother at the expense
of additional memory usage. It is not a substitute for creating efficient
delegates; the fewer objects and bindings in a delegate, the faster a view may be
scrolled.
*/
void QQuickGridView::setHighlightMoveDuration(int duration)
{
Q_D(QQuickGridView);
1541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
if (d->highlightMoveDuration != duration) {
if (d->highlightYAnimator) {
d->highlightXAnimator->userDuration = duration;
d->highlightYAnimator->userDuration = duration;
}
QQuickItemView::setHighlightMoveDuration(duration);
}
}
/*!
\qmlproperty enumeration QtQuick2::GridView::flow
This property holds the flow of the grid.
Possible values:
\list
\li GridView.LeftToRight (default) - Items are laid out from left to right, and the view scrolls vertically
\li GridView.TopToBottom - Items are laid out from top to bottom, and the view scrolls horizontally
\endlist
*/
QQuickGridView::Flow QQuickGridView::flow() const
{
Q_D(const QQuickGridView);
return d->flow;
}
void QQuickGridView::setFlow(Flow flow)
{
Q_D(QQuickGridView);
if (d->flow != flow) {
d->flow = flow;
if (d->flow == FlowLeftToRight) {
setContentWidth(-1);
setFlickableDirection(VerticalFlick);
} else {
setContentHeight(-1);
setFlickableDirection(HorizontalFlick);
}
setContentX(0);
setContentY(0);
d->regenerate();
emit flowChanged();
}
}
/*!
\qmlproperty real QtQuick2::GridView::cellWidth
\qmlproperty real QtQuick2::GridView::cellHeight
These properties holds the width and height of each cell in the grid.
The default cell size is 100x100.
*/
qreal QQuickGridView::cellWidth() const
{
Q_D(const QQuickGridView);
return d->cellWidth;
}
void QQuickGridView::setCellWidth(qreal cellWidth)
{
Q_D(QQuickGridView);
if (cellWidth != d->cellWidth && cellWidth > 0) {
d->cellWidth = qMax(qreal(1), cellWidth);
d->updateViewport();
emit cellWidthChanged();
d->forceLayoutPolish();
}
}
1611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
qreal QQuickGridView::cellHeight() const
{
Q_D(const QQuickGridView);
return d->cellHeight;
}
void QQuickGridView::setCellHeight(qreal cellHeight)
{
Q_D(QQuickGridView);
if (cellHeight != d->cellHeight && cellHeight > 0) {
d->cellHeight = qMax(qreal(1), cellHeight);
d->updateViewport();
emit cellHeightChanged();
d->forceLayoutPolish();
}
}
/*!
\qmlproperty enumeration QtQuick2::GridView::snapMode
This property determines how the view scrolling will settle following a drag or flick.
The possible values are:
\list
\li GridView.NoSnap (default) - the view stops anywhere within the visible area.
\li GridView.SnapToRow - the view settles with a row (or column for \c GridView.TopToBottom flow)
aligned with the start of the view.
\li GridView.SnapOneRow - the view will settle no more than one row (or column for \c GridView.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
*/
QQuickGridView::SnapMode QQuickGridView::snapMode() const
{
Q_D(const QQuickGridView);
return d->snapMode;
}
void QQuickGridView::setSnapMode(SnapMode mode)
{
Q_D(QQuickGridView);
if (d->snapMode != mode) {
d->snapMode = mode;
emit snapModeChanged();
}
}
/*!
\qmlproperty Component QtQuick2::GridView::footer
This property holds the component to use as the footer.
An instance of the footer component is created for each view. The
footer is positioned at the end of the view, after any items.
\sa header, footerItem
*/
/*!
\qmlproperty Component QtQuick2::GridView::header
This property holds the component to use as the header.
An instance of the header component is created for each view. The
header is positioned at the beginning of the view, before any items.
\sa footer, headerItem
*/
/*!
\qmlproperty Item QtQuick2::GridView::headerItem
1681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
This holds the header item created from the \l header component.
An instance of the header component is created for each view. The
header is positioned at the beginning of the view, before any items.
\sa header, footerItem
*/
/*!
\qmlproperty Item QtQuick2::GridView::footerItem
This holds the footer item created from the \l footer component.
An instance of the footer component is created for each view. The
footer is positioned at the end of the view, after any items.
\sa footer, headerItem
*/
/*!
\qmlproperty Transition QtQuick2::GridView::populate
This property holds the transition to apply to the items that are initially created
for a view.
It is applied to all items that are created when:
\list
\li The view is first created
\li The view's \l model changes
\li The view's \l model is \l {QAbstractItemModel::reset()}{reset}, if the model is a QAbstractItemModel subclass
\endlist
For example, here is a view that specifies such a transition:
\code
GridView {
...
populate: Transition {
NumberAnimation { properties: "x,y"; duration: 1000 }
}
}
\endcode
When the view is initialized, the view will create all the necessary items for the view,
then animate them to their correct positions within the view over one second.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
\sa add, ViewTransition
*/
/*!
\qmlproperty Transition QtQuick2::GridView::add
This property holds the transition to apply to items that are added to the view.
For example, here is a view that specifies such a transition:
\code
GridView {
...
add: Transition {
NumberAnimation { properties: "x,y"; from: 100; duration: 1000 }
}
}
\endcode
Whenever an item is added to the above view, the item will be animated from the position (100,100)
to its final x,y position within the view, over one second. The transition only applies to
1751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
the new items that are added to the view; it does not apply to the items below that are
displaced by the addition of the new items. To animate the displaced items, set the \l displaced
or \l addDisplaced properties.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
\note This transition is not applied to the items that are created when the view is initially
populated, or when the view's \l model changes. (In those cases, the \l populate transition is
applied instead.) Additionally, this transition should \e not animate the height of the new item;
doing so will cause any items beneath the new item to be laid out at the wrong position. Instead,
the height can be animated within a \l {ListView::onAdd()}{ListView.onAdd} in the delegate.
\sa addDisplaced, populate, ViewTransition
*/
/*!
\qmlproperty Transition QtQuick2::GridView::addDisplaced
This property holds the transition to apply to items within the view that are displaced by
the addition of other items to the view.
For example, here is a view that specifies such a transition:
\code
GridView {
...
addDisplaced: Transition {
NumberAnimation { properties: "x,y"; duration: 1000 }
}
}
\endcode
Whenever an item is added to the above view, all items beneath the new item are displaced, causing
them to move down (or sideways, if horizontally orientated) within the view. As this
displacement occurs, the items' movement to their new x,y positions within the view will be
animated by a NumberAnimation over one second, as specified. This transition is not applied to
the new item that has been added to the view; to animate the added items, set the \l add
property.
If an item is displaced by multiple types of operations at the same time, it is not defined as to
whether the addDisplaced, moveDisplaced or removeDisplaced transition will be applied. Additionally,
if it is not necessary to specify different transitions depending on whether an item is displaced
by an add, move or remove operation, consider setting the \l displaced property instead.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
\note This transition is not applied to the items that are created when the view is initially
populated, or when the view's \l model changes. In those cases, the \l populate transition is
applied instead.
\sa displaced, add, populate, ViewTransition
*/
/*!
\qmlproperty Transition QtQuick2::GridView::move
This property holds the transition to apply to items in the view that are being moved due
to a move operation in the view's \l model.
For example, here is a view that specifies such a transition:
\code
GridView {
...
move: Transition {
NumberAnimation { properties: "x,y"; duration: 1000 }
}
}
\endcode
1821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890
Whenever the \l model performs a move operation to move a particular set of indexes, the
respective items in the view will be animated to their new positions in the view over one
second. The transition only applies to the items that are the subject of the move operation
in the model; it does not apply to items below them that are displaced by the move operation.
To animate the displaced items, set the \l displaced or \l moveDisplaced properties.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
\sa moveDisplaced, ViewTransition
*/
/*!
\qmlproperty Transition QtQuick2::GridView::moveDisplaced
This property holds the transition to apply to items that are displaced by a move operation in
the view's \l model.
For example, here is a view that specifies such a transition:
\code
GridView {
...
moveDisplaced: Transition {
NumberAnimation { properties: "x,y"; duration: 1000 }
}
}
\endcode
Whenever the \l model performs a move operation to move a particular set of indexes, the items
between the source and destination indexes of the move operation are displaced, causing them
to move upwards or downwards (or sideways, if horizontally orientated) within the view. As this
displacement occurs, the items' movement to their new x,y positions within the view will be
animated by a NumberAnimation over one second, as specified. This transition is not applied to
the items that are the actual subjects of the move operation; to animate the moved items, set
the \l move property.
If an item is displaced by multiple types of operations at the same time, it is not defined as to
whether the addDisplaced, moveDisplaced or removeDisplaced transition will be applied. Additionally,
if it is not necessary to specify different transitions depending on whether an item is displaced
by an add, move or remove operation, consider setting the \l displaced property instead.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
\sa displaced, move, ViewTransition
*/
/*!
\qmlproperty Transition QtQuick2::GridView::remove
This property holds the transition to apply to items that are removed from the view.
For example, here is a view that specifies such a transition:
\code
GridView {
...
remove: Transition {
ParallelAnimation {
NumberAnimation { property: "opacity"; to: 0; duration: 1000 }
NumberAnimation { properties: "x,y"; to: 100; duration: 1000 }
}
}
}
\endcode
Whenever an item is removed from the above view, the item will be animated to the position (100,100)
over one second, and in parallel will also change its opacity to 0. The transition
1891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
only applies to the items that are removed from the view; it does not apply to the items below
them that are displaced by the removal of the items. To animate the displaced items, set the
\l displaced or \l removeDisplaced properties.
Note that by the time the transition is applied, the item has already been removed from the
model; any references to the model data for the removed index will not be valid.
Additionally, if the \l delayRemove attached property has been set for a delegate item, the
remove transition will not be applied until \l delayRemove becomes false again.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
\sa removeDisplaced, ViewTransition
*/
/*!
\qmlproperty Transition QtQuick2::GridView::removeDisplaced
This property holds the transition to apply to items in the view that are displaced by the
removal of other items in the view.
For example, here is a view that specifies such a transition:
\code
GridView {
...
removeDisplaced: Transition {
NumberAnimation { properties: "x,y"; duration: 1000 }
}
}
\endcode
Whenever an item is removed from the above view, all items beneath it are displaced, causing
them to move upwards (or sideways, if horizontally orientated) within the view. As this
displacement occurs, the items' movement to their new x,y positions within the view will be
animated by a NumberAnimation over one second, as specified. This transition is not applied to
the item that has actually been removed from the view; to animate the removed items, set the
\l remove property.
If an item is displaced by multiple types of operations at the same time, it is not defined as to
whether the addDisplaced, moveDisplaced or removeDisplaced transition will be applied. Additionally,
if it is not necessary to specify different transitions depending on whether an item is displaced
by an add, move or remove operation, consider setting the \l displaced property instead.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
\sa displaced, remove, ViewTransition
*/
/*!
\qmlproperty Transition QtQuick2::GridView::displaced
This property holds the generic transition to apply to items that have been displaced by
any model operation that affects the view.
This is a convenience for specifying a generic transition for items that are displaced
by add, move or remove operations, without having to specify the individual addDisplaced,
moveDisplaced and removeDisplaced properties. For example, here is a view that specifies
a displaced transition:
\code
GridView {
...
displaced: Transition {
NumberAnimation { properties: "x,y"; duration: 1000 }
}
}
\endcode
1961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
When any item is added, moved or removed within the above view, the items below it are
displaced, causing them to move down (or sideways, if horizontally orientated) within the
view. As this displacement occurs, the items' movement to their new x,y positions within
the view will be animated by a NumberAnimation over one second, as specified.
If a view specifies this generic displaced transition as well as a specific addDisplaced,
moveDisplaced or removeDisplaced transition, the more specific transition will be used
instead of the generic displaced transition when the relevant operation occurs, providing that
the more specific transition has not been disabled (by setting \l {Transition::enabled}{enabled}
to false). If it has indeed been disabled, the generic displaced transition is applied instead.
For more details and examples on how to use view transitions, see the ViewTransition
documentation.
\sa addDisplaced, moveDisplaced, removeDisplaced, ViewTransition
*/
void QQuickGridView::viewportMoved(Qt::Orientations orient)
{
Q_D(QQuickGridView);
QQuickItemView::viewportMoved(orient);
if (!d->itemCount)
return;
if (d->inViewportMoved)
return;
d->inViewportMoved = true;
if (yflick()) {
if (d->isContentFlowReversed())
d->bufferMode = d->vData.smoothVelocity < 0 ? QQuickItemViewPrivate::BufferAfter : QQuickItemViewPrivate::BufferBefore;
else
d->bufferMode = d->vData.smoothVelocity < 0 ? QQuickItemViewPrivate::BufferBefore : QQuickItemViewPrivate::BufferAfter;
} else {
if (d->isContentFlowReversed())
d->bufferMode = d->hData.smoothVelocity < 0 ? QQuickItemViewPrivate::BufferAfter : QQuickItemViewPrivate::BufferBefore;
else
d->bufferMode = d->hData.smoothVelocity < 0 ? QQuickItemViewPrivate::BufferBefore : QQuickItemViewPrivate::BufferAfter;
}
d->refillOrLayout();
// Set visibility of items to eliminate cost of items outside the visible area.
qreal from = d->isContentFlowReversed() ? -d->position()-d->size() : d->position();
qreal to = d->isContentFlowReversed() ? -d->position() : d->position()+d->size();
for (int i = 0; i < d->visibleItems.count(); ++i) {
FxGridItemSG *item = static_cast<FxGridItemSG*>(d->visibleItems.at(i));
QQuickItemPrivate::get(item->item)->setCulled(item->rowPos() + d->rowSize() < from || item->rowPos() > to);
}
if (d->currentItem) {
FxGridItemSG *item = static_cast<FxGridItemSG*>(d->currentItem);
QQuickItemPrivate::get(item->item)->setCulled(item->rowPos() + d->rowSize() < from || item->rowPos() > to);
}
if (d->hData.flicking || d->vData.flicking || d->hData.moving || d->vData.moving)
d->moveReason = QQuickGridViewPrivate::Mouse;
if (d->moveReason != QQuickGridViewPrivate::SetIndex) {
if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange && d->highlight) {
// reposition highlight
qreal pos = d->highlight->position();
qreal viewPos = d->isContentFlowReversed() ? -d->position()-d->size() : d->position();
if (pos > viewPos + d->highlightRangeEnd - d->highlight->size())
pos = viewPos + d->highlightRangeEnd - d->highlight->size();
if (pos < viewPos + d->highlightRangeStart)
pos = viewPos + d->highlightRangeStart;
if (pos != d->highlight->position()) {
d->highlightXAnimator->stop();
d->highlightYAnimator->stop();
static_cast<FxGridItemSG*>(d->highlight)->setPosition(static_cast<FxGridItemSG*>(d->highlight)->colPos(), pos);
} else {
2031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
d->updateHighlight();
}
// update current index
int idx = d->snapIndex();
if (idx >= 0 && idx != d->currentIndex) {
d->updateCurrent(idx);
if (d->currentItem && static_cast<FxGridItemSG*>(d->currentItem)->colPos() != static_cast<FxGridItemSG*>(d->highlight)->colPos() && d->autoHighlight) {
if (d->flow == FlowLeftToRight)
d->highlightXAnimator->to = d->currentItem->itemX();
else
d->highlightYAnimator->to = d->currentItem->itemY();
}
}
}
}
d->inViewportMoved = false;
}
void QQuickGridView::keyPressEvent(QKeyEvent *event)
{
Q_D(QQuickGridView);
if (d->model && d->model->count() && d->interactive) {
d->moveReason = QQuickGridViewPrivate::SetIndex;
int oldCurrent = currentIndex();
switch (event->key()) {
case Qt::Key_Up:
moveCurrentIndexUp();
break;
case Qt::Key_Down:
moveCurrentIndexDown();
break;
case Qt::Key_Left:
moveCurrentIndexLeft();
break;
case Qt::Key_Right:
moveCurrentIndexRight();
break;
default:
break;
}
if (oldCurrent != currentIndex() || d->wrap) {
event->accept();
return;
}
}
event->ignore();
QQuickItemView::keyPressEvent(event);
}
void QQuickGridView::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
Q_D(QQuickGridView);
d->resetColumns();
if (newGeometry.width() != oldGeometry.width()
&& newGeometry.height() != oldGeometry.height()) {
d->setPosition(d->position());
} else if (newGeometry.width() != oldGeometry.width()) {
QQuickFlickable::setContentX(d->contentXForPosition(d->position()));
} else if (newGeometry.height() != oldGeometry.height()) {
QQuickFlickable::setContentY(d->contentYForPosition(d->position()));
}
QQuickItemView::geometryChanged(newGeometry, oldGeometry);
}
void QQuickGridView::initItem(int index, QObject *obj)
{
2101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
QQuickItemView::initItem(index, obj);
QQuickItem *item = qmlobject_cast<QQuickItem*>(obj);
if (item) {
QQuickGridViewAttached *attached = static_cast<QQuickGridViewAttached *>(
qmlAttachedPropertiesObject<QQuickGridView>(item));
if (attached)
attached->setView(this);
}
}
/*!
\qmlmethod QtQuick2::GridView::moveCurrentIndexUp()
Move the currentIndex up one item in the view.
The current index will wrap if keyNavigationWraps is true and it
is currently at the end. This method has no effect if the \l count is zero.
\b Note: methods should only be called after the Component has completed.
*/
void QQuickGridView::moveCurrentIndexUp()
{
Q_D(QQuickGridView);
const int count = d->model ? d->model->count() : 0;
if (!count)
return;
if (d->verticalLayoutDirection == QQuickItemView::TopToBottom) {
if (d->flow == QQuickGridView::FlowLeftToRight) {
if (currentIndex() >= d->columns || d->wrap) {
int index = currentIndex() - d->columns;
setCurrentIndex((index >= 0 && index < count) ? index : count-1);
}
} else {
if (currentIndex() > 0 || d->wrap) {
int index = currentIndex() - 1;
setCurrentIndex((index >= 0 && index < count) ? index : count-1);
}
}
} else {
if (d->flow == QQuickGridView::FlowLeftToRight) {
if (currentIndex() < count - d->columns || d->wrap) {
int index = currentIndex()+d->columns;
setCurrentIndex((index >= 0 && index < count) ? index : 0);
}
} else {
if (currentIndex() < count - 1 || d->wrap) {
int index = currentIndex() + 1;
setCurrentIndex((index >= 0 && index < count) ? index : 0);
}
}
}
}
/*!
\qmlmethod QtQuick2::GridView::moveCurrentIndexDown()
Move the currentIndex down one item in the view.
The current index will wrap if keyNavigationWraps is true and it
is currently at the end. This method has no effect if the \l count is zero.
\b Note: methods should only be called after the Component has completed.
*/
void QQuickGridView::moveCurrentIndexDown()
{
Q_D(QQuickGridView);
const int count = d->model ? d->model->count() : 0;
if (!count)
return;
2171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
if (d->verticalLayoutDirection == QQuickItemView::TopToBottom) {
if (d->flow == QQuickGridView::FlowLeftToRight) {
if (currentIndex() < count - d->columns || d->wrap) {
int index = currentIndex()+d->columns;
setCurrentIndex((index >= 0 && index < count) ? index : 0);
}
} else {
if (currentIndex() < count - 1 || d->wrap) {
int index = currentIndex() + 1;
setCurrentIndex((index >= 0 && index < count) ? index : 0);
}
}
} else {
if (d->flow == QQuickGridView::FlowLeftToRight) {
if (currentIndex() >= d->columns || d->wrap) {
int index = currentIndex() - d->columns;
setCurrentIndex((index >= 0 && index < count) ? index : count-1);
}
} else {
if (currentIndex() > 0 || d->wrap) {
int index = currentIndex() - 1;
setCurrentIndex((index >= 0 && index < count) ? index : count-1);
}
}
}
}
/*!
\qmlmethod QtQuick2::GridView::moveCurrentIndexLeft()
Move the currentIndex left one item in the view.
The current index will wrap if keyNavigationWraps is true and it
is currently at the end. This method has no effect if the \l count is zero.
\b Note: methods should only be called after the Component has completed.
*/
void QQuickGridView::moveCurrentIndexLeft()
{
Q_D(QQuickGridView);
const int count = d->model ? d->model->count() : 0;
if (!count)
return;
if (effectiveLayoutDirection() == Qt::LeftToRight) {
if (d->flow == QQuickGridView::FlowLeftToRight) {
if (currentIndex() > 0 || d->wrap) {
int index = currentIndex() - 1;
setCurrentIndex((index >= 0 && index < count) ? index : count-1);
}
} else {
if (currentIndex() >= d->columns || d->wrap) {
int index = currentIndex() - d->columns;
setCurrentIndex((index >= 0 && index < count) ? index : count-1);
}
}
} else {
if (d->flow == QQuickGridView::FlowLeftToRight) {
if (currentIndex() < count - 1 || d->wrap) {
int index = currentIndex() + 1;
setCurrentIndex((index >= 0 && index < count) ? index : 0);
}
} else {
if (currentIndex() < count - d->columns || d->wrap) {
int index = currentIndex() + d->columns;
setCurrentIndex((index >= 0 && index < count) ? index : 0);
}
}
}
}
2241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
/*!
\qmlmethod QtQuick2::GridView::moveCurrentIndexRight()
Move the currentIndex right one item in the view.
The current index will wrap if keyNavigationWraps is true and it
is currently at the end. This method has no effect if the \l count is zero.
\b Note: methods should only be called after the Component has completed.
*/
void QQuickGridView::moveCurrentIndexRight()
{
Q_D(QQuickGridView);
const int count = d->model ? d->model->count() : 0;
if (!count)
return;
if (effectiveLayoutDirection() == Qt::LeftToRight) {
if (d->flow == QQuickGridView::FlowLeftToRight) {
if (currentIndex() < count - 1 || d->wrap) {
int index = currentIndex() + 1;
setCurrentIndex((index >= 0 && index < count) ? index : 0);
}
} else {
if (currentIndex() < count - d->columns || d->wrap) {
int index = currentIndex()+d->columns;
setCurrentIndex((index >= 0 && index < count) ? index : 0);
}
}
} else {
if (d->flow == QQuickGridView::FlowLeftToRight) {
if (currentIndex() > 0 || d->wrap) {
int index = currentIndex() - 1;
setCurrentIndex((index >= 0 && index < count) ? index : count-1);
}
} else {
if (currentIndex() >= d->columns || d->wrap) {
int index = currentIndex() - d->columns;
setCurrentIndex((index >= 0 && index < count) ? index : count-1);
}
}
}
}
bool QQuickGridViewPrivate::applyInsertionChange(const QQmlChangeSet::Insert &change, ChangeResult *insertResult, QList<FxViewItem *> *addedItems, QList<MovedItem> *movingIntoView)
{
Q_Q(QQuickGridView);
int modelIndex = change.index;
int count = change.count;
int index = visibleItems.count() ? mapFromModel(modelIndex) : 0;
if (index < 0) {
int i = visibleItems.count() - 1;
while (i > 0 && visibleItems.at(i)->index == -1)
--i;
if (visibleItems.at(i)->index + 1 == modelIndex) {
// Special case of appending an item to the model.
index = visibleItems.count();
} else {
if (modelIndex <= visibleIndex) {
// Insert before visible items
visibleIndex += count;
for (int i = 0; i < visibleItems.count(); ++i) {
FxViewItem *item = visibleItems.at(i);
if (item->index != -1 && item->index >= modelIndex)
item->index += count;
}
}
return true;
}
2311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
}
qreal tempPos = isContentFlowReversed() ? -position()-size()+q->width()+1 : position();
qreal colPos = 0;
qreal rowPos = 0;
int colNum = 0;
if (visibleItems.count()) {
if (index < visibleItems.count()) {
FxGridItemSG *gridItem = static_cast<FxGridItemSG*>(visibleItems.at(index));
colPos = gridItem->colPos();
rowPos = gridItem->rowPos();
colNum = qFloor((colPos+colSize()/2) / colSize());
} else {
// appending items to visible list
FxGridItemSG *gridItem = static_cast<FxGridItemSG*>(visibleItems.at(index-1));
rowPos = gridItem->rowPos();
colNum = qFloor((gridItem->colPos()+colSize()/2) / colSize());
if (++colNum >= columns) {
colNum = 0;
rowPos += rowSize();
}
colPos = colNum * colSize();
}
}
// Update the indexes of the following visible items.
for (int i = 0; i < visibleItems.count(); ++i) {
FxViewItem *item = visibleItems.at(i);
if (item->index != -1 && item->index >= modelIndex) {
item->index += count;
if (change.isMove())
item->transitionNextReposition(transitioner, QQuickItemViewTransitioner::MoveTransition, false);
else
item->transitionNextReposition(transitioner, QQuickItemViewTransitioner::AddTransition, false);
}
}
int prevVisibleCount = visibleItems.count();
if (insertResult->visiblePos.isValid() && rowPos < insertResult->visiblePos) {
// Insert items before the visible item.
int insertionIdx = index;
int i = count - 1;
int from = tempPos - buffer;
while (i >= 0) {
if (rowPos > from && insertionIdx < visibleIndex) {
// item won't be visible, just note the size for repositioning
insertResult->countChangeBeforeVisible++;
} else {
// item is before first visible e.g. in cache buffer
FxViewItem *item = 0;
if (change.isMove() && (item = currentChanges.removedItems.take(change.moveKey(modelIndex + i))))
item->index = modelIndex + i;
if (!item)
item = createItem(modelIndex + i);
if (!item)
return false;
QQuickItemPrivate::get(item->item)->setCulled(false);
visibleItems.insert(insertionIdx, item);
if (insertionIdx == 0)
insertResult->changedFirstItem = true;
if (!change.isMove()) {
addedItems->append(item);
item->transitionNextReposition(transitioner, QQuickItemViewTransitioner::AddTransition, true);
}
insertResult->sizeChangesBeforeVisiblePos += rowSize();
}
if (--colNum < 0 ) {
2381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450
colNum = columns - 1;
rowPos -= rowSize();
}
colPos = colNum * colSize();
index++;
i--;
}
} else {
int i = 0;
int to = buffer+tempPos+size()-1;
while (i < count && rowPos <= to + rowSize()*(columns - colNum)/qreal(columns+1)) {
FxViewItem *item = 0;
if (change.isMove() && (item = currentChanges.removedItems.take(change.moveKey(modelIndex + i))))
item->index = modelIndex + i;
bool newItem = !item;
if (!item)
item = createItem(modelIndex + i);
if (!item)
return false;
QQuickItemPrivate::get(item->item)->setCulled(false);
visibleItems.insert(index, item);
if (index == 0)
insertResult->changedFirstItem = true;
if (change.isMove()) {
// we know this is a move target, since move displaced items that are
// shuffled into view due to a move would be added in refill()
if (newItem && transitioner && transitioner->canTransition(QQuickItemViewTransitioner::MoveTransition, true))
movingIntoView->append(MovedItem(item, change.moveKey(item->index)));
} else {
addedItems->append(item);
item->transitionNextReposition(transitioner, QQuickItemViewTransitioner::AddTransition, true);
}
insertResult->sizeChangesAfterVisiblePos += rowSize();
if (++colNum >= columns) {
colNum = 0;
rowPos += rowSize();
}
colPos = colNum * colSize();
++index;
++i;
}
}
updateVisibleIndex();
return visibleItems.count() > prevVisibleCount;
}
void QQuickGridViewPrivate::translateAndTransitionItemsAfter(int afterModelIndex, const ChangeResult &insertionResult, const ChangeResult &removalResult)
{
if (!transitioner)
return;
int markerItemIndex = -1;
for (int i=0; i<visibleItems.count(); i++) {
if (visibleItems[i]->index == afterModelIndex) {
markerItemIndex = i;
break;
}
}
if (markerItemIndex < 0)
return;
const qreal viewEndPos = isContentFlowReversed() ? -position() : position() + size();
int countItemsRemoved = -(removalResult.sizeChangesAfterVisiblePos / rowSize());
// account for whether first item has changed if < 1 row was removed before visible
int changeBeforeVisible = insertionResult.countChangeBeforeVisible - removalResult.countChangeBeforeVisible;
2451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520
if (changeBeforeVisible != 0)
countItemsRemoved += (changeBeforeVisible % columns) - (columns - 1);
countItemsRemoved -= removalResult.countChangeAfterVisibleItems;
for (int i=markerItemIndex+1; i<visibleItems.count() && visibleItems.at(i)->position() < viewEndPos; i++) {
FxGridItemSG *gridItem = static_cast<FxGridItemSG *>(visibleItems[i]);
if (!gridItem->transitionScheduledOrRunning()) {
qreal origRowPos = gridItem->colPos();
qreal origColPos = gridItem->rowPos();
int indexDiff = gridItem->index - countItemsRemoved;
gridItem->setPosition((indexDiff % columns) * colSize(), (indexDiff / columns) * rowSize());
gridItem->transitionNextReposition(transitioner, QQuickItemViewTransitioner::RemoveTransition, false);
gridItem->setPosition(origRowPos, origColPos);
}
}
}
bool QQuickGridViewPrivate::needsRefillForAddedOrRemovedIndex(int modelIndex) const
{
// If we add or remove items before visible items, a layout may be
// required to ensure item 0 is in the first column.
return modelIndex < visibleIndex;
}
/*!
\qmlmethod QtQuick2::GridView::positionViewAtIndex(int index, PositionMode mode)
Positions the view such that the \a index is at the position specified by
\a mode:
\list
\li GridView.Beginning - position item at the top (or left for \c GridView.TopToBottom flow) of the view.
\li GridView.Center - position item in the center of the view.
\li GridView.End - position item at bottom (or right for horizontal orientation) of the view.
\li GridView.Visible - if any part of the item is visible then take no action, otherwise
bring the item into view.
\li GridView.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 \c GridView.TopToBottom flow) of the view.
\li GridView.SnapPosition - position the item at \l preferredHighlightBegin. This mode
is only valid if \l highlightRangeMode is StrictlyEnforceRange or snapping is enabled
via \l snapMode.
\endlist
If positioning the view at the index would cause empty space to be displayed at
the beginning or end of the view, the view will be positioned at the boundary.
It is not recommended to use \l {Flickable::}{contentX} or \l {Flickable::}{contentY} to position the view
at a particular index. This is unreliable since removing items from the start
of the view does not cause all other items to be repositioned.
The correct way to bring an item into view is with \c positionViewAtIndex.
\b Note: methods should only be called after the Component has completed. To position
the view at startup, this method should be called by Component.onCompleted. For
example, to position the view at the end:
\code
Component.onCompleted: positionViewAtIndex(count - 1, GridView.Beginning)
\endcode
*/
/*!
\qmlmethod QtQuick2::GridView::positionViewAtBeginning()
\qmlmethod QtQuick2::GridView::positionViewAtEnd()
Positions the view at the beginning or end, taking into account any header or footer.
It is not recommended to use \l {Flickable::}{contentX} or \l {Flickable::}{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, and because
252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
the actual start of the view can vary based on the size of the delegates.
\b Note: methods should only be called after the Component has completed. To position
the view at startup, this method should be called by Component.onCompleted. For
example, to position the view at the end on startup:
\code
Component.onCompleted: positionViewAtEnd()
\endcode
*/
/*!
\qmlmethod int QtQuick2::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.
\b Note: methods should only be called after the Component has completed.
*/
/*!
\qmlmethod Item QtQuick2::GridView::itemAt(int x, int y)
Returns 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 null is returned.
If the item is outside the visible area, null is returned, regardless of
whether an item will exist at that point when scrolled into view.
\b Note: methods should only be called after the Component has completed.
*/
/*!
\qmlmethod QtQuick2::GridView::forceLayout()
Responding to changes in the model is usually batched to happen only once
per frame. This means that inside script blocks it is possible for the
underlying model to have changed, but the GridView has not caught up yet.
This method forces the GridView to immediately respond to any outstanding
changes in the model.
\since QtQuick 2.1
\b Note: methods should only be called after the Component has completed.
*/
QQuickGridViewAttached *QQuickGridView::qmlAttachedProperties(QObject *obj)
{
return new QQuickGridViewAttached(obj);
}
QT_END_NAMESPACE