From e93db692f88ad788a89a229ca974d5598dc104c2 Mon Sep 17 00:00:00 2001 From: Jan Arve Saether Date: Mon, 20 Oct 2014 15:40:18 +0200 Subject: Adjust the layout if a QGraphicsWidget is explicitly hidden Task-number: QTBUG-20132 Change-Id: Iab59fc9b61d4ca1bb2208c479a027da6eb0283a9 Reviewed-by: Paul Olav Tvete (cherry picked from qtbase/bf200fc948d89e1a735a8bcca879654028f944d2) --- src/gui/graphicsview/qgraphicswidget.cpp | 3 + src/gui/graphicsview/qgridlayoutengine.cpp | 11 +++- src/gui/graphicsview/qgridlayoutengine_p.h | 1 + .../tst_qgraphicsgridlayout.cpp | 65 ++++++++++++++++++++++ tests/auto/qgraphicslayout/tst_qgraphicslayout.cpp | 24 ++++++++ .../tst_qgraphicslinearlayout.cpp | 28 ++++++++++ 6 files changed, 131 insertions(+), 1 deletion(-) diff --git a/src/gui/graphicsview/qgraphicswidget.cpp b/src/gui/graphicsview/qgraphicswidget.cpp index 2883945..2497742 100644 --- a/src/gui/graphicsview/qgraphicswidget.cpp +++ b/src/gui/graphicsview/qgraphicswidget.cpp @@ -1138,6 +1138,9 @@ QVariant QGraphicsWidget::itemChange(GraphicsItemChange change, const QVariant & setAttribute(Qt::WA_Resized, false); } } + // layout size hint only changes if an item changes from/to explicitly hidden state + if (value.toBool() || d->explicitlyHidden) + updateGeometry(); break; case ItemVisibleHasChanged: if (!value.toBool()) { diff --git a/src/gui/graphicsview/qgridlayoutengine.cpp b/src/gui/graphicsview/qgridlayoutengine.cpp index b2e963b..74a074f 100644 --- a/src/gui/graphicsview/qgridlayoutengine.cpp +++ b/src/gui/graphicsview/qgridlayoutengine.cpp @@ -49,6 +49,7 @@ #include "qgridlayoutengine_p.h" #include "qstyleoption.h" #include "qvarlengtharray.h" +#include "qgraphicswidget_p.h" #include #include @@ -698,6 +699,14 @@ QRectF QGridLayoutItem::geometryWithin(qreal x, qreal y, qreal width, qreal heig } } +bool QGridLayoutItem::isIgnored() +{ + if (QGraphicsItem *item = layoutItem()->graphicsItem()) { + return QGraphicsItemPrivate::get(item)->explicitlyHidden; + } + return false; +} + void QGridLayoutItem::setGeometry(const QRectF &rect) { q_layoutItem->setGeometry(rect); @@ -1410,7 +1419,7 @@ void QGridLayoutEngine::fillRowData(QGridLayoutRowData *rowData, const QLayoutSt if (rowIsIdenticalToPrevious && item != itemAt(row - 1, column, orientation)) rowIsIdenticalToPrevious = false; - if (item) + if (item && !item->isIgnored()) rowIsEmpty = false; } diff --git a/src/gui/graphicsview/qgridlayoutengine_p.h b/src/gui/graphicsview/qgridlayoutengine_p.h index 1f1078d..88e3501 100644 --- a/src/gui/graphicsview/qgridlayoutengine_p.h +++ b/src/gui/graphicsview/qgridlayoutengine_p.h @@ -289,6 +289,7 @@ public: QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; QGridLayoutBox box(Qt::Orientation orientation, qreal constraint = -1.0) const; QRectF geometryWithin(qreal x, qreal y, qreal width, qreal height, qreal rowDescent) const; + bool isIgnored(); QGraphicsLayoutItem *layoutItem() const { return q_layoutItem; } diff --git a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp index 68ad99d..3bd9918 100644 --- a/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp +++ b/tests/auto/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp @@ -132,6 +132,7 @@ private slots: void heightForWidthWithSpanning(); void stretchAndHeightForWidth(); void testDefaultAlignment(); + void hiddenItems(); }; class RectWidget : public QGraphicsWidget @@ -3484,6 +3485,70 @@ void tst_QGraphicsGridLayout::testDefaultAlignment() QCOMPARE(w->geometry(), QRectF(0,0,50,50)); QCOMPARE(w2->geometry(), QRectF(0,50,100,100)); } + +static RectWidget *addWidget(QGraphicsGridLayout *grid, int row, int column) +{ + RectWidget *w = new RectWidget; + w->setPreferredSize(20, 20); + grid->addItem(w, row, column); + return w; +} + +static void setVisible(bool visible, QGraphicsWidget **widgets) +{ + for (int i = 0; i < 3; ++i) + if (widgets[i]) widgets[i]->setVisible(visible); +} + +void tst_QGraphicsGridLayout::hiddenItems() +{ + QGraphicsWidget *widget = new QGraphicsWidget; + QGraphicsGridLayout *layout = new QGraphicsGridLayout(widget); + layout->setContentsMargins(0, 0, 0, 0); + layout->setSpacing(2); + + // Create a 3x3 layout + addWidget(layout, 0, 0); + RectWidget *w01 = addWidget(layout, 0, 1); + addWidget(layout, 0, 2); + RectWidget *w10 = addWidget(layout, 1, 0); + RectWidget *w11 = addWidget(layout, 1, 1); + RectWidget *w12 = addWidget(layout, 1, 2); + addWidget(layout, 2, 0); + RectWidget *w21 = addWidget(layout, 2, 1); + addWidget(layout, 2, 2); + + QGraphicsWidget *middleColumn[] = {w01, w11, w21 }; + QGraphicsWidget *topTwoOfMiddleColumn[] = {w01, w11, 0 }; + + // hide and show middle column + QCOMPARE(layout->preferredWidth(), qreal(64)); + setVisible(false, middleColumn); // hide middle column + QCOMPARE(layout->preferredWidth(), qreal(42)); + setVisible(true, middleColumn); // show middle column + QCOMPARE(layout->preferredWidth(), qreal(64)); + + // Hide only two items, => column should not collapse + setVisible(false, topTwoOfMiddleColumn); + QCOMPARE(layout->preferredWidth(), qreal(64)); + + + QGraphicsWidget *middleRow[] = {w10, w11, w12 }; + QGraphicsWidget *leftMostTwoOfMiddleRow[] = {w10, w11, 0 }; + + // hide and show middle row + QCOMPARE(layout->preferredHeight(), qreal(64)); + setVisible(false, middleRow); + QCOMPARE(layout->preferredHeight(), qreal(42)); + setVisible(true, middleRow); + QCOMPARE(layout->preferredHeight(), qreal(64)); + // Hide only two items => row should not collapse + setVisible(false, leftMostTwoOfMiddleRow); + QCOMPARE(layout->preferredHeight(), qreal(64)); + +} + + QTEST_MAIN(tst_QGraphicsGridLayout) #include "tst_qgraphicsgridlayout.moc" diff --git a/tests/auto/qgraphicslayout/tst_qgraphicslayout.cpp b/tests/auto/qgraphicslayout/tst_qgraphicslayout.cpp index 1e20bb7..52ce9bb 100644 --- a/tests/auto/qgraphicslayout/tst_qgraphicslayout.cpp +++ b/tests/auto/qgraphicslayout/tst_qgraphicslayout.cpp @@ -62,6 +62,7 @@ private slots: void compressLayoutRequest(); void automaticReparenting(); void verifyActivate(); + void sizeHintOfHiddenLayout(); void invalidate(); void moveAndResize_data(); void moveAndResize(); @@ -299,6 +300,29 @@ void tst_QGraphicsLayout::verifyActivate() } + +void tst_QGraphicsLayout::sizeHintOfHiddenLayout() +{ + QGraphicsScene scene; + QGraphicsWidget *window = new QGraphicsWidget(0, Qt::Window); + scene.addItem(window); + TestLayout *lout = new TestLayout(window); + lout->setContentsMargins(1,2,2,1); + QGraphicsWidget *w = new QGraphicsWidget; + w->setPreferredSize(20, 20); + w->setMaximumSize(50, 50); + lout->addItem(w); + window->setLayout(lout); + + for (int pass = 0; pass < 3; ++pass) { + QCOMPARE(lout->sizeHint(Qt::MinimumSize), QSizeF(3,3)); + QCOMPARE(lout->sizeHint(Qt::PreferredSize), QSizeF(23,23)); + QCOMPARE(lout->sizeHint(Qt::MaximumSize), QSizeF(53,53)); + window->setVisible(pass % 2); + } +} + + static void clearAllCounters(TestGraphicsWidget *widget) { if (!widget) diff --git a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp index dd5b9d0..ab20bfc 100644 --- a/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp +++ b/tests/auto/qgraphicslinearlayout/tst_qgraphicslinearlayout.cpp @@ -108,6 +108,7 @@ private slots: void testOffByOneInLargerLayout(); void testDefaultAlignment(); void combineSizePolicies(); + void hiddenItems(); // Task specific tests void task218400_insertStretchCrash(); @@ -1649,6 +1650,33 @@ void tst_QGraphicsLinearLayout::combineSizePolicies() QCOMPARE(layout->maximumHeight(), qreal(200)); } +void tst_QGraphicsLinearLayout::hiddenItems() +{ + QGraphicsWidget *widget = new QGraphicsWidget; + QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Horizontal, widget); + layout->setContentsMargins(0, 0, 0, 0); + layout->setSpacing(2); + + RectWidget *w1 = new RectWidget; + w1->setPreferredSize(QSizeF(20, 20)); + layout->addItem(w1); + + RectWidget *w2 = new RectWidget; + w2->setPreferredSize(QSizeF(20, 20)); + layout->addItem(w2); + + RectWidget *w3 = new RectWidget; + w3->setPreferredSize(QSizeF(20, 20)); + layout->addItem(w3); + + QCOMPARE(layout->preferredWidth(), qreal(64)); + w2->hide(); + QCOMPARE(layout->preferredWidth(), qreal(42)); + w2->show(); + QCOMPARE(layout->preferredWidth(), qreal(64)); +} + + QTEST_MAIN(tst_QGraphicsLinearLayout) #include "tst_qgraphicslinearlayout.moc" -- cgit v0.12