Add support for individual row and column spacing to Grid object.
authorGlenn Watson <glenn.watson@nokia.com>
Wed, 10 Aug 2011 03:51:32 +0000 (13:51 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 10 Aug 2011 05:34:18 +0000 (07:34 +0200)
Add rowSpacing and columnSpacing properties to QML Grid object. If
these are specified, use them when laying out the grid. If they are
not specified, default to using the spacing property that was
previously used, so it is backwards compatible.

Task-number: QTBUG-11876
Change-Id: I5c809e89fe124d8d5ea1667b273f19e2c37ff18a
Reviewed-on: http://codereview.qt.nokia.com/2797
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Bea Lam <bea.lam@nokia.com>
doc/src/declarative/whatsnew.qdoc
src/declarative/items/qsgpositioners.cpp
src/declarative/items/qsgpositioners_p.h
tests/auto/declarative/qsgpositioners/data/grid-row-column-spacing.qml [new file with mode: 0644]
tests/auto/declarative/qsgpositioners/tst_qsgpositioners.cpp

index d3db134..3707d41 100644 (file)
@@ -52,6 +52,8 @@ The Loader element now only emits the \c sourceChanged signal when the source is
 \c sourceComponentChanged signal when the sourceComponent is changed. It used to emit both signals when one
 of the properties was changed.
 
+Grid now has rowSpacing and columnSpacing properties.
+
 \section2 QtQuick 1 is now a separate library and module
 
 Writing C++ applications using QtQuick 1 specific API, i.e. QDeclarativeView or QDeclarativeItem
index 5ebf2d7..d500afd 100644 (file)
@@ -780,7 +780,7 @@ void QSGRow::reportConflictingAnchors()
   \sa rows, columns
 */
 QSGGrid::QSGGrid(QSGItem *parent) :
-    QSGBasePositioner(Both, parent), m_rows(-1), m_columns(-1), m_flow(LeftToRight)
+    QSGBasePositioner(Both, parent), m_rows(-1), m_columns(-1), m_rowSpacing(-1), m_columnSpacing(-1), m_flow(LeftToRight)
 {
 }
 
@@ -848,6 +848,40 @@ void QSGGrid::setFlow(Flow flow)
 }
 
 /*!
+    \qmlproperty int QtQuick2::Grid::rowSpacing
+
+    This property holds the spacing in pixels between rows.
+
+    \sa columnSpacing
+    \since QtQuick2.0
+*/
+void QSGGrid::setRowSpacing(const int rowSpacing)
+{
+    if (rowSpacing == m_rowSpacing)
+        return;
+    m_rowSpacing = rowSpacing;
+    prePositioning();
+    emit rowSpacingChanged();
+}
+
+/*!
+    \qmlproperty int QtQuick2::Grid::columnSpacing
+
+    This property holds the spacing in pixels between columns.
+
+    \sa rowSpacing
+    \since QtQuick2.0
+*/
+void QSGGrid::setColumnSpacing(const int columnSpacing)
+{
+    if (columnSpacing == m_columnSpacing)
+        return;
+    m_columnSpacing = columnSpacing;
+    prePositioning();
+    emit columnSpacingChanged();
+}
+
+/*!
     \qmlproperty enumeration QtQuick2::Grid::layoutDirection
 
     This property holds the layout direction of the layout.
@@ -967,17 +1001,25 @@ void QSGGrid::doPositioning(QSizeF *contentSize)
         }
     }
 
+    int columnSpacing = m_columnSpacing;
+    if (columnSpacing == -1)
+        columnSpacing = spacing();
+
+    int rowSpacing = m_rowSpacing;
+    if (rowSpacing == -1)
+        rowSpacing = spacing();
+
     int widthSum = 0;
     for (int j=0; j < maxColWidth.size(); j++){
         if (j)
-            widthSum += spacing();
+            widthSum += columnSpacing;
         widthSum += maxColWidth[j];
     }
 
     int heightSum = 0;
     for (int i=0; i < maxRowHeight.size(); i++){
         if (i)
-            heightSum += spacing();
+            heightSum += rowSpacing;
         heightSum += maxRowHeight[i];
     }
 
@@ -1008,13 +1050,13 @@ void QSGGrid::doPositioning(QSizeF *contentSize)
 
         if (m_flow == LeftToRight) {
             if (d->isLeftToRight())
-                xoffset += maxColWidth[curCol]+spacing();
+                xoffset += maxColWidth[curCol]+columnSpacing;
             else
-                xoffset -= maxColWidth[curCol]+spacing();
+                xoffset -= maxColWidth[curCol]+columnSpacing;
             curCol++;
             curCol%=c;
             if (!curCol){
-                yoffset += maxRowHeight[curRow]+spacing();
+                yoffset += maxRowHeight[curRow]+rowSpacing;
                 if (d->isLeftToRight())
                     xoffset = 0;
                 else
@@ -1024,14 +1066,14 @@ void QSGGrid::doPositioning(QSizeF *contentSize)
                     break;
             }
         } else {
-            yoffset+=maxRowHeight[curRow]+spacing();
+            yoffset+=maxRowHeight[curRow]+rowSpacing;
             curRow++;
             curRow%=r;
             if (!curRow){
                 if (d->isLeftToRight())
-                    xoffset += maxColWidth[curCol]+spacing();
+                    xoffset += maxColWidth[curCol]+columnSpacing;
                 else
-                    xoffset -= maxColWidth[curCol]+spacing();
+                    xoffset -= maxColWidth[curCol]+columnSpacing;
                 yoffset=0;
                 curCol++;
                 if (curCol>=c)
index a23c9d4..8f9d3eb 100644 (file)
@@ -155,6 +155,8 @@ class Q_AUTOTEST_EXPORT QSGGrid : public QSGBasePositioner
     Q_OBJECT
     Q_PROPERTY(int rows READ rows WRITE setRows NOTIFY rowsChanged)
     Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged)
+    Q_PROPERTY(int rowSpacing READ rowSpacing WRITE setRowSpacing NOTIFY rowSpacingChanged)
+    Q_PROPERTY(int columnSpacing READ columnSpacing WRITE setColumnSpacing NOTIFY columnSpacingChanged)
     Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged)
     Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged)
     Q_PROPERTY(Qt::LayoutDirection effectiveLayoutDirection READ effectiveLayoutDirection NOTIFY effectiveLayoutDirectionChanged)
@@ -168,6 +170,12 @@ public:
     int columns() const {return m_columns;}
     void setColumns(const int columns);
 
+    int rowSpacing() const { return m_rowSpacing; }
+    void setRowSpacing(int);
+
+    int columnSpacing() const { return m_columnSpacing; }
+    void setColumnSpacing(int);
+
     Q_ENUMS(Flow)
     enum Flow { LeftToRight, TopToBottom };
     Flow flow() const;
@@ -183,6 +191,8 @@ Q_SIGNALS:
     void flowChanged();
     void layoutDirectionChanged();
     void effectiveLayoutDirectionChanged();
+    void rowSpacingChanged();
+    void columnSpacingChanged();
 
 protected:
     virtual void doPositioning(QSizeF *contentSize);
@@ -191,6 +201,8 @@ protected:
 private:
     int m_rows;
     int m_columns;
+    int m_rowSpacing;
+    int m_columnSpacing;
     Flow m_flow;
     Q_DISABLE_COPY(QSGGrid)
 };
diff --git a/tests/auto/declarative/qsgpositioners/data/grid-row-column-spacing.qml b/tests/auto/declarative/qsgpositioners/data/grid-row-column-spacing.qml
new file mode 100644 (file)
index 0000000..49bbd33
--- /dev/null
@@ -0,0 +1,43 @@
+import QtQuick 2.0
+
+Item {
+    width: 640
+    height: 480
+    Grid {
+        objectName: "grid"
+        columns: 3
+        spacing: 4
+        rowSpacing: 7
+        columnSpacing: 11
+        Rectangle {
+            objectName: "one"
+            color: "red"
+            width: 50
+            height: 50
+        }
+        Rectangle {
+            objectName: "two"
+            color: "green"
+            width: 20
+            height: 50
+        }
+        Rectangle {
+            objectName: "three"
+            color: "blue"
+            width: 50
+            height: 20
+        }
+        Rectangle {
+            objectName: "four"
+            color: "cyan"
+            width: 50
+            height: 50
+        }
+        Rectangle {
+            objectName: "five"
+            color: "magenta"
+            width: 10
+            height: 10
+        }
+    }
+}
index 3b2a504..497f511 100644 (file)
@@ -76,6 +76,7 @@ private slots:
     void test_grid_topToBottom();
     void test_grid_rightToLeft();
     void test_grid_spacing();
+    void test_grid_row_column_spacing();
     void test_grid_animated();
     void test_grid_animated_rightToLeft();
     void test_grid_zero_columns();
@@ -622,6 +623,39 @@ void tst_qsgpositioners::test_grid_spacing()
     delete canvas;
 }
 
+void tst_qsgpositioners::test_grid_row_column_spacing()
+{
+    QSGView *canvas = createView(SRCDIR "/data/grid-row-column-spacing.qml");
+
+    QSGRectangle *one = canvas->rootObject()->findChild<QSGRectangle*>("one");
+    QVERIFY(one != 0);
+    QSGRectangle *two = canvas->rootObject()->findChild<QSGRectangle*>("two");
+    QVERIFY(two != 0);
+    QSGRectangle *three = canvas->rootObject()->findChild<QSGRectangle*>("three");
+    QVERIFY(three != 0);
+    QSGRectangle *four = canvas->rootObject()->findChild<QSGRectangle*>("four");
+    QVERIFY(four != 0);
+    QSGRectangle *five = canvas->rootObject()->findChild<QSGRectangle*>("five");
+    QVERIFY(five != 0);
+
+    QCOMPARE(one->x(), 0.0);
+    QCOMPARE(one->y(), 0.0);
+    QCOMPARE(two->x(), 61.0);
+    QCOMPARE(two->y(), 0.0);
+    QCOMPARE(three->x(), 92.0);
+    QCOMPARE(three->y(), 0.0);
+    QCOMPARE(four->x(), 0.0);
+    QCOMPARE(four->y(), 57.0);
+    QCOMPARE(five->x(), 61.0);
+    QCOMPARE(five->y(), 57.0);
+
+    QSGItem *grid = canvas->rootObject()->findChild<QSGItem*>("grid");
+    QCOMPARE(grid->width(), 142.0);
+    QCOMPARE(grid->height(), 107.0);
+
+    delete canvas;
+}
+
 void tst_qsgpositioners::test_grid_animated()
 {
     QSGView *canvas = createView(SRCDIR "/data/grid-animated.qml");