-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressring.cpp
203 lines (170 loc) · 5.15 KB
/
progressring.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "progressring.h"
#include "QPainter"
#include "QDebug"
ProgressRing::ProgressRing(QWidget *parent) : QWidget(parent)
{
}
void ProgressRing::setValue(int value)
{
this->currentValue = value;
}
void ProgressRing::setRange(int minValue, int maxValue)
{
this->minValue = minValue;
this->maxValue = maxValue;
}
void ProgressRing::setStartAngle(int startAngle)
{
this->startAngle = startAngle;
}
void ProgressRing::setClockWise(bool clockWise)
{
this->clockWise = clockWise;
}
void ProgressRing::setBgColor(const QColor &bgColor)
{
this->bgColor = bgColor;
}
void ProgressRing::setTextColor(const QColor &textColor)
{
this->textColor = textColor;
}
void ProgressRing::setRingColor(const QColor &ringColor)
{
this->ringColor = ringColor;
}
void ProgressRing::setRingBgColor(const QColor &ringBgColor)
{
this->ringBgColor = ringBgColor;
}
void ProgressRing::setCircleColor(const QColor &circleColor)
{
this->circleColor = circleColor;
}
void ProgressRing::setRingPadding(int ringPadding)
{
this->ringPadding = ringPadding;
}
void ProgressRing::setRingWidth(int ringWidth)
{
this->ringWidth = ringWidth;
}
void ProgressRing::paintEvent(QPaintEvent *)
{
int width = this->width();
int height = this->height();
int side = qMin(width, height);
qDebug()<<QString("width:%1,height:%2").arg(width).arg(height);
//绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.translate(width / 2, height / 2);
painter.scale(side / 200.0, side / 200.0);
//绘制背景
drawBg(&painter);
//绘制进度
drawRing(&painter);
//绘制间隔,重新绘制一个圆遮住,产生间距效果
if (ringPadding > 0) {
drawPadding(&painter);
}
//绘制中间圆
drawCircle(&painter);
//绘制当前值
drawValue(&painter);
}
void ProgressRing::drawBg(QPainter *painter)
{
int radius = 99;
painter->save();
painter->setPen(Qt::NoPen);
//这里有个技巧,如果没有间距则设置成圆环的背景色
painter->setBrush(ringPadding == 0 ? ringBgColor : bgColor);
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
painter->restore();
}
void ProgressRing::drawRing(QPainter *painter)
{
int radius = 99 - ringPadding;
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(ringColor);
QRectF rect(-radius, -radius, radius * 2, radius * 2);
//计算总范围角度,当前值范围角度,剩余值范围角度
double angleAll = 360.0;
double angleCurrent = angleAll * ((currentValue - minValue) / (maxValue - minValue));
double angleOther = angleAll - angleCurrent;
qDebug()<<QString("angleCurrent:%1,ringPadding:%2").arg(angleCurrent).arg(ringPadding);
//如果逆时针
if (!clockWise) {
angleCurrent = -angleCurrent;
angleOther = -angleOther;
}
//动态设置当前进度颜色
QColor color = ringColor;
// if (alarmMode == 1) {
// if (currentValue >= ringValue3) {
// color = ringColor3;
// } else if (currentValue >= ringValue2) {
// color = ringColor2;
// } else {
// color = ringColor1;
// }
// } else if (alarmMode == 2) {
// if (currentValue <= ringValue1) {
// color = ringColor1;
// } else if (currentValue <= ringValue2) {
// color = ringColor2;
// } else {
// color = ringColor3;
// }
// }
//绘制当前值饼圆
painter->setBrush(color);
painter->drawPie(rect, (startAngle - angleCurrent) * 16, angleCurrent * 16);
//绘制剩余值饼圆
painter->setBrush(ringBgColor);
painter->drawPie(rect, (startAngle - angleCurrent - angleOther) * 16, angleOther * 16);
painter->restore();
}
void ProgressRing::drawPadding(QPainter *painter)
{
int radius = 99 - ringWidth - ringPadding;
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(bgColor);
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
painter->restore();
}
void ProgressRing::drawCircle(QPainter *painter)
{
//文字的区域要减去进度的宽度及间距
int radius = 99 - ringWidth - (ringPadding * 2);
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(circleColor);
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
painter->restore();
}
void ProgressRing::drawValue(QPainter *painter)
{
//文字的区域要减去进度的宽度及间距
int radius = 99 - ringWidth - (ringPadding * 2);
painter->save();
painter->setPen(textColor);
QFont font;
int fontSize = radius - (showPercent ? 5 : 6);
qDebug()<<QString("fontsize:%1,radius:%2,showPercent:%d").arg(fontSize).arg(radius).arg(showPercent);
font.setPixelSize(fontSize);
painter->setFont(font);
QRectF textRect(-radius, -radius, radius * 2, radius * 2);
QString strValue;
if (showPercent) {
double percent = (currentValue * 100) / (maxValue - minValue);
strValue = QString("%1%").arg(percent, 0, 'f', precision);
} else {
strValue = QString("%1").arg(currentValue, 0, 'f', precision);
}
painter->drawText(textRect, Qt::AlignCenter, strValue);
painter->restore();
}