summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eblomfel@trolltech.com>2009-05-22 15:42:25 (GMT)
committerEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>2010-01-14 12:46:58 (GMT)
commitc74c00b3a006491c4e7140fd7c5c2553d8abf45d (patch)
tree9803abf9f70d207dba1125bd51d0c453f83f7d7c
parent1271197f70862fd1565039aff3b0e40665d3adde (diff)
downloadQt-c74c00b3a006491c4e7140fd7c5c2553d8abf45d.zip
Qt-c74c00b3a006491c4e7140fd7c5c2553d8abf45d.tar.gz
Qt-c74c00b3a006491c4e7140fd7c5c2553d8abf45d.tar.bz2
Add lazy initialization to QStaticText
People should be able to initialize the QStaticText lazily, or change its data after instantiation. Each of the setters will recalculate the layout to make sure it's always in sync.
-rw-r--r--src/gui/text/qstatictext.cpp108
-rw-r--r--src/gui/text/qstatictext.h16
-rw-r--r--src/gui/text/qstatictext_p.h2
3 files changed, 114 insertions, 12 deletions
diff --git a/src/gui/text/qstatictext.cpp b/src/gui/text/qstatictext.cpp
index 1256427..d665abe 100644
--- a/src/gui/text/qstatictext.cpp
+++ b/src/gui/text/qstatictext.cpp
@@ -92,10 +92,18 @@ QT_BEGIN_NAMESPACE
passed to QPainter::drawStaticText() and can change from call to call without affecting
performance.
- \sa QPainter::drawStaticText().
+ \sa QPainter::drawText(), QPainter::drawStaticText(), QTextLayout, QTextDocument
*/
/*!
+ Constructs an empty QStaticText
+*/
+QStaticText::QStaticText()
+ : d_ptr(new QStaticTextPrivate)
+{
+}
+
+/*!
\fn QStaticText::QStaticText(const QString &text, const QFont &font, const QSizeF &maximumSize)
Constructs a QStaticText object with the given \a text which is to be rendered in the given
@@ -106,7 +114,12 @@ QStaticText::QStaticText(const QString &text, const QFont &font, const QSizeF &s
: d_ptr(new QStaticTextPrivate)
{
Q_D(QStaticText);
- d->init(text, font, sz);
+
+ d->textLayout->setText(text);
+ d->textLayout->setFont(font);
+ d->size = sz;
+
+ d->init();
}
/*!
@@ -118,8 +131,84 @@ QStaticText::~QStaticText()
delete d;
}
+/*!
+ Sets the text of the QStaticText to \a text.
+
+ \note This function will cause the layout of the text to be recalculated.
+
+ \sa text()
+*/
+void QStaticText::setText(const QString &text)
+{
+ Q_D(QStaticText);
+ d->textLayout->setText(text);
+ d->init();
+}
+
+/*!
+ Returns the text of the QStaticText.
+
+ \sa setText()
+*/
+QString QStaticText::text() const
+{
+ Q_D(const QStaticText);
+ return d->textLayout->text();
+}
+
+/*!
+ Sets the font of the QStaticText to \a font.
+
+ \note This function will cause the layout of the text to be recalculated.
+
+ \sa font()
+*/
+void QStaticText::setFont(const QFont &font)
+{
+ Q_D(QStaticText);
+ d->textLayout->setFont(font);
+ d->init();
+}
+
+/*!
+ Returns the font of the QStaticText.
+
+ \sa setFont()
+*/
+QFont QStaticText::font() const
+{
+ Q_D(const QStaticText);
+ return d->textLayout->font();
+}
+
+/*!
+ Sets the maximum size of the QStaticText to \a maximumSize. If a valid maximum size is set for
+ the QStaticText, it will be formatted to fit within its width, and clipped by its height.
+
+ \note This function will cause the layout of the text to be recalculated.
+
+ \sa maximumSize()
+*/
+void QStaticText::setMaximumSize(const QSizeF &maximumSize)
+{
+ Q_D(QStaticText);
+ d->size = maximumSize;
+ d->init();
+}
+
+/*!
+ Returns the maximum size of the QStaticText.
+
+ \sa setMaximumSize()
+*/
+QSizeF QStaticText::maximumSize() const
+{
+ Q_D(const QStaticText);
+ return d->size;
+}
+
QStaticTextPrivate::QStaticTextPrivate()
- : textLayout(0)
+ : textLayout(new QTextLayout())
{
}
@@ -133,25 +222,24 @@ QStaticTextPrivate *QStaticTextPrivate::get(const QStaticText *q)
return q->d_ptr;
}
-void QStaticTextPrivate::init(const QString &text, const QFont &font, const QSizeF &sz)
+void QStaticTextPrivate::init()
{
- Q_ASSERT(textLayout == 0);
- size = sz;
-
- textLayout = new QTextLayout(text, font);
+ Q_ASSERT(textLayout != 0);
textLayout->setCacheEnabled(true);
- QFontMetrics fontMetrics(font);
+ QFontMetrics fontMetrics(textLayout->font());
textLayout->beginLayout();
int h = size.isValid() ? 0 : -fontMetrics.ascent();
QTextLine line;
+ qreal lineWidth = size.isValid() ? size.width() : fontMetrics.width(textLayout->text());
while ((line = textLayout->createLine()).isValid()) {
- line.setLineWidth(size.isValid() ? size.width() : fontMetrics.width(text));
+ line.setLineWidth(lineWidth);
line.setPosition(QPointF(0, h));
h += line.height();
}
+ textLayout->endLayout();
}
QT_END_NAMESPACE
diff --git a/src/gui/text/qstatictext.h b/src/gui/text/qstatictext.h
index fc10712..a666a7e 100644
--- a/src/gui/text/qstatictext.h
+++ b/src/gui/text/qstatictext.h
@@ -56,9 +56,23 @@ class Q_GUI_EXPORT QStaticText
{
Q_DECLARE_PRIVATE(QStaticText);
public:
- QStaticText(const QString &text, const QFont &font, const QSizeF &maximumSize = QSizeF());
+ QStaticText();
+ QStaticText(const QString &text);
+ QStaticText(const QString &text, const QFont &font = QFont(), const QSizeF &maximumSize = QSizeF());
~QStaticText();
+ void isValid() const;
+
+ void setText(const QString &text);
+ QString text() const;
+
+ void setFont(const QFont &font);
+ QFont font() const;
+
+ void setMaximumSize(const QSizeF &maximumSize);
+ QSizeF maximumSize() const;
+
+
private:
QStaticTextPrivate *d_ptr;
};
diff --git a/src/gui/text/qstatictext_p.h b/src/gui/text/qstatictext_p.h
index b2f29f7..1e334d7 100644
--- a/src/gui/text/qstatictext_p.h
+++ b/src/gui/text/qstatictext_p.h
@@ -64,7 +64,7 @@ public:
QStaticTextPrivate();
~QStaticTextPrivate();
- void init(const QString &text, const QFont &font, const QSizeF &size);
+ void init();
QTextLayout *textLayout;
QSizeF size;