tdesktop/Telegram/SourceFiles/ui/widgets/level_meter.cpp

48 lines
1.1 KiB
C++
Raw Normal View History

2019-01-05 11:08:02 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "ui/widgets/level_meter.h"
2022-09-16 20:23:27 +00:00
#include "ui/painter.h"
2019-01-05 11:08:02 +00:00
namespace Ui {
2019-01-11 10:07:56 +00:00
LevelMeter::LevelMeter(QWidget *parent, const style::LevelMeter &st)
: RpWidget(parent)
, _st(st) {
2019-01-05 11:08:02 +00:00
}
2019-01-11 10:07:56 +00:00
void LevelMeter::setValue(float value) {
2019-01-05 11:08:02 +00:00
_value = value;
repaint();
}
2019-01-11 10:07:56 +00:00
void LevelMeter::paintEvent(QPaintEvent* event) {
2022-09-16 20:23:27 +00:00
auto p = QPainter(this);
2019-01-05 11:08:02 +00:00
PainterHighQualityEnabler hq(p);
p.setPen(Qt::NoPen);
2019-01-11 10:07:56 +00:00
const auto activeFg = _st.activeFg;
const auto inactiveFg = _st.inactiveFg;
const auto radius = _st.lineWidth / 2;
const auto rect = QRect(0, 0, _st.lineWidth, height());
2019-01-05 11:08:02 +00:00
p.setBrush(activeFg);
for (auto i = 0; i < _st.lineCount; ++i) {
2019-01-11 10:07:56 +00:00
const auto valueAtLine = (float)(i + 1) / _st.lineCount;
2019-01-05 11:08:02 +00:00
if (valueAtLine > _value) {
p.setBrush(inactiveFg);
}
2019-01-11 10:07:56 +00:00
p.drawRoundedRect(
rect.translated((_st.lineWidth + _st.lineSpacing) * i, 0),
radius,
radius);
2019-01-05 11:08:02 +00:00
}
}
} // namespace Ui