Add font property to Plasma::Label; this also helps getting more accurate sizeHint than using Arial 12pt when the default font is Arial 10pt

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=726342
This commit is contained in:
Hoàng Đức Hiếu 2007-10-17 15:11:29 +00:00
parent ed1430faa4
commit b14fb9605e
2 changed files with 24 additions and 1 deletions

View File

@ -14,6 +14,7 @@ class Label::Private
QString text; QString text;
Qt::Alignment alignment; Qt::Alignment alignment;
QPen textPen; QPen textPen;
QFont textFont;
}; };
Label::Label(Widget *parent) Label::Label(Widget *parent)
@ -47,7 +48,7 @@ qreal Label::heightForWidth(qreal w) const
QSizeF Label::sizeHint() const QSizeF Label::sizeHint() const
{ {
QFontMetricsF m(QFont("Arial", 12)); QFontMetricsF m(d->textFont);
return m.boundingRect(QRectF(0,0,9999,9999), d->alignment | Qt::TextWordWrap, d->text).size(); return m.boundingRect(QRectF(0,0,9999,9999), d->alignment | Qt::TextWordWrap, d->text).size();
} }
@ -82,12 +83,23 @@ QPen Label::pen() const
return d->textPen; return d->textPen;
} }
void Label::setFont(const QFont& font)
{
d->textFont = font;
}
QFont Label::font() const
{
return d->textFont;
}
void Label::paintWidget(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget) void Label::paintWidget(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget)
{ {
Q_UNUSED(option); Q_UNUSED(option);
Q_UNUSED(widget); Q_UNUSED(widget);
p->setPen(d->textPen); p->setPen(d->textPen);
p->setFont(d->textFont);
p->drawText(option->rect, d->alignment | Qt::TextWordWrap, d->text); p->drawText(option->rect, d->alignment | Qt::TextWordWrap, d->text);
} }

View File

@ -24,6 +24,7 @@
#include <plasma/widgets/widget.h> #include <plasma/widgets/widget.h>
#include <QtGui/QPen> #include <QtGui/QPen>
#include <QtGui/QFont>
class QGraphicsTextItem; class QGraphicsTextItem;
@ -106,6 +107,16 @@ class PLASMA_EXPORT Label : public Plasma::Widget
*/ */
QPen pen() const; QPen pen() const;
/**
* Sets the font used for the text.
*/
void setFont(const QFont& font);
/**
* Returns the font used for the text.
*/
QFont font() const;
/** /**
* Paint function. * Paint function.
*/ */