From 104fac002e228fb17304ec76d57febe33f8e8e7f Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Mon, 14 Jun 2010 12:45:10 +0200 Subject: Add QTextOption API to QStaticText To allow more control over the layout process for a QStaticText, introduce a QTextOption API. This will allow you to, e.g., set an alignment for the text layout. Patch also contains a couple of bug fixes to make right alignment work correctly when you set a text width on the QStaticText. Task-number: QTBUG-9031 Reviewed-by: Samuel --- src/gui/text/qstatictext.cpp | 34 ++++++++++++++++++++++++++++++---- src/gui/text/qstatictext.h | 5 ++++- src/gui/text/qstatictext_p.h | 6 +++++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/gui/text/qstatictext.cpp b/src/gui/text/qstatictext.cpp index 10870aa..c742455 100644 --- a/src/gui/text/qstatictext.cpp +++ b/src/gui/text/qstatictext.cpp @@ -324,6 +324,26 @@ QStaticText::PerformanceHint QStaticText::performanceHint() const } /*! + Sets the text option structure that controls the layout process to the given \a textOption. + + \sa textOption() +*/ +void QStaticText::setTextOption(const QTextOption &textOption) +{ + detach(); + data->textOption = textOption; + data->invalidate(); +} + +/*! + Returns the current text option used to control the layout process. +*/ +QTextOption QStaticText::textOption() const +{ + return data->textOption; +} + +/*! Sets the preferred width for this QStaticText. If the text is wider than the specified width, it will be broken into multiple lines and grow vertically. If the text cannot be split into multiple lines, it will be larger than the specified \a textWidth. @@ -580,6 +600,7 @@ void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p) QTextLayout textLayout; textLayout.setText(text); textLayout.setFont(font); + textLayout.setTextOption(textOption); qreal leading = QFontMetricsF(font).leading(); qreal height = -leading; @@ -610,21 +631,26 @@ void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p) .arg(QString::number(color.blue(), 16), 2, QLatin1Char('0'))); #endif document.setDefaultFont(font); - document.setDocumentMargin(0.0); - if (textWidth >= 0.0) - document.setTextWidth(textWidth); + document.setDocumentMargin(0.0); #ifndef QT_NO_TEXTHTMLPARSER document.setHtml(text); #else document.setPlainText(text); #endif + if (textWidth >= 0.0) + document.setTextWidth(textWidth); + else + document.adjustSize(); + document.setDefaultTextOption(textOption); - document.adjustSize(); p->save(); p->translate(topLeftPosition); document.drawContents(p); p->restore(); + if (textWidth >= 0.0) + document.adjustSize(); // Find optimal size + actualSize = document.size(); } } diff --git a/src/gui/text/qstatictext.h b/src/gui/text/qstatictext.h index f3bef93..4febde2 100644 --- a/src/gui/text/qstatictext.h +++ b/src/gui/text/qstatictext.h @@ -48,7 +48,7 @@ #include #include - +#include QT_BEGIN_HEADER @@ -79,6 +79,9 @@ public: void setTextWidth(qreal textWidth); qreal textWidth() const; + void setTextOption(const QTextOption &textOption); + QTextOption textOption() const; + QSizeF size() const; void prepare(const QTransform &matrix = QTransform(), const QFont &font = QFont()); diff --git a/src/gui/text/qstatictext_p.h b/src/gui/text/qstatictext_p.h index 1a96291..cb60626 100644 --- a/src/gui/text/qstatictext_p.h +++ b/src/gui/text/qstatictext_p.h @@ -53,6 +53,8 @@ // We mean it. // +#include "qstatictext.h" + #include #include @@ -148,12 +150,14 @@ public: QFixedPoint *positionPool; // 4 bytes per text QChar *charPool; // 4 bytes per text + QTextOption textOption; // 28 bytes per text + unsigned char needsRelayout : 1; // 1 byte per text unsigned char useBackendOptimizations : 1; unsigned char textFormat : 2; unsigned char untransformedCoordinates : 1; // ================ - // 167 bytes per text + // 195 bytes per text static QStaticTextPrivate *get(const QStaticText *q); }; -- cgit v0.12