summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Jones <martin.jones@nokia.com>2009-11-09 04:54:19 (GMT)
committerMartin Jones <martin.jones@nokia.com>2009-11-09 04:54:19 (GMT)
commit26dbd51beadc290ea2ad7ce046d1bf8485fbffc4 (patch)
treea999bda642740dc22e25e3b3c24649075ae26e5a
parent714a957a9144e6c4189dacf96132cf01bf9350e8 (diff)
downloadQt-26dbd51beadc290ea2ad7ce046d1bf8485fbffc4.zip
Qt-26dbd51beadc290ea2ad7ce046d1bf8485fbffc4.tar.gz
Qt-26dbd51beadc290ea2ad7ce046d1bf8485fbffc4.tar.bz2
More tests and fixes for ListView and GridView
-rw-r--r--src/declarative/graphicsitems/qmlgraphicsgridview.cpp47
-rw-r--r--tests/auto/declarative/qmlgraphicsgridview/data/gridview-initCurrent.qml51
-rw-r--r--tests/auto/declarative/qmlgraphicsgridview/tst_qmlgraphicsgridview.cpp111
-rw-r--r--tests/auto/declarative/qmlgraphicslistview/data/itemlist.qml43
-rw-r--r--tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp35
5 files changed, 259 insertions, 28 deletions
diff --git a/src/declarative/graphicsitems/qmlgraphicsgridview.cpp b/src/declarative/graphicsitems/qmlgraphicsgridview.cpp
index f39f5c7..10050f2 100644
--- a/src/declarative/graphicsitems/qmlgraphicsgridview.cpp
+++ b/src/declarative/graphicsitems/qmlgraphicsgridview.cpp
@@ -309,7 +309,7 @@ public:
QmlComponent *highlightComponent;
FxGridItem *highlight;
FxGridItem *trackedItem;
- enum MovementReason { Other, Key, Mouse };
+ enum MovementReason { Other, SetIndex, Mouse };
MovementReason moveReason;
int buffer;
QmlEaseFollow *highlightXAnimator;
@@ -338,7 +338,6 @@ void QmlGraphicsGridViewPrivate::clear()
visibleIndex = 0;
releaseItem(currentItem);
currentItem = 0;
- currentIndex = -1;
createHighlight();
trackedItem = 0;
}
@@ -508,7 +507,6 @@ void QmlGraphicsGridViewPrivate::layout(bool removed)
}
}
q->refill();
- q->trackedPositionChanged();
updateHighlight();
if (flow == QmlGraphicsGridView::LeftToRight) {
q->setViewportHeight(endPosition() - startPosition());
@@ -547,6 +545,8 @@ void QmlGraphicsGridViewPrivate::updateTrackedItem()
if (highlight)
item = highlight;
+ FxGridItem *oldTracked = trackedItem;
+
if (trackedItem && item != trackedItem) {
QObject::disconnect(trackedItem->item, SIGNAL(yChanged()), q, SLOT(trackedPositionChanged()));
QObject::disconnect(trackedItem->item, SIGNAL(xChanged()), q, SLOT(trackedPositionChanged()));
@@ -557,9 +557,8 @@ void QmlGraphicsGridViewPrivate::updateTrackedItem()
trackedItem = item;
QObject::connect(trackedItem->item, SIGNAL(yChanged()), q, SLOT(trackedPositionChanged()));
QObject::connect(trackedItem->item, SIGNAL(xChanged()), q, SLOT(trackedPositionChanged()));
- q->trackedPositionChanged();
}
- if (trackedItem)
+ if (trackedItem && trackedItem != oldTracked)
q->trackedPositionChanged();
}
@@ -615,20 +614,20 @@ void QmlGraphicsGridViewPrivate::updateHighlight()
{
if ((!currentItem && highlight) || (currentItem && !highlight))
createHighlight();
- updateTrackedItem();
- if (currentItem && autoHighlight && highlight) {
+ if (currentItem && autoHighlight && highlight && !moving) {
// auto-update highlight
highlightXAnimator->setSourceValue(currentItem->item->x());
highlightYAnimator->setSourceValue(currentItem->item->y());
highlight->item->setWidth(currentItem->item->width());
highlight->item->setHeight(currentItem->item->height());
}
+ updateTrackedItem();
}
void QmlGraphicsGridViewPrivate::updateCurrent(int modelIndex)
{
Q_Q(QmlGraphicsGridView);
- if (!isValid() || modelIndex < 0 || modelIndex >= model->count()) {
+ if (!q->isComponentComplete() || !isValid() || modelIndex < 0 || modelIndex >= model->count()) {
if (currentItem) {
currentItem->attached->setIsCurrentItem(false);
releaseItem(currentItem);
@@ -799,16 +798,20 @@ void QmlGraphicsGridView::setModel(const QVariant &model)
dataModel->setModel(model);
}
if (d->model) {
- if (d->currentIndex >= d->model->count() || d->currentIndex < 0)
- setCurrentIndex(0);
- else
- d->updateCurrent(d->currentIndex);
+ if (isComponentComplete()) {
+ refill();
+ if (d->currentIndex >= d->model->count() || d->currentIndex < 0) {
+ setCurrentIndex(0);
+ } else {
+ d->moveReason = QmlGraphicsGridViewPrivate::SetIndex;
+ d->updateCurrent(d->currentIndex);
+ }
+ }
connect(d->model, SIGNAL(itemsInserted(int,int)), this, SLOT(itemsInserted(int,int)));
connect(d->model, SIGNAL(itemsRemoved(int,int)), this, SLOT(itemsRemoved(int,int)));
connect(d->model, SIGNAL(itemsMoved(int,int,int)), this, SLOT(itemsMoved(int,int,int)));
connect(d->model, SIGNAL(createdItem(int, QmlGraphicsItem*)), this, SLOT(createdItem(int,QmlGraphicsItem*)));
connect(d->model, SIGNAL(destroyingItem(QmlGraphicsItem*)), this, SLOT(destroyingItem(QmlGraphicsItem*)));
- refill();
emit countChanged();
}
}
@@ -841,8 +844,11 @@ void QmlGraphicsGridView::setDelegate(QmlComponent *delegate)
}
if (QmlGraphicsVisualDataModel *dataModel = qobject_cast<QmlGraphicsVisualDataModel*>(d->model)) {
dataModel->setDelegate(delegate);
- d->updateCurrent(d->currentIndex);
- refill();
+ if (isComponentComplete()) {
+ refill();
+ d->moveReason = QmlGraphicsGridViewPrivate::SetIndex;
+ d->updateCurrent(d->currentIndex);
+ }
}
}
@@ -863,7 +869,8 @@ int QmlGraphicsGridView::currentIndex() const
void QmlGraphicsGridView::setCurrentIndex(int index)
{
Q_D(QmlGraphicsGridView);
- if (d->isValid() && index != d->currentIndex && index < d->model->count() && index >= 0) {
+ if (isComponentComplete() && d->isValid() && index != d->currentIndex && index < d->model->count() && index >= 0) {
+ d->moveReason = QmlGraphicsGridViewPrivate::SetIndex;
cancelFlick();
d->updateCurrent(index);
} else {
@@ -1126,7 +1133,7 @@ void QmlGraphicsGridView::keyPressEvent(QKeyEvent *event)
return;
if (d->model && d->model->count() && d->interactive) {
- d->moveReason = QmlGraphicsGridViewPrivate::Key;
+ d->moveReason = QmlGraphicsGridViewPrivate::SetIndex;
int oldCurrent = currentIndex();
switch (event->key()) {
case Qt::Key_Up:
@@ -1250,9 +1257,11 @@ void QmlGraphicsGridView::componentComplete()
Q_D(QmlGraphicsGridView);
QmlGraphicsFlickable::componentComplete();
d->updateGrid();
+ refill();
if (d->currentIndex < 0)
d->updateCurrent(0);
- refill();
+ else
+ d->updateCurrent(d->currentIndex);
}
void QmlGraphicsGridView::trackedPositionChanged()
@@ -1260,7 +1269,7 @@ void QmlGraphicsGridView::trackedPositionChanged()
Q_D(QmlGraphicsGridView);
if (!d->trackedItem)
return;
- if (!isFlicking() && !d->pressed && d->moveReason == QmlGraphicsGridViewPrivate::Key) {
+ if (!isFlicking() && !d->moving && d->moveReason != QmlGraphicsGridViewPrivate::Mouse) {
if (d->trackedItem->rowPos() < d->position()) {
d->setPosition(d->trackedItem->rowPos());
} else if (d->trackedItem->endRowPos() > d->position() + d->size()) {
diff --git a/tests/auto/declarative/qmlgraphicsgridview/data/gridview-initCurrent.qml b/tests/auto/declarative/qmlgraphicsgridview/data/gridview-initCurrent.qml
new file mode 100644
index 0000000..d9e9f27
--- /dev/null
+++ b/tests/auto/declarative/qmlgraphicsgridview/data/gridview-initCurrent.qml
@@ -0,0 +1,51 @@
+import Qt 4.6
+
+Rectangle {
+ width: 240
+ height: 320
+ color: "#ffffff"
+ resources: [
+ Component {
+ id: myDelegate
+ Rectangle {
+ id: wrapper
+ objectName: "wrapper"
+ width: 80
+ height: 60
+ border.color: "blue"
+ Text {
+ text: index
+ }
+ Text {
+ x: 40
+ text: wrapper.x + ", " + wrapper.y
+ }
+ Text {
+ y: 20
+ id: textName
+ objectName: "textName"
+ text: name
+ }
+ Text {
+ y: 40
+ id: textNumber
+ objectName: "textNumber"
+ text: number
+ }
+ color: GridView.isCurrentItem ? "lightsteelblue" : "white"
+ }
+ }
+ ]
+ GridView {
+ id: grid
+ objectName: "grid"
+ focus: true
+ width: 240
+ height: 320
+ currentIndex: 5
+ cellWidth: 80
+ cellHeight: 60
+ model: testModel
+ delegate: myDelegate
+ }
+}
diff --git a/tests/auto/declarative/qmlgraphicsgridview/tst_qmlgraphicsgridview.cpp b/tests/auto/declarative/qmlgraphicsgridview/tst_qmlgraphicsgridview.cpp
index c6ea25a..7c32d14 100644
--- a/tests/auto/declarative/qmlgraphicsgridview/tst_qmlgraphicsgridview.cpp
+++ b/tests/auto/declarative/qmlgraphicsgridview/tst_qmlgraphicsgridview.cpp
@@ -53,6 +53,7 @@ public:
tst_QmlGraphicsGridView();
private slots:
+ void currentIndex();
void items();
void changed();
void inserted();
@@ -168,15 +169,6 @@ void tst_QmlGraphicsGridView::items()
QCOMPARE(number->text(), model.number(i));
}
- gridview->moveCurrentIndexRight();
- QCOMPARE(gridview->currentIndex(), 1);
- gridview->moveCurrentIndexDown();
- QCOMPARE(gridview->currentIndex(), 4);
- gridview->moveCurrentIndexUp();
- QCOMPARE(gridview->currentIndex(), 1);
- gridview->moveCurrentIndexLeft();
- QCOMPARE(gridview->currentIndex(), 0);
-
// set an empty model and confirm that items are destroyed
TestModel model2;
ctxt->setContextProperty("testModel", &model2);
@@ -515,6 +507,107 @@ void tst_QmlGraphicsGridView::moved()
delete canvas;
}
+void tst_QmlGraphicsGridView::currentIndex()
+{
+ TestModel model;
+ for (int i = 0; i < 30; i++)
+ model.addItem("Item" + QString::number(i), QString::number(i));
+
+ QmlView *canvas = new QmlView(0);
+ canvas->setFixedSize(240,320);
+
+ QmlContext *ctxt = canvas->rootContext();
+ ctxt->setContextProperty("testModel", &model);
+
+ QString filename(SRCDIR "/data/gridview-initCurrent.qml");
+ QFile file(filename);
+ file.open(QFile::ReadOnly);
+ QString qml = file.readAll();
+ canvas->setQml(qml, filename);
+
+ canvas->execute();
+ qApp->processEvents();
+
+ QmlGraphicsGridView *gridview = findItem<QmlGraphicsGridView>(canvas->root(), "grid");
+ QVERIFY(gridview != 0);
+
+ QmlGraphicsItem *viewport = gridview->viewport();
+ QVERIFY(viewport != 0);
+
+ // current item should be third item
+ QCOMPARE(gridview->currentIndex(), 5);
+ QCOMPARE(gridview->currentItem(), findItem<QmlGraphicsItem>(viewport, "wrapper", 5));
+
+ gridview->moveCurrentIndexRight();
+ QCOMPARE(gridview->currentIndex(), 6);
+ gridview->moveCurrentIndexDown();
+ QCOMPARE(gridview->currentIndex(), 9);
+ gridview->moveCurrentIndexUp();
+ QCOMPARE(gridview->currentIndex(), 6);
+ gridview->moveCurrentIndexLeft();
+ QCOMPARE(gridview->currentIndex(), 5);
+
+ // no wrap
+ gridview->setCurrentIndex(0);
+ QCOMPARE(gridview->currentIndex(), 0);
+
+ gridview->moveCurrentIndexUp();
+ QCOMPARE(gridview->currentIndex(), 0);
+
+ gridview->moveCurrentIndexLeft();
+ QCOMPARE(gridview->currentIndex(), 0);
+
+ gridview->setCurrentIndex(model.count()-1);
+ QTest::qWait(500);
+ QCOMPARE(gridview->currentIndex(), model.count()-1);
+
+ gridview->moveCurrentIndexRight();
+ QCOMPARE(gridview->currentIndex(), model.count()-1);
+
+ gridview->moveCurrentIndexDown();
+ QCOMPARE(gridview->currentIndex(), model.count()-1);
+
+ // with wrap
+ gridview->setWrapEnabled(true);
+
+ gridview->setCurrentIndex(0);
+ QCOMPARE(gridview->currentIndex(), 0);
+ QTest::qWait(500);
+
+ gridview->moveCurrentIndexLeft();
+ QCOMPARE(gridview->currentIndex(), model.count()-1);
+
+ QTest::qWait(500);
+ QCOMPARE(gridview->viewportY(), 279.0);
+
+ gridview->moveCurrentIndexRight();
+ QCOMPARE(gridview->currentIndex(), 0);
+
+ QTest::qWait(500);
+ QCOMPARE(gridview->viewportY(), 0.0);
+
+ // Test keys
+ canvas->show();
+ qApp->processEvents();
+
+ QEvent wa(QEvent::WindowActivate);
+ QApplication::sendEvent(canvas, &wa);
+ QFocusEvent fe(QEvent::FocusIn);
+ QApplication::sendEvent(canvas, &fe);
+
+ QKeyEvent key(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier, "", false, 1);
+ QApplication::sendEvent(canvas, &key);
+ QVERIFY(key.isAccepted());
+ QCOMPARE(gridview->currentIndex(), 3);
+
+ key = QKeyEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier, "", false, 1);
+ QApplication::sendEvent(canvas, &key);
+ QVERIFY(key.isAccepted());
+ QCOMPARE(gridview->currentIndex(), 0);
+
+ delete canvas;
+}
+
QmlView *tst_QmlGraphicsGridView::createView(const QString &filename)
{
QmlView *canvas = new QmlView(0);
diff --git a/tests/auto/declarative/qmlgraphicslistview/data/itemlist.qml b/tests/auto/declarative/qmlgraphicslistview/data/itemlist.qml
new file mode 100644
index 0000000..36d3501
--- /dev/null
+++ b/tests/auto/declarative/qmlgraphicslistview/data/itemlist.qml
@@ -0,0 +1,43 @@
+// This example demonstrates placing items in a view using
+// a VisualItemModel
+
+import Qt 4.6
+
+Rectangle {
+ color: "lightgray"
+ width: 240
+ height: 320
+
+ VisualItemModel {
+ id: itemModel
+ objectName: "itemModel"
+ Rectangle {
+ objectName: "item1"
+ height: view.height; width: view.width; color: "#FFFEF0"
+ Text { text: "Page 1"; font.bold: true; anchors.centerIn: parent }
+ }
+ Rectangle {
+ objectName: "item2"
+ height: view.height; width: view.width; color: "#F0FFF7"
+ Text { text: "Page 2"; font.bold: true; anchors.centerIn: parent }
+ }
+ Rectangle {
+ objectName: "item3"
+ height: view.height; width: view.width; color: "#F4F0FF"
+ Text { text: "Page 3"; font.bold: true; anchors.centerIn: parent }
+ }
+ }
+
+ ListView {
+ id: view
+ objectName: "view"
+ anchors.fill: parent
+ anchors.bottomMargin: 30
+ model: itemModel
+ preferredHighlightBegin: 0
+ preferredHighlightEnd: 0
+ highlightRangeMode: "StrictlyEnforceRange"
+ orientation: ListView.Horizontal
+ flickDeceleration: 2000
+ }
+}
diff --git a/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp b/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp
index de26635..afef47a 100644
--- a/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp
+++ b/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp
@@ -43,6 +43,7 @@
#include <qmlview.h>
#include <private/qmlgraphicslistview_p.h>
#include <private/qmlgraphicstext_p.h>
+#include <private/qmlgraphicsvisualitemmodel_p.h>
#include <qmlcontext.h>
#include <qmlexpression.h>
@@ -53,6 +54,7 @@ public:
tst_QmlGraphicsListView();
private slots:
+ void itemList();
// Test both QListModelInterface and QAbstractItemModel model types
void qListModelInterface_items();
void qAbstractItemModel_items();
@@ -844,6 +846,39 @@ void tst_QmlGraphicsListView::currentIndex()
delete canvas;
}
+void tst_QmlGraphicsListView::itemList()
+{
+ QmlView *canvas = createView(SRCDIR "/data/itemlist.qml");
+
+ canvas->execute();
+ qApp->processEvents();
+
+ QmlGraphicsListView *listview = findItem<QmlGraphicsListView>(canvas->root(), "view");
+ QVERIFY(listview != 0);
+
+ QmlGraphicsItem *viewport = listview->viewport();
+ QVERIFY(viewport != 0);
+
+ QmlGraphicsVisualItemModel *model = canvas->root()->findChild<QmlGraphicsVisualItemModel*>("itemModel");
+ QVERIFY(model != 0);
+
+ QVERIFY(model->count() == 3);
+ QCOMPARE(listview->currentIndex(), 0);
+
+ QmlGraphicsItem *item = findItem<QmlGraphicsItem>(viewport, "item1");
+ QVERIFY(item);
+ QCOMPARE(item->x(), 0.0);
+
+ listview->setCurrentIndex(2);
+ QTest::qWait(1000);
+
+ item = findItem<QmlGraphicsItem>(viewport, "item3");
+ QVERIFY(item);
+ QCOMPARE(item->x(), 480.0);
+
+ delete canvas;
+}
+
void tst_QmlGraphicsListView::qListModelInterface_items()
{
items<TestModel>();