Skip to content

Commit

Permalink
Reduce the size of QTextPrivate.
Browse files Browse the repository at this point in the history
Remove some unnecessary members, store colors as QRgb values instead
of QColor, and reorder members to minimise alignment padding.

Change-Id: Id3958429008c97a5714734a529250fe881e2161b
Reviewed-by: Yann Bodson <yann.bodson@nokia.com>
  • Loading branch information
Andrew den Exter authored and Qt by Nokia committed Feb 21, 2012
1 parent ed20c5e commit e060245
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 88 deletions.
88 changes: 47 additions & 41 deletions src/quick/items/qquicktext.cpp
Expand Up @@ -71,21 +71,21 @@ QT_BEGIN_NAMESPACE
const QChar QQuickTextPrivate::elideChar = QChar(0x2026);

QQuickTextPrivate::QQuickTextPrivate()
: color((QRgb)0), linkColor((QRgb)255), style(QQuickText::Normal), hAlign(QQuickText::AlignLeft),
vAlign(QQuickText::AlignTop), elideMode(QQuickText::ElideNone),
format(QQuickText::AutoText), wrapMode(QQuickText::NoWrap), lineHeight(1),
lineHeightMode(QQuickText::ProportionalHeight), lineCount(1), maximumLineCount(INT_MAX),
maximumLineCountValid(false), fontSizeMode(QQuickText::FixedSize), multilengthEos(-1),
minimumPixelSize(12), minimumPointSize(12), updateOnComponentComplete(true),
richText(false), styledText(false), singleline(false), internalWidthUpdate(false),
requireImplicitWidth(false), truncated(false), hAlignImplicit(true), rightToLeftText(false),
layoutTextElided(false), textHasChanged(true),
needToUpdateLayout(false), naturalWidth(0), doc(0), elideLayout(0), textLine(0),
updateType(UpdatePaintNode), nbActiveDownloads(0)

: lineHeight(1)
, elideLayout(0), textLine(0), doc(0)
#if defined(Q_OS_MAC)
, layoutThread(0), paintingThread(0)
, layoutThread(0), paintingThread(0)
#endif
, color(0xFF000000), linkColor(0xFF0000FF), styleColor(0xFF000000)
, lineCount(1), maximumLineCount(INT_MAX), multilengthEos(-1), minimumPixelSize(12), minimumPointSize(12), nbActiveDownloads(0)
, hAlign(QQuickText::AlignLeft), vAlign(QQuickText::AlignTop), elideMode(QQuickText::ElideNone)
, format(QQuickText::AutoText), wrapMode(QQuickText::NoWrap)
, lineHeightMode(QQuickText::ProportionalHeight), style(QQuickText::Normal)
, fontSizeMode(QQuickText::FixedSize), updateType(UpdatePaintNode)
, maximumLineCountValid(false), updateOnComponentComplete(true), richText(false)
, styledText(false), singleline(false), internalWidthUpdate(false), requireImplicitWidth(false)
, truncated(false), hAlignImplicit(true), rightToLeftText(false)
, layoutTextElided(false), textHasChanged(true), needToUpdateLayout(false)
{
}

Expand Down Expand Up @@ -378,23 +378,25 @@ void QQuickTextPrivate::updateSize()
if (text.isEmpty()) {
qreal fontHeight = fm.height();
q->setImplicitSize(0, fontHeight);
contentSize = QSize(0, fontHeight);
layedOutTextRect = QRect(0, 0, 0, fontHeight);
emit q->contentSizeChanged();
updateType = UpdatePaintNode;
q->update();
return;
}

qreal naturalWidth = 0;

int dy = q->height();
QSize size(0, 0);

QSize previousSize = layedOutTextRect.size();
#if defined(Q_OS_MAC)
layoutThread = QThread::currentThread();
#endif

//setup instance of QTextLayout for all cases other than richtext
if (!richText) {
QRect textRect = setupTextLayout();
QRect textRect = setupTextLayout(&naturalWidth);
layedOutTextRect = textRect;
size = textRect.size();
dy -= size.height();
Expand Down Expand Up @@ -450,10 +452,8 @@ void QQuickTextPrivate::updateSize()

if (iWidth == -1)
q->setImplicitHeight(size.height());
if (contentSize != size) {
contentSize = size;
if (layedOutTextRect.size() != previousSize)
emit q->contentSizeChanged();
}
updateType = UpdatePaintNode;
q->update();
}
Expand Down Expand Up @@ -618,7 +618,7 @@ QString QQuickTextPrivate::elidedText(int lineWidth, const QTextLine &line, QTex
already absolutely positioned horizontally).
*/

