~ubuntu-branches/ubuntu/natty/kde4libs/natty-proposed

« back to all changes in this revision

Viewing changes to kdeui/widgets/kled.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell, Scott Kitterman, Jonathan Riddell
  • Date: 2010-11-22 17:59:02 UTC
  • mfrom: (1.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101122175902-yubxubd0pg6hn11z
Tags: 4:4.5.80a-0ubuntu1
[ Scott Kitterman ]
* New upstream beta release
  - Refreshed all patches
  - Updated debian/patches/10_make_libkdeinit4_private.diff to use Qfile
    instead of Qstring in kdecore/kernel/kstandarddirs_unix.cpp
  - Updated debian/patches/kubuntu_01_kubuntu_useragent.diff to provide
    Kubuntu in the Konqueror user agen string with the changes in
    kio/kio/kprotocolmanager.cpp
  - Partially updated debian/patches/kubuntu_05_langpack_desktop_files.diff
    and left the balance in kdecore/localization/klocale.cpp.rej for later
    revision
  - Update debian/patches/kubuntu_06_user_disk_mounting.diff for changes in
    solid/solid/backends/hal/halstorageaccess.cpp
  - Remove debian/patches/kubuntu_71_backport_plasma_webview_changes.diff
    (backported from upstream, so already present now)
  - Add minimum version for libattica-dev of 0.1.90 to build-depends
  - Bump minimum version for libsoprano-dev build-depend to 2.5.60
  - Add minimum version for shared-desktop-ontologies of 0.5 in build-dep

[ Jonathan Riddell ]
* Add build-depends on grantlee, libudev-dev, hupnp (FIXME needs packaging fixes)
* Update kubuntu_04_add_langpack_path.diff 28_find_old_kde4_html_documentation.diff
  22_hack_in_etc_kde4_in_kstandarddirs.diff for QT_NO_CAST_FROM_ASCII
* Update kubuntu_05_langpack_desktop_files.diff for new upstream code
* Add kubuntu_78_solid_trunk.diff to fix solid linking
* Add libnepomukutils4 package for new library
* Don't install kcm_ssl for now, e-mailed upstream to suggest moving to kdebase
* Add kubuntu_79_knewstuff_fix.diff to fix compile broken by non-trunk commit
  http://websvn.kde.org/?view=revision&revision=1199825
* kdelibs5-data replaces old kdebase-runtime-data due to moved file
* Add kubuntu_80_find_hupnp.diff to find hupnp include files, committed upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* This file is part of the KDE libraries
2
2
    Copyright (C) 1998 Jörg Habenicht (j.habenicht@europemail.com)
 
3
    Copyright (C) 2010 Christoph Feck <christoph@maxiom.de>
3
4
 
4
5
    This library is free software; you can redistribute it and/or
5
6
    modify it under the terms of the GNU Library General Public
19
20
 
20
21
#include "kled.h"
21
22
 
22
 
#define PAINT_BENCH
23
 
#undef PAINT_BENCH
24
 
 
25
 
#ifdef PAINT_BENCH
26
 
#include <qdatetime.h>
27
 
#include <stdio.h>
28
 
#endif
29
 
 
 
23
#include <kcolorutils.h>
30
24
 
31
25
#include <QtGui/QPainter>
32
26
#include <QtGui/QImage>
33
 
#include <QtGui/QColor>
34
 
#include <QtGui/QApplication>
 
27
#include <QtGui/QStyle>
 
28
#include <QtGui/QStyleOption>
35
29
 
36
30
class KLed::Private
37
31
{
38
32
  public:
39
33
    Private()
40
 
      : darkFactor( 300 ), color( Qt::green ),
41
 
        state( On ), look( Raised ), shape( Circular ),
42
 
        offMap( 0 ), onMap( 0 )
43
 
    {
44
 
      offColor = color.dark( darkFactor );
45
 
    }
46
 
 
47
 
    ~Private()
48
 
    {
49
 
      delete offMap;
50
 
      delete onMap;
 
34
      : darkFactor( 300 ),
 
35
        state( On ), look( Raised ), shape( Circular )
 
36
    {
51
37
    }
52
38
 
53
39
    int darkFactor;
56
42
    Look look;
57
43
    Shape shape;
58
44
 
59
 
    QColor offColor;
60
 
    QPixmap *offMap;
61
 
    QPixmap *onMap;
 
45
    QPixmap cachedPixmap[2]; // for both states
 
46
    QStyle::ControlElement ce_indicatorLedCircular;
 
47
    QStyle::ControlElement ce_indicatorLedRectangular;
62
48
};
63
49
 
64
50
 
67
53
  : QWidget( parent ),
68
54
    d( new Private )
69
55
{
70
 
  setColor( QColor( Qt::green ) );
 
56
  setColor( Qt::green );
71
57
}
72
58
 
73
59
 
83
69
  : QWidget( parent ),
84
70
    d( new Private )
85
71
{
86
 
  d->state = state;
 
72
  d->state = (state == Off ? Off : On);
87
73
  d->look = look;
88
74
  d->shape = shape;
89
75
 
97
83
 
98
84
void KLed::paintEvent( QPaintEvent* )
99
85
{
100
 
#ifdef PAINT_BENCH
101
 
  const int rounds = 1000;
102
 
  QTime t;
103
 
  t.start();
104
 
  for ( int i = 0; i < rounds; i++ ) {
105
 
#endif
106
86
    switch( d->shape ) {
107
87
      case Rectangular:
108
88
        switch ( d->look ) {
115
95
          case Flat:
116
96
            paintRect();
117
97
            break;
118
 
          default:
119
 
            qWarning( "%s: in class KLed: no KLed::Look set", qApp->argv()[0] );
120
98
        }
121
99
        break;
122
100
      case Circular:
130
108
          case Sunken:
131
109
            paintSunken();
132
110
            break;
133
 
          default:
134
 
            qWarning( "%s: in class KLed: no KLed::Look set", qApp->argv()[0] );
135
111
        }
136
112
        break;
137
 
      default:
138
 
        qWarning( "%s: in class KLed: no KLed::Shape set", qApp->argv()[0] );
139
 
        break;
140
113
    }
141
 
#ifdef PAINT_BENCH
142
 
  }
143
 
 
144
 
  int ready = t.elapsed();
145
 
  qWarning( "elapsed: %d msec. for %d rounds", ready, rounds );
146
 
#endif
147
114
}
148
115
 
149
116
int KLed::ledWidth() const
150
117
{
151
 
  // Initialize coordinates, width, and height of the LED
152
 
  int width = this->width();
153
 
 
154
118
  // Make sure the LED is round!
155
 
  if ( width > this->height() )
156
 
    width = this->height();
 
119
  int size = qMin(width(), height());
157
120
 
158
121
  // leave one pixel border
159
 
  width -= 2;
160
 
 
161
 
  if ( width < 0 )
162
 
    width = 0;
163
 
 
164
 
  return width;
 
122
  size -= 2;
 
123
 
 
124
  return qMax(0, size);
165
125
}
166
126
 
167
127
bool KLed::paintCachedPixmap()
168
128
{
169
 
  if ( d->state == On ) {
170
 
    if ( d->onMap ) {
171
 
      QPainter paint( this );
172
 
      paint.drawPixmap( 0, 0, *d->onMap );
173
 
      return true;
174
 
    }
175
 
  } else {
176
 
    if ( d->offMap ) {
177
 
      QPainter paint( this );
178
 
      paint.drawPixmap( 0, 0, *d->offMap );
179
 
      return true;
180
 
    }
181
 
  }
182
 
 
183
 
  return false;
 
129
    if (d->cachedPixmap[d->state].isNull()) {
 
130
        return false;
 
131
    }
 
132
    QPainter painter(this);
 
133
    painter.drawPixmap(1, 1, d->cachedPixmap[d->state]);
 
134
    return true;
184
135
}
185
136
 
186
 
// paint a ROUND FLAT led lamp
187
137
void KLed::paintFlat()
188
138
{
189
 
  if ( paintCachedPixmap() )
190
 
    return;
191
 
 
192
 
  QPainter paint;
193
 
  QColor color;
194
 
  QBrush brush;
195
 
  QPen pen;
196
 
 
197
 
  int width = ledWidth();
198
 
 
199
 
  int scale = 3;
200
 
  QPixmap *tmpMap = 0;
201
 
 
202
 
  width *= scale;
203
 
 
204
 
  tmpMap = new QPixmap( width + 6, width + 6 );
205
 
  tmpMap->fill( palette().color( backgroundRole() ) );
206
 
 
207
 
  // start painting widget
208
 
  paint.begin( tmpMap );
209
 
  paint.setRenderHint(QPainter::Antialiasing);
210
 
 
211
 
  // Set the color of the LED according to given parameters
212
 
  color = ( d->state ) ? d->color : d->offColor;
213
 
 
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 );
218
 
 
219
 
  pen.setWidth( scale );
220
 
  color = palette().color( QPalette::Dark );
221
 
  pen.setColor( color ); // Set the pen accordingly
222
 
 
223
 
  paint.setPen( pen ); // Select pen for drawing
224
 
  paint.setBrush( brush ); // Assign the brush to the painter
225
 
 
226
 
  // Draws a "flat" LED with the given color:
227
 
  paint.drawEllipse( scale, scale, width - scale * 2, width - scale * 2 );
228
 
 
229
 
  paint.end();
230
 
 
231
 
  // painting done
232
 
  QPixmap *&dest = ( d->state == On ? d->onMap : d->offMap );
233
 
  QImage i = tmpMap->toImage();
234
 
  width /= 3;
235
 
  i = i.scaled( width, width, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
236
 
  delete tmpMap;
237
 
 
238
 
  dest = new QPixmap( QPixmap::fromImage( i ) );
239
 
  paint.begin( this );
240
 
  paint.drawPixmap( 0, 0, *dest );
241
 
  paint.end();
 
139
    paintLed(Circular, Flat);
242
140
}
243
141
 
244
 
// paint a ROUND RAISED led lamp
245
142
void KLed::paintRaised()
246
143
{
247
 
  if ( paintCachedPixmap() )
248
 
    return;
249
 
 
250
 
  QPainter paint;
251
 
  QColor color;
252
 
  QBrush brush;
253
 
  QPen pen;
254
 
 
255
 
  // Initialize coordinates, width, and height of the LED
256
 
  int width = ledWidth();
257
 
 
258
 
  int scale = 3;
259
 
  QPixmap *tmpMap = 0;
260
 
 
261
 
  width *= scale;
262
 
 
263
 
  tmpMap = new QPixmap( width + 6, width + 6 );
264
 
  tmpMap->fill( palette().color( backgroundRole() ) );
265
 
  paint.begin( tmpMap );
266
 
  paint.setRenderHint(QPainter::Antialiasing);
267
 
 
268
 
  // Set the color of the LED according to given parameters
269
 
  color = ( d->state == On ? d->color : d->offColor );
270
 
 
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
276
 
 
277
 
  // Draws a "flat" LED with the given color:
278
 
  paint.drawEllipse( scale, scale, width - scale * 2, width - scale * 2 );
279
 
 
280
 
  // Draw the bright light spot of the LED now, using modified "old"
281
 
  // painter routine taken from KDEUI's KLed widget:
282
 
 
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 );
286
 
 
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;
290
 
  light_width *= 2;
291
 
  light_width /= 3;
292
 
 
293
 
  // Calculate the LED's "light factor":
294
 
  int light_quote = ( 130 * 2 / ( light_width ? light_width : 1 ) ) + 100;
295
 
 
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)
302
 
    light_width--;
303
 
 
304
 
    if ( !light_width )
305
 
      break;
306
 
 
307
 
    paint.drawEllipse( pos, pos, light_width, light_width );
308
 
    light_width--;
309
 
 
310
 
    if ( !light_width )
311
 
      break;
312
 
 
313
 
    paint.drawEllipse( pos, pos, light_width, light_width );
314
 
    pos++;
315
 
    light_width--;
316
 
  }
