From 5b2dfbc649a7c20a93223cd4c274a4b0fe847df8 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 24 May 2019 21:20:37 +0200 Subject: Long live QSize(F)::grownBy/shrunkBy() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These functions tighten the integration of QMargins(F) with the rest of the geometry classes by providing a way to apply margins to sizes (and later, rects). Apply them in a few obvious cases across QtWidgets. [ChangeLog][QtCore][QSize/QSizeF] Added grownBy(QMargin(F))/shrunkBy(QMargin(F)). Change-Id: I8a549436824cdb7fb6125a8cde89d5bf02826934 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Shawn Rutledge Reviewed-by: Edward Welbourne --- tests/auto/corelib/tools/qsize/tst_qsize.cpp | 44 ++++++++++++++++++++++++++ tests/auto/corelib/tools/qsizef/tst_qsizef.cpp | 44 ++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) (limited to 'tests/auto/corelib/tools') diff --git a/tests/auto/corelib/tools/qsize/tst_qsize.cpp b/tests/auto/corelib/tools/qsize/tst_qsize.cpp index 385ff18ce5..6824bad9c8 100644 --- a/tests/auto/corelib/tools/qsize/tst_qsize.cpp +++ b/tests/auto/corelib/tools/qsize/tst_qsize.cpp @@ -29,6 +29,7 @@ #include #include +Q_DECLARE_METATYPE(QMargins) class tst_QSize : public QObject { @@ -43,6 +44,9 @@ private slots: void boundedTo_data(); void boundedTo(); + void grownOrShrunkBy_data(); + void grownOrShrunkBy(); + void transpose_data(); void transpose(); }; @@ -186,6 +190,46 @@ void tst_QSize::boundedTo() QCOMPARE( input1.boundedTo(input2), expected); } +void tst_QSize::grownOrShrunkBy_data() +{ + QTest::addColumn("input"); + QTest::addColumn("margins"); + QTest::addColumn("grown"); + QTest::addColumn("shrunk"); + + auto row = [](QSize i, QMargins m, QSize g, QSize s) { + QTest::addRow("{%d,%d}/{%d,%d,%d,%d}", i.width(), i.height(), + m.left(), m.top(), m.right(), m.bottom()) + << i << m << g << s; + }; + + const QSize zero = {0, 0}; + const QSize some = {100, 200}; + const QMargins zeroMargins = {}; + const QMargins negative = {-1, -2, -3, -4}; + const QMargins positive = { 1, 2, 3, 4}; + + row(zero, zeroMargins, zero, zero); + row(zero, negative, {-4, -6}, { 4, 6}); + row(zero, positive, { 4, 6}, {-4, -6}); + row(some, zeroMargins, some, some); + row(some, negative, { 96, 194}, {104, 206}); + row(some, positive, {104, 206}, { 96, 194}); +} + +void tst_QSize::grownOrShrunkBy() +{ + QFETCH(const QSize, input); + QFETCH(const QMargins, margins); + QFETCH(const QSize, grown); + QFETCH(const QSize, shrunk); + + QCOMPARE(input.grownBy(margins), grown); + QCOMPARE(input.shrunkBy(margins), shrunk); + QCOMPARE(grown.shrunkBy(margins), input); + QCOMPARE(shrunk.grownBy(margins), input); +} + void tst_QSize::transpose_data() { QTest::addColumn("input1"); diff --git a/tests/auto/corelib/tools/qsizef/tst_qsizef.cpp b/tests/auto/corelib/tools/qsizef/tst_qsizef.cpp index 42801d63a9..bbffa74a62 100644 --- a/tests/auto/corelib/tools/qsizef/tst_qsizef.cpp +++ b/tests/auto/corelib/tools/qsizef/tst_qsizef.cpp @@ -29,6 +29,7 @@ #include #include +Q_DECLARE_METATYPE(QMarginsF) class tst_QSizeF : public QObject { @@ -45,6 +46,9 @@ private slots: void boundedTo_data(); void boundedTo(); + void grownOrShrunkBy_data(); + void grownOrShrunkBy(); + void transpose_data(); void transpose(); }; @@ -152,6 +156,46 @@ void tst_QSizeF::boundedTo() { QCOMPARE( input1.boundedTo(input2), expected); } +void tst_QSizeF::grownOrShrunkBy_data() +{ + QTest::addColumn("input"); + QTest::addColumn("margins"); + QTest::addColumn("grown"); + QTest::addColumn("shrunk"); + + auto row = [](QSizeF i, QMarginsF m, QSizeF g, QSizeF s) { + QTest::addRow("{%g,%g}/{%g,%g,%g,%g}", i.width(), i.height(), + m.left(), m.top(), m.right(), m.bottom()) + << i << m << g << s; + }; + + const QSizeF zero = {0, 0}; + const QSizeF some = {100, 200}; + const QMarginsF zeroMargins = {}; + const QMarginsF negative = {-1, -2, -3, -4}; + const QMarginsF positive = { 1, 2, 3, 4}; + + row(zero, zeroMargins, zero, zero); + row(zero, negative, {-4, -6}, { 4, 6}); + row(zero, positive, { 4, 6}, {-4, -6}); + row(some, zeroMargins, some, some); + row(some, negative, { 96, 194}, {104, 206}); + row(some, positive, {104, 206}, { 96, 194}); +} + +void tst_QSizeF::grownOrShrunkBy() +{ + QFETCH(const QSizeF, input); + QFETCH(const QMarginsF, margins); + QFETCH(const QSizeF, grown); + QFETCH(const QSizeF, shrunk); + + QCOMPARE(input.grownBy(margins), grown); + QCOMPARE(input.shrunkBy(margins), shrunk); + QCOMPARE(grown.shrunkBy(margins), input); + QCOMPARE(shrunk.grownBy(margins), input); +} + void tst_QSizeF::transpose_data() { QTest::addColumn("input1"); QTest::addColumn("expected"); -- cgit v1.2.3