Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #ifndef LEDPANEL_HPP
  2. #define LEDPANEL_HPP
  3.  
  4. #include <QWidget>
  5.  
  6. #include <cassert>
  7. #include <QPainter>
  8.  
  9. class LedPanel : public QWidget
  10. {
  11. Q_OBJECT
  12.  
  13. public:
  14. LedPanel(QWidget *parent = 0)
  15. : QWidget(parent)
  16. {
  17. for (int x = 0; x < 32; ++x)
  18. {
  19. for (int y = 0; y < 8; ++y)
  20. {
  21. states[y][x] = false;
  22. }
  23. }
  24. }
  25.  
  26. void on(unsigned int x, unsigned int y)
  27. {
  28. assert(x < 32);
  29. assert(y < 8);
  30. states[y][x] = true;
  31. }
  32.  
  33. protected:
  34. void paintEvent(QPaintEvent*) override
  35. {
  36. QPainter painter(this);
  37.  
  38. // Background
  39. QBrush background(QColor(23, 23, 34));
  40. painter.setBrush(background);
  41. painter.drawRect(0, 0, width(), height());
  42.  
  43. // LED
  44. int w = width() / 32;
  45. int h = height() / 8;
  46.  
  47. for (int x = 0; x < 32; ++x)
  48. {
  49. for (int y = 0; y < 8; ++y)
  50. {
  51. if (states[y][x])
  52. {
  53. QBrush led(QColor(0, 125, 110));
  54. painter.setBrush(led);
  55. QRect rect(x*w, y*h, w, h);
  56. painter.drawRect(rect);
  57. }
  58. }
  59. }
  60. }
  61.  
  62. private:
  63. bool states[8][32]; // 8 lines of 32 LEDs each
  64. };
  65.  
  66. #endif // LEDPANEL_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement