kdeui Library API Documentation

kled.cpp

00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1998 Jörg Habenicht (j.habenicht@europemail.com) 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 /************************************************************************* 00021 * $Id: kled.cpp,v 1.26 2003/09/06 19:06:31 binner Exp $ 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 //setShape(Circular); 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 //setShape(shape); 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() // paint a ROUND FLAT led lamp 00157 { 00158 QPainter paint; 00159 QColor color; 00160 QBrush brush; 00161 QPen pen; 00162 00163 // Initialize coordinates, width, and height of the LED 00164 // 00165 int width = this->width(); 00166 // Make sure the LED is round! 00167 if (width > this->height()) 00168 width = this->height(); 00169 width -= 2; // leave one pixel border 00170 if (width < 0) 00171 width = 0; 00172 00173 00174 // start painting widget 00175 // 00176 paint.begin( this ); 00177 00178 // Set the color of the LED according to given parameters 00179 color = ( led_state ) ? led_color : d->offcolor; 00180 00181 // Set the brush to SolidPattern, this fills the entire area 00182 // of the ellipse which is drawn with a thin gray "border" (pen) 00183 brush.setStyle( QBrush::SolidPattern ); 00184 brush.setColor( color ); 00185 00186 pen.setWidth( 1 ); 00187 color = colorGroup().dark(); 00188 pen.setColor( color ); // Set the pen accordingly 00189 00190 paint.setPen( pen ); // Select pen for drawing 00191 paint.setBrush( brush ); // Assign the brush to the painter 00192 00193 // Draws a "flat" LED with the given color: 00194 paint.drawEllipse( 1, 1, width, width ); 00195 00196 paint.end(); 00197 // 00198 // painting done 00199 } 00200 00201 void 00202 KLed::paintRound() // paint a ROUND RAISED led lamp 00203 { 00204 QPainter paint; 00205 QColor color; 00206 QBrush brush; 00207 QPen pen; 00208 00209 // Initialize coordinates, width, and height of the LED 00210 int width = this->width(); 00211 00212 // Make sure the LED is round! 00213 if (width > this->height()) 00214 width = this->height(); 00215 width -= 2; // leave one pixel border 00216 if (width < 0) 00217 width = 0; 00218 00219 // start painting widget 00220 // 00221 paint.begin( this ); 00222 00223 // Set the color of the LED according to given parameters 00224 color = ( led_state ) ? led_color : d->offcolor; 00225 00226 // Set the brush to SolidPattern, this fills the entire area 00227 // of the ellipse which is drawn first 00228 brush.setStyle( QBrush::SolidPattern ); 00229 brush.setColor( color ); 00230 paint.setBrush( brush ); // Assign the brush to the painter 00231 00232 // Draws a "flat" LED with the given color: 00233 paint.drawEllipse( 1, 1, width, width ); 00234 00235 // Draw the bright light spot of the LED now, using modified "old" 00236 // painter routine taken from KDEUI´s KLed widget: 00237 00238 // Setting the new width of the pen is essential to avoid "pixelized" 00239 // shadow like it can be observed with the old LED code 00240 pen.setWidth( 2 ); 00241 00242 // shrink the light on the LED to a size about 2/3 of the complete LED 00243 int pos = width/5 + 1; 00244 int light_width = width; 00245 light_width *= 2; 00246 light_width /= 3; 00247 00248 // Calculate the LED´s "light factor": 00249 int light_quote = (130*2/(light_width?light_width:1))+100; 00250 00251 // Now draw the bright spot on the LED: 00252 while (light_width) { 00253 color = color.light( light_quote ); // make color lighter 00254 pen.setColor( color ); // set color as pen color 00255 paint.setPen( pen ); // select the pen for drawing 00256 paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle) 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 // Drawing of bright spot finished, now draw a thin gray border 00269 // around the LED; it looks nicer that way. We do this here to 00270 // avoid that the border can be erased by the bright spot of the LED 00271 00272 pen.setWidth( 1 ); 00273 color = colorGroup().dark(); 00274 pen.setColor( color ); // Set the pen accordingly 00275 paint.setPen( pen ); // Select pen for drawing 00276 brush.setStyle( QBrush::NoBrush ); // Switch off the brush 00277 paint.setBrush( brush ); // This avoids filling of the ellipse 00278 00279 paint.drawEllipse( 1, 1, width, width ); 00280 00281 paint.end(); 00282 // 00283 // painting done 00284 } 00285 00286 void 00287 KLed::paintSunken() // paint a ROUND SUNKEN led lamp 00288 { 00289 QPainter paint; 00290 QColor color; 00291 QBrush brush; 00292 QPen pen; 00293 00294 // First of all we want to know what area should be updated 00295 // Initialize coordinates, width, and height of the LED 00296 int width = this->width(); 00297 00298 // Make sure the LED is round! 00299 if (width > this->height()) 00300 width = this->height(); 00301 width -= 2; // leave one pixel border 00302 if (width < 0) 00303 width = 0; 00304 00305 // maybe we could stop HERE, if width <=0 ? 00306 00307 // start painting widget 00308 // 00309 paint.begin( this ); 00310 00311 // Set the color of the LED according to given parameters 00312 color = ( led_state ) ? led_color : d->offcolor; 00313 00314 // Set the brush to SolidPattern, this fills the entire area 00315 // of the ellipse which is drawn first 00316 brush.setStyle( QBrush::SolidPattern ); 00317 brush.setColor( color ); 00318 paint.setBrush( brush ); // Assign the brush to the painter 00319 00320 // Draws a "flat" LED with the given color: 00321 paint.drawEllipse( 1, 1, width, width ); 00322 00323 // Draw the bright light spot of the LED now, using modified "old" 00324 // painter routine taken from KDEUI´s KLed widget: 00325 00326 // Setting the new width of the pen is essential to avoid "pixelized" 00327 // shadow like it can be observed with the old LED code 00328 pen.setWidth( 2 ); 00329 00330 // shrink the light on the LED to a size about 2/3 of the complete LED 00331 int pos = width/5 + 1; 00332 int light_width = width; 00333 light_width *= 2; 00334 light_width /= 3; 00335 00336 // Calculate the LED´s "light factor": 00337 int light_quote = (130*2/(light_width?light_width:1))+100; 00338 00339 // Now draw the bright spot on the LED: 00340 while (light_width) { 00341 color = color.light( light_quote ); // make color lighter 00342 pen.setColor( color ); // set color as pen color 00343 paint.setPen( pen ); // select the pen for drawing 00344 paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle) 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 // Drawing of bright spot finished, now draw a thin border 00357 // around the LED which resembles a shadow with light coming 00358 // from the upper left. 00359 00360 pen.setWidth( 3 ); // ### shouldn't this value be smaller for smaller LEDs? 00361 brush.setStyle( QBrush::NoBrush ); // Switch off the brush 00362 paint.setBrush( brush ); // This avoids filling of the ellipse 00363 00364 // Set the initial color value to colorGroup().light() (bright) and start 00365 // drawing the shadow border at 45° (45*16 = 720). 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 ); //FIXME: this should somehow use the contrast value 00376 } // end for ( angle = 720; angle < 6480; angle += 160 ) 00377 00378 paint.end(); 00379 // 00380 // painting done 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 // Draw verticals 00406 int i; 00407 for(i=0; i < w; i+= 4 /* dx */) 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 // setColor(led_color); 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 { /*BASE::virtual_hook( id, data );*/ } 00566 00567 #include "kled.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Oct 8 11:14:26 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003