Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hled.cpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <QtGui>
  2. #include "hled.h"
  3. struct HLed::Private
  4. {
  5. public:
  6. Private()
  7. : darkerFactor(300), color(Qt::green), isOn(true)
  8. { }
  9. int darkerFactor;
  10. QColor color;
  11. bool isOn;
  12. };
  13. HLed::HLed(QWidget *parent)
  14. :QWidget(parent), m_d(new Private)
  15. {
  16. }
  17. HLed::~HLed()
  18. {
  19. delete m_d;
  20. }
  21. QColor HLed::color() const
  22. {
  23. return m_d->color;
  24. }
  25. void HLed::setColor(const QColor &color)
  26. {
  27. if (m_d->color == color)
  28. return;
  29. update();
  30. }
  31. QSize HLed::sizeHint() const
  32. {
  33. return QSize(20, 20);
  34. }
  35. QSize HLed::minimumSizeHint() const
  36. {
  37. return QSize(16, 16);
  38. }
  39. void HLed::toggle()
  40. {
  41. m_d->isOn = !m_d->isOn;
  42. update();
  43. }
  44. void HLed::turnOn(bool on)
  45. {
  46. m_d->isOn = on;
  47. update();
  48. }
  49. void HLed::turnOff(bool off)
  50. {
  51. turnOn(!off);
  52. }
  53. void HLed::paintEvent(QPaintEvent* /* event*/)
  54. {
  55. int width = ledWidth();
  56. QPainter painter(this);
  57. painter.setRenderHint(QPainter::Antialiasing);
  58. QColor color = m_d->isOn ? m_d->color
  59. : m_d->color.darker(m_d->darkerFactor);
  60. QBrush brush;
  61. brush.setStyle(Qt::SolidPattern);
  62. brush.setColor(color);
  63. painter.setBrush(brush);
  64. // draw plain
  65. painter.drawEllipse(1, 1, width-1, width-1);
  66. QPen pen;
  67. pen.setWidth(2);
  68. int pos = width / 5 + 1;
  69. int lightWidth = width * 2 / 3;
  70. int lightQuote = 130 * 2 / (lightWidth ? lightWidth : 1) + 100;
  71. // draw bright spot
  72. while (lightWidth) {
  73. color = color.lighter(lightQuote);
  74. pen.setColor(color);
  75. painter.setPen(pen);
  76. painter.drawEllipse(pos, pos, lightWidth, lightWidth);
  77. lightWidth--;
  78. if (!lightWidth)
  79. break;
  80. painter.drawEllipse(pos, pos, lightWidth, lightWidth);
  81. lightWidth--;
  82. if (!lightWidth)
  83. break;
  84. painter.drawEllipse(pos, pos, lightWidth, lightWidth);
  85. pos++;
  86. lightWidth--;
  87. }
  88. //draw border
  89. painter.setBrush(Qt::NoBrush);
  90. int angle = -720;
  91. color = palette().color(QPalette::Light);
  92. for (int arc=120; arc<2880; arc+=240) {
  93. pen.setColor(color);
  94. painter.setPen(pen);
  95. int w = width - pen.width()/2;
  96. painter.drawArc(pen.width()/2, pen.width()/2, w, w, angle+arc, 240);
  97. painter.drawArc(pen.width()/2, pen.width()/2, w, w, angle-arc, 240);
  98. color = color.darker(110);
  99. }
  100. }
  101. int HLed::ledWidth() const
  102. {
  103. int width = qMin(this->width(), this->height());
  104. width -= 2;
  105. return width > 0 ? width : 0;
  106. }