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
|
#include "arcgauge.hpp"
#include <QtMath>
#include <qpainter.h>
#include <qpen.h>
namespace caelestia::internal {
ArcGauge::ArcGauge(QQuickItem* parent)
: QQuickPaintedItem(parent) {
setAntialiasing(true);
}
void ArcGauge::paint(QPainter* painter) {
const qreal w = width();
const qreal h = height();
const qreal side = qMin(w, h);
const qreal radius = (side - m_lineWidth - 2.0) / 2.0;
const qreal cx = w / 2.0;
const qreal cy = h / 2.0;
const QRectF arcRect(cx - radius, cy - radius, radius * 2.0, radius * 2.0);
// Convert from Canvas convention (CW radians from 3 o'clock) to QPainter (CCW 1/16th degrees)
const int startAngle16 = qRound(-(m_startAngle * 180.0 / M_PI) * 16.0);
const int sweepAngle16 = qRound(-(m_sweepAngle * 180.0 / M_PI) * 16.0);
painter->setRenderHint(QPainter::Antialiasing, true);
// Draw track arc
QPen trackPen(m_trackColor, m_lineWidth);
trackPen.setCapStyle(Qt::RoundCap);
painter->setPen(trackPen);
painter->setBrush(Qt::NoBrush);
painter->drawArc(arcRect, startAngle16, sweepAngle16);
// Draw value arc
if (m_percentage > 0.0) {
const int valueSweep16 = qRound(static_cast<qreal>(sweepAngle16) * m_percentage);
QPen valuePen(m_accentColor, m_lineWidth);
valuePen.setCapStyle(Qt::RoundCap);
painter->setPen(valuePen);
painter->drawArc(arcRect, startAngle16, valueSweep16);
}
}
qreal ArcGauge::percentage() const {
return m_percentage;
}
void ArcGauge::setPercentage(qreal percentage) {
if (qFuzzyCompare(m_percentage, percentage))
return;
m_percentage = percentage;
emit percentageChanged();
update();
}
QColor ArcGauge::accentColor() const {
return m_accentColor;
}
void ArcGauge::setAccentColor(const QColor& color) {
if (m_accentColor == color)
return;
m_accentColor = color;
emit accentColorChanged();
update();
}
QColor ArcGauge::trackColor() const {
return m_trackColor;
}
void ArcGauge::setTrackColor(const QColor& color) {
if (m_trackColor == color)
return;
m_trackColor = color;
emit trackColorChanged();
update();
}
qreal ArcGauge::startAngle() const {
return m_startAngle;
}
void ArcGauge::setStartAngle(qreal angle) {
if (qFuzzyCompare(m_startAngle, angle))
return;
m_startAngle = angle;
emit startAngleChanged();
update();
}
qreal ArcGauge::sweepAngle() const {
return m_sweepAngle;
}
void ArcGauge::setSweepAngle(qreal angle) {
if (qFuzzyCompare(m_sweepAngle, angle))
return;
m_sweepAngle = angle;
emit sweepAngleChanged();
update();
}
qreal ArcGauge::lineWidth() const {
return m_lineWidth;
}
void ArcGauge::setLineWidth(qreal width) {
if (qFuzzyCompare(m_lineWidth, width))
return;
m_lineWidth = width;
emit lineWidthChanged();
update();
}
} // namespace caelestia::internal
|