diff options
author | Yann Bodson <yann.bodson@nokia.com> | 2010-11-08 03:34:05 (GMT) |
---|---|---|
committer | Yann Bodson <yann.bodson@nokia.com> | 2010-11-08 03:36:40 (GMT) |
commit | 0f1599edcc8624682efb9514bb9b4ac0519c7017 (patch) | |
tree | a55bfaea979c174d61d44015588cad3b3547504b /src/declarative/graphicsitems | |
parent | afa9d82702cbce1a23f951ed045d038f1d16b417 (diff) | |
download | Qt-0f1599edcc8624682efb9514bb9b4ac0519c7017.zip Qt-0f1599edcc8624682efb9514bb9b4ac0519c7017.tar.gz Qt-0f1599edcc8624682efb9514bb9b4ac0519c7017.tar.bz2 |
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
Diffstat (limited to 'src/declarative/graphicsitems')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativetext.cpp | 58 |
1 files changed, 46 insertions, 12 deletions
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()) { |