From 43a9da5339b38ca2b4e507efe2d4fa72df6b2ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Arve=20S=C3=A6ther?= Date: Tue, 5 Jan 2010 14:17:05 +0100 Subject: Fixed a bug with distribution of spans. If a span required more size than the rows/columns it spanned, the size of the span was not distributed to the rows it spanned. The result was that the size hints of the layout was not correct, causing the layout to be potentially smaller than the spanning item. Task: QT-2261 Reviewed-by: Alexis --- src/gui/graphicsview/qgridlayoutengine.cpp | 4 +- .../tst_qgraphicsgridlayout.cpp | 382 ++++++++++++--------- 2 files changed, 224 insertions(+), 162 deletions(-) diff --git a/src/gui/graphicsview/qgridlayoutengine.cpp b/src/gui/graphicsview/qgridlayoutengine.cpp index 9497a2f..1fece7a 100644 --- a/src/gui/graphicsview/qgridlayoutengine.cpp +++ b/src/gui/graphicsview/qgridlayoutengine.cpp @@ -182,9 +182,9 @@ void QGridLayoutRowData::distributeMultiCells() QVarLengthArray newSizes(span); for (int j = 0; j < NSizes; ++j) { - qreal extra = compare(totalBox, box, j); + qreal extra = compare(box, totalBox, j); if (extra > 0.0) { - calculateGeometries(start, end, totalBox.q_sizes(j), dummy.data(), newSizes.data(), + calculateGeometries(start, end, box.q_sizes(j), dummy.data(), newSizes.data(), 0, totalBox); for (int k = 0; k < span; ++k) diff --git a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp index cd1eedd..3d95f92 100644 --- a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp +++ b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp @@ -108,6 +108,157 @@ private slots: void task236367_maxSizeHint(); }; +class RectWidget : public QGraphicsWidget +{ +public: + RectWidget(QGraphicsItem *parent = 0) : QGraphicsWidget(parent){} + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) + { + Q_UNUSED(option); + Q_UNUSED(widget); + painter->drawRoundRect(rect()); + painter->drawLine(rect().topLeft(), rect().bottomRight()); + painter->drawLine(rect().bottomLeft(), rect().topRight()); + } + + QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const + { + if (m_sizeHints[which].isValid()) { + return m_sizeHints[which]; + } + return QGraphicsWidget::sizeHint(which, constraint); + } + + void setSizeHint(Qt::SizeHint which, const QSizeF &size) { + m_sizeHints[which] = size; + updateGeometry(); + } + + QSizeF m_sizeHints[Qt::NSizeHints]; +}; + +struct ItemDesc +{ + ItemDesc(int row, int col) + : m_pos(qMakePair(row, col)), + m_rowSpan(1), + m_colSpan(1), + m_sizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred)), + m_align(0) + { + } + + ItemDesc &rowSpan(int span) { + m_rowSpan = span; + return (*this); + } + + ItemDesc &colSpan(int span) { + m_colSpan = span; + return (*this); + } + + ItemDesc &sizePolicy(const QSizePolicy &sp) { + m_sizePolicy = sp; + return (*this); + } + + ItemDesc &sizePolicy(QSizePolicy::Policy horAndVer) { + m_sizePolicy = QSizePolicy(horAndVer, horAndVer); + return (*this); + } + + ItemDesc &sizePolicyH(QSizePolicy::Policy hor) { + m_sizePolicy.setHorizontalPolicy(hor); + return (*this); + } + + ItemDesc &sizePolicyV(QSizePolicy::Policy ver) { + m_sizePolicy.setVerticalPolicy(ver); + return (*this); + } + + ItemDesc &sizePolicy(QSizePolicy::Policy hor, QSizePolicy::Policy ver) { + m_sizePolicy = QSizePolicy(hor, ver); + return (*this); + } + + ItemDesc &sizeHint(Qt::SizeHint which, const QSizeF &sh) { + m_sizeHints[which] = sh; + return (*this); + } + + ItemDesc &preferredSizeHint(const QSizeF &sh) { + m_sizeHints[Qt::PreferredSize] = sh; + return (*this); + } + + ItemDesc &minSize(const QSizeF &sz) { + m_sizes[Qt::MinimumSize] = sz; + return (*this); + } + ItemDesc &preferredSize(const QSizeF &sz) { + m_sizes[Qt::PreferredSize] = sz; + return (*this); + } + ItemDesc &maxSize(const QSizeF &sz) { + m_sizes[Qt::MaximumSize] = sz; + return (*this); + } + + ItemDesc &alignment(Qt::Alignment alignment) { + m_align = alignment; + return (*this); + } + + void apply(QGraphicsGridLayout *layout, QGraphicsWidget *item) { + item->setSizePolicy(m_sizePolicy); + for (int i = 0; i < Qt::NSizeHints; ++i) { + if (!m_sizes[i].isValid()) + continue; + switch ((Qt::SizeHint)i) { + case Qt::MinimumSize: + item->setMinimumSize(m_sizes[i]); + break; + case Qt::PreferredSize: + item->setPreferredSize(m_sizes[i]); + break; + case Qt::MaximumSize: + item->setMaximumSize(m_sizes[i]); + break; + default: + qWarning("not implemented"); + break; + } + } + layout->addItem(item, m_pos.first, m_pos.second, m_rowSpan, m_colSpan); + layout->setAlignment(item, m_align); + } + + void apply(QGraphicsGridLayout *layout, RectWidget *item) { + for (int i = 0; i < Qt::NSizeHints; ++i) + item->setSizeHint((Qt::SizeHint)i, m_sizeHints[i]); + apply(layout, static_cast(item)); + } + +//private: + QPair m_pos; // row,col + int m_rowSpan; + int m_colSpan; + QSizePolicy m_sizePolicy; + QSizeF m_sizeHints[Qt::NSizeHints]; + QSizeF m_sizes[Qt::NSizeHints]; + Qt::Alignment m_align; +}; + +typedef QList ItemList; +Q_DECLARE_METATYPE(ItemList); + +typedef QList SizeList; +Q_DECLARE_METATYPE(SizeList); + + // This will be called before the first test function is executed. // It is only called once. void tst_QGraphicsGridLayout::initTestCase() @@ -190,36 +341,6 @@ void tst_QGraphicsGridLayout::qgraphicsgridlayout() layout.verticalSpacing(); } -class RectWidget : public QGraphicsWidget -{ -public: - RectWidget(QGraphicsItem *parent = 0) : QGraphicsWidget(parent){} - - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) - { - Q_UNUSED(option); - Q_UNUSED(widget); - painter->drawRoundRect(rect()); - painter->drawLine(rect().topLeft(), rect().bottomRight()); - painter->drawLine(rect().bottomLeft(), rect().topRight()); - } - - QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const - { - if (m_sizeHints[which].isValid()) { - return m_sizeHints[which]; - } - return QGraphicsWidget::sizeHint(which, constraint); - } - - void setSizeHint(Qt::SizeHint which, const QSizeF &size) { - m_sizeHints[which] = size; - updateGeometry(); - } - - QSizeF m_sizeHints[Qt::NSizeHints]; -}; - static void populateLayout(QGraphicsGridLayout *gridLayout, int width, int height) { for (int y = 0; y < height; ++y) { @@ -1144,7 +1265,7 @@ void tst_QGraphicsGridLayout::setColumnSpacing() } void tst_QGraphicsGridLayout::setGeometry_data() -{ +{ QTest::addColumn("rect"); QTest::newRow("null") << QRectF(); QTest::newRow("normal") << QRectF(0,0, 50, 50); @@ -1233,28 +1354,84 @@ void tst_QGraphicsGridLayout::setSpacing() void tst_QGraphicsGridLayout::sizeHint_data() { + QTest::addColumn("itemDescriptions"); + QTest::addColumn("expectedMinimumSizeHint"); + QTest::addColumn("expectedPreferredSizeHint"); + QTest::addColumn("expectedMaximumSizeHint"); + + QTest::newRow("rowSpan_larger_than_rows") << (ItemList() + << ItemDesc(0,0) + .minSize(QSizeF(50,300)) + .maxSize(QSizeF(50,300)) + .rowSpan(2) + << ItemDesc(0,1) + .minSize(QSizeF(50,0)) + .preferredSize(QSizeF(50,50)) + .maxSize(QSize(50, 1000)) + << ItemDesc(1,1) + .minSize(QSizeF(50,0)) + .preferredSize(QSizeF(50,50)) + .maxSize(QSize(50, 1000)) + ) + << QSizeF(100, 300) + << QSizeF(100, 300) + << QSizeF(100, 2000); + + QTest::newRow("rowSpan_smaller_than_rows") << (ItemList() + << ItemDesc(0,0) + .minSize(QSizeF(50, 0)) + .preferredSize(QSizeF(50, 50)) + .maxSize(QSizeF(50, 300)) + .rowSpan(2) + << ItemDesc(0,1) + .minSize(QSizeF(50, 50)) + .preferredSize(QSizeF(50, 50)) + .maxSize(QSize(50, 50)) + << ItemDesc(1,1) + .minSize(QSizeF(50, 50)) + .preferredSize(QSizeF(50, 50)) + .maxSize(QSize(50, 50)) + ) + << QSizeF(100, 100) + << QSizeF(100, 100) + << QSizeF(100, 100); - /* - QTest::addColumn("which"); - QTest::addColumn("constraint"); - QTest::addColumn("sizeHint"); - QTest::newRow("null") << 0; - */ } // public QSizeF sizeHint(Qt::SizeHint which, QSizeF const& constraint = QSizeF()) const void tst_QGraphicsGridLayout::sizeHint() { - /* - QFETCH(Qt::SizeHint, which); - QFETCH(QSizeF, constraint); - QFETCH(QSizeF, sizeHint); + QFETCH(ItemList, itemDescriptions); + QFETCH(QSizeF, expectedMinimumSizeHint); + QFETCH(QSizeF, expectedPreferredSizeHint); + QFETCH(QSizeF, expectedMaximumSizeHint); - QGraphicsGridLayout layout; + QGraphicsScene scene; + QGraphicsView view(&scene); + QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); + QGraphicsGridLayout *layout = new QGraphicsGridLayout; + scene.addItem(widget); + widget->setLayout(layout); + layout->setContentsMargins(0, 0, 0, 0); + layout->setSpacing(0.0); + widget->setContentsMargins(0, 0, 0, 0); + + int i; + for (i = 0; i < itemDescriptions.count(); ++i) { + ItemDesc desc = itemDescriptions.at(i); + RectWidget *item = new RectWidget(widget); + desc.apply(layout, item); + } + + QApplication::sendPostedEvents(0, 0); + + widget->show(); + view.show(); + view.resize(400,300); + QCOMPARE(layout->sizeHint(Qt::MinimumSize), expectedMinimumSizeHint); + QCOMPARE(layout->sizeHint(Qt::PreferredSize), expectedPreferredSizeHint); + QCOMPARE(layout->sizeHint(Qt::MaximumSize), expectedMaximumSizeHint); - layout.sizeHint(); - */ - QSKIP("Test unimplemented", SkipSingle); } void tst_QGraphicsGridLayout::verticalSpacing_data() @@ -1373,121 +1550,6 @@ void tst_QGraphicsGridLayout::removeLayout() QCOMPARE(pushButton->geometry(), r2); } -struct ItemDesc -{ - ItemDesc(int row, int col) - : m_pos(qMakePair(row, col)), - m_rowSpan(1), - m_colSpan(1), - m_sizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred)), - m_align(0) - { - } - - ItemDesc &rowSpan(int span) { - m_rowSpan = span; - return (*this); - } - - ItemDesc &colSpan(int span) { - m_colSpan = span; - return (*this); - } - - ItemDesc &sizePolicy(const QSizePolicy &sp) { - m_sizePolicy = sp; - return (*this); - } - - ItemDesc &sizePolicy(QSizePolicy::Policy horAndVer) { - m_sizePolicy = QSizePolicy(horAndVer, horAndVer); - return (*this); - } - - ItemDesc &sizePolicyH(QSizePolicy::Policy hor) { - m_sizePolicy.setHorizontalPolicy(hor); - return (*this); - } - - ItemDesc &sizePolicyV(QSizePolicy::Policy ver) { - m_sizePolicy.setVerticalPolicy(ver); - return (*this); - } - - ItemDesc &sizePolicy(QSizePolicy::Policy hor, QSizePolicy::Policy ver) { - m_sizePolicy = QSizePolicy(hor, ver); - return (*this); - } - - ItemDesc &sizeHint(Qt::SizeHint which, const QSizeF &sh) { - m_sizeHints[which] = sh; - return (*this); - } - - ItemDesc &preferredSizeHint(const QSizeF &sh) { - m_sizeHints[Qt::PreferredSize] = sh; - return (*this); - } - - ItemDesc &minSize(const QSizeF &sz) { - m_sizes[Qt::MinimumSize] = sz; - return (*this); - } - ItemDesc &preferredSize(const QSizeF &sz) { - m_sizes[Qt::PreferredSize] = sz; - return (*this); - } - ItemDesc &maxSize(const QSizeF &sz) { - m_sizes[Qt::MaximumSize] = sz; - return (*this); - } - - ItemDesc &alignment(Qt::Alignment alignment) { - m_align = alignment; - return (*this); - } - - void apply(QGraphicsGridLayout *layout, RectWidget *item) { - item->setSizePolicy(m_sizePolicy); - for (int i = 0; i < Qt::NSizeHints; ++i) { - item->setSizeHint((Qt::SizeHint)i, m_sizeHints[i]); - if (!m_sizes[i].isValid()) - continue; - switch ((Qt::SizeHint)i) { - case Qt::MinimumSize: - item->setMinimumSize(m_sizes[i]); - break; - case Qt::PreferredSize: - item->setPreferredSize(m_sizes[i]); - break; - case Qt::MaximumSize: - item->setMaximumSize(m_sizes[i]); - break; - default: - qWarning("not implemented"); - break; - } - } - layout->addItem(item, m_pos.first, m_pos.second, m_rowSpan, m_colSpan); - layout->setAlignment(item, m_align); - } - -//private: - QPair m_pos; // row,col - int m_rowSpan; - int m_colSpan; - QSizePolicy m_sizePolicy; - QSizeF m_sizeHints[Qt::NSizeHints]; - QSizeF m_sizes[Qt::NSizeHints]; - Qt::Alignment m_align; -}; - -typedef QList ItemList; -Q_DECLARE_METATYPE(ItemList); - -typedef QList SizeList; -Q_DECLARE_METATYPE(SizeList); - void tst_QGraphicsGridLayout::defaultStretchFactors_data() { QTest::addColumn("itemDescriptions"); -- cgit v0.12