317
 
 
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
321
 
 
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
328
 
 
329
 
  paint.drawEllipse( 2, 2, width, width );
330
 
 
331
 
  paint.end();
332
 
 
333
 
  // painting done
334
 
  QPixmap *&dest = ( d->state == On ? d->onMap : d->offMap );
335
 
  QImage i = tmpMap->toImage();
336
 
  width /= 3;
337
 
  i = i.scaled( width, width, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
338
 
  delete tmpMap;
339
 
 
340
 
  dest = new QPixmap( QPixmap::fromImage( i ) );
341
 
  paint.begin( this );
342
 
  paint.drawPixmap( 0, 0, *dest );
343
 
  paint.end();
 
144
    paintLed(Circular, Raised);
344
145
}
345
146
 
346
 
// paint a ROUND SUNKEN led lamp
347
147
void KLed::paintSunken()
348
148
{
349
 
  if ( paintCachedPixmap() )
350
 
    return;
351
 
 
352
 
  QPainter paint;
353
 
  QColor color;
354
 
  QBrush brush;
355
 
  QPen pen;
356
 
 
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();
360
 
 
361
 
  int scale = 3;
362
 
  QPixmap *tmpMap = 0;
363
 
 
364
 
  width *= scale;
365
 
 
366
 
  tmpMap = new QPixmap( width, width );
367
 
  tmpMap->fill( palette().color( backgroundRole() ) );
368
 
  paint.begin( tmpMap );
369
 
  paint.setRenderHint(QPainter::Antialiasing);
370
 
 
371
 
  // Set the color of the LED according to given parameters
372
 
  color = ( d->state == On ) ? d->color : d->offColor;
373
 
 
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
379
 
 
380
 
  // Draws a "flat" LED with the given color:
381
 
  paint.drawEllipse( scale, scale, width - scale * 2, width - scale * 2 );
382
 
 
383
 
  // Draw the bright light spot of the LED now, using modified "old"
384
 
  // painter routine taken from KDEUI's KLed widget:
385
 
 
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 );
389
 
 
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;
393
 
  light_width *= 2;
