From ae1a4a918137e99417e70a4f2c2c43f14406f993 Mon Sep 17 00:00:00 2001 From: Carolina Gomes Date: Wed, 11 Aug 2010 11:11:41 +0200 Subject: Patch to QTBUG-3078 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Merge-request: 723 Reviewed-by: Jan-Arve Sæther --- src/gui/graphicsview/qgraphicsgridlayout.cpp | 12 ++++++++++++ src/gui/graphicsview/qgraphicsgridlayout.h | 1 + src/gui/graphicsview/qgridlayoutengine.cpp | 9 +++++++++ src/gui/graphicsview/qgridlayoutengine_p.h | 1 + .../qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp | 18 ++++++++++++++++++ 5 files changed, 41 insertions(+) diff --git a/src/gui/graphicsview/qgraphicsgridlayout.cpp b/src/gui/graphicsview/qgraphicsgridlayout.cpp index 062b5ac..2660b06 100644 --- a/src/gui/graphicsview/qgraphicsgridlayout.cpp +++ b/src/gui/graphicsview/qgraphicsgridlayout.cpp @@ -589,6 +589,18 @@ void QGraphicsGridLayout::removeAt(int index) } /*! + Removes the layout item \a item without destroying it. + Ownership of the item is transferred to the caller. + + \sa addItem() +*/ +void QGraphicsGridLayout::removeItem(QGraphicsLayoutItem *item) +{ + Q_D(QGraphicsGridLayout); + int index = d->engine.indexOf(item); + removeAt(index); +} +/*! \reimp */ void QGraphicsGridLayout::invalidate() diff --git a/src/gui/graphicsview/qgraphicsgridlayout.h b/src/gui/graphicsview/qgraphicsgridlayout.h index ddfb447..d10235c 100644 --- a/src/gui/graphicsview/qgraphicsgridlayout.h +++ b/src/gui/graphicsview/qgraphicsgridlayout.h @@ -114,6 +114,7 @@ public: int count() const; QGraphicsLayoutItem *itemAt(int index) const; void removeAt(int index); + void removeItem(QGraphicsLayoutItem *item); void invalidate(); diff --git a/src/gui/graphicsview/qgridlayoutengine.cpp b/src/gui/graphicsview/qgridlayoutengine.cpp index a084647..f68df73 100644 --- a/src/gui/graphicsview/qgridlayoutengine.cpp +++ b/src/gui/graphicsview/qgridlayoutengine.cpp @@ -775,6 +775,15 @@ QGridLayoutItem *QGridLayoutEngine::itemAt(int index) const return q_items.at(index); } +int QGridLayoutEngine::indexOf(QGraphicsLayoutItem *item) const +{ + for(int i = 0; i < q_items.size(); ++i) { + if(item == q_items.at(i)->layoutItem()) + return i; + } + return -1; +} + int QGridLayoutEngine::effectiveFirstRow(Qt::Orientation orientation) const { ensureEffectiveFirstAndLastRows(); diff --git a/src/gui/graphicsview/qgridlayoutengine_p.h b/src/gui/graphicsview/qgridlayoutengine_p.h index 9ac9a8e..a4ef21d 100644 --- a/src/gui/graphicsview/qgridlayoutengine_p.h +++ b/src/gui/graphicsview/qgridlayoutengine_p.h @@ -326,6 +326,7 @@ public: // returns the number of items inserted, which may be less than (rowCount * columnCount) int itemCount() const; QGridLayoutItem *itemAt(int index) const; + int indexOf(QGraphicsLayoutItem *item) const; int effectiveFirstRow(Qt::Orientation orientation = Qt::Vertical) const; int effectiveLastRow(Qt::Orientation orientation = Qt::Vertical) const; diff --git a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp index d1d6860..123dd89 100644 --- a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp +++ b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp @@ -78,6 +78,7 @@ private slots: void horizontalSpacing(); void itemAt(); void removeAt(); + void removeLayoutItems(); void rowAlignment(); void rowCount(); void rowMaximumHeight(); @@ -992,6 +993,23 @@ void tst_QGraphicsGridLayout::removeAt() delete widget; } +void tst_QGraphicsGridLayout::removeLayoutItems() +{ + QGraphicsScene scene; + QGraphicsView view(&scene); + + QGraphicsWidget *widget = new QGraphicsWidget(0, Qt::Window); + scene.addItem(widget); + QGraphicsGridLayout *l = new QGraphicsGridLayout(); + widget->setLayout(l); + + populateLayout(l, 3, 2); + QCOMPARE(l->count(), 6); + l->removeItem(l->itemAt(5)); + l->removeItem(l->itemAt(4)); + QCOMPARE(l->count(), 4); +} + // public Qt::Alignment rowAlignment(int row) const void tst_QGraphicsGridLayout::rowAlignment() { -- cgit v0.12 From 23122981e186e6b071f6690dec0fd1d57ee3bbdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Arve=20S=C3=A6ther?= Date: Wed, 11 Aug 2010 11:16:10 +0200 Subject: Some polishing to merge request 723 --- src/gui/graphicsview/qgraphicsgridlayout.cpp | 2 +- src/gui/graphicsview/qgridlayoutengine.cpp | 4 ++-- tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp | 13 +++++++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsgridlayout.cpp b/src/gui/graphicsview/qgraphicsgridlayout.cpp index 2660b06..8a3f17a 100644 --- a/src/gui/graphicsview/qgraphicsgridlayout.cpp +++ b/src/gui/graphicsview/qgraphicsgridlayout.cpp @@ -589,7 +589,7 @@ void QGraphicsGridLayout::removeAt(int index) } /*! - Removes the layout item \a item without destroying it. + Removes the layout item \a item without destroying it. Ownership of the item is transferred to the caller. \sa addItem() diff --git a/src/gui/graphicsview/qgridlayoutengine.cpp b/src/gui/graphicsview/qgridlayoutengine.cpp index f68df73..8b65282 100644 --- a/src/gui/graphicsview/qgridlayoutengine.cpp +++ b/src/gui/graphicsview/qgridlayoutengine.cpp @@ -777,8 +777,8 @@ QGridLayoutItem *QGridLayoutEngine::itemAt(int index) const int QGridLayoutEngine::indexOf(QGraphicsLayoutItem *item) const { - for(int i = 0; i < q_items.size(); ++i) { - if(item == q_items.at(i)->layoutItem()) + for (int i = 0; i < q_items.size(); ++i) { + if (item == q_items.at(i)->layoutItem()) return i; } return -1; diff --git a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp index 123dd89..55d75af 100644 --- a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp +++ b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp @@ -78,7 +78,7 @@ private slots: void horizontalSpacing(); void itemAt(); void removeAt(); - void removeLayoutItems(); + void removeItem(); void rowAlignment(); void rowCount(); void rowMaximumHeight(); @@ -993,7 +993,7 @@ void tst_QGraphicsGridLayout::removeAt() delete widget; } -void tst_QGraphicsGridLayout::removeLayoutItems() +void tst_QGraphicsGridLayout::removeItem() { QGraphicsScene scene; QGraphicsView view(&scene); @@ -1008,6 +1008,15 @@ void tst_QGraphicsGridLayout::removeLayoutItems() l->removeItem(l->itemAt(5)); l->removeItem(l->itemAt(4)); QCOMPARE(l->count(), 4); + + // Avoid crashing. Note that the warning message might change in the future. + QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QGraphicsGridLayout::removeAt: invalid index -1").toLatin1().constData()); + l->removeItem(0); + QCOMPARE(l->count(), 4); + + QTest::ignoreMessage(QtWarningMsg, QString::fromAscii("QGraphicsGridLayout::removeAt: invalid index -1").toLatin1().constData()); + l->removeItem(new QGraphicsWidget); + QCOMPARE(l->count(), 4); } // public Qt::Alignment rowAlignment(int row) const -- cgit v0.12