Make cellWidth/cellHeight reals rather than ints.
authorMichael Brasser <michael.brasser@nokia.com>
Mon, 19 Sep 2011 03:50:03 +0000 (13:50 +1000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 19 Sep 2011 04:24:47 +0000 (06:24 +0200)
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>

src/declarative/items/qsggridview.cpp
src/declarative/items/qsggridview_p.h
tests/auto/declarative/qsggridview/data/gridview4.qml [new file with mode: 0644]
tests/auto/declarative/qsggridview/tst_qsggridview.cpp

index e2fe5cd..4d794aa 100644 (file)
@@ -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;
index 1cdf81d..c3b92d0 100644 (file)
@@ -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 (file)
index 0000000..01b31da
--- /dev/null
@@ -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" }
+}
index 98953ae..5d35ffc 100644 (file)
@@ -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);