From 7fbffd29e5904151a3a249004031d0502a16f4fc Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Thu, 20 Aug 2009 12:57:34 +0200 Subject: Remove font property in QStaticText and fix handling translation on painter 1. The font property in QStaticText has been removed. Rather than set a font on the text explicitly, it picks up the font from the painter. If you change the font on the painter, the text layout will have to be updated, like with a matrix. 2. The translation might not be the only transformation on the painter, so rather than translate back to origo, we explicitly set dx and dy on the transform to 0.0 for the duration of the function. 3. Update test to reflect changes --- src/gui/painting/qpainter.cpp | 35 ++++++++++++++++++++---------- src/gui/text/qstatictext.cpp | 33 ++++------------------------ src/gui/text/qstatictext.h | 7 +----- tests/auto/qstatictext/tst_qstatictext.cpp | 19 ++++++++++++---- 4 files changed, 44 insertions(+), 50 deletions(-) diff --git a/src/gui/painting/qpainter.cpp b/src/gui/painting/qpainter.cpp index 9ad19f0..c75547f 100644 --- a/src/gui/painting/qpainter.cpp +++ b/src/gui/painting/qpainter.cpp @@ -5748,15 +5748,28 @@ void QPainter::drawStaticText(const QPointF &position, const QStaticText &static QPointF transformedPosition = position * d->state->matrix; QTransform matrix = d->state->matrix; - // Already added in transformedPosition - if (d->state->matrix.isTranslating()) - translate(-d->state->matrix.dx(), -d->state->matrix.dy()); + // The translation has been applied to transformedPosition. Remove translation + // component from matrix. + if (d->state->matrix.isTranslating()) { + qreal m11 = d->state->matrix.m11(); + qreal m12 = d->state->matrix.m12(); + qreal m13 = d->state->matrix.m13(); + qreal m21 = d->state->matrix.m21(); + qreal m22 = d->state->matrix.m22(); + qreal m23 = d->state->matrix.m23(); + qreal m33 = d->state->matrix.m33(); + + d->state->matrix.setMatrix(m11, m12, m13, + m21, m22, m23, + 0.0, 0.0, m33); + } // If the transform is not identical to the text transform, // we have to relayout the text (for other transformations than plain translation) + bool staticTextNeedsReinit = false; if (staticText_d->matrix != d->state->matrix) { staticText_d->matrix = d->state->matrix; - staticText_d->init(); + staticTextNeedsReinit = true; } bool restoreWhenFinished = false; @@ -5767,15 +5780,15 @@ void QPainter::drawStaticText(const QPointF &position, const QStaticText &static restoreWhenFinished = true; } - // ### Should we pick up the painter's font and recalculate the layout, like we do - // with the matrix? if (font() != staticText_d->font) { - setFont(staticText_d->font); - - save(); - restoreWhenFinished = true; + staticText_d->font = font(); + staticTextNeedsReinit = true; } + // Recreate the layout of the static text because the matrix or font has changed + if (staticTextNeedsReinit) + staticText_d->init(); + for (int i=0; iitemCount; ++i) { QStaticTextItem *item = staticText_d->items + i; d->extended->drawStaticTextItem(transformedPosition, item); @@ -5785,7 +5798,7 @@ void QPainter::drawStaticText(const QPointF &position, const QStaticText &static restore(); if (matrix.isTranslating()) - setTransform(matrix); + d->state->matrix = matrix; } /*! diff --git a/src/gui/text/qstatictext.cpp b/src/gui/text/qstatictext.cpp index 30935f9..d4f8fbd 100644 --- a/src/gui/text/qstatictext.cpp +++ b/src/gui/text/qstatictext.cpp @@ -116,11 +116,10 @@ QStaticText::QStaticText() \a font and bounded by the given \a maximumSize. If an invalid size is passed for \a maximumSize the text will be unbounded. */ -QStaticText::QStaticText(const QString &text, const QFont &font, const QSizeF &size) +QStaticText::QStaticText(const QString &text, const QSizeF &size) : d_ptr(new QStaticTextPrivate) { d_ptr->text = text; - d_ptr->font = font; d_ptr->size = size; d_ptr->init(); } @@ -223,7 +222,7 @@ void QStaticText::setMaximumSize(const QSizeF &size) /*! Returns the maximum size of the QStaticText. - \sa setMaximumSize + \sa setMaximumSize() */ QSizeF QStaticText::maximumSize() const { @@ -231,34 +230,10 @@ QSizeF QStaticText::maximumSize() const } /*! - Sets the font of the QStaticText to \a font. + Returns true if the text of the QStaticText is empty, and false if not. - \note This function will cause the layout of the text to be recalculated. - - \sa font() -*/ -void QStaticText::setFont(const QFont &font) -{ - detach(); - d_ptr->font = font; - d_ptr->init(); -} - -/*! - Returns the font of the QStaticText. - - \sa setFont() + \sa text() */ -QFont QStaticText::font() const -{ - return d_ptr->font; -} - -QString QStaticText::toString() const -{ - return text(); -} - bool QStaticText::isEmpty() const { return d_ptr->text.isEmpty(); diff --git a/src/gui/text/qstatictext.h b/src/gui/text/qstatictext.h index 9830b32..c7b5429 100644 --- a/src/gui/text/qstatictext.h +++ b/src/gui/text/qstatictext.h @@ -57,16 +57,13 @@ class Q_GUI_EXPORT QStaticText { public: QStaticText(); - QStaticText(const QString &text, const QFont &font = QFont(), const QSizeF &maximumSize = QSizeF()); + QStaticText(const QString &text, const QSizeF &maximumSize = QSizeF()); QStaticText(const QStaticText &other); ~QStaticText(); void setText(const QString &text); QString text() const; - void setFont(const QFont &font); - QFont font() const; - void setMaximumSize(const QSizeF &maximumSize); QSizeF maximumSize() const; @@ -74,8 +71,6 @@ public: bool operator==(const QStaticText &) const; bool operator!=(const QStaticText &) const; - QString toString() const; - bool isEmpty() const; private: diff --git a/tests/auto/qstatictext/tst_qstatictext.cpp b/tests/auto/qstatictext/tst_qstatictext.cpp index 3a407c8..0e1532b 100644 --- a/tests/auto/qstatictext/tst_qstatictext.cpp +++ b/tests/auto/qstatictext/tst_qstatictext.cpp @@ -112,7 +112,7 @@ void tst_QStaticText::drawToRect() QImage imageDrawStaticText(1000, 1000, QImage::Format_ARGB32_Premultiplied); { QPainter p(&imageDrawStaticText); - QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", QFont(), QSizeF(10, 500)); + QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", QSizeF(10, 500)); p.drawStaticText(11, 12, text); } @@ -128,6 +128,8 @@ void tst_QStaticText::setFont() QImage imageDrawText(1000, 1000, QImage::Format_ARGB32_Premultiplied); { QPainter p(&imageDrawText); + p.drawText(0, 0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit."); + p.setFont(font); p.drawText(11, 120, "Lorem ipsum dolor sit amet, consectetur adipiscing elit."); } @@ -135,9 +137,12 @@ void tst_QStaticText::setFont() QImage imageDrawStaticText(1000, 1000, QImage::Format_ARGB32_Premultiplied); { QPainter p(&imageDrawStaticText); + QStaticText text; text.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit."); - text.setFont(font); + + p.drawStaticText(0, 0, text); + p.setFont(font); p.drawStaticText(11, 120, text); } @@ -300,6 +305,7 @@ void tst_QStaticText::translationDoesntCauseRelayout() QImage imageDrawText(1000, 1000, QImage::Format_ARGB32_Premultiplied); { QPainter p(&imageDrawText); + p.scale(2.0, 2.0); p.drawText(0, 0, "Lorem ipsum dolor sit amet, consectetur adipiscing elit."); p.translate(100, 200); @@ -309,15 +315,15 @@ void tst_QStaticText::translationDoesntCauseRelayout() QImage imageDrawStaticText(1000, 1000, QImage::Format_ARGB32_Premultiplied); { QPainter p(&imageDrawStaticText); + p.scale(2.0, 2.0); QStaticText text("Lorem ipsum dolor sit amet, consectetur adipiscing elit."); + p.drawStaticText(0, 0, text); QStaticTextPrivate *textd = QStaticTextPrivate::get(&text); glyph_t *glyphPool = textd->glyphPool; QFixedPoint *positionPool = textd->positionPool; QStaticTextItem *items = textd->items; - p.drawStaticText(0, 0, text); - p.translate(100, 200); p.drawStaticText(0, 0, text); @@ -329,6 +335,11 @@ void tst_QStaticText::translationDoesntCauseRelayout() QCOMPARE(textd->items, items); } +#if defined(DEBUG_SAVE_IMAGE) + imageDrawText.save("translationDoesntCauseRelayout_imageDrawText.png"); + imageDrawStaticText.save("translationDoesntCauseRelayout_imageDrawStaticText.png"); +#endif + QCOMPARE(imageDrawStaticText, imageDrawText); } -- cgit v0.12