forked from CUIT2014102073/QRoundProgressBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQRoundProgressBar.cpp
174 lines (152 loc) · 4.72 KB
/
QRoundProgressBar.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
#include "QRoundProgressBar.h"
QRoundProgressBar::QRoundProgressBar(QWidget *parent) :
QWidget(parent)
{
//给变量设置初始值-默认值
startAngle=-45;
endAngle=225;
ringWidth=20;
maxRadius=110;
iconWidth=15;
currAngle=270;
circularBorderColor=qRgb(120,120,120);
insideMaskColor=qRgb(68,68,68);
startGradientColor=qRgb(80,80,80);
endGradientColor=qRgb(150,140,20);
}
void QRoundProgressBar::SetStartAngle(int value)
{
startAngle=value;
}
void QRoundProgressBar::SetEndAngle(int value)
{
endAngle=value;
}
void QRoundProgressBar::SetMaxRadius(int value)
{
maxRadius=value;
}
void QRoundProgressBar::SetRingWidth(int value)
{
ringWidth=value;
}
void QRoundProgressBar::SetIcomWidth(int value)
{
iconWidth=value;
}
void QRoundProgressBar::SetCircularBorderColor(QRgb value)
{
circularBorderColor=value;
}
void QRoundProgressBar::SetInsideMaskColor(QRgb value)
{
insideMaskColor=value;
}
void QRoundProgressBar::SetRadialGradient(QRgb startColor,QRgb endColor)
{
startGradientColor=startColor;
endGradientColor=endColor;
}
void QRoundProgressBar::SetCurrPrograss(int value)
{
currAngle=value;
update();
}
void QRoundProgressBar::paintEvent(QPaintEvent*)
{
//初始化变量
QPainter painter(this);
// 右移1位 相当于width()/2
painter.translate(width() >> 1, height() >> 1);
//启动反锯齿
painter.setRenderHint(QPainter::Antialiasing);
//绘制外圆
DrawOutSideCircle(painter);
//绘制两个小圆外圆
DrawTwoSmallCircle(painter,0);
//绘制圆环
DrawCircularRing(painter,0);
//绘制两个小圆内圆
DrawTwoSmallCircle(painter,1);
//绘制渐变色圆环
DrawGradientCircle(painter,maxRadius-1);
//绘制内圆
DrawInSideCircle(painter);
//绘制进度小圆
DrawBarIcon(painter);
}
void QRoundProgressBar::GradientArc(QPainter &painter,int radius,QRgb color)
{
// 渐变色
QRadialGradient gradient(0, 0, radius);
gradient.setColorAt(0, color);
gradient.setColorAt(1.0, color);
painter.setBrush(gradient);
QRectF rect(-radius, -radius, radius << 1, radius << 1);
QPainterPath path;
path.arcTo(rect, startAngle, endAngle-startAngle);
painter.setPen(Qt::NoPen);
painter.drawPath(path);
}
void QRoundProgressBar::GradientFullArc(QPainter &painter,QPointF& point,int radius,QRgb color)
{
// 渐变色
QRadialGradient gradient(0, 0, radius);
gradient.setColorAt(0, color);
gradient.setColorAt(1.0, color);
painter.setBrush(gradient);
painter.drawEllipse(point,radius,radius);
}
void QRoundProgressBar::DrawOutSideCircle(QPainter& painter)
{
GradientArc(painter,maxRadius,circularBorderColor);
}
void QRoundProgressBar::DrawCircularRing(QPainter& painter,int)
{
GradientArc(painter,maxRadius-1,startGradientColor);
}
void QRoundProgressBar::DrawTwoSmallCircle(QPainter& painter,int type)
{
//计算小圆坐标
QPoint rightCircle(0,0);
QPoint leftCircle(0,0);
rightCircle.setY(-(qSin(startAngle*M_PI/180)*(maxRadius-ringWidth/2)));
rightCircle.setX(qCos(startAngle*M_PI/180)*(maxRadius-ringWidth/2)+1);
leftCircle.setX(-rightCircle.rx());
leftCircle.setY(rightCircle.ry());
if(type==0){
painter.drawEllipse(rightCircle,ringWidth/2,ringWidth/2);
painter.drawEllipse(leftCircle,ringWidth/2,ringWidth/2);
}
else{
painter.drawEllipse(rightCircle,ringWidth/2-1,ringWidth/2-1);
painter.drawEllipse(leftCircle,ringWidth/2-1,ringWidth/2-1);
}
}
void QRoundProgressBar::DrawInSideCircle(QPainter& painter)
{
QPointF point(0,0);
GradientArc(painter,maxRadius-ringWidth+1,circularBorderColor);
GradientFullArc(painter,point,maxRadius-ringWidth,insideMaskColor);
//GradientFullArc(painter,maxRadius*3/4,qRgb(48,48,48));
}
void QRoundProgressBar::DrawGradientCircle(QPainter& painter,int radius)
{
QConicalGradient conicalGradient(QPointF(0, 0), endAngle+1);
conicalGradient.setColorAt((360-qAbs(endAngle-startAngle)+currAngle)/360.0, endGradientColor);
conicalGradient.setColorAt(1, startGradientColor);
painter.setBrush(conicalGradient);
QPainterPath path;
QRectF rect(-radius, -radius, radius << 1, radius << 1);
path.arcTo(rect, startAngle+currAngle, endAngle-startAngle-currAngle);
painter.setPen(Qt::NoPen);
painter.drawPath(path);
}
void QRoundProgressBar::DrawBarIcon(QPainter& painter)
{
QPointF point(0,0);
point.setX(point.x()+qCos((startAngle+currAngle)*M_PI/180)*(maxRadius-ringWidth/2));
point.setY(point.y()-qSin((startAngle+currAngle)*M_PI/180)*(maxRadius-ringWidth/2));
GradientFullArc(painter,point,iconWidth,endGradientColor);
GradientFullArc(painter,point,iconWidth*0.6,startGradientColor);
}