qledindicator.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. qreal realSize = qMin(width() / 1.3, height() / 1.3);
  35. QRadialGradient gradient;
  36. QPainter painter(this);
  37. QPen pen(Qt::black);
  38. pen.setWidth(1);
  39. painter.setRenderHint(QPainter::Antialiasing);
  40. painter.translate(width() / 2, height() / 2);
  41. painter.scale(realSize / scaledSize, realSize / scaledSize);
  42. gradient = QRadialGradient(QPointF(-500, -500), 1500, QPointF(-500, -500));
  43. gradient.setColorAt(0, QColor(224, 224, 224));
  44. gradient.setColorAt(1, QColor(28, 28, 28));
  45. painter.setPen(pen);
  46. painter.setBrush(QBrush(gradient));
  47. painter.drawEllipse(QPointF(0, 0), 500, 500);
  48. gradient = QRadialGradient(QPointF(500, 500), 1500, QPointF(500, 500));
  49. gradient.setColorAt(0, QColor(224, 224, 224));
  50. gradient.setColorAt(1, QColor(28, 28, 28));
  51. painter.setPen(pen);
  52. painter.setBrush(QBrush(gradient));
  53. painter.drawEllipse(QPointF(0, 0), 450, 450);
  54. painter.setPen(pen);
  55. if (isChecked())
  56. {
  57. gradient = QRadialGradient(QPointF(-500, -500), 1500,
  58. QPointF(-500, -500));
  59. gradient.setColorAt(0, onColor1);
  60. gradient.setColorAt(1, onColor2);
  61. }
  62. else
  63. {
  64. gradient = QRadialGradient(QPointF(500, 500), 1500, QPointF(500, 500));
  65. gradient.setColorAt(0, offColor1);
  66. gradient.setColorAt(1, offColor2);
  67. }
  68. painter.setBrush(gradient);
  69. painter.drawEllipse(QPointF(0, 0), 400, 400);
  70. }
  71. void QLedIndicator::setChecked(bool checked)
  72. {
  73. this->checked = checked;
  74. update();
  75. }
  76. bool QLedIndicator::isChecked() const
  77. {
  78. return this->checked;
  79. }