2007-11-24 03:45:34 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2007 by Matias Valdenegro T. <mvaldenegro@informatica.utem.cl>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
2007-11-25 14:28:25 +01:00
|
|
|
* (at your option) any later version.
|
2007-11-24 03:45:34 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
Qt::Orientations Label::expandingDirections() const
|
|
|
|
{
|
|
|
|
return Qt::Horizontal | Qt::Vertical;
|
|
|
|
}
|
|
|
|
|
2007-07-23 00:16:40 +02:00
|
|
|
bool Label::hasHeightForWidth() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal Label::heightForWidth(qreal w) const
|
|
|
|
{
|
2007-11-22 08:19:43 +01:00
|
|
|
Q_UNUSED( w );
|
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);
|
2007-11-09 20:28:28 +01:00
|
|
|
//return m.boundingRect(QRectF(0, 0, w, 9999), d->alignment | Qt::TextWordWrap, d->text).height();
|
|
|
|
return 0;
|
2007-07-23 00:16:40 +02:00
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
QSizeF Label::sizeHint() const
|
2007-07-23 00:16:40 +02:00
|
|
|
{
|
2007-10-17 17:11:29 +02:00
|
|
|
QFontMetricsF m(d->textFont);
|
2007-11-09 20:28:28 +01:00
|
|
|
return m.boundingRect(QRectF(0,0,9999,9999), d->alignment | Qt::TextWordWrap, d->text).size();
|
|
|
|
//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;
|
2007-11-09 20:28:28 +01:00
|
|
|
updateGeometry();
|
2007-12-21 00:50:11 +01:00
|
|
|
update();
|
2007-07-23 00:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2007-11-09 20:28:28 +01:00
|
|
|
updateGeometry();
|
2007-12-21 00:50:11 +01:00
|
|
|
update();
|
2007-07-26 09:25:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2007-11-09 20:28:28 +01:00
|
|
|
updateGeometry();
|
2007-12-21 00:50:11 +01:00
|
|
|
update();
|
2007-10-17 17:11:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|