summaryrefslogtreecommitdiffstats
path: root/src/declarative/graphicsitems
diff options
context:
space:
mode:
authorMartin Jones <martin.jones@nokia.com>2010-03-17 03:14:24 (GMT)
committerMartin Jones <martin.jones@nokia.com>2010-03-17 03:14:24 (GMT)
commit9bf133d66f91e29b77ecff16b595280565ab0837 (patch)
treed86382ac3cf2a7f3b2de1c8210b3eb285c380b89 /src/declarative/graphicsitems
parentdc829ac31694f382bf7b8a0702f73409762e36f0 (diff)
downloadQt-9bf133d66f91e29b77ecff16b595280565ab0837.zip
Qt-9bf133d66f91e29b77ecff16b595280565ab0837.tar.gz
Qt-9bf133d66f91e29b77ecff16b595280565ab0837.tar.bz2
Add ListView and GridView.indexAt() methods to get the index at a point.
Diffstat (limited to 'src/declarative/graphicsitems')
-rw-r--r--src/declarative/graphicsitems/qdeclarativegridview.cpp40
-rw-r--r--src/declarative/graphicsitems/qdeclarativegridview_p.h4
-rw-r--r--src/declarative/graphicsitems/qdeclarativelistview.cpp30
-rw-r--r--src/declarative/graphicsitems/qdeclarativelistview_p.h4
4 files changed, 65 insertions, 13 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp
index b1391f9..93f8d06 100644
--- a/src/declarative/graphicsitems/qdeclarativegridview.cpp
+++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp
@@ -81,6 +81,10 @@ public:
item->setPos(QPointF(row, col));
}
}
+ bool contains(int x, int y) const {
+ return (x >= item->x() && x < item->x() + view->cellWidth() &&
+ y >= item->y() && y < item->y() + view->cellHeight());
+ }
QDeclarativeItem *item;
QDeclarativeGridView *view;
@@ -115,7 +119,7 @@ public:
void updateGrid();
void scheduleLayout();
- void layout(bool removed=false);
+ void layout();
void updateUnrequestedIndexes();
void updateUnrequestedPositions();
void updateTrackedItem();
@@ -274,7 +278,7 @@ public:
|| newGeometry.width() != oldGeometry.width()) {
if (q->isComponentComplete()) {
updateGrid();
- layout();
+ scheduleLayout();
}
}
}
@@ -538,7 +542,7 @@ void QDeclarativeGridViewPrivate::scheduleLayout()
}
}
-void QDeclarativeGridViewPrivate::layout(bool removed)
+void QDeclarativeGridViewPrivate::layout()
{
Q_Q(QDeclarativeGridView);
layoutScheduled = false;
@@ -547,8 +551,6 @@ void QDeclarativeGridViewPrivate::layout(bool removed)
qreal colPos = visibleItems.first()->colPos();
int col = visibleIndex % columns;
if (colPos != col * colSize()) {
- if (removed)
- rowPos -= rowSize();
colPos = col * colSize();
visibleItems.first()->setPosition(colPos, rowPos);
}
@@ -1503,7 +1505,7 @@ qreal QDeclarativeGridView::maxYExtent() const
return QDeclarativeFlickable::maxYExtent();
qreal extent;
if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange)
- extent = -(d->rowPosAt(count()-1) - d->highlightRangeEnd);
+ extent = -(d->endPosition() - d->highlightRangeEnd);
else
extent = -(d->endPosition() - height());
const qreal minY = minYExtent();
@@ -1530,7 +1532,7 @@ qreal QDeclarativeGridView::maxXExtent() const
return QDeclarativeFlickable::maxXExtent();
qreal extent;
if (d->haveHighlightRange && d->highlightRange == StrictlyEnforceRange)
- extent = -(d->rowPosAt(count()-1) - d->highlightRangeEnd);
+ extent = -(d->endPosition() - d->highlightRangeEnd);
else
extent = -(d->endPosition() - height());
const qreal minX = minXExtent();
@@ -1744,6 +1746,28 @@ void QDeclarativeGridView::positionViewAtIndex(int index, int mode)
d->fixupPosition();
}
+/*!
+ \qmlmethod int GridView::indexAt(int x, int y)
+
+ Returns the index of the visible item containing the point \a x, \a y in content
+ coordinates. If there is no item at the point specified, or the item is
+ not visible -1 is returned.
+
+ If the item is outside the visible area, -1 is returned, regardless of
+ whether an item will exist at that point when scrolled into view.
+*/
+int QDeclarativeGridView::indexAt(int x, int y) const
+{
+ Q_D(const QDeclarativeGridView);
+ for (int i = 0; i < d->visibleItems.count(); ++i) {
+ const FxGridItem *listItem = d->visibleItems.at(i);
+ if(listItem->contains(x, y))
+ return listItem->index;
+ }
+
+ return -1;
+}
+
void QDeclarativeGridView::componentComplete()
{
Q_D(QDeclarativeGridView);
@@ -2120,7 +2144,7 @@ void QDeclarativeGridView::itemsMoved(int from, int to, int count)
d->releaseItem(item);
}
- d->layout(removedBeforeVisible);
+ d->layout();
}
void QDeclarativeGridView::modelReset()
diff --git a/src/declarative/graphicsitems/qdeclarativegridview_p.h b/src/declarative/graphicsitems/qdeclarativegridview_p.h
index f14ec14..f032240 100644
--- a/src/declarative/graphicsitems/qdeclarativegridview_p.h
+++ b/src/declarative/graphicsitems/qdeclarativegridview_p.h
@@ -140,6 +140,9 @@ public:
enum PositionMode { Beginning, Center, End, Visible, Contain };
Q_ENUMS(PositionMode)
+ Q_INVOKABLE void positionViewAtIndex(int index, int mode);
+ Q_INVOKABLE int indexAt(int x, int y) const;
+
static QDeclarativeGridViewAttached *qmlAttachedProperties(QObject *);
public Q_SLOTS:
@@ -147,7 +150,6 @@ public Q_SLOTS:
void moveCurrentIndexDown();
void moveCurrentIndexLeft();
void moveCurrentIndexRight();
- void positionViewAtIndex(int index, int mode);
Q_SIGNALS:
void countChanged();
diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp
index e5013a9..84281c8 100644
--- a/src/declarative/graphicsitems/qdeclarativelistview.cpp
+++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp
@@ -129,6 +129,10 @@ public:
item->setX(pos);
}
}
+ bool contains(int x, int y) const {
+ return (x >= item->x() && x < item->x() + item->width() &&
+ y >= item->y() && y < item->y() + item->height());
+ }
QDeclarativeItem *item;
QDeclarativeItem *section;
@@ -393,9 +397,8 @@ public:
QDeclarativeFlickablePrivate::itemGeometryChanged(item, newGeometry, oldGeometry);
if (item != viewport) {
if ((orient == QDeclarativeListView::Vertical && newGeometry.height() != oldGeometry.height())
- || newGeometry.width() != oldGeometry.width()) {
- layout();
- fixupPosition();
+ || (orient == QDeclarativeListView::Horizontal && newGeometry.width() != oldGeometry.width())) {
+ scheduleLayout();
}
}
}
@@ -2260,6 +2263,27 @@ void QDeclarativeListView::positionViewAtIndex(int index, int mode)
d->fixupPosition();
}
+/*!
+ \qmlmethod int ListView::indexAt(int x, int y)
+
+ Returns the index of the visible item containing the point \a x, \a y in content
+ coordinates. If there is no item at the point specified, or the item is
+ not visible -1 is returned.
+
+ If the item is outside the visible area, -1 is returned, regardless of
+ whether an item will exist at that point when scrolled into view.
+*/
+int QDeclarativeListView::indexAt(int x, int y) const
+{
+ Q_D(const QDeclarativeListView);
+ for (int i = 0; i < d->visibleItems.count(); ++i) {
+ const FxListItem *listItem = d->visibleItems.at(i);
+ if(listItem->contains(x, y))
+ return listItem->index;
+ }
+
+ return -1;
+}
void QDeclarativeListView::componentComplete()
{
diff --git a/src/declarative/graphicsitems/qdeclarativelistview_p.h b/src/declarative/graphicsitems/qdeclarativelistview_p.h
index 0c2677c..1ea0080 100644
--- a/src/declarative/graphicsitems/qdeclarativelistview_p.h
+++ b/src/declarative/graphicsitems/qdeclarativelistview_p.h
@@ -194,10 +194,12 @@ public:
enum PositionMode { Beginning, Center, End, Visible, Contain };
Q_ENUMS(PositionMode)
+ Q_INVOKABLE void positionViewAtIndex(int index, int mode);
+ Q_INVOKABLE int indexAt(int x, int y) const;
+
public Q_SLOTS:
void incrementCurrentIndex();
void decrementCurrentIndex();
- void positionViewAtIndex(int index, int mode);
Q_SIGNALS:
void countChanged();