00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
#define PAINT_BENCH
00025
#undef PAINT_BENCH
00026
00027
#ifdef PAINT_BENCH
00028
#include <qdatetime.h>
00029
#include <stdio.h>
00030
#endif
00031
00032
00033
#include <qpainter.h>
00034
#include <qcolor.h>
00035
#include <kapplication.h>
00036
#include <kpixmapeffect.h>
00037
#include "kled.h"
00038
00039
00040
00041
class KLed::KLedPrivate
00042 {
00043
friend class KLed;
00044
00045
int dark_factor;
00046
QColor offcolor;
00047 };
00048
00049
00050
00051 KLed::KLed(
QWidget *parent,
const char *name)
00052 :
QWidget( parent, name),
00053 led_state(On),
00054 led_look(Raised),
00055 led_shape(Circular)
00056 {
00057
QColor col(green);
00058 d =
new KLed::KLedPrivate;
00059 d->dark_factor = 300;
00060 d->offcolor = col.
dark(300);
00061
00062
setColor(col);
00063 }
00064
00065
00066 KLed::KLed(
const QColor& col,
QWidget *parent,
const char *name)
00067 :
QWidget( parent, name),
00068 led_state(On),
00069 led_look(Raised),
00070 led_shape(Circular)
00071 {
00072 d =
new KLed::KLedPrivate;
00073 d->dark_factor = 300;
00074 d->offcolor = col.
dark(300);
00075
00076
setColor(col);
00077
00078 }
00079
00080 KLed::KLed(
const QColor& col, KLed::State state,
00081 KLed::Look look, KLed::Shape shape,
QWidget *parent,
const char *name )
00082 :
QWidget(parent, name),
00083 led_state(state),
00084 led_look(look),
00085 led_shape(shape)
00086 {
00087 d =
new KLed::KLedPrivate;
00088 d->dark_factor = 300;
00089 d->offcolor = col.
dark(300);
00090
00091
00092
setColor(col);
00093 }
00094
00095
00096 KLed::~KLed()
00097 {
00098
delete d;
00099 }
00100
00101
void
00102 KLed::paintEvent(
QPaintEvent *)
00103 {
00104
#ifdef PAINT_BENCH
00105
const int rounds = 1000;
00106
QTime t;
00107 t.
start();
00108
for (
int i=0; i<rounds; i++) {
00109
#endif
00110
switch(led_shape)
00111 {
00112
case Rectangular:
00113
switch (led_look)
00114 {
00115
case Sunken :
00116 paintRectFrame(
false);
00117
break;
00118
case Raised :
00119 paintRectFrame(
true);
00120
break;
00121
case Flat :
00122
paintRect();
00123
break;
00124
default :
00125 qWarning(
"%s: in class KLed: no KLed::Look set",qApp->argv()[0]);
00126 }
00127
break;
00128
case Circular:
00129
switch (led_look)
00130 {
00131
case Flat :
00132
paintFlat();
00133
break;
00134
case Raised :
00135
paintRound();
00136
break;
00137
case Sunken :
00138
paintSunken();
00139
break;
00140
default:
00141 qWarning(
"%s: in class KLed: no KLed::Look set",qApp->argv()[0]);
00142 }
00143
break;
00144
default:
00145 qWarning(
"%s: in class KLed: no KLed::Shape set",qApp->argv()[0]);
00146
break;
00147 }
00148
#ifdef PAINT_BENCH
00149
}
00150
int ready = t.
elapsed();
00151 qWarning(
"elapsed: %d msec. for %d rounds", ready, rounds);
00152
#endif
00153
}
00154
00155
void
00156 KLed::paintFlat()
00157 {
00158
QPainter paint;
00159
QColor color;
00160
QBrush brush;
00161
QPen pen;
00162
00163
00164
00165
int width = this->
width();
00166
00167
if (width > this->
height())
00168 width = this->
height();
00169 width -= 2;
00170
if (width < 0)
00171 width = 0;
00172
00173
00174
00175
00176 paint.
begin(
this );
00177
00178
00179 color = ( led_state ) ? led_color : d->offcolor;
00180
00181
00182
00183 brush.
setStyle( QBrush::SolidPattern );
00184 brush.
setColor( color );
00185
00186 pen.
setWidth( 1 );
00187 color =
colorGroup().dark();
00188 pen.
setColor( color );
00189
00190 paint.
setPen( pen );
00191 paint.
setBrush( brush );
00192
00193
00194 paint.
drawEllipse( 1, 1, width, width );
00195
00196 paint.
end();
00197
00198
00199 }
00200
00201
void
00202 KLed::paintRound()
00203 {
00204
QPainter paint;
00205
QColor color;
00206
QBrush brush;
00207
QPen pen;
00208
00209
00210
int width = this->
width();
00211
00212
00213
if (width > this->
height())
00214 width = this->
height();
00215 width -= 2;
00216
if (width < 0)
00217 width = 0;
00218
00219
00220
00221 paint.
begin(
this );
00222
00223
00224 color = ( led_state ) ? led_color : d->offcolor;
00225
00226
00227
00228 brush.
setStyle( QBrush::SolidPattern );
00229 brush.
setColor( color );
00230 paint.
setBrush( brush );
00231
00232
00233 paint.
drawEllipse( 1, 1, width, width );
00234
00235
00236
00237
00238
00239
00240 pen.
setWidth( 2 );
00241
00242
00243
int pos = width/5 + 1;
00244
int light_width = width;
00245 light_width *= 2;
00246 light_width /= 3;
00247
00248
00249
int light_quote = (130*2/(light_width?light_width:1))+100;
00250
00251
00252
while (light_width) {
00253 color = color.
light( light_quote );
00254 pen.
setColor( color );
00255 paint.
setPen( pen );
00256 paint.
drawEllipse( pos, pos, light_width, light_width );
00257 light_width--;
00258
if (!light_width)
00259
break;
00260 paint.
drawEllipse( pos, pos, light_width, light_width );
00261 light_width--;
00262
if (!light_width)
00263
break;
00264 paint.
drawEllipse( pos, pos, light_width, light_width );
00265 pos++; light_width--;
00266 }
00267
00268
00269
00270
00271
00272 pen.
setWidth( 1 );
00273 color =
colorGroup().dark();
00274 pen.
setColor( color );
00275 paint.
setPen( pen );
00276 brush.
setStyle( QBrush::NoBrush );
00277 paint.
setBrush( brush );
00278
00279 paint.
drawEllipse( 1, 1, width, width );
00280
00281 paint.
end();
00282
00283
00284 }
00285
00286
void
00287 KLed::paintSunken()
00288 {
00289
QPainter paint;
00290
QColor color;
00291
QBrush brush;
00292
QPen pen;
00293
00294
00295
00296
int width = this->
width();
00297
00298
00299
if (width > this->
height())
00300 width = this->
height();
00301 width -= 2;
00302
if (width < 0)
00303 width = 0;
00304
00305
00306
00307
00308
00309 paint.
begin(
this );
00310
00311
00312 color = ( led_state ) ? led_color : d->offcolor;
00313
00314
00315
00316 brush.
setStyle( QBrush::SolidPattern );
00317 brush.
setColor( color );
00318 paint.
setBrush( brush );
00319
00320
00321 paint.
drawEllipse( 1, 1, width, width );
00322
00323
00324
00325
00326
00327
00328 pen.
setWidth( 2 );
00329
00330
00331
int pos = width/5 + 1;
00332
int light_width = width;
00333 light_width *= 2;
00334 light_width /= 3;
00335
00336
00337
int light_quote = (130*2/(light_width?light_width:1))+100;
00338
00339
00340
while (light_width) {
00341 color = color.
light( light_quote );
00342 pen.
setColor( color );
00343 paint.
setPen( pen );
00344 paint.
drawEllipse( pos, pos, light_width, light_width );
00345 light_width--;
00346
if (!light_width)
00347
break;
00348 paint.
drawEllipse( pos, pos, light_width, light_width );
00349 light_width--;
00350
if (!light_width)
00351
break;
00352 paint.
drawEllipse( pos, pos, light_width, light_width );
00353 pos++; light_width--;
00354 }
00355
00356
00357
00358
00359
00360 pen.
setWidth( 3 );
00361 brush.
setStyle( QBrush::NoBrush );
00362 paint.
setBrush( brush );
00363
00364
00365
00366
00367
int angle = -720;
00368 color =
colorGroup().light();
00369
00370
for (
int arc = 120; arc < 2880; arc += 240 ) {
00371 pen.
setColor( color );
00372 paint.
setPen( pen );
00373 paint.
drawArc( 1, 1, width, width, angle + arc, 240 );
00374 paint.
drawArc( 1, 1, width, width, angle - arc, 240 );
00375 color = color.
dark( 110 );
00376 }
00377
00378 paint.
end();
00379
00380
00381 }
00382
00383
void
00384 KLed::paintRect()
00385 {
00386
QPainter painter(
this);
00387
QBrush lightBrush(led_color);
00388
QBrush darkBrush(d->offcolor);
00389
QPen pen(led_color.
dark(300));
00390
int w=
width();
00391
int h=
height();
00392
00393
switch(led_state)
00394 {
00395
case On:
00396 painter.
setBrush(lightBrush);
00397 painter.
drawRect(0, 0, w, h);
00398
break;
00399
case Off:
00400 painter.
setBrush(darkBrush);
00401 painter.
drawRect(0, 0, w, h);
00402 painter.
setPen(pen);
00403 painter.
drawLine(0, 0, w, 0);
00404 painter.
drawLine(0, h-1, w, h-1);
00405
00406
int i;
00407
for(i=0; i < w; i+= 4 )
00408 painter.
drawLine(i, 1, i, h-1);
00409
break;
00410
default:
break;
00411 }
00412 }
00413
00414
void
00415 KLed::paintRectFrame(
bool raised)
00416 {
00417
QPainter painter(
this);
00418
QBrush lightBrush(led_color);
00419
QBrush darkBrush(d->offcolor);
00420
int w=
width();
00421
int h=
height();
00422
QColor black=Qt::black;
00423
QColor white=Qt::white;
00424
00425
if(raised)
00426 {
00427 painter.
setPen(white);
00428 painter.
drawLine(0, 0, 0, h-1);
00429 painter.
drawLine(1, 0, w-1, 0);
00430 painter.
setPen(black);
00431 painter.
drawLine(1, h-1, w-1, h-1);
00432 painter.
drawLine(w-1, 1, w-1, h-1);
00433 painter.
fillRect(1, 1, w-2, h-2,
00434 (led_state==On)? lightBrush : darkBrush);
00435 }
else {
00436 painter.
setPen(black);
00437 painter.
drawRect(0,0,w,h);
00438 painter.
drawRect(0,0,w-1,h-1);
00439 painter.
setPen(white);
00440 painter.
drawRect(1,1,w-1,h-1);
00441 painter.
fillRect(2, 2, w-4, h-4,
00442 (led_state==On)? lightBrush : darkBrush);
00443 }
00444 }
00445
00446 KLed::State
00447
KLed::state()
const
00448
{
00449
return led_state;
00450 }
00451
00452
KLed::Shape
00453 KLed::shape()
const
00454
{
00455
return led_shape;
00456 }
00457
00458
QColor
00459
KLed::color()
const
00460
{
00461
return led_color;
00462 }
00463
00464
KLed::Look
00465
KLed::look()
const
00466
{
00467
return led_look;
00468 }
00469
00470
void
00471 KLed::setState( State state )
00472 {
00473
if (led_state != state)
00474 {
00475 led_state = state;
00476
update();
00477 }
00478 }
00479
00480
void
00481 KLed::toggleState()
00482 {
00483 led_state = (led_state == On) ? Off : On;
00484
00485
update();
00486 }
00487
00488
void
00489 KLed::setShape(KLed::Shape s)
00490 {
00491
if(led_shape!=s)
00492 {
00493 led_shape = s;
00494
update();
00495 }
00496 }
00497
00498
void
00499 KLed::setColor(
const QColor& col)
00500 {
00501
if(led_color!=col) {
00502 led_color = col;
00503 d->offcolor = col.dark(d->dark_factor);
00504
update();
00505 }
00506 }
00507
00508
void
00509 KLed::setDarkFactor(
int darkfactor)
00510 {
00511
if (d->dark_factor != darkfactor) {
00512 d->dark_factor = darkfactor;
00513 d->offcolor = led_color.
dark(darkfactor);
00514
update();
00515 }
00516 }
00517
00518
int
00519
KLed::darkFactor()
const
00520
{
00521
return d->dark_factor;
00522 }
00523
00524
void
00525 KLed::setLook( Look look )
00526 {
00527
if(led_look!=look)
00528 {
00529 led_look = look;
00530
update();
00531 }
00532 }
00533
00534
void
00535 KLed::toggle()
00536 {
00537
toggleState();
00538 }
00539
00540
void
00541 KLed::on()
00542 {
00543
setState(On);
00544 }
00545
00546
void
00547 KLed::off()
00548 {
00549
setState(Off);
00550 }
00551
00552
QSize
00553
KLed::sizeHint()
const
00554
{
00555
return QSize(16, 16);
00556 }
00557
00558
QSize
00559
KLed::minimumSizeHint()
const
00560
{
00561
return QSize(16, 16 );
00562 }
00563
00564
void KLed::virtual_hook(
int,
void* )
00565 { }
00566
00567
#include "kled.moc"