From 0f1599edcc8624682efb9514bb9b4ac0519c7017 Mon Sep 17 00:00:00 2001 From: Yann Bodson Date: Mon, 8 Nov 2010 13:34:05 +1000 Subject: Text alignment is broken with multi-line text and implicit size. Also add some visual tests for multi-line text elememts. Task-number: QTBUG-15018 --- src/declarative/graphicsitems/qdeclarativetext.cpp | 58 +++++++++++++++---- .../qmlvisual/qdeclarativetext/font/TestText.qml | 13 +++++ .../font/data-MAC/plaintext3.0.png | Bin 0 -> 53503 bytes .../qdeclarativetext/font/data-MAC/plaintext3.qml | 11 ++++ .../qdeclarativetext/font/data/plaintext3.0.png | Bin 0 -> 53503 bytes .../qdeclarativetext/font/data/plaintext3.qml | 11 ++++ .../qmlvisual/qdeclarativetext/font/plaintext3.qml | 62 +++++++++++++++++++++ 7 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/font/TestText.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/font/data-MAC/plaintext3.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/font/data-MAC/plaintext3.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/font/data/plaintext3.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/font/data/plaintext3.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/font/plaintext3.qml diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 84f276e..615c619 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -285,35 +285,56 @@ QSize QDeclarativeTextPrivate::setupTextLayout() { // ### text layout handling should be profiled and optimized as needed // what about QStackTextEngine engine(tmp, d->font.font()); QTextLayout textLayout(&engine); - Q_Q(QDeclarativeText); layout.setCacheEnabled(true); qreal height = 0; + qreal widthUsed = 0; qreal lineWidth = 0; - QTextOption textOption = layout.textOption(); - textOption.setWrapMode(QTextOption::NoWrap); - textOption.setAlignment(Qt::Alignment(hAlign)); - - // if the item has an explicit width, we set the line width and enable wrapping - if (q->widthValid()) { + //set manual width + if ((wrapMode != QDeclarativeText::NoWrap || elideMode != QDeclarativeText::ElideNone) && q->widthValid()) lineWidth = q->width(); - textOption.setWrapMode(QTextOption::WrapMode(wrapMode)); - } + QTextOption textOption = layout.textOption(); + textOption.setWrapMode(QTextOption::WrapMode(wrapMode)); layout.setTextOption(textOption); + layout.beginLayout(); - while (1) { + forever { QTextLine line = layout.createLine(); if (!line.isValid()) break; - line.setLineWidth(lineWidth); + if (lineWidth) + line.setLineWidth(lineWidth); + } + layout.endLayout(); + + for (int i = 0; i < layout.lineCount(); ++i) { + QTextLine line = layout.lineAt(i); + widthUsed = qMax(widthUsed, line.naturalTextWidth()); + } + + qreal layoutWidth = q->widthValid() ? q->width() : widthUsed; + + qreal x = 0; + for (int i = 0; i < layout.lineCount(); ++i) { + QTextLine line = layout.lineAt(i); line.setPosition(QPointF(0, height)); height += line.height(); + + if (!cacheAllTextAsImage) { + if (hAlign == QDeclarativeText::AlignLeft) { + x = 0; + } else if (hAlign == QDeclarativeText::AlignRight) { + x = layoutWidth - line.naturalTextWidth(); + } else if (hAlign == QDeclarativeText::AlignHCenter) { + x = (layoutWidth - line.naturalTextWidth()) / 2; + } + line.setPosition(QPointF(x, line.y())); + } } - layout.endLayout(); return layout.boundingRect().toAlignedRect().size(); } @@ -327,6 +348,19 @@ QPixmap QDeclarativeTextPrivate::textLayoutImage(bool drawStyle) //do layout QSize size = layedOutTextSize; + qreal x = 0; + for (int i = 0; i < layout.lineCount(); ++i) { + QTextLine line = layout.lineAt(i); + if (hAlign == QDeclarativeText::AlignLeft) { + x = 0; + } else if (hAlign == QDeclarativeText::AlignRight) { + x = size.width() - line.naturalTextWidth(); + } else if (hAlign == QDeclarativeText::AlignHCenter) { + x = (size.width() - line.naturalTextWidth()) / 2; + } + line.setPosition(QPointF(x, line.y())); + } + //paint text QPixmap img(size); if (!size.isEmpty()) { diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/font/TestText.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/TestText.qml new file mode 100644 index 0000000..690cb15 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/TestText.qml @@ -0,0 +1,13 @@ +import QtQuick 1.0 + +Text { + id: testText + + property color bcolor: "blue" + + text: "The quick brown fox\njumps over\nthe lazy dog." + font.family: "Helvetica" + font.pointSize: 16 + + Rectangle { id: borderr; color: "transparent"; border.color: bcolor; anchors.fill: parent; opacity: 0.2 } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data-MAC/plaintext3.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data-MAC/plaintext3.0.png new file mode 100644 index 0000000..0d3c672 Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data-MAC/plaintext3.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data-MAC/plaintext3.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data-MAC/plaintext3.qml new file mode 100644 index 0000000..13f413a --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data-MAC/plaintext3.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "plaintext3.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data/plaintext3.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data/plaintext3.0.png new file mode 100644 index 0000000..0d3c672 Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data/plaintext3.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data/plaintext3.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data/plaintext3.qml new file mode 100644 index 0000000..13f413a --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/data/plaintext3.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "plaintext3.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/font/plaintext3.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/plaintext3.qml new file mode 100644 index 0000000..087dfbe --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/font/plaintext3.qml @@ -0,0 +1,62 @@ +import QtQuick 1.0 + +Rectangle { + id: main + width: 800; height: 600 + + + Grid { + x: 4; y: 4 + spacing: 8 + columns: 4 + + Column { + spacing: 4 + TestText { } + TestText { horizontalAlignment: Text.AlignHCenter } + TestText { horizontalAlignment: Text.AlignRight } + } + + Column { + spacing: 4 + TestText { wrapMode: Text.Wrap } + TestText { horizontalAlignment: Text.AlignHCenter; wrapMode: Text.Wrap } + TestText { horizontalAlignment: Text.AlignRight; wrapMode: Text.Wrap } + } + + Column { + spacing: 4 + TestText { wrapMode: Text.Wrap; elide: Text.ElideRight } + TestText { horizontalAlignment: Text.AlignHCenter; wrapMode: Text.Wrap; elide: Text.ElideRight } + TestText { horizontalAlignment: Text.AlignRight; wrapMode: Text.Wrap; elide: Text.ElideRight } + } + + Column { + spacing: 4 + TestText { width: 230; wrapMode: Text.Wrap; elide: Text.ElideRight } + TestText { width: 230; horizontalAlignment: Text.AlignHCenter; wrapMode: Text.Wrap; elide: Text.ElideRight } + TestText { width: 230; horizontalAlignment: Text.AlignRight; wrapMode: Text.Wrap; elide: Text.ElideRight } + } + + Column { + spacing: 4 + TestText { width: 120; wrapMode: Text.Wrap; elide: Text.ElideRight } + TestText { width: 120; horizontalAlignment: Text.AlignHCenter; wrapMode: Text.Wrap; elide: Text.ElideRight } + TestText { width: 120; horizontalAlignment: Text.AlignRight; wrapMode: Text.Wrap; elide: Text.ElideRight } + } + + Column { + spacing: 4 + TestText { width: 120; wrapMode: Text.Wrap } + TestText { width: 120; horizontalAlignment: Text.AlignHCenter; wrapMode: Text.Wrap } + TestText { width: 120; horizontalAlignment: Text.AlignRight; wrapMode: Text.Wrap } + } + + Column { + spacing: 4 + TestText { width: 120 } + TestText { width: 120; horizontalAlignment: Text.AlignHCenter } + TestText { width: 120; horizontalAlignment: Text.AlignRight } + } + } +} -- cgit v0.12