summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Jones <martin.jones@nokia.com>2009-11-27 04:26:49 (GMT)
committerMartin Jones <martin.jones@nokia.com>2009-11-27 04:26:49 (GMT)
commita47c6ddde353b1ffe8fd4037817143ff9f338654 (patch)
tree25b8250c786c4b0f52aa27766903d4bca2a09fd2
parentdd6262e2c4e977f7a02cd8bdc740a08adb0cad49 (diff)
downloadQt-a47c6ddde353b1ffe8fd4037817143ff9f338654.zip
Qt-a47c6ddde353b1ffe8fd4037817143ff9f338654.tar.gz
Qt-a47c6ddde353b1ffe8fd4037817143ff9f338654.tar.bz2
Add GridView.positionViewAtIndex().
-rw-r--r--src/declarative/graphicsitems/qmlgraphicsgridview.cpp41
-rw-r--r--src/declarative/graphicsitems/qmlgraphicsgridview_p.h1
-rw-r--r--tests/auto/declarative/qmlgraphicsgridview/tst_qmlgraphicsgridview.cpp91
3 files changed, 127 insertions, 6 deletions
diff --git a/src/declarative/graphicsitems/qmlgraphicsgridview.cpp b/src/declarative/graphicsitems/qmlgraphicsgridview.cpp
index e36ea50..81128e7 100644
--- a/src/declarative/graphicsitems/qmlgraphicsgridview.cpp
+++ b/src/declarative/graphicsitems/qmlgraphicsgridview.cpp
@@ -388,9 +388,9 @@ void QmlGraphicsGridViewPrivate::refill(qreal from, qreal to)
to += buffer;
bool changed = false;
- int colPos = 0;
- int rowPos = 0;
- int modelIndex = 0;
+ int colPos = colPosAt(visibleIndex);
+ int rowPos = rowPosAt(visibleIndex);
+ int modelIndex = visibleIndex;
if (visibleItems.count()) {
rowPos = visibleItems.last()->rowPos();
colPos = visibleItems.last()->colPos() + colSize();
@@ -822,14 +822,14 @@ void QmlGraphicsGridView::setModel(const QVariant &model)
}
/*!
- \qmlproperty component GridView::delegate
+ \qmlproperty component 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 {qmlmodels}{Data Model}.
- Here is an example delegate:
- \snippet doc/src/snippets/declarative/gridview/gridview.qml 0
+ Here is an example delegate:
+ \snippet doc/src/snippets/declarative/gridview/gridview.qml 0
*/
QmlComponent *QmlGraphicsGridView::delegate() const
{
@@ -1292,6 +1292,35 @@ void QmlGraphicsGridView::moveCurrentIndexRight()
}
}
+void QmlGraphicsGridView::positionViewAtIndex(int index)
+{
+ Q_D(QmlGraphicsGridView);
+ if (!d->isValid() || index < 0 || index >= d->model->count())
+ return;
+
+ qreal maxExtent = d->flow == QmlGraphicsGridView::LeftToRight ? -maxYExtent() : -maxXExtent();
+ FxGridItem *item = d->visibleItem(index);
+ if (item) {
+ // Already created - just move to top of view
+ int pos = qMin(item->rowPos(), maxExtent);
+ d->setPosition(pos);
+ } else {
+ int pos = d->rowPosAt(index);
+ // save the currently visible items in case any of them end up visible again
+ QList<FxGridItem*> oldVisible = d->visibleItems;
+ d->visibleItems.clear();
+ d->visibleIndex = index - index % d->columns;
+ d->setPosition(pos);
+ // setPosition() will cause refill. Adjust if we have moved beyond range
+ if (d->position() > maxExtent)
+ d->setPosition(maxExtent);
+ // now release the reference to all the old visible items.
+ for (int i = 0; i < oldVisible.count(); ++i)
+ d->releaseItem(oldVisible.at(i));
+ }
+}
+
+
void QmlGraphicsGridView::componentComplete()
{
Q_D(QmlGraphicsGridView);
diff --git a/src/declarative/graphicsitems/qmlgraphicsgridview_p.h b/src/declarative/graphicsitems/qmlgraphicsgridview_p.h
index 3e09cf3..99515a3 100644
--- a/src/declarative/graphicsitems/qmlgraphicsgridview_p.h
+++ b/src/declarative/graphicsitems/qmlgraphicsgridview_p.h
@@ -121,6 +121,7 @@ public Q_SLOTS:
void moveCurrentIndexDown();
void moveCurrentIndexLeft();
void moveCurrentIndexRight();
+ void positionViewAtIndex(int index);
Q_SIGNALS:
void countChanged();
diff --git a/tests/auto/declarative/qmlgraphicsgridview/tst_qmlgraphicsgridview.cpp b/tests/auto/declarative/qmlgraphicsgridview/tst_qmlgraphicsgridview.cpp
index f31ea49..96a164b 100644
--- a/tests/auto/declarative/qmlgraphicsgridview/tst_qmlgraphicsgridview.cpp
+++ b/tests/auto/declarative/qmlgraphicsgridview/tst_qmlgraphicsgridview.cpp
@@ -65,6 +65,7 @@ private slots:
void currentIndex();
void defaultValues();
void properties();
+ void positionViewAtIndex();
private:
QmlView *createView(const QString &filename);
@@ -809,6 +810,96 @@ void tst_QmlGraphicsGridView::properties()
delete obj;
}
+void tst_QmlGraphicsGridView::positionViewAtIndex()
+{
+ QmlView *canvas = createView(SRCDIR "/data/gridview.qml");
+
+ TestModel model;
+ for (int i = 0; i < 40; i++)
+ model.addItem("Item" + QString::number(i), "");
+
+ QmlContext *ctxt = canvas->rootContext();
+ ctxt->setContextProperty("testModel", &model);
+ ctxt->setContextProperty("testTopToBottom", QVariant(false));
+
+ canvas->execute();
+ qApp->processEvents();
+
+ QmlGraphicsGridView *gridview = findItem<QmlGraphicsGridView>(canvas->root(), "grid");
+ QVERIFY(gridview != 0);
+
+ QmlGraphicsItem *viewport = gridview->viewport();
+ QVERIFY(viewport != 0);
+
+ // Confirm items positioned correctly
+ int itemCount = findItems<QmlGraphicsItem>(viewport, "wrapper").count();
+ for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
+ QmlGraphicsItem *item = findItem<QmlGraphicsItem>(viewport, "wrapper", i);
+ if (!item) qWarning() << "Item" << i << "not found";
+ QVERIFY(item);
+ QCOMPARE(item->x(), (i%3)*80.);
+ QCOMPARE(item->y(), (i/3)*60.);
+ }
+
+ // Position on a currently visible item
+ gridview->positionViewAtIndex(4);
+ QCOMPARE(gridview->viewportY(), 60.);
+
+ // Confirm items positioned correctly
+ itemCount = findItems<QmlGraphicsItem>(viewport, "wrapper").count();
+ for (int i = 3; i < model.count() && i < itemCount-3-1; ++i) {
+ QmlGraphicsItem *item = findItem<QmlGraphicsItem>(viewport, "wrapper", i);
+ if (!item) qWarning() << "Item" << i << "not found";
+ QVERIFY(item);
+ QCOMPARE(item->x(), (i%3)*80.);
+ QCOMPARE(item->y(), (i/3)*60.);
+ }
+
+ // Position on an item beyond the visible items
+ gridview->positionViewAtIndex(21);
+ QCOMPARE(gridview->viewportY(), 420.);
+
+ // Confirm items positioned correctly
+ itemCount = findItems<QmlGraphicsItem>(viewport, "wrapper").count();
+ for (int i = 22; i < model.count() && i < itemCount-22-1; ++i) {
+ QmlGraphicsItem *item = findItem<QmlGraphicsItem>(viewport, "wrapper", i);
+ if (!item) qWarning() << "Item" << i << "not found";
+ QVERIFY(item);
+ QCOMPARE(item->x(), (i%3)*80.);
+ QCOMPARE(item->y(), (i/3)*60.);
+ }
+
+ // Position on an item that would leave empty space if positioned at the top
+ gridview->positionViewAtIndex(31);
+ QCOMPARE(gridview->viewportY(), 520.);
+
+ // Confirm items positioned correctly
+ itemCount = findItems<QmlGraphicsItem>(viewport, "wrapper").count();
+ for (int i = 24; i < model.count() && i < itemCount-24-1; ++i) {
+ QmlGraphicsItem *item = findItem<QmlGraphicsItem>(viewport, "wrapper", i);
+ if (!item) qWarning() << "Item" << i << "not found";
+ QVERIFY(item);
+ QCOMPARE(item->x(), (i%3)*80.);
+ QCOMPARE(item->y(), (i/3)*60.);
+ }
+
+ // Position at the beginning again
+ gridview->positionViewAtIndex(0);
+ QCOMPARE(gridview->viewportY(), 0.);
+
+ // Confirm items positioned correctly
+ itemCount = findItems<QmlGraphicsItem>(viewport, "wrapper").count();
+ for (int i = 0; i < model.count() && i < itemCount-1; ++i) {
+ QmlGraphicsItem *item = findItem<QmlGraphicsItem>(viewport, "wrapper", i);
+ if (!item) qWarning() << "Item" << i << "not found";
+ QVERIFY(item);
+ QCOMPARE(item->x(), (i%3)*80.);
+ QCOMPARE(item->y(), (i/3)*60.);
+ }
+
+ delete canvas;
+}
+
QmlView *tst_QmlGraphicsGridView::createView(const QString &filename)
{
QmlView *canvas = new QmlView(0);