-
Notifications
You must be signed in to change notification settings - Fork 9
/
myprogressbar1.cpp
123 lines (96 loc) · 3.36 KB
/
myprogressbar1.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
#include "myprogressbar1.h"
myProgressBar1::myProgressBar1(QWidget *parent) :
QWidget(parent)
{
this->initVariables();
}
void myProgressBar1::initVariables()
{
m_min = 0;
m_max = 100;
m_value = 0;
}
void myProgressBar1::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing|QPainter::HighQualityAntialiasing);
drawBg(&painter);
drawBar(&painter);
}
void myProgressBar1::drawBg(QPainter *painter)
{
painter->save();
painter->setPen(PYP_BG_COLOR);
QLinearGradient BgGradient(QPoint(0,0),QPoint(width(),0));
BgGradient.setColorAt(0.0,QColor(222,222,222));
BgGradient.setColorAt(0.05,PYP_BG_COLOR);
BgGradient.setColorAt(1.0,PYP_BG_COLOR);
painter->setBrush(BgGradient);
painter->drawRoundedRect(rect(),PYP_RECT_RADIUS,PYP_RECT_RADIUS);
painter->restore();
}
void myProgressBar1::drawBar(QPainter *painter)
{
painter->save();
// DRAW UNDER BAR
painter->setPen(Qt::NoPen);
QPointF uTopLeft(PYP_LEFT_SPACE1,PYP_TOP_SPACE1);
QPointF uBottomRight(width() - PYP_RIGHT_SPACE1 * 2 - PYP_TEXT_WIDTH,height() - PYP_TOP_SPACE1);
QRectF uRect(uTopLeft,uBottomRight);
painter->setBrush(PYP_UNDER_BAR_COLOR);
painter->drawRoundedRect(uRect,PYP_RECT_RADIUS,PYP_RECT_RADIUS);
// DRAW REAL BAR
qreal range = (m_max - m_min);
qreal RectWidth = width() - PYP_LEFT_SPACE1 - PYP_RIGHT_SPACE1 * 2 - PYP_TEXT_WIDTH;
qreal dX = (qreal) RectWidth/range;
qreal initX = PYP_LEFT_SPACE1;
QPointF TopLeft(initX,PYP_TOP_SPACE1);
QPointF BottomRight(initX + dX * m_value,height() - PYP_TOP_SPACE1);
QRectF BarRect(TopLeft,BottomRight);
QLinearGradient BarGradient(uRect.topLeft(),uRect.topRight());
BarGradient.setColorAt(0.0,PYP_BAR_START_COLOR);
BarGradient.setColorAt(0.5,PYP_BAR_MIDDLE_COLOR);
BarGradient.setColorAt(1.0,PYP_BAR_END_COLOR);
painter->setBrush(BarGradient);
painter->drawRoundedRect(BarRect,PYP_RECT_RADIUS,PYP_RECT_RADIUS);
// DRAW COVER LINES
int nLineCnt = uRect.width()/PYP_D_LINE;
int dLineX = (qreal)uRect.width()/(nLineCnt - 1);
painter->setPen(PYP_LINE_COLOR);
while(initX < uRect.width())
{
QPointF topPot(initX + dLineX,PYP_TOP_SPACE1);
QPointF bottomPot(initX + dLineX,height() - PYP_TOP_SPACE1);
painter->drawLine(topPot,bottomPot);
/// increment initX
initX += dLineX;
}
// draw text
QPointF TextTopLeft(width() - PYP_RIGHT_SPACE1 - PYP_TEXT_WIDTH,PYP_TOP_SPACE1);
QPointF TextBottomRight(width() - PYP_RIGHT_SPACE1,height() - PYP_TOP_SPACE1);
QRectF TextRect(TextTopLeft,TextBottomRight);
painter->setPen(PYP_UNDER_BAR_COLOR);
painter->setBrush(PYP_UNDER_BAR_COLOR);
painter->drawRoundedRect(TextRect,PYP_RECT_RADIUS,PYP_RECT_RADIUS);
painter->setPen(PYP_TEXT_COLOR);
QFont TextFont("Times New Roman");
TextFont.setBold(true);
painter->setFont(TextFont);
QString strText;
qreal value = (qreal)m_value * 100/(m_max - m_min);
strText = tr("%1%").arg(value,3,'f',1,'0');
painter->drawText(TextRect,strText,Qt::AlignVCenter|Qt::AlignHCenter);
painter->restore();
}
void myProgressBar1::setRange(qreal min, qreal max)
{
Q_ASSERT(min < max);
m_min = min;
m_max = max;
update();
}
void myProgressBar1::setValue(qreal value)
{
m_value = value;
update();
}