Add color, font, alignment support for labels.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=722537
This commit is contained in:
Petri Damstén 2007-10-07 14:52:17 +00:00
parent 1f01e27b83
commit 1fce16d307
2 changed files with 100 additions and 7 deletions

View File

@ -45,14 +45,26 @@ public:
} }
} }
void text(QPainter *p, const QString& elementID, const QString &text) void text(QPainter *p, int index)
{ {
QString elementID = QString("label%1").arg(index);
QString text = labels[index];
if (image->elementExists(elementID)) { if (image->elementExists(elementID)) {
QRectF elementRect = image->elementRect(elementID); QRectF elementRect = image->elementRect(elementID);
Qt::Alignment align = Qt::AlignCenter;
p->setPen(QPen(Qt::lightGray)); if (colors.count() > index) {
p->setPen(QPen(colors[index]));
}
if (fonts.count() > index) {
p->setFont(fonts[index]);
}
if (alignments.count() > index) {
align = alignments[index];
}
if (elementRect.width() > elementRect.height()) { if (elementRect.width() > elementRect.height()) {
p->drawText(elementRect, Qt::AlignCenter, text); p->drawText(elementRect, align, text);
} else { } else {
p->save(); p->save();
QPointF rotateCenter( QPointF rotateCenter(
@ -63,7 +75,7 @@ public:
p->translate(elementRect.height() / -2, p->translate(elementRect.height() / -2,
elementRect.width() / -2); elementRect.width() / -2);
QRectF r(0, 0, elementRect.height(), elementRect.width()); QRectF r(0, 0, elementRect.height(), elementRect.width());
p->drawText(r, Qt::AlignCenter, text); p->drawText(r, align, text);
p->restore(); p->restore();
} }
} }
@ -79,7 +91,7 @@ public:
{ {
p->restore(); p->restore();
for (int i = 0; i < labels.count(); ++i) { for (int i = 0; i < labels.count(); ++i) {
text(p, QString("label%1").arg(i), labels[i]); text(p, i);
} }
paint(p, "foreground"); paint(p, "foreground");
} }
@ -88,6 +100,9 @@ public:
int maximum; int maximum;
int value; int value;
QStringList labels; QStringList labels;
QList<Qt::Alignment> alignments;
QList<QColor> colors;
QList<QFont> fonts;
QString svg; QString svg;
MeterType meterType; MeterType meterType;
Plasma::Svg *image; Plasma::Svg *image;
@ -139,7 +154,7 @@ int Meter::value() const
return d->value; return d->value;
} }
void Meter::setLabel(int index, const QString& text) void Meter::setLabel(int index, const QString &text)
{ {
while (d->labels.count() <= index) { while (d->labels.count() <= index) {
d->labels << QString(); d->labels << QString();
@ -152,6 +167,45 @@ QString Meter::label(int index) const
return d->labels[index]; return d->labels[index];
} }
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];
}
void Meter::updated(QString sourceName, Plasma::DataEngine::Data data) void Meter::updated(QString sourceName, Plasma::DataEngine::Data data)
{ {
Q_UNUSED(sourceName) Q_UNUSED(sourceName)

View File

@ -141,7 +141,7 @@ public:
* @param index label index. * @param index label index.
* @param text text for the label. * @param text text for the label.
*/ */
void setLabel(int index, const QString & text); void setLabel(int index, const QString &text);
/** /**
* @param index label index * @param index label index
@ -149,6 +149,45 @@ public:
*/ */
QString label(int index) const; QString label(int index) const;
/**
* Set text label color for the meter
* @param index label index.
* @param text color for the label.
*/
void setLabelColor(int index, const QColor &color);
/**
* @param index label index
* @return text label color for the meter
*/
QColor labelColor(int index) const;
/**
* Set text label font for the meter
* @param index label index.
* @param text font for the label.
*/
void setLabelFont(int index, const QFont &font);
/**
* @param index label index
* @return text label font for the meter
*/
QFont labelFont(int index) const;
/**
* Set text label alignment for the meter
* @param index label index.
* @param text alignment for the label.
*/
void setLabelAlignment(int index, const Qt::Alignment alignment);
/**
* @param index label index
* @return text label alignment for the meter
*/
Qt::Alignment labelAlignment(int index) const;
public Q_SLOTS: public Q_SLOTS:
/** /**
* Used when connecting to a DataEngine * Used when connecting to a DataEngine