From 451f1f33fa65ba213b1585592225c358f124146a Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Thu, 15 Apr 2010 13:34:07 +1000 Subject: Add flow property to Grid, adding TopToBottom mode. Task-number: QTBUG-9183 --- .../graphicsitems/qdeclarativepositioners.cpp | 138 +++++++++++++++------ .../graphicsitems/qdeclarativepositioners_p.h | 17 ++- .../data/grid-toptobottom.qml | 41 ++++++ .../tst_qdeclarativepositioners.cpp | 32 +++++ 4 files changed, 185 insertions(+), 43 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativepositioners/data/grid-toptobottom.qml diff --git a/src/declarative/graphicsitems/qdeclarativepositioners.cpp b/src/declarative/graphicsitems/qdeclarativepositioners.cpp index f436471..21c33e2 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners.cpp +++ b/src/declarative/graphicsitems/qdeclarativepositioners.cpp @@ -656,10 +656,8 @@ Grid { */ QDeclarativeGrid::QDeclarativeGrid(QDeclarativeItem *parent) : - QDeclarativeBasePositioner(Both, parent) + QDeclarativeBasePositioner(Both, parent), m_rows(-1), m_columns(-1), m_flow(LeftToRight) { - _columns=-1; - _rows=-1; } /*! @@ -682,55 +680,101 @@ QDeclarativeGrid::QDeclarativeGrid(QDeclarativeItem *parent) : void QDeclarativeGrid::setColumns(const int columns) { - if (columns == _columns) + if (columns == m_columns) return; - _columns = columns; + m_columns = columns; prePositioning(); emit columnsChanged(); } void QDeclarativeGrid::setRows(const int rows) { - if (rows == _rows) + if (rows == m_rows) return; - _rows = rows; + m_rows = rows; prePositioning(); emit rowsChanged(); } +/*! + \qmlproperty enumeration Flow::flow + This property holds the flow of the layout. + + Possible values are \c LeftToRight (default) and \c TopToBottom. + + If \a flow is \c LeftToRight, the items are positioned next to + to each other from left to right, then wrapped to the next line. + If \a flow is \c TopToBottom, the items are positioned next to each + other from top to bottom, then wrapped to the next column. +*/ +QDeclarativeGrid::Flow QDeclarativeGrid::flow() const +{ + return m_flow; +} + +void QDeclarativeGrid::setFlow(Flow flow) +{ + if (m_flow != flow) { + m_flow = flow; + prePositioning(); + emit flowChanged(); + } +} + void QDeclarativeGrid::doPositioning(QSizeF *contentSize) { - int c = _columns; - int r = _rows; + int c = m_columns; + int r = m_rows; int numVisible = positionedItems.count(); - if (_columns <= 0 && _rows <= 0){ + if (m_columns <= 0 && m_rows <= 0){ c = 4; r = (numVisible+3)/4; - } else if (_rows <= 0){ - r = (numVisible+(_columns-1))/_columns; - } else if (_columns <= 0){ - c = (numVisible+(_rows-1))/_rows; + } else if (m_rows <= 0){ + r = (numVisible+(m_columns-1))/m_columns; + } else if (m_columns <= 0){ + c = (numVisible+(m_rows-1))/m_rows; } QList maxColWidth; QList maxRowHeight; int childIndex =0; - for (int i=0; iwidth() > maxColWidth[j]) - maxColWidth[j] = child.item->width(); - if (child.item->height() > maxRowHeight[i]) - maxRowHeight[i] = child.item->height(); + if (m_flow == LeftToRight) { + for (int i=0; i < r; i++){ + for (int j=0; j < c; j++){ + if (j==0) + maxRowHeight << 0; + if (i==0) + maxColWidth << 0; + + if (childIndex == positionedItems.count()) + continue; + const PositionedItem &child = positionedItems.at(childIndex++); + if (!child.item || isInvisible(child.item)) + continue; + if (child.item->width() > maxColWidth[j]) + maxColWidth[j] = child.item->width(); + if (child.item->height() > maxRowHeight[i]) + maxRowHeight[i] = child.item->height(); + } + } + } else { + for (int j=0; j < c; j++){ + for (int i=0; i < r; i++){ + if (j==0) + maxRowHeight << 0; + if (i==0) + maxColWidth << 0; + + if (childIndex == positionedItems.count()) + continue; + const PositionedItem &child = positionedItems.at(childIndex++); + if (!child.item || isInvisible(child.item)) + continue; + if (child.item->width() > maxColWidth[j]) + maxColWidth[j] = child.item->width(); + if (child.item->height() > maxRowHeight[i]) + maxRowHeight[i] = child.item->height(); + } } } @@ -747,18 +791,34 @@ void QDeclarativeGrid::doPositioning(QSizeF *contentSize) positionY(yoffset, child); } - contentSize->setWidth(qMax(contentSize->width(), xoffset + child.item->width())); - contentSize->setHeight(yoffset + maxRowHeight[curRow]); + if (m_flow == LeftToRight) { + contentSize->setWidth(qMax(contentSize->width(), xoffset + child.item->width())); + contentSize->setHeight(yoffset + maxRowHeight[curRow]); + + xoffset+=maxColWidth[curCol]+spacing(); + curCol++; + curCol%=c; + if (!curCol){ + yoffset+=maxRowHeight[curRow]+spacing(); + xoffset=0; + curRow++; + if (curRow>=r) + break; + } + } else { + contentSize->setHeight(qMax(contentSize->height(), yoffset + child.item->height())); + contentSize->setWidth(xoffset + maxColWidth[curCol]); - xoffset+=maxColWidth[curCol]+spacing(); - curCol++; - curCol%=c; - if (!curCol){ yoffset+=maxRowHeight[curRow]+spacing(); - xoffset=0; curRow++; - if (curRow>=r) - break; + curRow%=r; + if (!curRow){ + xoffset+=maxColWidth[curCol]+spacing(); + yoffset=0; + curCol++; + if (curCol>=c) + break; + } } } } diff --git a/src/declarative/graphicsitems/qdeclarativepositioners_p.h b/src/declarative/graphicsitems/qdeclarativepositioners_p.h index c4414d1..24b65fa 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners_p.h +++ b/src/declarative/graphicsitems/qdeclarativepositioners_p.h @@ -138,25 +138,34 @@ class Q_DECLARATIVE_EXPORT QDeclarativeGrid : public QDeclarativeBasePositioner Q_OBJECT Q_PROPERTY(int rows READ rows WRITE setRows NOTIFY rowChanged) Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged) + Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged) + public: QDeclarativeGrid(QDeclarativeItem *parent=0); - int rows() const {return _rows;} + int rows() const {return m_rows;} void setRows(const int rows); - int columns() const {return _columns;} + int columns() const {return m_columns;} void setColumns(const int columns); + Q_ENUMS(Flow) + enum Flow { LeftToRight, TopToBottom }; + Flow flow() const; + void setFlow(Flow); + Q_SIGNALS: void rowsChanged(); void columnsChanged(); + void flowChanged(); protected: virtual void doPositioning(QSizeF *contentSize); private: - int _rows; - int _columns; + int m_rows; + int m_columns; + Flow m_flow; Q_DISABLE_COPY(QDeclarativeGrid) }; diff --git a/tests/auto/declarative/qdeclarativepositioners/data/grid-toptobottom.qml b/tests/auto/declarative/qdeclarativepositioners/data/grid-toptobottom.qml new file mode 100644 index 0000000..34a84bf --- /dev/null +++ b/tests/auto/declarative/qdeclarativepositioners/data/grid-toptobottom.qml @@ -0,0 +1,41 @@ +import Qt 4.6 + +Item { + width: 640 + height: 480 + Grid { + objectName: "grid" + rows: 3 + flow: Grid.TopToBottom + 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 + } + } +} diff --git a/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp b/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp index 8692596..b4ac0e1 100644 --- a/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp +++ b/tests/auto/declarative/qdeclarativepositioners/tst_qdeclarativepositioners.cpp @@ -61,6 +61,7 @@ private slots: void test_vertical_spacing(); void test_vertical_animated(); void test_grid(); + void test_grid_topToBottom(); void test_grid_spacing(); void test_grid_animated(); void test_grid_zero_columns(); @@ -305,6 +306,37 @@ void tst_QDeclarativePositioners::test_grid() QCOMPARE(grid->height(), 100.0); } +void tst_QDeclarativePositioners::test_grid_topToBottom() +{ + QDeclarativeView *canvas = createView(SRCDIR "/data/grid-toptobottom.qml"); + + QDeclarativeRectangle *one = canvas->rootObject()->findChild("one"); + QVERIFY(one != 0); + QDeclarativeRectangle *two = canvas->rootObject()->findChild("two"); + QVERIFY(two != 0); + QDeclarativeRectangle *three = canvas->rootObject()->findChild("three"); + QVERIFY(three != 0); + QDeclarativeRectangle *four = canvas->rootObject()->findChild("four"); + QVERIFY(four != 0); + QDeclarativeRectangle *five = canvas->rootObject()->findChild("five"); + QVERIFY(five != 0); + + QCOMPARE(one->x(), 0.0); + QCOMPARE(one->y(), 0.0); + QCOMPARE(two->x(), 0.0); + QCOMPARE(two->y(), 50.0); + QCOMPARE(three->x(), 0.0); + QCOMPARE(three->y(), 100.0); + QCOMPARE(four->x(), 50.0); + QCOMPARE(four->y(), 0.0); + QCOMPARE(five->x(), 50.0); + QCOMPARE(five->y(), 50.0); + + QDeclarativeItem *grid = canvas->rootObject()->findChild("grid"); + QCOMPARE(grid->width(), 100.0); + QCOMPARE(grid->height(), 120.0); +} + void tst_QDeclarativePositioners::test_grid_spacing() { QDeclarativeView *canvas = createView(SRCDIR "/data/grid-spacing.qml"); -- cgit v0.12