Use QColor(0, 0, 0, 0) to mark body text in QStaticText

QStaticTextPrivate::paintText() now uses QColor(0, 0, 0, 0) to mark
body text. QPainter's pen color was used before this commit
(that was always black) that leads to different colors of body text if,
for example, links are in body (first block painted with correct color
set by QPainter::setPen() but after link body text was always black).
Now QPainter::drawStaticText() will draw body text with set pen in
any case.

[ChangeLog][QtGui][Important Behavior Changes] Fixed a bug where
QStaticText would not use the QPainter's pen color for text when other
text colors were also in use. Internally this reserves
QColor(0, 0, 0, 0) for use with QStaticText.

Task-number: QTBUG-45957
Change-Id: If3e180e3083cdac1b4236e738acd15572f19ab69
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
This commit is contained in:
Igor Mironchik 2017-12-31 19:41:14 +03:00
parent 276ec1ab00
commit 0d23b7f074
3 changed files with 20 additions and 14 deletions

View File

@ -5744,7 +5744,7 @@ void QPainter::drawStaticText(const QPointF &topLeftPosition, const QStaticText
if (d->extended == 0 if (d->extended == 0
|| !d->state->matrix.isAffine() || !d->state->matrix.isAffine()
|| !fe->supportsTransformation(d->state->matrix)) { || !fe->supportsTransformation(d->state->matrix)) {
staticText_d->paintText(topLeftPosition, this); staticText_d->paintText(topLeftPosition, this, pen().color());
return; return;
} }
@ -5816,11 +5816,16 @@ void QPainter::drawStaticText(const QPointF &topLeftPosition, const QStaticText
QPen oldPen = d->state->pen; QPen oldPen = d->state->pen;
QColor currentColor = oldPen.color(); QColor currentColor = oldPen.color();
static const QColor bodyIndicator(0, 0, 0, 0);
for (int i=0; i<staticText_d->itemCount; ++i) { for (int i=0; i<staticText_d->itemCount; ++i) {
QStaticTextItem *item = staticText_d->items + i; QStaticTextItem *item = staticText_d->items + i;
if (item->color.isValid() && currentColor != item->color) { if (item->color.isValid() && currentColor != item->color
&& item->color != bodyIndicator) {
setPen(item->color); setPen(item->color);
currentColor = item->color; currentColor = item->color;
} else if (item->color == bodyIndicator) {
setPen(oldPen);
currentColor = oldPen.color();
} }
d->extended->drawStaticTextItem(item); d->extended->drawStaticTextItem(item);

View File

@ -435,7 +435,7 @@ namespace {
public: public:
DrawTextItemRecorder(bool untransformedCoordinates, bool useBackendOptimizations) DrawTextItemRecorder(bool untransformedCoordinates, bool useBackendOptimizations)
: m_dirtyPen(false), m_useBackendOptimizations(useBackendOptimizations), : m_dirtyPen(false), m_useBackendOptimizations(useBackendOptimizations),
m_untransformedCoordinates(untransformedCoordinates), m_currentColor(Qt::black) m_untransformedCoordinates(untransformedCoordinates), m_currentColor(0, 0, 0, 0)
{ {
} }
@ -599,7 +599,7 @@ namespace {
}; };
} }
void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p) void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p, const QColor &pen)
{ {
bool preferRichText = textFormat == Qt::RichText bool preferRichText = textFormat == Qt::RichText
|| (textFormat == Qt::AutoText && Qt::mightBeRichText(text)); || (textFormat == Qt::AutoText && Qt::mightBeRichText(text));
@ -631,15 +631,16 @@ void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p)
textLayout.endLayout(); textLayout.endLayout();
actualSize = textLayout.boundingRect().size(); actualSize = textLayout.boundingRect().size();
p->setPen(pen);
textLayout.draw(p, topLeftPosition); textLayout.draw(p, topLeftPosition);
} else { } else {
QTextDocument document; QTextDocument document;
#ifndef QT_NO_CSSPARSER #ifndef QT_NO_CSSPARSER
QColor color = p->pen().color(); document.setDefaultStyleSheet(QString::fromLatin1("body { color: rgba(%1, %2, %3, %4%) }")
document.setDefaultStyleSheet(QString::fromLatin1("body { color: #%1%2%3 }") .arg(QString::number(pen.red()))
.arg(QString::number(color.red(), 16), 2, QLatin1Char('0')) .arg(QString::number(pen.green()))
.arg(QString::number(color.green(), 16), 2, QLatin1Char('0')) .arg(QString::number(pen.blue()))
.arg(QString::number(color.blue(), 16), 2, QLatin1Char('0'))); .arg(QString::number(pen.alpha())));
#endif #endif
document.setDefaultFont(font); document.setDefaultFont(font);
document.setDocumentMargin(0.0); document.setDocumentMargin(0.0);
@ -657,7 +658,7 @@ void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p)
p->save(); p->save();
p->translate(topLeftPosition); p->translate(topLeftPosition);
QAbstractTextDocumentLayout::PaintContext ctx; QAbstractTextDocumentLayout::PaintContext ctx;
ctx.palette.setColor(QPalette::Text, p->pen().color()); ctx.palette.setColor(QPalette::Text, pen);
document.documentLayout()->draw(p, ctx); document.documentLayout()->draw(p, ctx);
p->restore(); p->restore();
@ -682,7 +683,7 @@ void QStaticTextPrivate::init()
painter.setFont(font); painter.setFont(font);
painter.setTransform(matrix); painter.setTransform(matrix);
paintText(QPointF(0, 0), &painter); paintText(QPointF(0, 0), &painter, QColor(0, 0, 0, 0));
} }
QVector<QStaticTextItem> deviceItems = device.items(); QVector<QStaticTextItem> deviceItems = device.items();

View File

@ -131,7 +131,7 @@ public:
~QStaticTextPrivate(); ~QStaticTextPrivate();
void init(); void init();
void paintText(const QPointF &pos, QPainter *p); void paintText(const QPointF &pos, QPainter *p, const QColor &pen);
void invalidate() void invalidate()
{ {