134
qWarning( "%s: in class KLed: no KLed::Look set", qApp->argv()[0] );
138
qWarning( "%s: in class KLed: no KLed::Shape set", qApp->argv()[0] );
144
int ready = t.elapsed();
145
qWarning( "elapsed: %d msec. for %d rounds", ready, rounds );
149
116
int KLed::ledWidth() const
151
// Initialize coordinates, width, and height of the LED
152
int width = this->width();
154
118
// Make sure the LED is round!
155
if ( width > this->height() )
156
width = this->height();
119
int size = qMin(width(), height());
158
121
// leave one pixel border
124
return qMax(0, size);
167
127
bool KLed::paintCachedPixmap()
169
if ( d->state == On ) {
171
QPainter paint( this );
172
paint.drawPixmap( 0, 0, *d->onMap );
177
QPainter paint( this );
178
paint.drawPixmap( 0, 0, *d->offMap );
129
if (d->cachedPixmap[d->state].isNull()) {
132
QPainter painter(this);
133
painter.drawPixmap(1, 1, d->cachedPixmap[d->state]);
186
// paint a ROUND FLAT led lamp
187
137
void KLed::paintFlat()
189
if ( paintCachedPixmap() )
197
int width = ledWidth();
204
tmpMap = new QPixmap( width + 6, width + 6 );
205
tmpMap->fill( palette().color( backgroundRole() ) );
207
// start painting widget
208
paint.begin( tmpMap );
209
paint.setRenderHint(QPainter::Antialiasing);
211
// Set the color of the LED according to given parameters
212
color = ( d->state ) ? d->color : d->offColor;
214
// Set the brush to SolidPattern, this fills the entire area
215
// of the ellipse which is drawn with a thin gray "border" (pen)
216
brush.setStyle( Qt::SolidPattern );
217
brush.setColor( color );
219
pen.setWidth( scale );
220
color = palette().color( QPalette::Dark );
221
pen.setColor( color ); // Set the pen accordingly
223
paint.setPen( pen ); // Select pen for drawing
224
paint.setBrush( brush ); // Assign the brush to the painter
226
// Draws a "flat" LED with the given color:
227
paint.drawEllipse( scale, scale, width - scale * 2, width - scale * 2 );
232
QPixmap *&dest = ( d->state == On ? d->onMap : d->offMap );
233
QImage i = tmpMap->toImage();
235
i = i.scaled( width, width, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
238
dest = new QPixmap( QPixmap::fromImage( i ) );
240
paint.drawPixmap( 0, 0, *dest );
139
paintLed(Circular, Flat);
244
// paint a ROUND RAISED led lamp
245
142
void KLed::paintRaised()
247
if ( paintCachedPixmap() )
255
// Initialize coordinates, width, and height of the LED
256
int width = ledWidth();
263
tmpMap = new QPixmap( width + 6, width + 6 );
264
tmpMap->fill( palette().color( backgroundRole() ) );
265
paint.begin( tmpMap );
266
paint.setRenderHint(QPainter::Antialiasing);
268
// Set the color of the LED according to given parameters
269
color = ( d->state == On ? d->color : d->offColor );
271
// Set the brush to SolidPattern, this fills the entire area
272
// of the ellipse which is drawn first
273
brush.setStyle( Qt::SolidPattern );
274
brush.setColor( color );
275
paint.setBrush( brush ); // Assign the brush to the painter
277
// Draws a "flat" LED with the given color:
278
paint.drawEllipse( scale, scale, width - scale * 2, width - scale * 2 );
280
// Draw the bright light spot of the LED now, using modified "old"
281
// painter routine taken from KDEUI's KLed widget:
283
// Setting the new width of the pen is essential to avoid "pixelized"
284
// shadow like it can be observed with the old LED code
285
pen.setWidth( 2 * scale );
287
// shrink the light on the LED to a size about 2/3 of the complete LED
288
int pos = width / 5 + 1;
289
int light_width = width;
293
// Calculate the LED's "light factor":
294
int light_quote = ( 130 * 2 / ( light_width ? light_width : 1 ) ) + 100;
296
// Now draw the bright spot on the LED:
297
while ( light_width ) {
298
color = color.light( light_quote ); // make color lighter
299
pen.setColor( color ); // set color as pen color
300
paint.setPen( pen ); // select the pen for drawing
301
paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle)
307
paint.drawEllipse( pos, pos, light_width, light_width );
313
paint.drawEllipse( pos, pos, light_width, light_width );
318
// Drawing of bright spot finished, now draw a thin gray border
319
// around the LED; it looks nicer that way. We do this here to
320
// avoid that the border can be erased by the bright spot of the LED
322
pen.setWidth( 2 * scale + 1 );
323
color = palette().color( QPalette::Dark );
324
pen.setColor( color ); // Set the pen accordingly
325
paint.setPen( pen ); // Select pen for drawing
326
brush.setStyle( Qt::NoBrush ); // Switch off the brush
327
paint.setBrush( brush ); // This avoids filling of the ellipse
329
paint.drawEllipse( 2, 2, width, width );
334
QPixmap *&dest = ( d->state == On ? d->onMap : d->offMap );
335
QImage i = tmpMap->toImage();
337
i = i.scaled( width, width, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
340
dest = new QPixmap( QPixmap::fromImage( i ) );
342
paint.drawPixmap( 0, 0, *dest );
144
paintLed(Circular, Raised);
346
// paint a ROUND SUNKEN led lamp
347
147
void KLed::paintSunken()
349
if ( paintCachedPixmap() )
357
// First of all we want to know what area should be updated
358
// Initialize coordinates, width, and height of the LED
359
int width = ledWidth();
366
tmpMap = new QPixmap( width, width );
367
tmpMap->fill( palette().color( backgroundRole() ) );
368
paint.begin( tmpMap );
369
paint.setRenderHint(QPainter::Antialiasing);
371
// Set the color of the LED according to given parameters
372
color = ( d->state == On ) ? d->color : d->offColor;
374
// Set the brush to SolidPattern, this fills the entire area
375
// of the ellipse which is drawn first
376
brush.setStyle( Qt::SolidPattern );
377
brush.setColor( color );
378
paint.setBrush( brush ); // Assign the brush to the painter
380
// Draws a "flat" LED with the given color:
381
paint.drawEllipse( scale, scale, width - scale * 2, width - scale * 2 );
383
// Draw the bright light spot of the LED now, using modified "old"
384
// painter routine taken from KDEUI's KLed widget:
386
// Setting the new width of the pen is essential to avoid "pixelized"
387
// shadow like it can be observed with the old LED code
388
pen.setWidth( 2 * scale );
390
// shrink the light on the LED to a size about 2/3 of the complete LED
391
int pos = width / 5 + 1;
392
int light_width = width;
396
// Calculate the LED's "light factor":
397
int light_quote = ( 130 * 2 / ( light_width ? light_width : 1 ) ) + 100;
399
// Now draw the bright spot on the LED:
400
while ( light_width ) {
401
color = color.light( light_quote ); // make color lighter
402
pen.setColor( color ); // set color as pen color
403
paint.setPen( pen ); // select the pen for drawing
404
paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle)
410
paint.drawEllipse( pos, pos, light_width, light_width );
416
paint.drawEllipse( pos, pos, light_width, light_width );
421
// Drawing of bright spot finished, now draw a thin border
422
// around the LED which resembles a shadow with light coming
423
// from the upper left.
425
pen.setWidth( 2 * scale + 1 ); // ### shouldn't this value be smaller for smaller LEDs?
426
brush.setStyle( Qt::NoBrush ); // Switch off the brush
427
paint.setBrush( brush ); // This avoids filling of the ellipse
429
// Set the initial color value to QColorGroup(palette()).light() (bright) and start
430
// drawing the shadow border at 45° (45*16 = 720).
433
color = palette().color( QPalette::Light );
435
for ( int arc = 120; arc < 2880; arc += 240 ) {
436
pen.setColor( color );
438
int w = width - pen.width() / 2 - scale + 1;
439
paint.drawArc( pen.width() / 2, pen.width() / 2, w, w, angle + arc, 240 );
440
paint.drawArc( pen.width() / 2, pen.width() / 2, w, w, angle - arc, 240 );
441
color = color.dark( 110 ); //FIXME: this should somehow use the contrast value
442
} // end for ( angle = 720; angle < 6480; angle += 160 )
448
QPixmap *&dest = ( d->state == On ? d->onMap : d->offMap );
449
QImage i = tmpMap->toImage();
451
i = i.scaled( width, width, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
454
dest = new QPixmap( QPixmap::fromImage( i ) );
456
paint.setCompositionMode(QPainter::CompositionMode_Source);
457
paint.drawPixmap( 0, 0, *dest );
149
paintLed(Circular, Sunken);
461
152
void KLed::paintRect()
463
QPainter painter( this );
464
painter.setRenderHint(QPainter::Antialiasing);
465
QBrush lightBrush( d->color );
466
QBrush darkBrush( d->offColor );
468
QPen pen( d->color.dark( 300 ) );
473
switch ( d->state ) {
475
painter.setBrush( lightBrush );
476
painter.drawRect( 0, 0, w, h );
479
painter.setBrush( darkBrush );
480
painter.drawRect( 0, 0, w, h );
481
painter.setPen( pen );
482
painter.drawLine( 0, 0, w, 0 );
483
painter.drawLine( 0, h - 1, w, h - 1 );
487
for ( i = 0; i < w; i += 4 /* dx */)
488
painter.drawLine( i, 1, i, h - 1 );
154
paintLed(Rectangular, Flat);
495
157
void KLed::paintRectFrame( bool raised )
497
QPainter painter( this );
498
painter.setRenderHint(QPainter::Antialiasing);
499
QBrush lightBrush( d->color );
500
QBrush darkBrush( d->offColor );
505
painter.setPen( Qt::white );
506
painter.drawLine( 0, 0, 0, h - 1 );
507
painter.drawLine( 1, 0, w - 1, 0 );
508
painter.setPen( Qt::black );
509
painter.drawLine( 1, h - 1, w - 1, h - 1 );
510
painter.drawLine( w - 1, 1, w - 1, h - 1 );
511
painter.fillRect( 1, 1, w - 2, h - 2, ( d->state == On ? lightBrush : darkBrush ) );
513
painter.setPen( Qt::black );
514
painter.drawRect( 0, 0, w, h );
515
painter.drawRect( 0, 0, w - 1, h - 1 );
516
painter.setPen( Qt::white );
517
painter.drawRect( 1, 1, w - 1, h - 1 );
518
painter.fillRect( 2, 2, w - 4, h - 4, ( d->state == On ? lightBrush : darkBrush ) );
159
paintLed(Rectangular, raised ? Raised : Sunken);
522
162
KLed::State KLed::state() const
628
263
return QSize( 16, 16 );
266
void KLed::updateCachedPixmap()
268
d->cachedPixmap[Off] = QPixmap();
269
d->cachedPixmap[On] = QPixmap();
273
void KLed::paintLed(Shape shape, Look look)
275
if (paintCachedPixmap()) {
279
QSize size(width() - 2, height() - 2);
280
if (shape == Circular) {
281
const int width = ledWidth();
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);
302
borderColor = KColorUtils::overlayColors(borderColor, glowOverlay);
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);
315
if (shape == Rectangular) {
318
painter.drawEllipse(r);
322
d->cachedPixmap[d->state] = QPixmap::fromImage(image);
324
painter.drawPixmap(1, 1, d->cachedPixmap[d->state]);
631
328
#include "kled.moc"