394
 
  light_width /= 3;
395
 
 
396
 
  // Calculate the LED's "light factor":
397
 
  int light_quote = ( 130 * 2 / ( light_width ? light_width : 1 ) ) + 100;
398
 
 
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)
405
 
    light_width--;
406
 
 
407
 
    if ( !light_width )
408
 
      break;
409
 
 
410
 
    paint.drawEllipse( pos, pos, light_width, light_width );
411
 
    light_width--;
412
 
 
413
 
    if ( !light_width )
414
 
      break;
415
 
 
416
 
    paint.drawEllipse( pos, pos, light_width, light_width );
417
 
    pos++;
418
 
    light_width--;
419
 
  }
420
 
 
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.
424
 
 
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
428
 
 
429
 
  // Set the initial color value to QColorGroup(palette()).light() (bright) and start
430
 
  // drawing the shadow border at 45° (45*16 = 720).
431
 
 
432
 
  int angle = -720;
433
 
  color = palette().color( QPalette::Light );
434
 
 
435
 
  for ( int arc = 120; arc < 2880; arc += 240 ) {
436
 
    pen.setColor( color );
437
 
    paint.setPen( pen );
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 )
443
 
 
444
 
  paint.end();
445
 
 
446
 
  // painting done
447
 
 
448
 
  QPixmap *&dest = ( d->state == On ? d->onMap : d->offMap );
