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