QRect QQuickTextPrivate::setupTextLayout()
QRect QQuickTextPrivate::setupTextLayout(qreal *const naturalWidth)
{
Q_Q(QQuickText);
layout.setCacheEnabled(true);
Expand Down Expand Up @@ -652,7 +652,7 @@ QRect QQuickTextPrivate::setupTextLayout()
break;
}
layout.endLayout();
naturalWidth = layout.maximumWidth();
*naturalWidth = layout.maximumWidth();
layout.clearLayout();
}

Expand Down Expand Up @@ -695,7 +695,7 @@ QRect QQuickTextPrivate::setupTextLayout()
QString elideText;
bool once = true;

naturalWidth = 0;
*naturalWidth = 0;

int eos = multilengthEos;

Expand Down Expand Up @@ -815,7 +815,7 @@ QRect QQuickTextPrivate::setupTextLayout()

// Save the implicitWidth of the text on the first layout only.
if (once) {
naturalWidth = layout.maximumWidth();
*naturalWidth = layout.maximumWidth();
once = false;

if (requireImplicitWidth
Expand All @@ -831,7 +831,7 @@ QRect QQuickTextPrivate::setupTextLayout()
if (!line.isValid())
break;
}
naturalWidth = qMax(naturalWidth, widthLayout.maximumWidth());
*naturalWidth = qMax(*naturalWidth, widthLayout.maximumWidth());
}
}

Expand Down Expand Up @@ -1322,21 +1322,22 @@ void QQuickText::setText(const QString &n)
QColor QQuickText::color() const
{
Q_D(const QQuickText);
return d->color;
return QColor::fromRgba(d->color);
}

void QQuickText::setColor(const QColor &color)
{
Q_D(QQuickText);
if (d->color == color)
QRgb rgb = color.rgba();
if (d->color == rgb)
return;

d->color = color;
d->color = rgb;
if (isComponentComplete()) {
d->updateType = QQuickTextPrivate::UpdatePaintNode;
update();
}
emit colorChanged(d->color);
emit colorChanged();
}

