1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| #include "led.h"
Led::Led(QWidget* parent) :
QWidget (parent) ,
m_colour (QColor(0, 255, 0)) ,
m_ledOff () ,
m_ledOn () ,
m_lit (false)
{
setMinimumSize(3, 3);
}
Led::Led(const QColor& colour, QWidget* parent) :
QWidget (parent) ,
m_colour (colour) ,
m_ledOff () ,
m_ledOn () ,
m_lit (false)
{
setMinimumSize(3, 3);
}
Led::~Led()
{
}
void Led::setColour(const QColor& newColour)
{
if(m_colour != newColour)
{
m_colour = newColour;
update();
}
}
void Led::toggle()
{
m_lit = !m_lit;
update();
}
void Led::turnOff()
{
if(m_lit)
{
m_lit = !m_lit;
update();
}
}
void Led::turnOn()
{
if(!m_lit)
{
m_lit = !m_lit;
update();
}
}
bool Led::paintCachedPixmap()
{
if (m_lit && m_ledOn.isNull())
return false;
if (!m_lit && m_ledOff.isNull())
return false;
QPainter painter;
painter.begin(this);
if(m_lit)
painter.drawPixmap(1, 1, m_ledOn);
else
painter.drawPixmap(1, 1, m_ledOff);
painter.end();
return true;
}
void Led::paintEvent(QPaintEvent* e)
{
QWidget::paintEvent(e);
if (paintCachedPixmap())
return;
QColor borderColour;
QConicalGradient borderGradient;
QPointF center;
QColor fillColour;
QRadialGradient fillGradient;
QImage image;
QSize ledSize;
QPainter painter;
QBrush penBrush;
qreal penWidth;
QRectF r;
if(width() < height())
ledSize = QSize(width() - 2, width() - 2);
else
ledSize = QSize(height() - 2, height() - 2);
center = QPointF(ledSize.width() / 2.0, ledSize.height() / 2.0);
penWidth = ledSize.width() / 8.0;
r = QRectF(penWidth / 2.0, penWidth / 2.0, ledSize.width() - penWidth, ledSize.height() - penWidth);
image = QImage(ledSize, QImage::Format_ARGB32_Premultiplied);
if(m_lit)
fillColour = m_colour;
else
fillColour = m_colour.dark(300);
fillGradient = QRadialGradient(center, ledSize.width() / 2.0, QPointF(center.x(), ledSize.height() / 3.0));
fillGradient.setColorAt(0.0, fillColour.light(250));
fillGradient.setColorAt(0.5, fillColour.light(130));
fillGradient.setColorAt(1.0, fillColour);
borderColour = palette().color(QPalette::Dark);
borderGradient = QConicalGradient(center, 90);
borderGradient.setColorAt(0.2, borderColour);
borderGradient.setColorAt(0.5, palette().color(QPalette::Light));
borderGradient.setColorAt(0.8, borderColour);
penBrush = QBrush(borderGradient);
painter.begin(&image);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(QBrush(fillGradient));
painter.setPen(QPen(penBrush, penWidth));
painter.drawEllipse(r);
painter.end();
if(m_lit)
m_ledOn = QPixmap::fromImage(image);
else
m_ledOff = QPixmap::fromImage(image);
painter.begin(this);
if(m_lit)
painter.drawPixmap(1, 1, m_ledOn);
else
painter.drawPixmap(1, 1, m_ledOff);
painter.end();
}
void Led::resizeEvent(QResizeEvent* e)
{
QWidget::resizeEvent(e);
m_ledOff = QPixmap();
m_ledOn = QPixmap();
update();
} |
Partager