Fix alignment bug in QQuickText with multi-line text and implicit width

If the horizontal alignment is not left and the width of the text is not
known during the first pass, we need to relayout once we know the
maximum line width.

Task-number: QTBUG-18617
Change-Id: I0cad100946beda151034067e23439185684de144
Reviewed-by: Andrew den Exter <andrew.den-exter@nokia.com>
This commit is contained in:
Yann Bodson 2012-05-03 15:00:33 +10:00 committed by Qt by Nokia
parent bf817782e6
commit f50c0af753
3 changed files with 73 additions and 1 deletions

View File

@ -916,9 +916,13 @@ QRectF QQuickTextPrivate::setupTextLayout(qreal *const naturalWidth, qreal *cons
&& (q->heightValid() || (maximumLineCountValid && canWrap));
const qreal oldWidth = lineWidth;
lineWidth = q->widthValid() && q->width() > 0 ? q->width() : FLT_MAX;
lineWidth = q->widthValid() && q->width() > 0 ? q->width() : *naturalWidth;
if (lineWidth != oldWidth && (singlelineElide || multilineElide || canWrap || horizontalFit))
continue;
// If the horizontal alignment is not left and the width was not valid we need to relayout
// now that we know the maximum line width.
if (!q->widthValid() && maxLineCount > 1 && q->effectiveHAlign() != QQuickText::AlignLeft)
continue;
}
// If the next needs to be elided and there's an abbreviated string available

View File

@ -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
}
}

View File

@ -90,6 +90,7 @@ private slots:
void horizontalAlignment();
void horizontalAlignment_RightToLeft();
void verticalAlignment();
void hAlignImplicitWidth();
void font();
void style();
void color();
@ -158,6 +159,7 @@ private:
QQmlEngine engine;
QQuickView *createView(const QString &filename);
int numberOfNonWhitePixels(int fromX, int toX, const QImage &image);
};
tst_qquicktext::tst_qquicktext()
@ -836,6 +838,59 @@ void tst_qquicktext::horizontalAlignment_RightToLeft()
delete textObject;
}
int tst_qquicktext::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_qquicktext::hAlignImplicitWidth()
{
QQuickView view(testFileUrl("hAlignImplicitWidth.qml"));
view.show();
view.requestActivateWindow();
QTest::qWaitForWindowShown(&view);
QQuickText *text = view.rootObject()->findChild<QQuickText*>("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);
}
}
void tst_qquicktext::verticalAlignment()
{
//test one align each, and then test if two align fails.