/*!
Expand All @@ -1352,16 +1353,17 @@ void QQuickText::setColor(const QColor &color)
QColor QQuickText::linkColor() const
{
Q_D(const QQuickText);
return d->linkColor;
return QColor::fromRgba(d->linkColor);
}

void QQuickText::setLinkColor(const QColor &color)
{
Q_D(QQuickText);
if (d->linkColor == color)
QRgb rgb = color.rgba();
if (d->linkColor == rgb)
return;

d->linkColor = color;
d->linkColor = rgb;
update();
emit linkColorChanged();
}
Expand Down Expand Up @@ -1428,21 +1430,22 @@ void QQuickText::setStyle(QQuickText::TextStyle style)
QColor QQuickText::styleColor() const
{
Q_D(const QQuickText);
return d->styleColor;
return QColor::fromRgba(d->styleColor);
}

void QQuickText::setStyleColor(const QColor &color)
{
Q_D(QQuickText);
if (d->styleColor == color)
QRgb rgb = color.rgba();
if (d->styleColor == rgb)
return;

d->styleColor = color;
d->styleColor = rgb;
if (isComponentComplete()) {
d->updateType = QQuickTextPrivate::UpdatePaintNode;
update();
}
emit styleColorChanged(d->styleColor);
emit styleColorChanged();
}

/*!
Expand Down Expand Up @@ -1662,7 +1665,6 @@ void QQuickText::resetMaximumLineCount()
{
Q_D(QQuickText);
setMaximumLineCount(INT_MAX);
d->elidePos = QPointF();
if (d->truncated != false) {
d->truncated = false;
emit truncatedChanged();
Expand Down Expand Up @@ -1993,13 +1995,17 @@ QSGNode *QQuickText::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data
node->deleteContent();
node->setMatrix(QMatrix4x4());

const QColor color = QColor::fromRgba(d->color);
const QColor styleColor = QColor::fromRgba(d->styleColor);
const QColor linkColor = QColor::fromRgba(d->linkColor);

if (d->richText) {
d->ensureDoc();
node->addTextDocument(bounds.topLeft(), d->doc, d->color, d->style, d->styleColor, d->linkColor);
node->addTextDocument(bounds.topLeft(), d->doc, color, d->style, styleColor, linkColor);
} else if (d->elideMode == QQuickText::ElideNone || bounds.width() > 0.) {
node->addTextLayout(QPoint(0, bounds.y()), &d->layout, d->color, d->style, d->styleColor, d->linkColor);
node->addTextLayout(QPoint(0, bounds.y()), &d->layout, color, d->style, styleColor, linkColor);
if (d->elideLayout)
node->addTextLayout(QPoint(0, bounds.y()), d->elideLayout, d->color, d->style, d->styleColor, d->linkColor);
node->addTextLayout(QPoint(0, bounds.y()), d->elideLayout, color, d->style, styleColor, linkColor);
}

foreach (QDeclarativeStyledTextImgTag *img, d->visibleImgTags) {
Expand Down Expand Up @@ -2028,7 +2034,7 @@ void QQuickText::updatePolish()
qreal QQuickText::contentWidth() const
{
Q_D(const QQuickText);
return d->contentSize.width();
return d->layedOutTextRect.width();
}

/*!
Expand All @@ -2040,7 +2046,7 @@ qreal QQuickText::contentWidth() const
qreal QQuickText::contentHeight() const
{
Q_D(const QQuickText);
return d->contentSize.height();
return d->layedOutTextRect.height();
}

/*!
Expand Down
4 changes: 2 additions & 2 deletions src/quick/items/qquicktext_p.h
Expand Up @@ -203,10 +203,10 @@ class Q_QUICK_PRIVATE_EXPORT QQuickText : public QQuickImplicitSizeItem
void textChanged(const QString &text);
void linkActivated(const QString &link);
void fontChanged(const QFont &font);
void colorChanged(const QColor &color);
void colorChanged();
void linkColorChanged();
void styleChanged(TextStyle style);
void styleColorChanged(const QColor &color);
void styleColorChanged();
void horizontalAlignmentChanged(HAlignment alignment);
void verticalAlignmentChanged(VAlignment alignment);
void wrapModeChanged();
Expand Down
82 changes: 42 additions & 40 deletions src/quick/items/qquicktext_p_p.h
Expand Up @@ -84,33 +84,57 @@ class Q_AUTOTEST_EXPORT QQuickTextPrivate : public QQuickImplicitSizeItemPrivate
void setLineGeometry(QTextLine &line, qreal lineWidth, qreal &height);
QString elidedText(int lineWidth, const QTextLine &line, QTextLine *nextLine = 0) const;

QRect layedOutTextRect;

qreal lineHeight;

QString text;
QString activeLink;
QUrl baseUrl;
QFont font;
QFont sourceFont;
QColor color;
QColor linkColor;
QQuickText::TextStyle style;
QColor styleColor;
QString activeLink;
QList<QDeclarativeStyledTextImgTag*> imgTags;
QList<QDeclarativeStyledTextImgTag*> visibleImgTags;

QTextLayout layout;
QTextLayout *elideLayout;
QQuickTextLine *textLine;
QQuickTextDocumentWithImageResources *doc;

#if defined(Q_OS_MAC)
QList<QRectF> linesRects;
QThread *layoutThread;
QThread *paintingThread;
#endif

QRgb color;
QRgb linkColor;
QRgb styleColor;

int lineCount;
int maximumLineCount;
int multilengthEos;
int minimumPixelSize;
int minimumPointSize;
int nbActiveDownloads;

enum UpdateType {
UpdateNone,
UpdatePreprocess,
UpdatePaintNode
};

QQuickText::HAlignment hAlign;
QQuickText::VAlignment vAlign;
QQuickText::TextElideMode elideMode;
QQuickText::TextFormat format;
QQuickText::WrapMode wrapMode;
qreal lineHeight;
QQuickText::LineHeightMode lineHeightMode;
int lineCount;
int maximumLineCount;
int maximumLineCountValid;
QQuickText::TextStyle style;
QQuickText::FontSizeMode fontSizeMode;
int multilengthEos;
int minimumPixelSize;
int minimumPointSize;
QPointF elidePos;

static const QChar elideChar;
UpdateType updateType;

bool maximumLineCountValid:1;
bool updateOnComponentComplete:1;
bool updateLayoutOnPolish:1;
bool richText:1;
Expand All @@ -125,42 +149,20 @@ class Q_AUTOTEST_EXPORT QQuickTextPrivate : public QQuickImplicitSizeItemPrivate
bool textHasChanged:1;
bool needToUpdateLayout:1;

QRect layedOutTextRect;
QSize contentSize;
qreal naturalWidth;
static const QChar elideChar;

virtual qreal getImplicitWidth() const;

void ensureDoc();
QQuickTextDocumentWithImageResources *doc;

QRect setupTextLayout();
QRect setupTextLayout(qreal *const naturalWidth);
void setupCustomLineGeometry(QTextLine &line, qreal &height, int lineOffset = 0);
bool isLinkActivatedConnected();
QString anchorAt(const QPointF &pos);
QTextLayout layout;
QTextLayout *elideLayout;
QQuickTextLine *textLine;

static inline QQuickTextPrivate *get(QQuickText *t) {
return t->d_func();
}

enum UpdateType {
UpdateNone,
UpdatePreprocess,
UpdatePaintNode
};
UpdateType updateType;

QList<QDeclarativeStyledTextImgTag*> imgTags;
QList<QDeclarativeStyledTextImgTag*> visibleImgTags;
int nbActiveDownloads;

#if defined(Q_OS_MAC)
QList<QRectF> linesRects;
QThread *layoutThread;
QThread *paintingThread;
#endif
};

class QDeclarativePixmap;
Expand Down

0 comments on commit e060245

Please sign in to comment.