449
 
  QImage i = tmpMap->toImage();
450
 
  width /= 3;
451
 
  i = i.scaled( width, width, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
452
 
  delete tmpMap;
453
 
 
454
 
  dest = new QPixmap( QPixmap::fromImage( i ) );
455
 
  paint.begin( this );
456
 
  paint.setCompositionMode(QPainter::CompositionMode_Source);
457
 
  paint.drawPixmap( 0, 0, *dest );
458
 
  paint.end();
 
149
    paintLed(Circular, Sunken);
459
150
}
460
151
 
461
152
void KLed::paintRect()
462
153
{
463
 
  QPainter painter( this );
464
 
  painter.setRenderHint(QPainter::Antialiasing);
465
 
  QBrush lightBrush( d->color );
466
 
  QBrush darkBrush( d->offColor );
467
 
 
468
 
  QPen pen( d->color.dark( 300 ) );
469
 
  int w = width();
470
 
  int h = height();
471
 
 
472
 
  // -----
473
 
  switch ( d->state ) {
474
 
    case On:
475
 
      painter.setBrush( lightBrush );
476
 
      painter.drawRect( 0, 0, w, h );
477
 
      break;
478
 
    case Off:
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 );
484
 
 
485
 
      // Draw verticals
486
 
      int i;
487
 
      for ( i = 0; i < w; i += 4 /* dx */)
488
 
        painter.drawLine( i, 1, i, h - 1 );
489
 
      break;
490
 
    default:
491
 
      break;
492
 
  }
 
154
    paintLed(Rectangular, Flat);
493
155
}
494
156
 
495
157
void KLed::paintRectFrame( bool raised )
496
158
{
497
 
  QPainter painter( this );
498
 
  painter.setRenderHint(QPainter::Antialiasing);
499
 
  QBrush lightBrush( d->color );
500
 
  QBrush darkBrush( d->offColor );
501
 
  int w = width();
502
 
  int h = height();
503
 
 
504
 
  if ( raised ) {
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 ) );
512
 
  } else {
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 ) );
519
 
  }
 
