From 183abff6ffcb020b8f0c4041cc82bba0d3b03863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Arve=20S=C3=A6ther?= Date: Wed, 16 Jun 2010 10:05:44 +0200 Subject: Fixed problem with wrong size hints when items were removed. The reason was that the row/column count was not updated after an item was removed. (Note that qgraphicslinearlayout already did this, so we just follow the same pattern, except that the code for QGraphicsGridLayout is a bit more complex... Task-number: QTBUG-10314 Reviewed-by: Alexis Menard --- src/gui/graphicsview/qgraphicsgridlayout.cpp | 12 +++ src/gui/graphicsview/qgraphicslinearlayout.cpp | 2 +- src/gui/graphicsview/qgridlayoutengine_p.h | 4 +- .../tst_qgraphicsgridlayout.cpp | 86 ++++++++++++++++------ 4 files changed, 79 insertions(+), 25 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsgridlayout.cpp b/src/gui/graphicsview/qgraphicsgridlayout.cpp index 6ca799d..83db3ec 100644 --- a/src/gui/graphicsview/qgraphicsgridlayout.cpp +++ b/src/gui/graphicsview/qgraphicsgridlayout.cpp @@ -572,6 +572,18 @@ void QGraphicsGridLayout::removeAt(int index) if (QGraphicsLayoutItem *layoutItem = gridItem->layoutItem()) layoutItem->setParentLayoutItem(0); d->engine.removeItem(gridItem); + + // recalculate rowInfo.count if we remove an item that is on the right/bottommost row + for (int j = 0; j < NOrientations; ++j) { + // 0: Hor, 1: Ver + const Qt::Orientation orient = (j == 0 ? Qt::Horizontal : Qt::Vertical); + const int oldCount = d->engine.rowCount(orient); + if (gridItem->lastRow(orient) == oldCount - 1) { + const int newCount = d->engine.effectiveLastRow(orient) + 1; + d->engine.removeRows(newCount, oldCount - newCount, orient); + } + } + delete gridItem; invalidate(); } diff --git a/src/gui/graphicsview/qgraphicslinearlayout.cpp b/src/gui/graphicsview/qgraphicslinearlayout.cpp index 9722683..b828722 100644 --- a/src/gui/graphicsview/qgraphicslinearlayout.cpp +++ b/src/gui/graphicsview/qgraphicslinearlayout.cpp @@ -147,7 +147,7 @@ void QGraphicsLinearLayoutPrivate::removeGridItem(QGridLayoutItem *gridItem) { int index = gridItem->firstRow(orientation); engine.removeItem(gridItem); - engine.removeRow(index, orientation); + engine.removeRows(index, 1, orientation); } void QGraphicsLinearLayoutPrivate::fixIndex(int *index) const diff --git a/src/gui/graphicsview/qgridlayoutengine_p.h b/src/gui/graphicsview/qgridlayoutengine_p.h index cbf704e..9ac9a8e 100644 --- a/src/gui/graphicsview/qgridlayoutengine_p.h +++ b/src/gui/graphicsview/qgridlayoutengine_p.h @@ -363,8 +363,8 @@ public: QGridLayoutItem *itemAt(int row, int column, Qt::Orientation orientation = Qt::Vertical) const; inline void insertRow(int row, Qt::Orientation orientation = Qt::Vertical) { insertOrRemoveRows(row, +1, orientation); } - inline void removeRow(int row, Qt::Orientation orientation = Qt::Vertical) - { insertOrRemoveRows(row, -1, orientation); } + inline void removeRows(int row, int count, Qt::Orientation orientation) + { insertOrRemoveRows(row, -count, orientation); } void invalidate(); void setGeometries(const QLayoutStyleInfo &styleInfo, const QRectF &contentsGeometry); diff --git a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp index 8127f84..b9a5c66 100644 --- a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp +++ b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp @@ -741,31 +741,73 @@ void tst_QGraphicsGridLayout::setColumnFixedWidth() // public qreal columnSpacing(int column) const void tst_QGraphicsGridLayout::columnSpacing() { - QGraphicsScene scene; - QGraphicsView view(&scene); - QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); - QGraphicsGridLayout *layout = new QGraphicsGridLayout(); - scene.addItem(widget); - widget->setLayout(layout); - populateLayout(layout, 3, 2); - layout->setContentsMargins(0, 0, 0, 0); - layout->setSpacing(0); - QCOMPARE(layout->columnSpacing(0), 0.0); + { + QGraphicsScene scene; + QGraphicsView view(&scene); + QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); + QGraphicsGridLayout *layout = new QGraphicsGridLayout(); + scene.addItem(widget); + widget->setLayout(layout); + populateLayout(layout, 3, 2); + layout->setContentsMargins(0, 0, 0, 0); + layout->setSpacing(0); + QCOMPARE(layout->columnSpacing(0), 0.0); + + layout->setColumnSpacing(0, 20); + view.show(); + widget->show(); + widget->resize(widget->effectiveSizeHint(Qt::PreferredSize)); + QApplication::processEvents(); - layout->setColumnSpacing(0, 20); - view.show(); - widget->show(); - widget->resize(widget->effectiveSizeHint(Qt::PreferredSize)); - QApplication::processEvents(); + QCOMPARE(layout->itemAt(0,0)->geometry().left(), 0.0); + QCOMPARE(layout->itemAt(0,0)->geometry().right(), 25.0); + QCOMPARE(layout->itemAt(0,1)->geometry().left(), 45.0); + QCOMPARE(layout->itemAt(0,1)->geometry().right(), 70.0); + QCOMPARE(layout->itemAt(0,2)->geometry().left(), 70.0); + QCOMPARE(layout->itemAt(0,2)->geometry().right(), 95.0); - QCOMPARE(layout->itemAt(0,0)->geometry().left(), 0.0); - QCOMPARE(layout->itemAt(0,0)->geometry().right(), 25.0); - QCOMPARE(layout->itemAt(0,1)->geometry().left(), 45.0); - QCOMPARE(layout->itemAt(0,1)->geometry().right(), 70.0); - QCOMPARE(layout->itemAt(0,2)->geometry().left(), 70.0); - QCOMPARE(layout->itemAt(0,2)->geometry().right(), 95.0); + delete widget; + } + + { + // don't include items and spacings that was previously part of the layout + // (horizontal) + QGraphicsGridLayout *layout = new QGraphicsGridLayout; + populateLayout(layout, 3, 1); + layout->setContentsMargins(0, 0, 0, 0); + layout->setSpacing(0); + layout->setColumnSpacing(0, 10); + layout->setColumnSpacing(1, 10); + layout->setColumnSpacing(2, 10); + layout->setColumnSpacing(3, 10); + QCOMPARE(layout->preferredSize(), QSizeF(95, 25)); + layout->removeAt(2); + QCOMPARE(layout->preferredSize(), QSizeF(60, 25)); + layout->removeAt(1); + QCOMPARE(layout->preferredSize(), QSizeF(25, 25)); + delete layout; + } + { + // don't include items and spacings that was previously part of the layout + // (vertical) + QGraphicsGridLayout *layout = new QGraphicsGridLayout; + populateLayout(layout, 2, 2); + layout->setContentsMargins(0, 0, 0, 0); + layout->setSpacing(0); + layout->setColumnSpacing(0, 10); + layout->setColumnSpacing(1, 10); + layout->setRowSpacing(0, 10); + layout->setRowSpacing(1, 10); + QCOMPARE(layout->preferredSize(), QSizeF(60, 60)); + layout->removeAt(3); + QCOMPARE(layout->preferredSize(), QSizeF(60, 60)); + layout->removeAt(2); + QCOMPARE(layout->preferredSize(), QSizeF(60, 25)); + layout->removeAt(1); + QCOMPARE(layout->preferredSize(), QSizeF(25, 25)); + delete layout; + } - delete widget; } // public int columnStretchFactor(int column) const -- cgit v0.12