2007-09-25 17:26:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Petri Damsten <damu@iki.fi>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "meter.h"
|
|
|
|
#include "plasma/svg.h"
|
|
|
|
#include <kdebug.h>
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
namespace Plasma {
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
class MeterPrivate
|
2007-09-25 17:26:03 +02:00
|
|
|
{
|
|
|
|
public:
|
2008-10-12 03:31:54 +02:00
|
|
|
MeterPrivate(Meter *m)
|
|
|
|
: minimum(0),
|
|
|
|
maximum(100),
|
|
|
|
value(0),
|
|
|
|
meterType(Meter::AnalogMeter),
|
|
|
|
image(0),
|
|
|
|
minrotate(0),
|
|
|
|
maxrotate(360),
|
|
|
|
meter(m) {}
|
|
|
|
|
|
|
|
void paint(QPainter *p, const QString &elementID)
|
2007-09-25 17:26:03 +02:00
|
|
|
{
|
2008-04-21 18:26:18 +02:00
|
|
|
if (image->hasElement(elementID)) {
|
2007-09-25 17:26:03 +02:00
|
|
|
QRectF elementRect = image->elementRect(elementID);
|
|
|
|
image->paint(p, elementRect.topLeft(), elementID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 16:52:17 +02:00
|
|
|
void text(QPainter *p, int index)
|
2007-09-25 17:26:03 +02:00
|
|
|
{
|
2007-10-07 16:52:17 +02:00
|
|
|
QString elementID = QString("label%1").arg(index);
|
|
|
|
QString text = labels[index];
|
|
|
|
|
2008-04-21 18:26:18 +02:00
|
|
|
if (image->hasElement(elementID)) {
|
2007-09-25 17:26:03 +02:00
|
|
|
QRectF elementRect = image->elementRect(elementID);
|
2007-10-07 16:52:17 +02:00
|
|
|
Qt::Alignment align = Qt::AlignCenter;
|
2007-09-25 17:26:03 +02:00
|
|
|
|
2007-10-07 16:52:17 +02:00
|
|
|
if (colors.count() > index) {
|
|
|
|
p->setPen(QPen(colors[index]));
|
|
|
|
}
|
|
|
|
if (fonts.count() > index) {
|
|
|
|
p->setFont(fonts[index]);
|
|
|
|
}
|
|
|
|
if (alignments.count() > index) {
|
|
|
|
align = alignments[index];
|
|
|
|
}
|
2007-09-25 17:26:03 +02:00
|
|
|
if (elementRect.width() > elementRect.height()) {
|
2007-10-07 16:52:17 +02:00
|
|
|
p->drawText(elementRect, align, text);
|
2007-09-25 17:26:03 +02:00
|
|
|
} else {
|
|
|
|
p->save();
|
|
|
|
QPointF rotateCenter(
|
|
|
|
elementRect.left() + elementRect.width() / 2,
|
|
|
|
elementRect.top() + elementRect.height() / 2);
|
|
|
|
p->translate(rotateCenter);
|
|
|
|
p->rotate(-90);
|
|
|
|
p->translate(elementRect.height() / -2,
|
|
|
|
elementRect.width() / -2);
|
|
|
|
QRectF r(0, 0, elementRect.height(), elementRect.width());
|
2007-10-07 16:52:17 +02:00
|
|
|
p->drawText(r, align, text);
|
2007-09-25 17:26:03 +02:00
|
|
|
p->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void paintBackground(QPainter *p)
|
|
|
|
{
|
|
|
|
paint(p, "background");
|
|
|
|
p->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void paintForeground(QPainter *p)
|
|
|
|
{
|
|
|
|
p->restore();
|
|
|
|
for (int i = 0; i < labels.count(); ++i) {
|
2007-10-07 16:52:17 +02:00
|
|
|
text(p, i);
|
2007-09-25 17:26:03 +02:00
|
|
|
}
|
|
|
|
paint(p, "foreground");
|
|
|
|
}
|
|
|
|
|
2008-04-21 18:26:18 +02:00
|
|
|
void setSizePolicyAndPreferredSize()
|
|
|
|
{
|
|
|
|
switch (meterType) {
|
2008-07-01 20:56:43 +02:00
|
|
|
case Meter::BarMeterHorizontal:
|
2008-04-21 18:26:18 +02:00
|
|
|
meter->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
|
|
|
break;
|
2008-07-01 20:56:43 +02:00
|
|
|
case Meter::BarMeterVertical:
|
2008-04-21 18:26:18 +02:00
|
|
|
meter->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
|
|
|
|
break;
|
2008-07-01 20:56:43 +02:00
|
|
|
case Meter::AnalogMeter:
|
2008-04-21 18:26:18 +02:00
|
|
|
default:
|
|
|
|
meter->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (image) {
|
|
|
|
meter->setPreferredSize(image->size());
|
|
|
|
} else {
|
|
|
|
meter->setPreferredSize(QSizeF(30, 30));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-25 17:26:03 +02:00
|
|
|
int minimum;
|
|
|
|
int maximum;
|
|
|
|
int value;
|
|
|
|
QStringList labels;
|
2007-10-07 16:52:17 +02:00
|
|
|
QList<Qt::Alignment> alignments;
|
|
|
|
QList<QColor> colors;
|
|
|
|
QList<QFont> fonts;
|
2007-09-25 17:26:03 +02:00
|
|
|
QString svg;
|
2008-07-01 20:56:43 +02:00
|
|
|
Meter::MeterType meterType;
|
2007-09-25 17:26:03 +02:00
|
|
|
Plasma::Svg *image;
|
|
|
|
int minrotate;
|
|
|
|
int maxrotate;
|
2008-10-12 03:31:54 +02:00
|
|
|
Meter *meter;
|
2007-09-25 17:26:03 +02:00
|
|
|
};
|
|
|
|
|
2008-04-21 18:26:18 +02:00
|
|
|
Meter::Meter(QGraphicsItem *parent) :
|
|
|
|
QGraphicsWidget(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new MeterPrivate(this))
|
2007-09-25 17:26:03 +02:00
|
|
|
{
|
2008-04-21 18:26:18 +02:00
|
|
|
d->setSizePolicyAndPreferredSize();
|
2007-09-25 17:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Meter::~Meter()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Meter::setMaximum(int maximum)
|
|
|
|
{
|
|
|
|
d->maximum = maximum;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Meter::maximum() const
|
|
|
|
{
|
|
|
|
return d->maximum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Meter::setMinimum(int minimum)
|
|
|
|
{
|
|
|
|
d->minimum = minimum;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Meter::minimum() const
|
|
|
|
{
|
|
|
|
return d->minimum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Meter::setValue(int value)
|
|
|
|
{
|
|
|
|
d->value = value;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Meter::value() const
|
|
|
|
{
|
|
|
|
return d->value;
|
|
|
|
}
|
|
|
|
|
2007-10-07 16:52:17 +02:00
|
|
|
void Meter::setLabel(int index, const QString &text)
|
2007-09-25 17:26:03 +02:00
|
|
|
{
|
|
|
|
while (d->labels.count() <= index) {
|
|
|
|
d->labels << QString();
|
|
|
|
}
|
|
|
|
d->labels[index] = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Meter::label(int index) const
|
|
|
|
{
|
|
|
|
return d->labels[index];
|
|
|
|
}
|
|
|
|
|
2007-10-07 16:52:17 +02:00
|
|
|
void Meter::setLabelColor(int index, const QColor &color)
|
|
|
|
{
|
|
|
|
while (d->colors.count() <= index) {
|
|
|
|
d->colors << color;
|
|
|
|
}
|
|
|
|
d->colors[index] = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor Meter::labelColor(int index) const
|
|
|
|
{
|
|
|
|
return d->colors[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Meter::setLabelFont(int index, const QFont &font)
|
|
|
|
{
|
|
|
|
while (d->fonts.count() <= index) {
|
|
|
|
d->fonts << font;
|
|
|
|
}
|
|
|
|
d->fonts[index] = font;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont Meter::labelFont(int index) const
|
|
|
|
{
|
|
|
|
return d->fonts[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Meter::setLabelAlignment(int index, Qt::Alignment alignment)
|
|
|
|
{
|
|
|
|
while (d->alignments.count() <= index) {
|
|
|
|
d->alignments << alignment;
|
|
|
|
}
|
|
|
|
d->alignments[index] = alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Alignment Meter::labelAlignment(int index) const
|
|
|
|
{
|
|
|
|
return d->alignments[index];
|
|
|
|
}
|
|
|
|
|
2008-02-19 10:19:02 +01:00
|
|
|
void Meter::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
|
2007-09-25 17:26:03 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(sourceName)
|
|
|
|
|
2008-08-02 13:48:56 +02:00
|
|
|
foreach (const QVariant &v, data) {
|
2008-10-12 03:31:54 +02:00
|
|
|
if (v.type() == QVariant::Int ||
|
|
|
|
v.type() == QVariant::UInt ||
|
|
|
|
v.type() == QVariant::LongLong ||
|
|
|
|
v.type() == QVariant::ULongLong) {
|
2008-08-02 13:48:56 +02:00
|
|
|
setValue(v.toInt());
|
2007-09-25 17:26:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-19 10:19:02 +01:00
|
|
|
void Meter::setSvg(const QString &svg)
|
2007-09-25 17:26:03 +02:00
|
|
|
{
|
|
|
|
d->svg = svg;
|
|
|
|
delete d->image;
|
2008-04-23 15:07:41 +02:00
|
|
|
d->image = new Plasma::Svg(this);
|
|
|
|
d->image->setImagePath(svg);
|
2007-09-25 17:26:03 +02:00
|
|
|
// To create renderer and get default size
|
|
|
|
d->image->resize();
|
2008-04-21 18:26:18 +02:00
|
|
|
d->setSizePolicyAndPreferredSize();
|
|
|
|
if (d->image->hasElement("rotateminmax")) {
|
2007-09-25 17:26:03 +02:00
|
|
|
QRectF r = d->image->elementRect("rotateminmax");
|
|
|
|
d->minrotate = (int)r.height();
|
|
|
|
d->maxrotate = (int)r.width();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Meter::svg() const
|
|
|
|
{
|
|
|
|
return d->svg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Meter::setMeterType(MeterType meterType)
|
|
|
|
{
|
|
|
|
d->meterType = meterType;
|
|
|
|
if (d->svg.isEmpty()) {
|
|
|
|
if (meterType == BarMeterHorizontal) {
|
|
|
|
setSvg("widgets/bar_meter_horizontal");
|
|
|
|
} else if (meterType == BarMeterVertical) {
|
|
|
|
setSvg("widgets/bar_meter_vertical");
|
|
|
|
} else if (meterType == AnalogMeter) {
|
|
|
|
setSvg("widgets/analog_meter");
|
|
|
|
}
|
|
|
|
}
|
2008-04-21 18:26:18 +02:00
|
|
|
d->setSizePolicyAndPreferredSize();
|
2007-09-25 17:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Meter::MeterType Meter::meterType() const
|
|
|
|
{
|
|
|
|
return d->meterType;
|
|
|
|
}
|
|
|
|
|
2008-04-21 18:26:18 +02:00
|
|
|
void Meter::paint(QPainter *p,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
QWidget *widget)
|
2007-09-25 17:26:03 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
QRectF rect(QPointF(0, 0), size());
|
|
|
|
QRectF clipRect;
|
|
|
|
qreal percentage = 0.0;
|
|
|
|
qreal angle = 0.0;
|
|
|
|
QPointF rotateCenter;
|
|
|
|
QSize intSize = QSize((int)size().width(), (int)size().height());
|
|
|
|
|
|
|
|
if (intSize != d->image->size()) {
|
|
|
|
d->image->resize(intSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->maximum != d->minimum) {
|
|
|
|
percentage = (qreal)d->value / (d->maximum - d->minimum);
|
|
|
|
}
|
|
|
|
p->setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
switch (d->meterType) {
|
|
|
|
case BarMeterHorizontal:
|
|
|
|
case BarMeterVertical:
|
|
|
|
d->paintBackground(p);
|
|
|
|
|
|
|
|
clipRect = d->image->elementRect("bar");
|
|
|
|
if (clipRect.width() > clipRect.height()) {
|
|
|
|
clipRect.setWidth(clipRect.width() * percentage);
|
|
|
|
} else {
|
|
|
|
qreal bottom = clipRect.bottom();
|
|
|
|
clipRect.setHeight(clipRect.height() * percentage);
|
|
|
|
clipRect.moveBottom(bottom);
|
|
|
|
}
|
|
|
|
p->setClipRect(clipRect);
|
|
|
|
d->paint(p, "bar");
|
|
|
|
|
|
|
|
d->paintForeground(p);
|
|
|
|
break;
|
|
|
|
case AnalogMeter:
|
|
|
|
d->paintBackground(p);
|
|
|
|
|
2008-04-21 18:26:18 +02:00
|
|
|
if (d->image->hasElement("rotatecenter")) {
|
2007-09-25 17:26:03 +02:00
|
|
|
QRectF r = d->image->elementRect("rotatecenter");
|
|
|
|
rotateCenter = QPointF(r.left() + r.width() / 2,
|
|
|
|
r.top() + r.height() / 2);
|
|
|
|
} else {
|
|
|
|
rotateCenter = QPointF(rect.width() / 2, rect.height() / 2);
|
|
|
|
}
|
|
|
|
angle = percentage * (d->maxrotate - d->minrotate) + d->minrotate;
|
|
|
|
|
|
|
|
p->translate(rotateCenter);
|
|
|
|
p->rotate(angle);
|
|
|
|
p->translate(-1 * rotateCenter);
|
|
|
|
d->paint(p, "pointer");
|
|
|
|
|
|
|
|
d->paintForeground(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namepace
|
|
|
|
|
|
|
|
#include "meter.moc"
|