示例#1
0
// 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();
}
示例#2
0
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();
}
示例#3
0
// 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();
}
示例#4
0
// 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();
}