From 825f53b1a5bfff4bec3f5a1d4d25308477b9a98f Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Wed, 16 May 2012 16:07:47 +1000 Subject: [PATCH] Fix incorrect text alignment in TextEdit. QTextDocument always spans the full width of a TextEdit so the text node shouldn't be offset by the x position of the bounding rect. Change-Id: I882a44baeea4d054279cadc5632df6d8863caea1 Reviewed-by: Yann Bodson --- src/quick/items/qquicktextedit.cpp | 2 +- .../quick/qquicktextedit/data/hAlignVisual.qml | 13 ++++ .../quick/qquicktextedit/tst_qquicktextedit.cpp | 89 ++++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 tests/auto/quick/qquicktextedit/data/hAlignVisual.qml diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index cfb07f0..2d16c02 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -1624,7 +1624,7 @@ QSGNode *QQuickTextEdit::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData * QRectF bounds = boundingRect(); - node->addTextDocument(bounds.topLeft(), d->document, d->color, QQuickText::Normal, QColor(), + node->addTextDocument(QPointF(0, bounds.y()), d->document, d->color, QQuickText::Normal, QColor(), QColor(), d->selectionColor, d->selectedTextColor, selectionStart(), selectionEnd() - 1); // selectionEnd() returns first char after // selection diff --git a/tests/auto/quick/qquicktextedit/data/hAlignVisual.qml b/tests/auto/quick/qquicktextedit/data/hAlignVisual.qml new file mode 100644 index 0000000..136e5d2 --- /dev/null +++ b/tests/auto/quick/qquicktextedit/data/hAlignVisual.qml @@ -0,0 +1,13 @@ +import QtQuick 2.0 + +Rectangle { + width: 200 + height: 100 + + Text { + objectName: "textItem" + text: "AA\nBBBBB\nCCCCCCCCCCCCCCCC" + anchors.centerIn: parent + horizontalAlignment: Text.AlignLeft + } +} diff --git a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp index c7bf6af..34d7ced 100644 --- a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp @@ -109,6 +109,7 @@ private slots: // ### these tests may be trivial void hAlign(); void hAlign_RightToLeft(); + void hAlignVisual(); void vAlign(); void font(); void color(); @@ -792,6 +793,94 @@ void tst_qquicktextedit::hAlign_RightToLeft() QVERIFY(emptyEdit->cursorRectangle().left() > canvas.width()/2); } + +static int numberOfNonWhitePixels(int fromX, int toX, const QImage &image) +{ + int pixels = 0; + for (int x = fromX; x < toX; ++x) { + for (int y = 0; y < image.height(); ++y) { + if (image.pixel(x, y) != qRgb(255, 255, 255)) + pixels++; + } + } + return pixels; +} + +void tst_qquicktextedit::hAlignVisual() +{ + QQuickView view(testFileUrl("hAlignVisual.qml")); + view.show(); + view.requestActivateWindow(); + QTest::qWaitForWindowShown(&view); + + QQuickText *text = view.rootObject()->findChild("textItem"); + QVERIFY(text != 0); + { + // Left Align + QImage image = view.grabFrameBuffer(); + int left = numberOfNonWhitePixels(0, image.width() / 3, image); + int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image); + int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image); + QVERIFY(left > mid); + QVERIFY(mid > right); + } + { + // HCenter Align + text->setHAlign(QQuickText::AlignHCenter); + QImage image = view.grabFrameBuffer(); + int left = numberOfNonWhitePixels(0, image.width() / 3, image); + int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image); + int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image); + QVERIFY(left < mid); + QVERIFY(mid > right); + } + { + // Right Align + text->setHAlign(QQuickText::AlignRight); + QImage image = view.grabFrameBuffer(); + int left = numberOfNonWhitePixels(0, image.width() / 3, image); + int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image); + int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image); + QVERIFY(left < mid); + QVERIFY(mid < right); + } + + text->setWidth(200); + + { + // Left Align + QImage image = view.grabFrameBuffer(); + int x = qCeil(text->implicitWidth()); + int left = numberOfNonWhitePixels(0, x, image); + int right = numberOfNonWhitePixels(x, image.width() - x, image); + QVERIFY(left > 0); + QVERIFY(right == 0); + } + { + // HCenter Align + text->setHAlign(QQuickText::AlignHCenter); + QImage image = view.grabFrameBuffer(); + int x1 = qFloor(image.width() - text->implicitWidth()) / 2; + int x2 = image.width() - x1; + int left = numberOfNonWhitePixels(0, x1, image); + int mid = numberOfNonWhitePixels(x1, x2 - x1, image); + int right = numberOfNonWhitePixels(x2, image.width() - x2, image); + QVERIFY(left == 0); + QVERIFY(mid > 0); + QVERIFY(right == 0); + } + { + // Right Align + text->setHAlign(QQuickText::AlignRight); + QImage image = view.grabFrameBuffer(); + int x = image.width() - qCeil(text->implicitWidth()); + int left = numberOfNonWhitePixels(0, x, image); + int right = numberOfNonWhitePixels(x, image.width() - x, image); + QVERIFY(left == 0); + QVERIFY(right > 0); + } +} + void tst_qquicktextedit::vAlign() { //test one align each, and then test if two align fails. -- 2.7.4