2007-07-23 00:16:40 +02:00
|
|
|
#include "label.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QFontMetricsF>
|
2007-08-17 20:57:28 +02:00
|
|
|
#include <QStyleOptionGraphicsItem>
|
2007-07-23 00:16:40 +02:00
|
|
|
|
|
|
|
namespace Plasma {
|
|
|
|
|
|
|
|
class Label::Private
|
|
|
|
{
|
|
|
|
public:
|
2007-11-04 06:34:10 +01:00
|
|
|
Private()
|
|
|
|
: maximumWidth(9999)
|
|
|
|
{}
|
2007-07-23 00:16:40 +02:00
|
|
|
|
|
|
|
QString text;
|
2007-07-26 09:25:21 +02:00
|
|
|
Qt::Alignment alignment;
|
|
|
|
QPen textPen;
|
2007-10-17 17:11:29 +02:00
|
|
|
QFont textFont;
|
2007-11-04 06:34:10 +01:00
|
|
|
int maximumWidth;
|
2007-07-23 00:16:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Label::Label(Widget *parent)
|
2007-08-03 18:00:10 +02:00
|
|
|
: Plasma::Widget(parent),
|
2007-07-23 00:16:40 +02:00
|
|
|
d(new Private)
|
|
|
|
{
|
2007-07-26 09:25:21 +02:00
|
|
|
setAlignment(Qt::AlignHCenter);
|
|
|
|
setPen(QPen(Qt::black, 1));
|
2007-07-23 00:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Label::~Label()
|
|
|
|
{
|
2007-10-27 16:27:25 +02:00
|
|
|
delete d;
|
2007-07-23 00:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Orientations Label::expandingDirections() const
|
|
|
|
{
|
|
|
|
return Qt::Horizontal | Qt::Vertical;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::hasHeightForWidth() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal Label::heightForWidth(qreal w) const
|
|
|
|
{
|
2007-07-28 11:22:20 +02:00
|
|
|
//FIXME: this looks a bit odd?
|
2007-11-04 06:34:10 +01:00
|
|
|
QFontMetricsF m(d->textFont);
|
|
|
|
return m.boundingRect(QRectF(0, 0, w, 9999), d->alignment | Qt::TextWordWrap, d->text).height();
|
2007-07-23 00:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Label::sizeHint() const
|
|
|
|
{
|
2007-10-17 17:11:29 +02:00
|
|
|
QFontMetricsF m(d->textFont);
|
2007-11-04 06:34:10 +01:00
|
|
|
return m.boundingRect(QRectF(0, 0, d->maximumWidth, 9999), d->alignment | Qt::TextWordWrap, d->text).size();
|
2007-07-23 00:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setText(const QString& text)
|
|
|
|
{
|
|
|
|
d->text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Label::text() const
|
|
|
|
{
|
|
|
|
return d->text;
|
|
|
|
}
|
|
|
|
|
2007-07-26 09:25:21 +02:00
|
|
|
void Label::setAlignment(Qt::Alignment align)
|
|
|
|
{
|
|
|
|
d->alignment = align;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Alignment Label::alignment() const
|
|
|
|
{
|
|
|
|
return d->alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setPen(const QPen& pen)
|
|
|
|
{
|
|
|
|
d->textPen = pen;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPen Label::pen() const
|
|
|
|
{
|
|
|
|
return d->textPen;
|
|
|
|
}
|
|
|
|
|
2007-11-04 06:34:10 +01:00
|
|
|
void Label::setMaximumWidth(int width)
|
|
|
|
{
|
|
|
|
d->maximumWidth = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Label::maximumWidth() const
|
|
|
|
{
|
|
|
|
return d->maximumWidth;
|
|
|
|
}
|
|
|
|
|
2007-10-17 17:11:29 +02:00
|
|
|
void Label::setFont(const QFont& font)
|
|
|
|
{
|
|
|
|
d->textFont = font;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont Label::font() const
|
|
|
|
{
|
|
|
|
return d->textFont;
|
|
|
|
}
|
|
|
|
|
2007-08-01 22:51:27 +02:00
|
|
|
void Label::paintWidget(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2007-07-23 00:16:40 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(option);
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
|
2007-07-26 09:25:21 +02:00
|
|
|
p->setPen(d->textPen);
|
2007-10-17 17:11:29 +02:00
|
|
|
p->setFont(d->textFont);
|
2007-08-17 20:55:55 +02:00
|
|
|
p->drawText(option->rect, d->alignment | Qt::TextWordWrap, d->text);
|
2007-07-23 00:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|