From 57ddd7c69705dfbf3d06b8a0157e5e706120c818 Mon Sep 17 00:00:00 2001 From: Christopher Ham Date: Fri, 28 Jan 2011 12:55:57 +1000 Subject: Fix MaximumLineCount in Text and add tests Fixed MaximumLineCount not truncating when wrapMode was not set. Visual autotests were added to test various possible options for maximumLineCount. Task-number: QTBUG-12305 Reviewed-by: Michael Brasser --- src/declarative/graphicsitems/qdeclarativetext.cpp | 44 ++++++++++----------- .../maximumLineCount/data-X11/newLines.0.png | Bin 0 -> 343 bytes .../maximumLineCount/data-X11/newLines.qml | 11 ++++++ .../maximumLineCount/data-X11/newLines_wrap.0.png | Bin 0 -> 465 bytes .../maximumLineCount/data-X11/newLines_wrap.qml | 11 ++++++ .../data-X11/newLines_wrap_elide.0.png | Bin 0 -> 469 bytes .../data-X11/newLines_wrap_elide.qml | 11 ++++++ .../maximumLineCount/data-X11/wrap.0.png | Bin 0 -> 557 bytes .../maximumLineCount/data-X11/wrap.qml | 11 ++++++ .../maximumLineCount/data-X11/wrap_elide.0.png | Bin 0 -> 537 bytes .../maximumLineCount/data-X11/wrap_elide.qml | 11 ++++++ .../maximumLineCount/data/newLines.0.png | Bin 0 -> 343 bytes .../maximumLineCount/data/newLines.qml | 11 ++++++ .../maximumLineCount/data/newLines_wrap.0.png | Bin 0 -> 465 bytes .../maximumLineCount/data/newLines_wrap.qml | 11 ++++++ .../data/newLines_wrap_elide.0.png | Bin 0 -> 469 bytes .../maximumLineCount/data/newLines_wrap_elide.qml | 11 ++++++ .../maximumLineCount/data/wrap.0.png | Bin 0 -> 557 bytes .../maximumLineCount/data/wrap.qml | 11 ++++++ .../maximumLineCount/data/wrap_elide.0.png | Bin 0 -> 537 bytes .../maximumLineCount/data/wrap_elide.qml | 11 ++++++ .../qdeclarativetext/maximumLineCount/newLines.qml | 13 ++++++ .../maximumLineCount/newLines_wrap.qml | 14 +++++++ .../maximumLineCount/newLines_wrap_elide.qml | 15 +++++++ .../qdeclarativetext/maximumLineCount/wrap.qml | 14 +++++++ .../maximumLineCount/wrap_elide.qml | 15 +++++++ 26 files changed, 201 insertions(+), 24 deletions(-) create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap_elide.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap_elide.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap_elide.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap_elide.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap_elide.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap_elide.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap_elide.0.png create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap_elide.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines_wrap.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines_wrap_elide.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/wrap.qml create mode 100644 tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/wrap_elide.qml diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index d424e30..1e0988d 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -366,6 +366,8 @@ QSize QDeclarativeTextPrivate::setupTextLayout() if (maximumLineCountValid) { layout.beginLayout(); + if (!lineWidth) + lineWidth = INT_MAX; int y = 0; int linesLeft = maximumLineCount; while (linesLeft > 0) { @@ -374,34 +376,28 @@ QSize QDeclarativeTextPrivate::setupTextLayout() break; visibleCount++; - - if (lineWidth) { - if (--linesLeft == 0) { - line.setLineWidth(q->width()*2); // Set out more than is required, but not too much. - if (line.naturalTextWidth() > lineWidth) - line.setLineWidth(lineWidth - elideWidth); - visibleTextLength += line.textLength(); // Used to catch new lines that are shorter than the layout width. - - if (visibleTextLength < text.length()) { - truncate = true; - if (elideMode==QDeclarativeText::ElideRight) { - // Need to correct for alignment - int x = line.naturalTextWidth(); - if (hAlign == QDeclarativeText::AlignRight) { - x = q->width()-elideWidth; - } else if (hAlign == QDeclarativeText::AlignHCenter) { - x = (q->width()+line.naturalTextWidth()-elideWidth)/2; - } - elidePos = QPointF(x, y + fm.ascent()); + line.setLineWidth(lineWidth); + visibleTextLength += line.textLength(); + + if (--linesLeft == 0) { + if (visibleTextLength < text.length()) { + truncate = true; + if (elideMode==QDeclarativeText::ElideRight && q->widthValid()) { + // Need to correct for alignment + line.setLineWidth(lineWidth-elideWidth); + int x = line.naturalTextWidth(); + if (hAlign == QDeclarativeText::AlignRight) { + x = q->width()-elideWidth; + } else if (hAlign == QDeclarativeText::AlignHCenter) { + x = (q->width()+line.naturalTextWidth()-elideWidth)/2; } + elidePos = QPointF(x, y + fm.ascent()); + elideText = true; } - } else { - line.setLineWidth(lineWidth); - visibleTextLength += line.textLength(); } - - y += line.height(); } + + y += line.height(); } layout.endLayout(); diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines.0.png new file mode 100644 index 0000000..c24c7c6 Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines.qml new file mode 100644 index 0000000..affd3a6 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "newLines.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap.0.png new file mode 100644 index 0000000..0ba68e5 Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap.qml new file mode 100644 index 0000000..410b47d --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "newLines_wrap.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap_elide.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap_elide.0.png new file mode 100644 index 0000000..3f1144b Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap_elide.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap_elide.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap_elide.qml new file mode 100644 index 0000000..94cf1d2 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/newLines_wrap_elide.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "newLines_wrap_elide.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap.0.png new file mode 100644 index 0000000..f0ad965 Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap.qml new file mode 100644 index 0000000..4b55312 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "wrap.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap_elide.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap_elide.0.png new file mode 100644 index 0000000..62274ce Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap_elide.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap_elide.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap_elide.qml new file mode 100644 index 0000000..7d002ea --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data-X11/wrap_elide.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "wrap_elide.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines.0.png new file mode 100644 index 0000000..c24c7c6 Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines.qml new file mode 100644 index 0000000..affd3a6 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "newLines.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap.0.png new file mode 100644 index 0000000..0ba68e5 Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap.qml new file mode 100644 index 0000000..410b47d --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "newLines_wrap.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap_elide.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap_elide.0.png new file mode 100644 index 0000000..3f1144b Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap_elide.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap_elide.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap_elide.qml new file mode 100644 index 0000000..94cf1d2 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/newLines_wrap_elide.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "newLines_wrap_elide.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap.0.png new file mode 100644 index 0000000..f0ad965 Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap.qml new file mode 100644 index 0000000..4b55312 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "wrap.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap_elide.0.png b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap_elide.0.png new file mode 100644 index 0000000..62274ce Binary files /dev/null and b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap_elide.0.png differ diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap_elide.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap_elide.qml new file mode 100644 index 0000000..7d002ea --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/data/wrap_elide.qml @@ -0,0 +1,11 @@ +import Qt.VisualTest 4.7 + +VisualTest { + Frame { + msec: 0 + } + Frame { + msec: 16 + image: "wrap_elide.0.png" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines.qml new file mode 100644 index 0000000..4e993c6 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines.qml @@ -0,0 +1,13 @@ +import QtQuick 1.0 +import "../../shared" 1.0 + +Rectangle { + width: childrenRect.width + height: childrenRect.height + + TestText { + width: 80 + maximumLineCount: 2 + text: "Line1\nLine2\nLine3\nLine4" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines_wrap.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines_wrap.qml new file mode 100644 index 0000000..5d8f50b --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines_wrap.qml @@ -0,0 +1,14 @@ +import QtQuick 1.0 +import "../../shared" 1.0 + +Rectangle { + width: childrenRect.width + height: childrenRect.height + + TestText { + width: 80 + maximumLineCount: 3 + wrapMode: Text.WordWrap + text: "Line1 has a more\nLine2\nLine3\nLine4" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines_wrap_elide.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines_wrap_elide.qml new file mode 100644 index 0000000..ac7b5cb --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/newLines_wrap_elide.qml @@ -0,0 +1,15 @@ +import QtQuick 1.0 +import "../../shared" 1.0 + +Rectangle { + width: childrenRect.width + height: childrenRect.height + + TestText { + width: 80 + maximumLineCount: 3 + wrapMode: Text.WordWrap + elide: Text.ElideRight + text: "Line1 has a more\nLine2\nLine3\nLine4" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/wrap.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/wrap.qml new file mode 100644 index 0000000..6d72430 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/wrap.qml @@ -0,0 +1,14 @@ +import QtQuick 1.0 +import "../../shared" 1.0 + +Rectangle { + width: childrenRect.width + height: childrenRect.height + + TestText { + width: 80 + maximumLineCount: 3 + wrapMode: Text.WordWrap + text: "The quick brown fox jumped over the lazy dog" + } +} diff --git a/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/wrap_elide.qml b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/wrap_elide.qml new file mode 100644 index 0000000..785c5f5 --- /dev/null +++ b/tests/auto/declarative/qmlvisual/qdeclarativetext/maximumLineCount/wrap_elide.qml @@ -0,0 +1,15 @@ +import QtQuick 1.0 +import "../../shared" 1.0 + +Rectangle { + width: childrenRect.width + height: childrenRect.height + + TestText { + width: 80 + maximumLineCount: 3 + wrapMode: Text.WordWrap + elide: Text.ElideRight + text: "The quick brown fox jumped over the lazy dog" + } +} -- cgit v0.12