-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathnavigationprogressbar.cpp
208 lines (175 loc) · 5.15 KB
/
navigationprogressbar.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
204
205
206
207
208
#include "navigationprogressbar.h"
#include <QDateTime>
#include <QDebug>
#include <QPainter>
class NavigationProgressBarPrivate
{
public:
explicit NavigationProgressBarPrivate(QWidget *parent)
: owner(parent)
{
for (int i = 0; i < maxStep; i++)
topInfo << QString("Step%1").arg(i + 1);
}
QWidget *owner;
QColor backgroundColor = QColor(80, 80, 80);
QColor foregroundColor = QColor(254, 254, 254);
QColor currentBackgroundColor = QColor(QLatin1String("#4da1ff"));
int maxStep = 5;
int step = 0;
QStringList topInfo;
QStringList dateList;
};
NavigationProgressBar::NavigationProgressBar(QWidget *parent)
: QWidget(parent)
, d(new NavigationProgressBarPrivate(this))
{}
NavigationProgressBar::~NavigationProgressBar() = default;
auto NavigationProgressBar::sizeHint() const -> QSize
{
return {500, 100};
}
auto NavigationProgressBar::minimumSizeHint() const -> QSize
{
return {300, 75};
}
void NavigationProgressBar::setMessageList(const QStringList &list)
{
d->topInfo = list;
d->maxStep = list.size();
update();
}
auto NavigationProgressBar::messageList() const -> QStringList
{
return d->topInfo;
}
void NavigationProgressBar::setStep(const int step)
{
if (d->step >= step || step > d->maxStep) {
return;
}
int s = step - d->step;
for (int i = 0; i < s; i++)
d->dateList.append(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
d->step = step;
Q_ASSERT(d->step == d->dateList.size());
update();
}
auto NavigationProgressBar::step() const -> int
{
return d->step;
}
void NavigationProgressBar::setBackgroundColor(const QColor &color)
{
d->backgroundColor = color;
update();
}
auto NavigationProgressBar::backgroundColor() const -> QColor
{
return d->backgroundColor;
}
void NavigationProgressBar::setCurrentBackgroundColor(const QColor &color)
{
d->currentBackgroundColor = color;
update();
}
auto NavigationProgressBar::currentBackgroundColor() const -> QColor
{
return d->currentBackgroundColor;
}
void NavigationProgressBar::setForegroundColor(const QColor &color)
{
d->foregroundColor = color;
update();
}
auto NavigationProgressBar::foregroundColor() const -> QColor
{
return d->foregroundColor;
}
void NavigationProgressBar::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
drawBackground(&painter, false);
drawText(&painter, false);
drawBackground(&painter, true);
drawText(&painter, true);
}
void NavigationProgressBar::drawBackground(QPainter *painter, const bool ok)
{
//圆半径为高度一定比例,计算宽度,将宽度等分
double w = width() / d->maxStep * 1.0;
double h = height() / 3 * 1.0;
double radius = qMin(w, h) / 2;
double initX = 0;
double initY = height() / 2.0;
//逐个绘制连接线条
initX = w / 2;
int step = d->maxStep;
int penWidth = radius / 4;
QColor backgroundColor = d->backgroundColor;
if (ok) {
step = d->step;
penWidth = radius / 8;
backgroundColor = d->currentBackgroundColor;
radius = radius / 7 * 6;
}
QPen pen(backgroundColor);
pen.setWidthF(penWidth);
pen.setCapStyle(Qt::RoundCap);
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
for (int i = 0; i < step - 1; i++) {
painter->drawLine(QPoint(initX, initY), QPoint(initX + w, initY));
initX += w;
}
if (ok && (d->step > 0) && (d->step < d->maxStep)) {
painter->drawLine(QPoint(initX, initY), QPoint(initX + w / 2, initY));
}
//逐个绘制圆
initX = w / 2;
painter->setPen(Qt::NoPen);
painter->setBrush(backgroundColor);
for (int i = 0; i < step; i++) {
painter->drawEllipse(QPointF(initX, initY), radius, radius);
initX += w;
}
//逐个绘制圆中的数字
initX = w / 2;
QFont font("Microsoft YaHei", radius);
painter->setFont(font);
painter->setPen(d->foregroundColor);
painter->setBrush(Qt::NoBrush);
for (int i = 0; i < step; i++) {
QRect textRect(initX - radius, initY - radius, radius * 2, radius * 2);
painter->drawText(textRect, Qt::AlignCenter, QString::number(i + 1));
initX += w;
}
}
void NavigationProgressBar::drawText(QPainter *painter, const bool ok)
{
double w = width() / d->maxStep * 1.0;
double h = height() / 3.0;
double initX = 0;
double initY = 0;
QColor color = ok ? d->currentBackgroundColor : d->backgroundColor;
painter->setFont(QFont("Microsoft YaHei", h / 4));
painter->setPen(color);
painter->setBrush(Qt::NoBrush);
int step = ok ? d->step : d->maxStep;
for (int i = 0; i < step; i++) {
QRect textRect(initX, initY, w, h);
painter->drawText(textRect, Qt::AlignCenter, d->topInfo.at(i));
initX += w;
}
if (ok) {
initX = 0;
initY = h * 2;
for (int i = 0; i < step; i++) {
QRect textRect(initX, initY, w, h);
painter->drawText(textRect, Qt::AlignCenter, d->dateList.at(i));
initX += w;
}
}
}