level_meter.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "ui/widgets/level_meter.h"
  8. #include "ui/painter.h"
  9. #include "styles/style_widgets.h"
  10. namespace Ui {
  11. LevelMeter::LevelMeter(QWidget *parent, const style::LevelMeter &st)
  12. : RpWidget(parent)
  13. , _st(st) {
  14. }
  15. void LevelMeter::setValue(float value) {
  16. _value = value;
  17. repaint();
  18. }
  19. void LevelMeter::paintEvent(QPaintEvent* event) {
  20. auto p = QPainter(this);
  21. auto hq = PainterHighQualityEnabler(p);
  22. p.setPen(Qt::NoPen);
  23. const auto activeFg = _st.activeFg;
  24. const auto inactiveFg = _st.inactiveFg;
  25. const auto radius = _st.lineWidth / 2;
  26. const auto rect = QRect(0, 0, _st.lineWidth, height());
  27. p.setBrush(activeFg);
  28. for (auto i = 0; i < _st.lineCount; ++i) {
  29. const auto valueAtLine = (float64)(i + 1) / _st.lineCount;
  30. if (valueAtLine > _value) {
  31. p.setBrush(inactiveFg);
  32. }
  33. p.drawRoundedRect(
  34. rect.translated((_st.lineWidth + _st.lineSpacing) * i, 0),
  35. radius,
  36. radius);
  37. }
  38. }
  39. } // namespace Ui