Add itemAt(x,y) function to GridView and ListView.

Task-number: QTBUG-21592

Change-Id: I3169e789da701ce261250421239584abd7f29b57
Reviewed-by: Bea Lam <bea.lam@nokia.com>
This commit is contained in:
Martin Jones 2012-01-06 09:58:27 +10:00 committed by Qt by Nokia
parent c13098fa6e
commit 8e0ea24115
6 changed files with 93 additions and 14 deletions

View File

@ -1975,6 +1975,19 @@ bool QQuickGridViewPrivate::needsRefillForAddedOrRemovedIndex(int modelIndex) co
\bold 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.
\bold Note: methods should only be called after the Component has completed.
*/
QQuickGridViewAttached *QQuickGridView::qmlAttachedProperties(QObject *obj)
{
return new QQuickGridViewAttached(obj);

View File

@ -700,6 +700,18 @@ int QQuickItemView::indexAt(qreal x, qreal y) const
return -1;
}
QQuickItem *QQuickItemView::itemAt(qreal x, qreal y) const
{
Q_D(const QQuickItemView);
for (int i = 0; i < d->visibleItems.count(); ++i) {
const FxViewItem *item = d->visibleItems.at(i);
if (item->contains(x, y))
return item->item;
}
return 0;
}
void QQuickItemViewPrivate::applyPendingChanges()
{
Q_Q(QQuickItemView);

View File

@ -147,6 +147,7 @@ public:
Q_INVOKABLE void positionViewAtIndex(int index, int mode);
Q_INVOKABLE int indexAt(qreal x, qreal y) const;
Q_INVOKABLE QQuickItem *itemAt(qreal x, qreal y) const;
Q_INVOKABLE void positionViewAtBeginning();
Q_INVOKABLE void positionViewAtEnd();

View File

@ -2550,6 +2550,19 @@ bool QQuickListViewPrivate::applyInsertionChange(const QDeclarativeChangeSet::In
\bold Note: methods should only be called after the Component has completed.
*/
/*!
\qmlmethod Item QtQuick2::ListView::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.
\bold Note: methods should only be called after the Component has completed.
*/
QQuickListViewAttached *QQuickListView::qmlAttachedProperties(QObject *obj)
{
return new QQuickListViewAttached(obj);

View File

@ -103,7 +103,8 @@ private slots:
void header();
void header_data();
void resizeViewAndRepaint();
void indexAt();
void indexAt_itemAt_data();
void indexAt_itemAt();
void onAdd();
void onAdd_data();
void onRemove();
@ -2986,8 +2987,25 @@ void tst_QQuickGridView::resizeViewAndRepaint()
delete canvas;
}
void tst_QQuickGridView::indexAt()
void tst_QQuickGridView::indexAt_itemAt_data()
{
QTest::addColumn<qreal>("x");
QTest::addColumn<qreal>("y");
QTest::addColumn<int>("index");
QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
QTest::newRow("Item 0 - 79, 59") << 79. << 59. << 0;
QTest::newRow("Item 1 - 80, 0") << 80. << 0. << 1;
QTest::newRow("Item 3 - 0, 60") << 0. << 60. << 3;
QTest::newRow("No Item - 240, 0") << 240. << 0. << -1;
}
void tst_QQuickGridView::indexAt_itemAt()
{
QFETCH(qreal, x);
QFETCH(qreal, y);
QFETCH(int, index);
QQuickView *canvas = createView();
TestModel model;
@ -3015,11 +3033,13 @@ void tst_QQuickGridView::indexAt()
QTRY_COMPARE(gridview->count(), model.count());
QCOMPARE(gridview->indexAt(0, 0), 0);
QCOMPARE(gridview->indexAt(79, 59), 0);
QCOMPARE(gridview->indexAt(80, 0), 1);
QCOMPARE(gridview->indexAt(0, 60), 3);
QCOMPARE(gridview->indexAt(240, 0), -1);
QQuickItem *item = 0;
if (index >= 0) {
item = findItem<QQuickItem>(contentItem, "wrapper", index);
QVERIFY(item);
}
QCOMPARE(gridview->indexAt(x, y), index);
QVERIFY(gridview->itemAt(x, y) == item);
delete canvas;
}

View File

@ -138,7 +138,8 @@ private slots:
void resizeDelegate();
void resizeFirstDelegate();
void QTBUG_16037();
void indexAt();
void indexAt_itemAt_data();
void indexAt_itemAt();
void incrementalModel();
void onAdd();
void onAdd_data();
@ -3686,8 +3687,25 @@ void tst_QQuickListView::QTBUG_16037()
delete canvas;
}
void tst_QQuickListView::indexAt()
void tst_QQuickListView::indexAt_itemAt_data()
{
QTest::addColumn<qreal>("x");
QTest::addColumn<qreal>("y");
QTest::addColumn<int>("index");
QTest::newRow("Item 0 - 0, 0") << 0. << 0. << 0;
QTest::newRow("Item 0 - 0, 19") << 0. << 19. << 0;
QTest::newRow("Item 0 - 239, 19") << 239. << 19. << 0;
QTest::newRow("Item 1 - 0, 20") << 0. << 20. << 1;
QTest::newRow("No Item - 240, 20") << 240. << 20. << -1;
}
void tst_QQuickListView::indexAt_itemAt()
{
QFETCH(qreal, x);
QFETCH(qreal, y);
QFETCH(int, index);
QQuickView *canvas = createView();
TestModel model;
@ -3709,11 +3727,13 @@ void tst_QQuickListView::indexAt()
QQuickItem *contentItem = listview->contentItem();
QTRY_VERIFY(contentItem != 0);
QCOMPARE(listview->indexAt(0,0), 0);
QCOMPARE(listview->indexAt(0,19), 0);
QCOMPARE(listview->indexAt(239,19), 0);
QCOMPARE(listview->indexAt(0,20), 1);
QCOMPARE(listview->indexAt(240,20), -1);
QQuickItem *item = 0;
if (index >= 0) {
item = findItem<QQuickItem>(contentItem, "wrapper", index);
QVERIFY(item);
}
QCOMPARE(listview->indexAt(x,y), index);
QVERIFY(listview->itemAt(x,y) == item);
delete canvas;
delete testObject;