// paint a ROUND FLAT led lamp void KLed::paintFlat() { if ( paintCachedPixmap() ) return; QPainter paint; QColor color; QBrush brush; QPen pen; int width = ledWidth(); int scale = 3; QPixmap *tmpMap = 0; width *= scale; tmpMap = new QPixmap( width + 6, width + 6 ); tmpMap->fill( palette().color( backgroundRole() ) ); // start painting widget paint.begin( tmpMap ); paint.setRenderHint(QPainter::Antialiasing); // Set the color of the LED according to given parameters color = ( d->state ) ? d->color : d->offColor; // Set the brush to SolidPattern, this fills the entire area // of the ellipse which is drawn with a thin gray "border" (pen) brush.setStyle( Qt::SolidPattern ); brush.setColor( color ); pen.setWidth( scale ); color = palette().color( QPalette::Dark ); pen.setColor( color ); // Set the pen accordingly paint.setPen( pen ); // Select pen for drawing paint.setBrush( brush ); // Assign the brush to the painter // Draws a "flat" LED with the given color: paint.drawEllipse( scale, scale, width - scale * 2, width - scale * 2 ); paint.end(); // painting done QPixmap *&dest = ( d->state == On ? d->onMap : d->offMap ); QImage i = tmpMap->toImage(); width /= 3; i = i.scaled( width, width, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); delete tmpMap; dest = new QPixmap( QPixmap::fromImage( i ) ); paint.begin( this ); paint.drawPixmap( 0, 0, *dest ); paint.end(); }
void KLed::paintLed(Shape shape, Look look) { if (paintCachedPixmap()) { return; } QSize size(width() - 2, height() - 2); if (shape == Circular) { const int width = ledWidth(); size = QSize(width, width); } QPointF center(size.width() / 2.0, size.height() / 2.0); const int smallestSize = qMin(size.width(), size.height()); QPainter painter; QImage image(size, QImage::Format_ARGB32_Premultiplied); image.fill(0); QRadialGradient fillGradient(center, smallestSize / 2.0, QPointF(center.x(), size.height() / 3.0)); const QColor fillColor = d->state != Off ? d->color : d->color.dark(d->darkFactor); fillGradient.setColorAt(0.0, fillColor.light(250)); fillGradient.setColorAt(0.5, fillColor.light(130)); fillGradient.setColorAt(1.0, fillColor); QConicalGradient borderGradient(center, look == Sunken ? 90 : -90); QColor borderColor = palette().color(QPalette::Dark); if (d->state == On) { QColor glowOverlay = fillColor; glowOverlay.setAlpha(80); borderColor = KColorUtils::overlayColors(borderColor, glowOverlay); } borderGradient.setColorAt(0.2, borderColor); borderGradient.setColorAt(0.5, palette().color(QPalette::Light)); borderGradient.setColorAt(0.8, borderColor); painter.begin(&image); painter.setRenderHint(QPainter::Antialiasing); painter.setBrush(look == Flat ? QBrush(fillColor) : QBrush(fillGradient)); const QBrush penBrush = (look == Flat) ? QBrush(borderColor) : QBrush(borderGradient); const qreal penWidth = smallestSize / 8.0; painter.setPen(QPen(penBrush, penWidth)); QRectF r(penWidth / 2.0, penWidth / 2.0, size.width() - penWidth, size.height() - penWidth); if (shape == Rectangular) { painter.drawRect(r); } else { painter.drawEllipse(r); } painter.end(); d->cachedPixmap[d->state] = QPixmap::fromImage(image); painter.begin(this); painter.drawPixmap(1, 1, d->cachedPixmap[d->state]); painter.end(); }
void Led::paintEvent(QPaintEvent * /*event*/) { int width = ledWidth(); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); QColor color = params.isOn ? params.color : params.color.darker(params.darkerFactor); QBrush brush; brush.setStyle(Qt::SolidPattern); brush.setColor(color); painter.setBrush(brush); // draw plain painter.drawEllipse(1, 1, width-1, width-1); QPen pen; pen.setWidth(2); int pos = width / 5 + 1; int lightWidth = width * 2 / 3; int lightQuote = 130 * 2 / (lightWidth ? lightWidth : 1) + 100; // draw bright spot while (lightWidth) { color = color.lighter(lightQuote); pen.setColor(color); painter.setPen(pen); painter.drawEllipse(pos, pos, lightWidth, lightWidth); lightWidth--; if (!lightWidth) break; painter.drawEllipse(pos, pos, lightWidth, lightWidth); lightWidth--; if (!lightWidth) break; painter.drawEllipse(pos, pos, lightWidth, lightWidth); pos++; lightWidth--; } //draw border painter.setBrush(Qt::NoBrush); int angle = -720; color = palette().color(QPalette::Light); for (int arc=120; arc<2880; arc+=240) { pen.setColor(color); painter.setPen(pen); int w = width - pen.width()/2; painter.drawArc(pen.width()/2, pen.width()/2, w, w, angle+arc, 240); painter.drawArc(pen.width()/2, pen.width()/2, w, w, angle-arc, 240); color = color.darker(110); } }
// paint a ROUND SUNKEN led lamp void KLed::paintSunken() { if ( paintCachedPixmap() ) return; QPainter paint; QColor color; QBrush brush; QPen pen; // First of all we want to know what area should be updated // Initialize coordinates, width, and height of the LED int width = ledWidth(); int scale = 3; QPixmap *tmpMap = 0; width *= scale; tmpMap = new QPixmap( width, width ); tmpMap->fill( palette().color( backgroundRole() ) ); paint.begin( tmpMap ); paint.setRenderHint(QPainter::Antialiasing); // Set the color of the LED according to given parameters color = ( d->state == On ) ? d->color : d->offColor; // Set the brush to SolidPattern, this fills the entire area // of the ellipse which is drawn first brush.setStyle( Qt::SolidPattern ); brush.setColor( color ); paint.setBrush( brush ); // Assign the brush to the painter // Draws a "flat" LED with the given color: paint.drawEllipse( scale, scale, width - scale * 2, width - scale * 2 ); // Draw the bright light spot of the LED now, using modified "old" // painter routine taken from KDEUI's KLed widget: // Setting the new width of the pen is essential to avoid "pixelized" // shadow like it can be observed with the old LED code pen.setWidth( 2 * scale ); // shrink the light on the LED to a size about 2/3 of the complete LED int pos = width / 5 + 1; int light_width = width; light_width *= 2; light_width /= 3; // Calculate the LED's "light factor": int light_quote = ( 130 * 2 / ( light_width ? light_width : 1 ) ) + 100; // Now draw the bright spot on the LED: while ( light_width ) { color = color.light( light_quote ); // make color lighter pen.setColor( color ); // set color as pen color paint.setPen( pen ); // select the pen for drawing paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle) light_width--; if ( !light_width ) break; paint.drawEllipse( pos, pos, light_width, light_width ); light_width--; if ( !light_width ) break; paint.drawEllipse( pos, pos, light_width, light_width ); pos++; light_width--; } // Drawing of bright spot finished, now draw a thin border // around the LED which resembles a shadow with light coming // from the upper left. pen.setWidth( 2 * scale + 1 ); // ### shouldn't this value be smaller for smaller LEDs? brush.setStyle( Qt::NoBrush ); // Switch off the brush paint.setBrush( brush ); // This avoids filling of the ellipse // Set the initial color value to QColorGroup(palette()).light() (bright) and start // drawing the shadow border at 45° (45*16 = 720). int angle = -720; color = palette().color( QPalette::Light ); for ( int arc = 120; arc < 2880; arc += 240 ) { pen.setColor( color ); paint.setPen( pen ); int w = width - pen.width() / 2 - scale + 1; paint.drawArc( pen.width() / 2, pen.width() / 2, w, w, angle + arc, 240 ); paint.drawArc( pen.width() / 2, pen.width() / 2, w, w, angle - arc, 240 ); color = color.dark( 110 ); //FIXME: this should somehow use the contrast value } // end for ( angle = 720; angle < 6480; angle += 160 ) paint.end(); // painting done QPixmap *&dest = ( d->state == On ? d->onMap : d->offMap ); QImage i = tmpMap->toImage(); width /= 3; i = i.scaled( width, width, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); delete tmpMap; dest = new QPixmap( QPixmap::fromImage( i ) ); paint.begin( this ); paint.setCompositionMode(QPainter::CompositionMode_Source); paint.drawPixmap( 0, 0, *dest ); paint.end(); }
// paint a ROUND RAISED led lamp void KLed::paintRaised() { if ( paintCachedPixmap() ) return; QPainter paint; QColor color; QBrush brush; QPen pen; // Initialize coordinates, width, and height of the LED int width = ledWidth(); int scale = 3; QPixmap *tmpMap = 0; width *= scale; tmpMap = new QPixmap( width + 6, width + 6 ); tmpMap->fill( palette().color( backgroundRole() ) ); paint.begin( tmpMap ); paint.setRenderHint(QPainter::Antialiasing); // Set the color of the LED according to given parameters color = ( d->state == On ? d->color : d->offColor ); // Set the brush to SolidPattern, this fills the entire area // of the ellipse which is drawn first brush.setStyle( Qt::SolidPattern ); brush.setColor( color ); paint.setBrush( brush ); // Assign the brush to the painter // Draws a "flat" LED with the given color: paint.drawEllipse( scale, scale, width - scale * 2, width - scale * 2 ); // Draw the bright light spot of the LED now, using modified "old" // painter routine taken from KDEUI's KLed widget: // Setting the new width of the pen is essential to avoid "pixelized" // shadow like it can be observed with the old LED code pen.setWidth( 2 * scale ); // shrink the light on the LED to a size about 2/3 of the complete LED int pos = width / 5 + 1; int light_width = width; light_width *= 2; light_width /= 3; // Calculate the LED's "light factor": int light_quote = ( 130 * 2 / ( light_width ? light_width : 1 ) ) + 100; // Now draw the bright spot on the LED: while ( light_width ) { color = color.light( light_quote ); // make color lighter pen.setColor( color ); // set color as pen color paint.setPen( pen ); // select the pen for drawing paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle) light_width--; if ( !light_width ) break; paint.drawEllipse( pos, pos, light_width, light_width ); light_width--; if ( !light_width ) break; paint.drawEllipse( pos, pos, light_width, light_width ); pos++; light_width--; } // Drawing of bright spot finished, now draw a thin gray border // around the LED; it looks nicer that way. We do this here to // avoid that the border can be erased by the bright spot of the LED pen.setWidth( 2 * scale + 1 ); color = palette().color( QPalette::Dark ); pen.setColor( color ); // Set the pen accordingly paint.setPen( pen ); // Select pen for drawing brush.setStyle( Qt::NoBrush ); // Switch off the brush paint.setBrush( brush ); // This avoids filling of the ellipse paint.drawEllipse( 2, 2, width, width ); paint.end(); // painting done QPixmap *&dest = ( d->state == On ? d->onMap : d->offMap ); QImage i = tmpMap->toImage(); width /= 3; i = i.scaled( width, width, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); delete tmpMap; dest = new QPixmap( QPixmap::fromImage( i ) ); paint.begin( this ); paint.drawPixmap( 0, 0, *dest ); paint.end(); }