-
Notifications
You must be signed in to change notification settings - Fork 6
/
radialbar.cpp
229 lines (206 loc) · 5.21 KB
/
radialbar.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <QPainter>
#include "radialbar.h"
RadialBar::RadialBar(QQuickItem *parent)
: QQuickPaintedItem(parent),
m_Size(200),
m_StartAngle(40),
m_SpanAngle(280),
m_MinValue(0),
m_MaxValue(100),
m_Value(50),
m_DialWidth(15),
m_BackgroundColor(Qt::transparent),
m_DialColor(QColor(80,80,80)),
m_ProgressColor(QColor(135,26,5)),
m_TextColor(QColor(0, 0, 0)),
m_SuffixText(""),
m_ShowText(true),
m_PenStyle(Qt::FlatCap),
m_DialType(DialType::MinToMax)
{
setWidth(200);
setHeight(200);
setSmooth(true);
setAntialiasing(true);
}
void RadialBar::paint(QPainter *painter)
{
double startAngle;
double spanAngle;
qreal size = qMin(this->width(), this->height());
setWidth(size);
setHeight(size);
QRectF rect = this->boundingRect();
painter->setRenderHint(QPainter::Antialiasing);
QPen pen = painter->pen();
pen.setCapStyle(m_PenStyle);
startAngle = -90 - m_StartAngle;
if(FullDial != m_DialType)
{
spanAngle = 0 - m_SpanAngle;
}
else
{
spanAngle = -360;
}
//Draw outer dial
painter->save();
pen.setWidth(m_DialWidth);
pen.setColor(m_DialColor);
painter->setPen(pen);
qreal offset = m_DialWidth / 2;
if(m_DialType == MinToMax)
{
painter->drawArc(rect.adjusted(offset, offset, -offset, -offset), startAngle * 16, spanAngle * 16);
}
else if(m_DialType == FullDial)
{
painter->drawArc(rect.adjusted(offset, offset, -offset, -offset), -90 * 16, -360 * 16);
}
else
{
//do not draw dial
}
painter->restore();
//Draw background
painter->save();
painter->setBrush(m_BackgroundColor);
painter->setPen(m_BackgroundColor);
qreal inner = offset * 2;
painter->drawEllipse(rect.adjusted(inner, inner, -inner, -inner));
painter->restore();
//Draw progress text with suffix
painter->save();
painter->setFont(m_TextFont);
pen.setColor(m_TextColor);
painter->setPen(pen);
if(m_ShowText)
{
painter->drawText(rect.adjusted(offset, offset, -offset, -offset), Qt::AlignCenter,QString::number(m_Value) + m_SuffixText);
}
else
{
painter->drawText(rect.adjusted(offset, offset, -offset, -offset), Qt::AlignCenter, m_SuffixText);
}
painter->restore();
//Draw progress bar
painter->save();
pen.setWidth(m_DialWidth);
pen.setColor(m_ProgressColor);
qreal valueAngle = ((m_Value - m_MinValue)/(m_MaxValue - m_MinValue)) * spanAngle; //Map value to angle range
painter->setPen(pen);
painter->drawArc(rect.adjusted(offset, offset, -offset, -offset), startAngle * 16, valueAngle * 16);
painter->restore();
}
void RadialBar::setSize(qreal size)
{
if(m_Size == size)
return;
m_Size = size;
emit sizeChanged();
}
void RadialBar::setStartAngle(qreal angle)
{
if(m_StartAngle == angle)
return;
m_StartAngle = angle;
emit startAngleChanged();
}
void RadialBar::setSpanAngle(qreal angle)
{
if(m_SpanAngle == angle)
return;
m_SpanAngle = angle;
emit spanAngleChanged();
}
void RadialBar::setMinValue(qreal value)
{
if(m_MinValue == value)
return;
m_MinValue = value;
emit minValueChanged();
}
void RadialBar::setMaxValue(qreal value)
{
if(m_MaxValue == value)
return;
m_MaxValue = value;
emit maxValueChanged();
}
void RadialBar::setValue(qreal value)
{
if(m_Value == value)
return;
m_Value = value;
update(); //update the radialbar
emit valueChanged();
}
void RadialBar::setDialWidth(qreal width)
{
if(m_DialWidth == width)
return;
m_DialWidth = width;
emit dialWidthChanged();
}
void RadialBar::setBackgroundColor(QColor color)
{
if(m_BackgroundColor == color)
return;
m_BackgroundColor = color;
emit backgroundColorChanged();
}
void RadialBar::setForegroundColor(QColor color)
{
if(m_DialColor == color)
return;
m_DialColor = color;
emit foregroundColorChanged();
}
void RadialBar::setProgressColor(QColor color)
{
if(m_ProgressColor == color)
return;
m_ProgressColor = color;
emit progressColorChanged();
}
void RadialBar::setTextColor(QColor color)
{
if(m_TextColor == color)
return;
m_TextColor = color;
emit textColorChanged();
}
void RadialBar::setSuffixText(QString text)
{
if(m_SuffixText == text)
return;
m_SuffixText = text;
emit suffixTextChanged();
}
void RadialBar::setShowText(bool show)
{
if(m_ShowText == show)
return;
m_ShowText = show;
}
void RadialBar::setPenStyle(Qt::PenCapStyle style)
{
if(m_PenStyle == style)
return;
m_PenStyle = style;
emit penStyleChanged();
}
void RadialBar::setDialType(RadialBar::DialType type)
{
if(m_DialType == type)
return;
m_DialType = type;
emit dialTypeChanged();
}
void RadialBar::setTextFont(QFont font)
{
if(m_TextFont == font)
return;
m_TextFont = font;
emit textFontChanged();
}