summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>2010-03-26 09:43:40 (GMT)
committerEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>2010-03-26 10:46:46 (GMT)
commit47472906fd00e0eff820870330d481c4229ee285 (patch)
tree2543c9449bca6b7148a609567b9aa16f6d910f3d
parent075918e796b98155b81871f15bf2eb266a783561 (diff)
downloadQt-47472906fd00e0eff820870330d481c4229ee285.zip
Qt-47472906fd00e0eff820870330d481c4229ee285.tar.gz
Qt-47472906fd00e0eff820870330d481c4229ee285.tar.bz2
Synchronize rich text and plain text code paths in QStaticText
Drawing a string as plain text and rich text should have identical results unless special formatting information is added in the html. This means we should not have any implicit margin on the QTextDocument used to paint the text, and we should make up for the fact that the drawStaticText() call currently takes the position of the baseline and QTextDocument::drawContents() takes the top-left corner. Reviewed-by: Gunnar
-rw-r--r--src/gui/text/qstatictext.cpp6
-rw-r--r--tests/auto/qstatictext/tst_qstatictext.cpp36
2 files changed, 40 insertions, 2 deletions
diff --git a/src/gui/text/qstatictext.cpp b/src/gui/text/qstatictext.cpp
index 1fabf12..d685cd9 100644
--- a/src/gui/text/qstatictext.cpp
+++ b/src/gui/text/qstatictext.cpp
@@ -553,12 +553,14 @@ void QStaticTextPrivate::paintText(const QPointF &pos, QPainter *p)
} else {
QTextDocument document;
document.setDefaultFont(font);
+ document.setDocumentMargin(0.0);
document.setHtml(text);
- QRectF rect = maximumSize.isValid() ? QRectF(pos, maximumSize) : QRectF();
+ QPointF adjustedPos = pos - QPointF(0, QFontMetricsF(font).ascent());
+ QRectF rect = maximumSize.isValid() ? QRectF(adjustedPos, maximumSize) : QRectF();
document.adjustSize();
p->save();
- p->translate(pos);
+ p->translate(adjustedPos);
document.drawContents(p, rect);
p->restore();
actualSize = document.size();
diff --git a/tests/auto/qstatictext/tst_qstatictext.cpp b/tests/auto/qstatictext/tst_qstatictext.cpp
index 16832ad..a038878 100644
--- a/tests/auto/qstatictext/tst_qstatictext.cpp
+++ b/tests/auto/qstatictext/tst_qstatictext.cpp
@@ -79,6 +79,8 @@ private slots:
void projectedPainter();
void rotatedScaledAndTranslatedPainter();
void transformationChanged();
+
+ void plainTextVsRichText();
};
void tst_QStaticText::init()
@@ -482,5 +484,39 @@ void tst_QStaticText::transformationChanged()
QCOMPARE(imageDrawStaticText, imageDrawText);
}
+void tst_QStaticText::plainTextVsRichText()
+{
+ QPixmap imagePlainText(1000, 1000);
+ imagePlainText.fill(Qt::white);
+ {
+ QPainter p(&imagePlainText);
+
+ QStaticText staticText;
+ staticText.setText("FOObar");
+ staticText.setTextFormat(Qt::PlainText);
+
+ p.drawStaticText(10, 10, staticText);
+ }
+
+ QPixmap imageRichText(1000, 1000);
+ imageRichText.fill(Qt::white);
+ {
+ QPainter p(&imageRichText);
+
+ QStaticText staticText;
+ staticText.setText("<html><body>FOObar</body></html>");
+ staticText.setTextFormat(Qt::RichText);
+
+ p.drawStaticText(10, 10, staticText);
+ }
+
+#if defined(DEBUG_SAVE_IMAGE)
+ imagePlainText.save("plainTextVsRichText_imagePlainText.png");
+ imageRichText.save("plainTextVsRichText_imageRichText.png");
+#endif
+
+ QCOMPARE(imagePlainText, imageRichText);
+}
+
QTEST_MAIN(tst_QStaticText)
#include "tst_qstatictext.moc"