From b855e578044e49b588b32085968c63a910b9daae Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete Date: Tue, 27 Aug 2013 14:47:55 +0200 Subject: [PATCH] Fix layouts with expanding items with maximum size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Layout items with a Preferred size policy would be treated as fixed size if they were in the same layout as an Expanding item (or one with a stretch factor). This occurred e.g. if a layout was configured similar to this: 1. One item with ExpandFlag/stretch but with a maximumSize set, e.g. (100x100). 2. Another item with 'just' GrowFlag, and a maximum size bigger than its size hint. If the above layout was resized to e.g. (200x50) it would cause the expanding item to correctly get the size (100x50), but the 'growing' item would not stretch beyond its size hint. Instead, it would distribute space around both items, behaving as if the 'growing' item was fixed'. The expected behavior is to continue to grow the 'growing' item after the expanding item has reached its size limit. Task-number: QTBUG-33104 Change-Id: Ie410653d905f7ca4d702528dafb269f30a0e4f61 Reviewed-by: Paul Olav Tvete Reviewed-by: Jan Arve Sæther --- src/widgets/kernel/qlayoutengine.cpp | 25 ++++++++++------- .../kernel/qboxlayout/tst_qboxlayout.cpp | 27 +++++++++++++++++++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/widgets/kernel/qlayoutengine.cpp b/src/widgets/kernel/qlayoutengine.cpp index 8800da33b4..9ef2a7096f 100644 --- a/src/widgets/kernel/qlayoutengine.cpp +++ b/src/widgets/kernel/qlayoutengine.cpp @@ -83,9 +83,7 @@ void qGeomCalc(QVector &chain, int start, int count, int cMax = 0; int sumStretch = 0; int sumSpacing = 0; - - bool wannaGrow = false; // anyone who really wants to grow? - // bool canShrink = false; // anyone who could be persuaded to shrink? + int expandingCount = 0; bool allEmptyNonstretch = true; int pendingSpacing = -1; @@ -111,8 +109,9 @@ void qGeomCalc(QVector &chain, int start, int count, } pendingSpacing = data->effectiveSpacer(spacer); } - wannaGrow = wannaGrow || data->expansive || data->stretch > 0; - allEmptyNonstretch = allEmptyNonstretch && !wannaGrow && data->empty; + if (data->expansive) + expandingCount++; + allEmptyNonstretch = allEmptyNonstretch && data->empty && !data->expansive && data->stretch <= 0; } int extraspace = 0; @@ -233,13 +232,14 @@ void qGeomCalc(QVector &chain, int start, int count, QLayoutStruct *data = &chain[i]; if (!data->done && (data->maximumSize <= data->smartSizeHint() - || (wannaGrow && !data->expansive && data->stretch == 0) || (!allEmptyNonstretch && data->empty && !data->expansive && data->stretch == 0))) { data->size = data->smartSizeHint(); data->done = true; space_left -= data->size; sumStretch -= data->stretch; + if (data->expansive) + expandingCount--; n--; } } @@ -265,10 +265,13 @@ void qGeomCalc(QVector &chain, int start, int count, if (data->done) continue; extraspace = 0; - if (sumStretch <= 0) - fp_w += fp_space / n; - else + if (sumStretch > 0) { fp_w += (fp_space * data->stretch) / sumStretch; + } else if (expandingCount > 0) { + fp_w += (fp_space * (data->expansive ? 1 : 0)) / expandingCount; + } else { + fp_w += fp_space * 1 / n; + } int w = fRound(fp_w); data->size = w; fp_w -= toFixed(w); // give the difference to the next @@ -287,6 +290,8 @@ void qGeomCalc(QVector &chain, int start, int count, data->done = true; space_left -= data->smartSizeHint(); sumStretch -= data->stretch; + if (data->expansive) + expandingCount--; n--; } } @@ -300,6 +305,8 @@ void qGeomCalc(QVector &chain, int start, int count, data->done = true; space_left -= data->maximumSize; sumStretch -= data->stretch; + if (data->expansive) + expandingCount--; n--; } } diff --git a/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp b/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp index 7bbbff5736..bdc32ba197 100644 --- a/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp +++ b/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp @@ -451,6 +451,33 @@ void tst_QBoxLayout::testLayoutEngine_data() << (PosList() << 100 << 300 << 300) << (SizeList() << 100 << 0 << 100); + QTest::newRow("QTBUG-33104") + << (DescrList() << Descr(11, 75, 75, true) << Descr(75, 75)) + << 200 + << 0 + << (PosList() << 0 << 75) + << (SizeList() << 75 << 125); + + QTest::newRow("Expanding with maximumSize") + << (DescrList() << Descr(11, 75, 100, true) << Descr(75, 75)) + << 200 + << 0 + << (PosList() << 0 << 100) + << (SizeList() << 100 << 100); + + QTest::newRow("Stretch with maximumSize") + << (DescrList() << Descr(11, 75, 100, false, 1) << Descr(75, 75)) + << 200 + << 0 + << (PosList() << 0 << 100) + << (SizeList() << 100 << 100); + + QTest::newRow("Stretch with maximumSize last") + << (DescrList() << Descr(75, 75) << Descr(11, 75, 100, false, 1)) + << 200 + << 0 + << (PosList() << 0 << 100) + << (SizeList() << 100 << 100); } void tst_QBoxLayout::testLayoutEngine()