aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorReinhard Raschbauer <r.raschbauer@kabsi.at>2020-11-08 12:33:59 +0100
committerRaschbauer, Reinhard <r.raschbauer@kabsi.at>2020-11-26 20:16:12 +0100
commit7c751a57313ee2a2b4c6ca5a1815c03e3a589ff2 (patch)
tree60ed933ac8164a7ec94796445b1d52de7dfbf4ce
parent81629021ebc8789974c2f3c142b46eef799c1a95 (diff)
Correct height properties for QQuickText with reducing lineHeight
If the property lineHeight is used to reduce the line height, either by setting a proportional factor smaller 1.0 or a pixel size smaller than the font size, the offset calculated in lineHeightOffset is not taken in to account to calculate the height properties. But the offset is used to position the the rendered text. In the current implementation the property lineHeight does not have an effect on single line texts. This change takes that into account and adds lineHeightOffset to all height properties. Fixes: QTBUG-88229 Change-Id: Iab7d9b39a4c7876c7c95e43be6846623c10b0607 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r--src/quick/items/qquicktext.cpp25
-rw-r--r--tests/auto/quick/qquicktext/tst_qquicktext.cpp18
2 files changed, 29 insertions, 14 deletions
diff --git a/src/quick/items/qquicktext.cpp b/src/quick/items/qquicktext.cpp
index 0546f80fd5..f36be9b81a 100644
--- a/src/quick/items/qquicktext.cpp
+++ b/src/quick/items/qquicktext.cpp
@@ -350,13 +350,14 @@ void QQuickTextPrivate::updateBaseline(qreal baseline, qreal dy)
void QQuickTextPrivate::signalSizeChange(const QSizeF &previousSize)
{
Q_Q(QQuickText);
+ const QSizeF contentSize(q->contentWidth(), q->contentHeight());
- if (layedOutTextRect.size() != previousSize) {
+ if (contentSize != previousSize) {
emit q->contentSizeChanged();
- if (layedOutTextRect.width() != previousSize.width())
- emit q->contentWidthChanged(layedOutTextRect.width());
- if (layedOutTextRect.height() != previousSize.height())
- emit q->contentHeightChanged(layedOutTextRect.height());
+ if (contentSize.width() != previousSize.width())
+ emit q->contentWidthChanged(contentSize.width());
+ if (contentSize.height() != previousSize.height())
+ emit q->contentHeightChanged(contentSize.height());
}
}
@@ -380,7 +381,7 @@ void QQuickTextPrivate::updateSize()
qreal hPadding = q->leftPadding() + q->rightPadding();
qreal vPadding = q->topPadding() + q->bottomPadding();
- const QSizeF previousSize = layedOutTextRect.size();
+ const QSizeF previousSize(q->contentWidth(), q->contentHeight());
if (text.isEmpty() && !isLineLaidOutConnected() && fontSizeMode() == QQuickText::FixedSize) {
// How much more expensive is it to just do a full layout on an empty string here?
@@ -395,7 +396,7 @@ void QQuickTextPrivate::updateSize()
: fontHeight * lineHeight();
}
updateBaseline(fm.ascent(), q->height() - fontHeight - vPadding);
- q->setImplicitSize(hPadding, fontHeight + vPadding);
+ q->setImplicitSize(hPadding, fontHeight + qMax(lineHeightOffset(), 0) + vPadding);
layedOutTextRect = QRectF(0, 0, 0, fontHeight);
advance = QSizeF();
signalSizeChange(previousSize);
@@ -468,7 +469,7 @@ void QQuickTextPrivate::updateSize()
if (!q->widthValid())
iWidth = size.width();
if (iWidth > -1)
- q->setImplicitSize(iWidth + hPadding, size.height() + vPadding);
+ q->setImplicitSize(iWidth + hPadding, size.height() + qMax(lineHeightOffset(), 0) + vPadding);
internalWidthUpdate = false;
// If the implicit width update caused a recursive change of the width,
@@ -482,7 +483,7 @@ void QQuickTextPrivate::updateSize()
updateSizeRecursionGuard = false;
} else {
if (iWidth == -1)
- q->setImplicitHeight(size.height() + vPadding);
+ q->setImplicitHeight(size.height() + lineHeightOffset() + vPadding);
QTextBlock firstBlock = extra->doc->firstBlock();
while (firstBlock.layout()->lineCount() == 0)
@@ -946,7 +947,7 @@ QRectF QQuickTextPrivate::setupTextLayout(qreal *const baseline)
bool wasInLayout = internalWidthUpdate;
internalWidthUpdate = true;
- q->setImplicitSize(naturalWidth + q->leftPadding() + q->rightPadding(), naturalHeight + q->topPadding() + q->bottomPadding());
+ q->setImplicitSize(naturalWidth + q->leftPadding() + q->rightPadding(), naturalHeight + qMax(lineHeightOffset(), 0) + q->topPadding() + q->bottomPadding());
internalWidthUpdate = wasInLayout;
// Update any variables that are dependent on the validity of the width or height.
@@ -1010,7 +1011,7 @@ QRectF QQuickTextPrivate::setupTextLayout(qreal *const baseline)
bool wasInLayout = internalWidthUpdate;
internalWidthUpdate = true;
- q->setImplicitHeight(naturalHeight + q->topPadding() + q->bottomPadding());
+ q->setImplicitHeight(naturalHeight + qMax(lineHeightOffset(), 0) + q->topPadding() + q->bottomPadding());
internalWidthUpdate = wasInLayout;
multilineElide = elideMode == QQuickText::ElideRight
@@ -2560,7 +2561,7 @@ qreal QQuickText::contentWidth() const
qreal QQuickText::contentHeight() const
{
Q_D(const QQuickText);
- return d->layedOutTextRect.height();
+ return d->layedOutTextRect.height() + qMax(d->lineHeightOffset(), 0);
}
/*!
diff --git a/tests/auto/quick/qquicktext/tst_qquicktext.cpp b/tests/auto/quick/qquicktext/tst_qquicktext.cpp
index d1716c20da..6c257e0a9d 100644
--- a/tests/auto/quick/qquicktext/tst_qquicktext.cpp
+++ b/tests/auto/quick/qquicktext/tst_qquicktext.cpp
@@ -2293,8 +2293,12 @@ void tst_qquicktext::lineHeight()
QCOMPARE(myText->lineHeightMode(), QQuickText::ProportionalHeight);
qreal h = myText->height();
+ QVERIFY(myText->lineCount() != 0);
+ const qreal h1stLine = h / myText->lineCount();
+
myText->setLineHeight(1.5);
QCOMPARE(myText->height(), qreal(qCeil(h)) * 1.5);
+ QCOMPARE(myText->contentHeight(), qreal(qCeil(h)) * 1.5);
myText->setLineHeightMode(QQuickText::FixedHeight);
myText->setLineHeight(20);
@@ -2306,11 +2310,21 @@ void tst_qquicktext::lineHeight()
qreal h2 = myText->height();
myText->setLineHeight(2.0);
- QVERIFY(myText->height() == h2 * 2.0);
+ QCOMPARE(myText->height(), h2 * 2.0);
myText->setLineHeightMode(QQuickText::FixedHeight);
myText->setLineHeight(10);
- QCOMPARE(myText->height(), myText->lineCount() * 10.0);
+ QVERIFY(myText->lineCount() > 1);
+ QCOMPARE(myText->height(), h1stLine + (myText->lineCount() - 1) * 10.0);
+ QCOMPARE(myText->contentHeight(), h1stLine + (myText->lineCount() - 1) * 10.0);
+ QCOMPARE(myText->implicitHeight(), h1stLine + (myText->lineCount() - 1) * 10.0);
+
+ myText->setLineHeightMode(QQuickText::ProportionalHeight);
+ myText->setLineHeight(0.5);
+ const qreal reducedHeight = h1stLine + (myText->lineCount() - 1) * h1stLine * 0.5;
+ QVERIFY(qAbs(myText->height() - reducedHeight) < 1.0); // allow a deviation of one pixel because the exact height depends on the font.
+ QVERIFY(qAbs(myText->contentHeight() - reducedHeight) < 1.0);
+ QVERIFY(qAbs(myText->implicitHeight() - reducedHeight) < 1.0);
}
void tst_qquicktext::implicitSize_data()