aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brasser <michael.brasser@nokia.com>2011-09-19 13:50:03 +1000
committerQt by Nokia <qt-info@nokia.com>2011-09-19 06:24:47 +0200
commit1aa075b6319ded90d79dfe28605294283502bead (patch)
tree59751e7937657d1544a9a2db1ff71f432195db1e
parentb0539f0ed9e848afd6df2cb4d01eb5af70168df8 (diff)
Make cellWidth/cellHeight reals rather than ints.
When they were ints, it was relatively easy to end up with a different row or column count than expected, because of rounding. Change-Id: Ifc5eba2b5598cbc0220df25f91f031581c3b51a5 Reviewed-on: http://codereview.qt-project.org/5112 Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com> Reviewed-by: Bea Lam <bea.lam@nokia.com>
-rw-r--r--src/declarative/items/qsggridview.cpp30
-rw-r--r--src/declarative/items/qsggridview_p.h12
-rw-r--r--tests/auto/declarative/qsggridview/data/gridview4.qml11
-rw-r--r--tests/auto/declarative/qsggridview/tst_qsggridview.cpp28
4 files changed, 56 insertions, 25 deletions
diff --git a/src/declarative/items/qsggridview.cpp b/src/declarative/items/qsggridview.cpp
index e2fe5cd9c8..4d794aaa78 100644
--- a/src/declarative/items/qsggridview.cpp
+++ b/src/declarative/items/qsggridview.cpp
@@ -93,7 +93,7 @@ public:
qreal colPos() const {
if (view->flow() == QSGGridView::LeftToRight) {
if (view->effectiveLayoutDirection() == Qt::RightToLeft) {
- int colSize = view->cellWidth();
+ qreal colSize = view->cellWidth();
int columns = view->width()/colSize;
return colSize * (columns-1) - item->x();
} else {
@@ -152,8 +152,8 @@ public:
virtual qreal originPosition() const;
virtual qreal lastPosition() const;
- int rowSize() const;
- int colSize() const;
+ qreal rowSize() const;
+ qreal colSize() const;
qreal colPosAt(int modelIndex) const;
qreal rowPosAt(int modelIndex) const;
qreal snapPosAt(qreal pos) const;
@@ -196,8 +196,8 @@ public:
QDeclarativeTimeLineCallback::Callback fixupCallback, qreal velocity);
QSGGridView::Flow flow;
- int cellWidth;
- int cellHeight;
+ qreal cellWidth;
+ qreal cellHeight;
int columns;
QSGGridView::SnapMode snapMode;
@@ -276,10 +276,10 @@ qreal QSGGridViewPrivate::endPositionAt(int index) const
return rowPosAt(index) + rowSize();
}
-int QSGGridViewPrivate::rowSize() const {
+qreal QSGGridViewPrivate::rowSize() const {
return flow == QSGGridView::LeftToRight ? cellHeight : cellWidth;
}
-int QSGGridViewPrivate::colSize() const {
+qreal QSGGridViewPrivate::colSize() const {
return flow == QSGGridView::LeftToRight ? cellWidth : cellHeight;
}
@@ -1374,24 +1374,24 @@ void QSGGridView::setFlow(Flow flow)
/*!
- \qmlproperty int QtQuick2::GridView::cellWidth
- \qmlproperty int QtQuick2::GridView::cellHeight
+ \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.
*/
-int QSGGridView::cellWidth() const
+qreal QSGGridView::cellWidth() const
{
Q_D(const QSGGridView);
return d->cellWidth;
}
-void QSGGridView::setCellWidth(int cellWidth)
+void QSGGridView::setCellWidth(qreal cellWidth)
{
Q_D(QSGGridView);
if (cellWidth != d->cellWidth && cellWidth > 0) {
- d->cellWidth = qMax(1, cellWidth);
+ d->cellWidth = qMax(qreal(1), cellWidth);
d->updateViewport();
emit cellWidthChanged();
d->forceLayout = true;
@@ -1399,17 +1399,17 @@ void QSGGridView::setCellWidth(int cellWidth)
}
}
-int QSGGridView::cellHeight() const
+qreal QSGGridView::cellHeight() const
{
Q_D(const QSGGridView);
return d->cellHeight;
}
-void QSGGridView::setCellHeight(int cellHeight)
+void QSGGridView::setCellHeight(qreal cellHeight)
{
Q_D(QSGGridView);
if (cellHeight != d->cellHeight && cellHeight > 0) {
- d->cellHeight = qMax(1, cellHeight);
+ d->cellHeight = qMax(qreal(1), cellHeight);
d->updateViewport();
emit cellHeightChanged();
d->forceLayout = true;
diff --git a/src/declarative/items/qsggridview_p.h b/src/declarative/items/qsggridview_p.h
index 1cdf81d213..c3b92d0448 100644
--- a/src/declarative/items/qsggridview_p.h
+++ b/src/declarative/items/qsggridview_p.h
@@ -61,8 +61,8 @@ class Q_AUTOTEST_EXPORT QSGGridView : public QSGItemView
Q_DECLARE_PRIVATE(QSGGridView)
Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged)
- Q_PROPERTY(int cellWidth READ cellWidth WRITE setCellWidth NOTIFY cellWidthChanged)
- Q_PROPERTY(int cellHeight READ cellHeight WRITE setCellHeight NOTIFY cellHeightChanged)
+ Q_PROPERTY(qreal cellWidth READ cellWidth WRITE setCellWidth NOTIFY cellWidthChanged)
+ Q_PROPERTY(qreal cellHeight READ cellHeight WRITE setCellHeight NOTIFY cellHeightChanged)
Q_PROPERTY(SnapMode snapMode READ snapMode WRITE setSnapMode NOTIFY snapModeChanged)
@@ -81,11 +81,11 @@ public:
Flow flow() const;
void setFlow(Flow);
- int cellWidth() const;
- void setCellWidth(int);
+ qreal cellWidth() const;
+ void setCellWidth(qreal);
- int cellHeight() const;
- void setCellHeight(int);
+ qreal cellHeight() const;
+ void setCellHeight(qreal);
enum SnapMode { NoSnap, SnapToRow, SnapOneRow };
SnapMode snapMode() const;
diff --git a/tests/auto/declarative/qsggridview/data/gridview4.qml b/tests/auto/declarative/qsggridview/data/gridview4.qml
new file mode 100644
index 0000000000..01b31da082
--- /dev/null
+++ b/tests/auto/declarative/qsggridview/data/gridview4.qml
@@ -0,0 +1,11 @@
+import QtQuick 2.0
+
+GridView {
+ width: 400
+ height: 200
+ cellWidth: width/9
+ cellHeight: height/2
+
+ model: 18
+ delegate: Rectangle { objectName: "delegate"; width: 10; height: 10; color: "green" }
+}
diff --git a/tests/auto/declarative/qsggridview/tst_qsggridview.cpp b/tests/auto/declarative/qsggridview/tst_qsggridview.cpp
index 98953aef40..5d35ffc542 100644
--- a/tests/auto/declarative/qsggridview/tst_qsggridview.cpp
+++ b/tests/auto/declarative/qsggridview/tst_qsggridview.cpp
@@ -109,6 +109,7 @@ private slots:
void onRemove_data();
void testQtQuick11Attributes();
void testQtQuick11Attributes_data();
+ void columnCount();
private:
QSGView *createView();
@@ -1455,8 +1456,8 @@ void tst_QSGGridView::defaultValues()
QTRY_VERIFY(obj->flow() == 0);
QTRY_COMPARE(obj->isWrapEnabled(), false);
QTRY_COMPARE(obj->cacheBuffer(), 0);
- QTRY_COMPARE(obj->cellWidth(), 100); //### Should 100 be the default?
- QTRY_COMPARE(obj->cellHeight(), 100);
+ QTRY_COMPARE(obj->cellWidth(), qreal(100)); //### Should 100 be the default?
+ QTRY_COMPARE(obj->cellHeight(), qreal(100));
delete obj;
}
@@ -1478,8 +1479,8 @@ void tst_QSGGridView::properties()
QTRY_VERIFY(obj->flow() == 0);
QTRY_COMPARE(obj->isWrapEnabled(), true);
QTRY_COMPARE(obj->cacheBuffer(), 200);
- QTRY_COMPARE(obj->cellWidth(), 100);
- QTRY_COMPARE(obj->cellHeight(), 100);
+ QTRY_COMPARE(obj->cellWidth(), qreal(100));
+ QTRY_COMPARE(obj->cellHeight(), qreal(100));
delete obj;
}
@@ -2783,6 +2784,25 @@ void tst_QSGGridView::testQtQuick11Attributes_data()
<< "";
}
+void tst_QSGGridView::columnCount()
+{
+ QSGView canvas;
+ canvas.setSource(QUrl::fromLocalFile(SRCDIR "/data/gridview4.qml"));
+ canvas.show();
+ canvas.requestActivateWindow();
+ QTest::qWaitForWindowShown(&canvas);
+
+ QSGGridView *view = qobject_cast<QSGGridView*>(canvas.rootObject());
+
+ QCOMPARE(view->cellWidth(), qreal(400)/qreal(9));
+ QCOMPARE(view->cellHeight(), qreal(100));
+
+ QList<QSGItem*> items = findItems<QSGItem>(view, "delegate");
+ QCOMPARE(items.size(), 18);
+ QCOMPARE(items.at(8)->y(), qreal(0));
+ QCOMPARE(items.at(9)->y(), qreal(100));
+}
+
QSGView *tst_QSGGridView::createView()
{
QSGView *canvas = new QSGView(0);