-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolumemeter.cpp
More file actions
104 lines (90 loc) · 2.89 KB
/
volumemeter.cpp
File metadata and controls
104 lines (90 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <QMouseEvent>
#include <QStyleOptionProgressBar>
#include <QStylePainter>
#include "volumemeter.h"
volume_meter::volume_meter(QWidget *parent) : QProgressBar(parent)
{
setMouseTracking(true);
setAutoFillBackground(false);
}
void volume_meter::mousePressEvent(QMouseEvent *event)
{
setValueFromPosition(event->pos());
}
void volume_meter::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) != 0U)
{
setValueFromPosition(event->pos());
}
}
void volume_meter::paintEvent(QPaintEvent * /*event*/)
{
QStylePainter painter(this);
QStyleOptionProgressBar option;
initStyleOption(&option);
painter.setPen(QColor("#53B6D4"));
painter.setBrush(QColor("#0F75A8"));
painter.drawRoundedRect(rect().adjusted(0, 0, -1, -1), 2, 2);
const int num_blocks = 10;
const int max_value = qMax(1, maximum());
const int lit_blocks = static_cast<int>((static_cast<double>(value()) / static_cast<double>(max_value)) * num_blocks);
if (orientation() == Qt::Horizontal)
{
const double block_width = static_cast<double>(width()) / static_cast<double>(num_blocks);
for (int i = 0; i < lit_blocks; ++i)
{
const QRectF block_rect(static_cast<double>(i) * block_width, 0.0, block_width, static_cast<double>(height()));
painter.fillRect(block_rect.adjusted(1.0, 1.0, -1.0, -1.0), bar_color_);
}
}
else
{
const double block_height = static_cast<double>(height()) / static_cast<double>(num_blocks);
for (int i = 0; i < lit_blocks; ++i)
{
const double y = static_cast<double>(height()) - (static_cast<double>(i + 1) * block_height);
const QRectF block_rect(0.0, y, static_cast<double>(width()), block_height);
painter.fillRect(block_rect.adjusted(1.0, 1.0, -1.0, -1.0), bar_color_);
}
}
}
void volume_meter::wheelEvent(QWheelEvent *event)
{
const int step = 5;
int new_value = value();
if (event->angleDelta().y() > 0)
{
new_value += step;
}
else if (event->angleDelta().y() < 0)
{
new_value -= step;
}
new_value = qBound(minimum(), new_value, maximum());
if (new_value != value())
{
setValue(new_value);
emit value_changed(new_value);
}
event->accept();
}
void volume_meter::setValueFromPosition(const QPoint &pos)
{
double ratio = 0.0;
if (orientation() == Qt::Horizontal)
{
ratio = static_cast<double>(pos.x()) / static_cast<double>(qMax(1, width()));
}
else
{
ratio = static_cast<double>(height() - pos.y()) / static_cast<double>(qMax(1, height()));
}
int new_value = static_cast<int>(ratio * static_cast<double>(maximum()));
new_value = qBound(minimum(), new_value, maximum());
if (new_value != value())
{
setValue(new_value);
emit value_changed(new_value);
}
}