aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaiwei Li <daiweili@suitabletech.com>2014-12-14 21:31:30 -0800
committerDaiwei Li <daiweili@suitabletech.com>2014-12-16 22:11:19 +0100
commit119508602ea5fcd6cce135e547e6aef9bb93beeb (patch)
tree8576cdcee295e9607c8443a8d4932eecc0852fd5
parent2a0d1cc3794394107652d979051cb050e4615a36 (diff)
Fix QQuickText alignment when lineHeight is set
Text is always aligned to the top of the lineHeight rectangle, and no offset is added when verticalAlignment is set to AlignVCenter or AlignBottom. This change adds an offset based on the lineHeight and font height in those cases. Task-number: QTBUG-42050 Change-Id: Ibc63c80d4178b0f5c498cc126156df828855fd76 Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
-rw-r--r--src/quick/items/qquicktext.cpp17
-rw-r--r--src/quick/items/qquicktext_p_p.h1
2 files changed, 15 insertions, 3 deletions
diff --git a/src/quick/items/qquicktext.cpp b/src/quick/items/qquicktext.cpp
index 87255f4bd9..f373d129b3 100644
--- a/src/quick/items/qquicktext.cpp
+++ b/src/quick/items/qquicktext.cpp
@@ -1166,6 +1166,17 @@ void QQuickTextPrivate::setLineGeometry(QTextLine &line, qreal lineWidth, qreal
}
/*!
+ Returns the y offset when aligning text with a non-1.0 lineHeight
+*/
+int QQuickTextPrivate::lineHeightOffset() const
+{
+ QFontMetricsF fm(font);
+ qreal fontHeight = qCeil(fm.height()); // QScriptLine and therefore QTextLine rounds up
+ return lineHeightMode() == QQuickText::FixedHeight ? fontHeight - lineHeight()
+ : (1.0 - lineHeight()) * fontHeight;
+}
+
+/*!
Ensures the QQuickTextPrivate::doc variable is set to a valid text document
*/
void QQuickTextPrivate::ensureDoc()
@@ -2090,7 +2101,7 @@ QRectF QQuickText::boundingRect() const
QRectF rect = d->layedOutTextRect;
rect.moveLeft(QQuickTextUtil::alignedX(rect.width(), width(), effectiveHAlign()));
- rect.moveTop(QQuickTextUtil::alignedY(rect.height(), height(), d->vAlign));
+ rect.moveTop(QQuickTextUtil::alignedY(rect.height() + d->lineHeightOffset(), height(), d->vAlign));
if (d->style != Normal)
rect.adjust(-1, 0, 1, 2);
@@ -2209,7 +2220,7 @@ QSGNode *QQuickText::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data
d->updateType = QQuickTextPrivate::UpdateNone;
- const qreal dy = QQuickTextUtil::alignedY(d->layedOutTextRect.height(), height(), d->vAlign);
+ const qreal dy = QQuickTextUtil::alignedY(d->layedOutTextRect.height() + d->lineHeightOffset(), height(), d->vAlign);
QQuickTextNode *node = 0;
if (!oldNode)
@@ -2513,7 +2524,7 @@ QString QQuickTextPrivate::anchorAt(const QPointF &mousePos) const
{
Q_Q(const QQuickText);
QPointF translatedMousePos = mousePos;
- translatedMousePos.ry() -= QQuickTextUtil::alignedY(layedOutTextRect.height(), q->height(), vAlign);
+ translatedMousePos.ry() -= QQuickTextUtil::alignedY(layedOutTextRect.height() + lineHeightOffset(), q->height(), vAlign);
if (styledText) {
QString link = anchorAt(&layout, translatedMousePos);
if (link.isEmpty() && elideLayout)
diff --git a/src/quick/items/qquicktext_p_p.h b/src/quick/items/qquicktext_p_p.h
index d645ce5ed5..35cdb68104 100644
--- a/src/quick/items/qquicktext_p_p.h
+++ b/src/quick/items/qquicktext_p_p.h
@@ -76,6 +76,7 @@ public:
bool isLineLaidOutConnected();
void setLineGeometry(QTextLine &line, qreal lineWidth, qreal &height);
+ int lineHeightOffset() const;
QString elidedText(qreal lineWidth, const QTextLine &line, QTextLine *nextLine = 0) const;
void elideFormats(int start, int length, int offset, QList<QTextLayout::FormatRange> *elidedFormats);