-
Notifications
You must be signed in to change notification settings - Fork 12
/
batterywidget.cpp
178 lines (145 loc) · 4.69 KB
/
batterywidget.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "batterywidget.h"
#include <QtWidgets>
class BatteryWidget::BatteryWidgetPrivate
{
public:
BatteryWidgetPrivate(BatteryWidget *q)
: q_ptr(q)
{}
BatteryWidget *q_ptr;
QColor borderColor = QColor(80, 80, 80);
QColor powerColor = QColor(65, 205, 82);
QColor alarmColor = QColor(250, 118, 113);
int alarmValue = 20;
int value = 0;
QPropertyAnimation *animation;
};
BatteryWidget::BatteryWidget(QWidget *parent)
: QWidget(parent)
, d_ptr(new BatteryWidgetPrivate(this))
{
d_ptr->animation = new QPropertyAnimation(this, "value", this);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
connect(this, &BatteryWidget::valueChanged, this, &BatteryWidget::onStartAnimation);
}
BatteryWidget::~BatteryWidget() = default;
void BatteryWidget::setBorderColor(const QColor &color)
{
d_ptr->borderColor = color;
update();
}
auto BatteryWidget::borderColor() const -> QColor
{
return d_ptr->borderColor;
}
void BatteryWidget::setPowerColor(const QColor &color)
{
d_ptr->powerColor = color;
update();
}
auto BatteryWidget::powerColor() const -> QColor
{
return d_ptr->powerColor;
}
void BatteryWidget::setAlarmColor(const QColor &color)
{
d_ptr->alarmColor = color;
update();
}
auto BatteryWidget::alarmColor() const -> QColor
{
return d_ptr->alarmColor;
}
auto BatteryWidget::sizeHint() const -> QSize
{
return {150, 80};
}
auto BatteryWidget::minimumSizeHint() const -> QSize
{
return {80, 45};
}
void BatteryWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter painter(this);
painter.setPen(Qt::NoPen);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
auto linew = 2 * painter.pen().widthF() / painter.transform().m11();
qreal headWidth = width() / 11;
qreal batteryWidth = width() - headWidth;
auto batteryRect = QRectF(QPointF(5, 5), QPointF(batteryWidth, height() - 5));
// 边框
drawBorder(&painter, batteryRect, linew);
// 电量
drawPower(&painter, batteryRect, linew);
// 数值
drawValue(&painter, batteryRect);
// 头部
drawHeader(&painter, batteryRect);
}
void BatteryWidget::onStartAnimation(const int value)
{
if (value == d_ptr->value) {
return;
}
int start = d_ptr->value;
int end = value;
d_ptr->animation->setStartValue(start);
d_ptr->animation->setEndValue(end);
d_ptr->animation->start();
}
auto BatteryWidget::value() const -> int
{
return d_ptr->value;
}
void BatteryWidget::setValue(int value)
{
Q_ASSERT(value >= 0 && value <= 100);
if (value == d_ptr->value) {
return;
}
d_ptr->value = value;
update();
}
void BatteryWidget::drawBorder(QPainter *painter, const QRectF &batteryRect, const qreal linew)
{
painter->setPen(QPen(d_ptr->borderColor, linew));
painter->setBrush(Qt::NoBrush);
qreal borderRadius = batteryRect.height() / 30;
painter->drawRoundedRect(batteryRect, borderRadius, borderRadius);
}
void BatteryWidget::drawPower(QPainter *painter, const QRectF &batteryRect, const qreal linew)
{
auto powerColoer = d_ptr->value > d_ptr->alarmValue ? d_ptr->powerColor : d_ptr->alarmColor;
qreal margin = qMin(width(), height()) / 50.0;
margin = qMax(margin, linew);
qreal unit = (batteryRect.width() - (margin * 2)) / 100;
QPointF topLeft(batteryRect.topLeft().x() + margin, batteryRect.topLeft().y() + margin);
QPointF bottomRight(d_ptr->value * unit + margin + 5, batteryRect.bottomRight().y() - margin);
QRectF rect(topLeft, bottomRight);
qreal bgRadius = rect.height() / 30;
painter->setPen(Qt::NoPen);
painter->setBrush(powerColoer);
painter->drawRoundedRect(rect, bgRadius, bgRadius);
}
void BatteryWidget::drawValue(QPainter *painter, const QRectF &batteryRect)
{
auto fontColoer = d_ptr->value > d_ptr->alarmValue ? QColor(64, 65, 66) : d_ptr->alarmColor;
auto text = QString("%1%").arg(d_ptr->value);
QFont font("Microsoft YaHei", batteryRect.width() / 5);
font.setLetterSpacing(QFont::AbsoluteSpacing, batteryRect.width() / 25);
painter->setFont(font);
painter->setPen(fontColoer);
painter->setBrush(Qt::NoBrush);
painter->drawText(batteryRect, Qt::AlignCenter, text);
}
void BatteryWidget::drawHeader(QPainter *painter, const QRectF &batteryRect)
{
QPointF headRectTopLeft(batteryRect.topRight().x(), height() / 3);
QPointF headRectBottomRight(width(), height() - height() / 3);
QRectF headRect(headRectTopLeft, headRectBottomRight);
qreal headRadius = headRect.height() / 30;
painter->setPen(Qt::NoPen);
painter->setBrush(d_ptr->borderColor);
painter->drawRoundedRect(headRect, headRadius, headRadius);
}