qledindicator.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /***************************************************************************
  2. * Copyright (C) 2010 by Tn *
  3. * thenobody@poczta.fm *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU Library General Public License as *
  7. * published by the Free Software Foundation; either version 3 of the *
  8. * License, or (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU Library General Public *
  16. * License along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <QPainter>
  21. #include "qledindicator.h"
  22. const qreal QLedIndicator::scaledSize = 1000;
  23. QLedIndicator::QLedIndicator(QWidget *parent) :
  24. QWidget(parent)
  25. {
  26. this->checked = false;
  27. onColor1 = QColor(0, 255, 0);
  28. onColor2 = QColor(0, 192, 0);
  29. offColor1 = QColor(255, 0, 0);
  30. offColor2 = QColor(192, 0, 0);
  31. }
  32. void QLedIndicator::paintEvent(QPaintEvent *event)
  33. {
  34. (void) event;
  35. qreal realSize = qMin(width() / 1.3, height() / 1.3);
  36. QRadialGradient gradient;
  37. QPainter painter(this);
  38. QPen pen(Qt::black);
  39. pen.setWidth(1);
  40. painter.setRenderHint(QPainter::Antialiasing);
  41. painter.translate(width() / 2, height() / 2);
  42. painter.scale(realSize / scaledSize, realSize / scaledSize);
  43. gradient = QRadialGradient(QPointF(-500, -500), 1500, QPointF(-500, -500));
  44. gradient.setColorAt(0, QColor(224, 224, 224));
  45. gradient.setColorAt(1, QColor(28, 28, 28));
  46. painter.setPen(pen);
  47. painter.setBrush(QBrush(gradient));
  48. painter.drawEllipse(QPointF(0, 0), 500, 500);
  49. gradient = QRadialGradient(QPointF(500, 500), 1500, QPointF(500, 500));
  50. gradient.setColorAt(0, QColor(224, 224, 224));
  51. gradient.setColorAt(1, QColor(28, 28, 28));
  52. painter.setPen(pen);
  53. painter.setBrush(QBrush(gradient));
  54. painter.drawEllipse(QPointF(0, 0), 450, 450);
  55. painter.setPen(pen);
  56. if (isChecked())
  57. {
  58. gradient = QRadialGradient(QPointF(-500, -500), 1500,
  59. QPointF(-500, -500));
  60. gradient.setColorAt(0, onColor1);
  61. gradient.setColorAt(1, onColor2);
  62. }
  63. else
  64. {
  65. gradient = QRadialGradient(QPointF(500, 500), 1500, QPointF(500, 500));
  66. gradient.setColorAt(0, offColor1);
  67. gradient.setColorAt(1, offColor2);
  68. }
  69. painter.setBrush(gradient);
  70. painter.drawEllipse(QPointF(0, 0), 400, 400);
  71. }
  72. void QLedIndicator::setChecked(bool checked)
  73. {
  74. this->checked = checked;
  75. update();
  76. }
  77. bool QLedIndicator::isChecked() const
  78. {
  79. return this->checked;
  80. }