QuakeGod
2023-05-30 d3687ba77720285bac5e7ff51e7e65437e71c00f
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
#include "qmyroundprogress.h"
#include <QPen>
#include <QBrush>
#include <QPainter>
#include <QTimer>
 
QMyRoundProgress::QMyRoundProgress(QWidget *parent) : QWidget(parent)
{
    QTimer* timer = new QTimer(this);
        timer->start(50);
        connect(timer,&QTimer::timeout,this,&QMyRoundProgress::OnTimerd);
}
 
 
void QMyRoundProgress::OnTimerd()
{
    nCount++;
    update();
}
 
void QMyRoundProgress::paintEvent(QPaintEvent *event)
{
    //控件的绘制工具,通过该工具我们可以进行常用的绘制了
    QPainter painter(this);
     painter.setRenderHint(QPainter::Antialiasing);
 
        //取得画布大小和中心方形。
     int w = this->rect().width();
     int h = this->rect().height();
     int s;
     int wmargin=0;
     int hmargin=0;
     if (w>h) {
         s= h;
         wmargin=(w-s)/2;
 
     }else{
         s= w;
         hmargin=(h-s)/2;
     }
     //画边框
     QPen pen0(QColor("#4D9CF8"));
     painter.setPen(pen0);
     painter.drawRoundRect(this->rect(),5,5);
 
      int thick =  s/20;
 
     QPen pen1(QColor(80,80,80));
 
     //绘制弧线的背景
      pen1.setWidth(thick);
      pen1.setCapStyle(Qt::RoundCap);
      //      pen.setColor(QColor("#4D9CF8"));
      painter.setPen(pen1);
 
     QRectF in_rectangle(wmargin+10, hmargin+s/4, s-20, s-20);
     int in_startAngle = (0) * 16;
     int in_spanAngle = (180)*16;
     painter.drawArc(in_rectangle,in_startAngle,in_spanAngle);
 
 
//     in_startAngle = (nOffset + 90+270) * 16;
//     in_spanAngle = (45)*16;
//     painter.drawArc(in_rectangle,in_startAngle,in_spanAngle);
 
     int nOffset = nCount*5 % 180;
 
     position = nOffset *range /180;
 
     QPen pen2(QColor("#4D9CF8"));
     pen2.setWidth(thick);
     pen2.setCapStyle(Qt::RoundCap);
 
     QRectF sec_rectangle(wmargin+10, hmargin+s/4, s-20, s-20);
     int sec_startAngle = 180 * 16;
     int sec_spanAngle = -1*((position / range)*180)*16;
 
     QConicalGradient gradient;
     gradient.setCenter(sec_rectangle.center());
     gradient.setAngle(190);
     gradient.setColorAt(0, color2);
     gradient.setColorAt(0.5, color2);
     gradient.setColorAt(1, color1);
 
 
 //    int arcLengthApproximation = m_width + m_width / 3;
     QPen pen3(QBrush(gradient), thick);
     pen3.setCapStyle(Qt::RoundCap);
     painter.setPen(pen3);
     painter.drawArc(sec_rectangle,sec_startAngle,sec_spanAngle);
 
 
 
     //绘制文本,绘制白色的文字
     painter.setPen(QPen(QColor(255,255,255)));
     QString s1;
     s1=sTitle;
     //显示标题
     QRect rect2(wmargin + 20, hmargin +10, s-40,s/8);
     painter.setFont(QFont("Microsoft YaHei",s/12));
  //   painter.drawText(this->rect(),s1);
    painter.drawText(rect2,Qt::AlignCenter,s1);
     //显示前后刻度
    painter.setFont(QFont("Microsoft YaHei",s/18));
    s1="0";
 //   painter.drawText(this->rect(),s1);
    rect2.setRect(wmargin,hmargin+s*3/4,20,s/16);
   painter.drawText(rect2,Qt::AlignCenter,s1);
 
   s1=QString::number(range);
//   painter.drawText(this->rect(),s1);
   rect2.setRect(wmargin+s-30,hmargin+s*3/4,40,s/16);
  painter.drawText(rect2,Qt::AlignCenter,s1);
 
     //显示当前数量
  painter.setFont(QFont("Microsoft YaHei",s/8));
  s1=QString::number(position);
//   painter.drawText(this->rect(),s1);
  rect2.setRect(wmargin+10,hmargin+10-s/20,s-20,s-20);
 painter.drawText(rect2,Qt::AlignCenter,s1);
     //显示单位
 painter.setFont(QFont("Microsoft YaHei",s/16));
 s1=sUnit;
//   painter.drawText(this->rect(),s1);
 rect2.setRect(wmargin+10,hmargin+10+s/10,s-20,s-20);
painter.drawText(rect2,Qt::AlignCenter,s1);
 
    return;
    //   QRect rect2(wmargin + 20, hmargin +20, s-40,s-40);
 
     //添加绘制的字体和字号
     s1=QString::number(int(position));
     int l = s1.length();
     double fr = sqrt(l*l + 4);
     painter.setFont(QFont("Microsoft YaHei",(s-20)/fr));
  //   painter.drawText(this->rect(),s1);
    painter.drawText(rect2,Qt::AlignCenter,s1);
 
}
 
void QMyRoundProgress::SetRange(int n)
{
    range=n;
}
void QMyRoundProgress::SetPosition(float n)
{
    position=n;
    update();
}
void QMyRoundProgress::SetTitle(QString title)
{
    sTitle = title;
}
void QMyRoundProgress::SetUnit(QString unit)
{
    sUnit = unit;
}
void QMyRoundProgress::SetColor(QColor Start,QColor End)
{
    color1 = Start;
    color2 = End;
}