From 6e5a642c9484536fc173714f560f739944368cf5 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Thu, 21 Apr 2011 14:15:38 +1000 Subject: Elide has unexpected effect on Text's implicitWidth The elided string was used to calculate the implicitWidth rather than the full string. Change-Id: I51b8800b47d4e32f4d5eef07c71df10e2df905b7 Task-number: QTBUG-18627 Reviewed-by: Michael Brasser --- src/declarative/graphicsitems/qdeclarativetext.cpp | 23 +++++++++++++++++----- .../graphicsitems/qdeclarativetext_p_p.h | 1 + .../qdeclarativetext/data/elideimplicitwidth.qml | 7 +++++++ .../qdeclarativetext/tst_qdeclarativetext.cpp | 7 +++++++ 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativetext/data/elideimplicitwidth.qml diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 720692c..1d51840 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -101,7 +101,8 @@ QDeclarativeTextPrivate::QDeclarativeTextPrivate() format(QDeclarativeText::AutoText), wrapMode(QDeclarativeText::NoWrap), lineHeight(1), lineHeightMode(QDeclarativeText::ProportionalHeight), lineCount(1), truncated(false), maximumLineCount(INT_MAX), maximumLineCountValid(false), imageCacheDirty(true), updateOnComponentComplete(true), richText(false), singleline(false), - cacheAllTextAsImage(true), internalWidthUpdate(false), requireImplicitWidth(false), hAlignImplicit(true), rightToLeftText(false), naturalWidth(0), doc(0) + cacheAllTextAsImage(true), internalWidthUpdate(false), requireImplicitWidth(false), hAlignImplicit(true), + rightToLeftText(false), layoutTextElided(false), naturalWidth(0), doc(0) { cacheAllTextAsImage = enableImageCache(); QGraphicsItemPrivate::acceptedMouseButtons = Qt::LeftButton; @@ -217,6 +218,7 @@ void QDeclarativeTextPrivate::updateLayout() return; } + layoutTextElided = false; // Setup instance of QTextLayout for all cases other than richtext if (!richText) { layout.clearLayout(); @@ -227,10 +229,13 @@ void QDeclarativeTextPrivate::updateLayout() singleline = !tmp.contains(QChar::LineSeparator); if (singleline && !maximumLineCountValid && elideMode != QDeclarativeText::ElideNone && q->widthValid()) { QFontMetrics fm(font); - tmp = fm.elidedText(tmp,(Qt::TextElideMode)elideMode,q->width()); // XXX still worth layout...? - if (tmp != text && !truncated) { - truncated = true; - emit q->truncatedChanged(); + tmp = fm.elidedText(tmp,(Qt::TextElideMode)elideMode,q->width()); + if (tmp != text) { + layoutTextElided = true; + if (!truncated) { + truncated = true; + emit q->truncatedChanged(); + } } } layout.setText(tmp); @@ -377,6 +382,12 @@ QRect QDeclarativeTextPrivate::setupTextLayout() if (requireImplicitWidth && q->widthValid()) { // requires an extra layout + QString elidedText; + if (layoutTextElided) { + // We have provided elided text to the layout, but we must calculate unelided width. + elidedText = layout.text(); + layout.setText(text); + } layout.beginLayout(); forever { QTextLine line = layout.createLine(); @@ -390,6 +401,8 @@ QRect QDeclarativeTextPrivate::setupTextLayout() br = br.united(line.naturalTextRect()); } naturalWidth = br.width(); + if (layoutTextElided) + layout.setText(elidedText); } if (maximumLineCountValid) { diff --git a/src/declarative/graphicsitems/qdeclarativetext_p_p.h b/src/declarative/graphicsitems/qdeclarativetext_p_p.h index e3ab62a..6a3d581 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p_p.h @@ -116,6 +116,7 @@ public: bool requireImplicitWidth:1; bool hAlignImplicit:1; bool rightToLeftText:1; + bool layoutTextElided:1; QRect layedOutTextRect; QSize paintedSize; diff --git a/tests/auto/declarative/qdeclarativetext/data/elideimplicitwidth.qml b/tests/auto/declarative/qdeclarativetext/data/elideimplicitwidth.qml new file mode 100644 index 0000000..60ae15c --- /dev/null +++ b/tests/auto/declarative/qdeclarativetext/data/elideimplicitwidth.qml @@ -0,0 +1,7 @@ +import QtQuick 1.1 + +Text { + text: "Hello World" + elide: Text.ElideRight + width: 30 +} diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp index ca6e87a..82b6f73 100644 --- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp +++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp @@ -390,6 +390,13 @@ void tst_qdeclarativetext::elide() QCOMPARE(textObject->width(), 100.); } } + + // QTBUG-18627 + QUrl qmlfile = QUrl::fromLocalFile(SRCDIR "/data/elideimplicitwidth.qml"); + QDeclarativeComponent textComponent(&engine, qmlfile); + QDeclarativeItem *item = qobject_cast(textComponent.create()); + QVERIFY(item != 0); + QVERIFY(item->implicitWidth() > item->width()); } void tst_qdeclarativetext::textFormat() -- cgit v0.12