159
    paintLed(Rectangular, raised ? Raised : Sunken);
520
160
}
521
161
 
522
162
KLed::State KLed::state() const
544
184
  if ( d->state == state)
545
185
    return;
546
186
 
547
 
  d->state = state;
548
 
  update();
 
187
  d->state = (state == Off ? Off : On);
 
188
  updateCachedPixmap();
549
189
}
550
190
 
551
191
void KLed::setShape( Shape shape )
554
194
    return;
555
195
 
556
196
  d->shape = shape;
557
 
  update();
 
197
  updateCachedPixmap();
558
198
}
559
199
 
560
200
void KLed::setColor( const QColor &color )
562
202
  if ( d->color == color )
563
203
    return;
564
204
 
565
 
  if ( d->onMap ) {
566
 
    delete d->onMap;
567
 
    d->onMap = 0;
568
 
  }
569
 
 
570
 
  if ( d->offMap ) {
571
 
    delete d->offMap;
572
 
    d->offMap = 0;
573
 
  }
574
 
 
575
205
  d->color = color;
576
 
  d->offColor = color.dark( d->darkFactor );
577
 
 
578
 
  update();
 
206
  updateCachedPixmap();
579
207
}
580
208
 
581
209
void KLed::setDarkFactor( int darkFactor )
584
212
    return;
585
213
 
586
214
  d->darkFactor = darkFactor;
587
 
  d->offColor = d->color.dark( darkFactor );
588
 
  update();
 
215
  updateCachedPixmap();
589
216
}
590
217
 
591
218
int KLed::darkFactor() const
599
226
    return;
600
227
 
601
228
  d->look = look;
602
 
  update();
 
229
  updateCachedPixmap();
603
230
}
604
231
 
605
232
void KLed::toggle()
606
233
{
607
234
  d->state = (d->state == On ? Off : On);
608
 
  update();
 
235
  updateCachedPixmap();
609
236
}
610
237
 
611
238
void KLed::on()
618
245
  setState( Off );
619
246
}
620
247
 
 
248
void KLed::resizeEvent( QResizeEvent * )
 
249
{
 
250
    updateCachedPixmap();
 
251
}
 
252
 
621
253
QSize KLed::sizeHint() const
622
254
{
623
 
  return QSize( 16, 16 );
 
255
    QStyleOption option;
 
256
    option.initFrom(this);
 
257
    int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, &option, this);
 
258
    return QSize( iconSize,  iconSize );
624
259
}
625
260
 
626
261
QSize KLed::minimumSizeHint() const
628
263
  return QSize( 16, 16 );
629
264
}
630
265
 
 
266
void KLed::updateCachedPixmap()
 
267
{
 
268
    d->cachedPixmap[Off] = QPixmap();
 
269
    d->cachedPixmap[On] = QPixmap();
 
270
    update();
 
271
}
 
272
 
 
273
void KLed::paintLed(Shape shape, Look look)
 
274
{
 
275
    if (paintCachedPixmap()) {
 
276
        return;
 
277
    }
 
278
 
 
279
    QSize size(width() - 2, height() - 2);
 
280
    if (shape == Circular) {
 
281
        const int width = ledWidth();
 
282
        size = QSize(width, width);
 
283
    }
 
284
    QPointF center(size.width() / 2.0, size.height() / 2.0);
 
285
    const int smallestSize = qMin(size.width(), size.height());
 
286
    QPainter painter;
 
287
 
 
288
    QImage image(size, QImage::Format_ARGB32_Premultiplied);
 
289
    image.fill(0);
 
290
 
 
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);
 
296
 
 
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);
 
303
    }
 
304
    borderGradient.setColorAt(0.2, borderColor);
 
305
    borderGradient.setColorAt(0.5, palette().color(QPalette::Light));
 
306
    borderGradient.setColorAt(0.8, borderColor);
 
307
 
 
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) {
 
316
        painter.drawRect(r);
 
317
    } else {
 
318
        painter.drawEllipse(r);
 
319
    }
 
320
    painter.end();
 
321
 
 
322
    d->cachedPixmap[d->state] = QPixmap::fromImage(image);
 
323
    painter.begin(this);
 
324
    painter.drawPixmap(1, 1, d->cachedPixmap[d->state]);
 
325
    painter.end();
 
326
}
 
327
 
631
328
#include "kled.moc"