25 #include <QtGui/QPainter>
26 #include <QtGui/QImage>
27 #include <QtGui/QStyle>
28 #include <QtGui/QStyleOption>
45 QPixmap cachedPixmap[2];
46 QStyle::ControlElement ce_indicatorLedCircular;
47 QStyle::ControlElement ce_indicatorLedRectangular;
72 d->state = (state ==
Off ?
Off :
On);
119 int size = qMin(width(), height());
124 return qMax(0, size);
129 if (d->cachedPixmap[d->state].isNull()) {
132 QPainter painter(
this);
133 painter.drawPixmap(1, 1, d->cachedPixmap[d->state]);
184 if ( d->state == state)
187 d->state = (state ==
Off ?
Off :
On);
193 if ( d->shape == shape )
202 if ( d->color == color )
211 if ( d->darkFactor == darkFactor )
220 return d->darkFactor;
225 if ( d->look == look)
234 d->state = (d->state ==
On ?
Off :
On);
256 option.initFrom(
this);
257 int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, &option,
this);
258 return QSize( iconSize, iconSize );
263 return QSize( 16, 16 );
268 d->cachedPixmap[
Off] = QPixmap();
269 d->cachedPixmap[
On] = QPixmap();
279 QSize size(width() - 2, height() - 2);
282 size =
QSize(width, width);
284 QPointF center(size.width() / 2.0, size.height() / 2.0);
285 const int smallestSize = qMin(size.width(), size.height());
288 QImage image(size, QImage::Format_ARGB32_Premultiplied);
291 QRadialGradient fillGradient(center, smallestSize / 2.0, QPointF(center.x(), size.height() / 3.0));
292 const QColor fillColor = d->state !=
Off ? d->color : d->color.dark(d->darkFactor);
293 fillGradient.setColorAt(0.0, fillColor.light(250));
294 fillGradient.setColorAt(0.5, fillColor.light(130));
295 fillGradient.setColorAt(1.0, fillColor);
297 QConicalGradient borderGradient(center, look ==
Sunken ? 90 : -90);
298 QColor borderColor = palette().color(QPalette::Dark);
299 if (d->state ==
On) {
300 QColor glowOverlay = fillColor;
301 glowOverlay.setAlpha(80);
304 borderGradient.setColorAt(0.2, borderColor);
305 borderGradient.setColorAt(0.5, palette().
color(QPalette::Light));
306 borderGradient.setColorAt(0.8, borderColor);
308 painter.begin(&image);
309 painter.setRenderHint(QPainter::Antialiasing);
310 painter.setBrush(look ==
Flat ? QBrush(fillColor) : QBrush(fillGradient));
311 const QBrush penBrush = (look ==
Flat) ? QBrush(borderColor) : QBrush(borderGradient);
312 const qreal penWidth = smallestSize / 8.0;
313 painter.setPen(QPen(penBrush, penWidth));
314 QRectF r(penWidth / 2.0, penWidth / 2.0, size.width() - penWidth, size.height() - penWidth);
318 painter.drawEllipse(r);
322 d->cachedPixmap[d->state] = QPixmap::fromImage(image);
324 painter.drawPixmap(1, 1, d->cachedPixmap[d->state]);
int darkFactor() const
Returns the factor to darken the LED.
void setColor(const QColor &color)
Set the color of the widget.
virtual void paintRect()
Paints a rectangular, flat LED.
void on()
Sets the state of the widget to On.
KLed(QWidget *parent=0)
Constructs a green, round LED widget which will initially be turned on.
QColor color() const
Returns the current color of the widget.
void paintEvent(QPaintEvent *)
void off()
Sets the state of the widget to Off.
void setShape(Shape shape)
Set the shape of the LED.
virtual void paintFlat()
Paints a circular, flat LED.
void toggle()
Toggles the state of the led from Off to On or vice versa.
Look look() const
Returns the current look of the widget.
void paintLed(Shape shape, Look look)
Look
Displays a flat, round or sunken LED.
virtual QSize sizeHint() const
virtual void paintRectFrame(bool raised)
Paints a rectangular LED, either raised or sunken, depending on its argument.
void setLook(Look look)
Sets the look of the widget.
virtual void paintRaised()
Paints a circular, raised LED.
QColor overlayColors(const QColor &base, const QColor &paint, QPainter::CompositionMode comp=QPainter::CompositionMode_SourceOver)
Blend two colors into a new color by painting the second color over the first using the specified com...
void resizeEvent(QResizeEvent *)
State state() const
Returns the current state of the widget (on/off).
void setDarkFactor(int darkFactor)
Sets the factor to darken the LED in KLed::Off state.
State
Status of the light is on/off.
void updateCachedPixmap()
virtual QSize minimumSizeHint() const
virtual void paintSunken()
Paints a circular, sunken LED.
bool paintCachedPixmap()
Paint the cached antialiased pixmap corresponding to the state if any.
~KLed()
Destroys the LED widget.
virtual int ledWidth() const
Returns the width of the led.
Shape shape() const
Returns the current shape of the widget.
void setState(State state)
Sets the state of the widget to On or Off.