diff options
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativetext.cpp')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativetext.cpp | 53 |
1 files changed, 44 insertions, 9 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index 03c9765..615c619 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -219,6 +219,7 @@ void QDeclarativeTextPrivate::updateSize() QFontMetrics fm(font); if (text.isEmpty()) { + q->setImplicitWidth(0); q->setImplicitHeight(fm.height()); emit q->paintedSizeChanged(); q->update(); @@ -284,37 +285,58 @@ 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; //set manual width - if (q->widthValid()) + if ((wrapMode != QDeclarativeText::NoWrap || elideMode != QDeclarativeText::ElideNone) && q->widthValid()) lineWidth = q->width(); QTextOption textOption = layout.textOption(); textOption.setWrapMode(QTextOption::WrapMode(wrapMode)); - textOption.setAlignment(Qt::Alignment(hAlign)); layout.setTextOption(textOption); layout.beginLayout(); - while (1) { + forever { QTextLine line = layout.createLine(); if (!line.isValid()) break; - if (q->widthValid()) { + if (lineWidth) line.setLineWidth(lineWidth); - line.setPosition(QPointF(0, height)); - height += line.height(); - } } layout.endLayout(); - return QSize(qCeil(layout.boundingRect().width()), layout.boundingRect().height()); + 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())); + } + } + + return layout.boundingRect().toAlignedRect().size(); } /*! @@ -326,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()) { |