-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmdonutplot.cpp
More file actions
96 lines (80 loc) · 2.25 KB
/
smdonutplot.cpp
File metadata and controls
96 lines (80 loc) · 2.25 KB
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
#include "smdonutplot.h"
#include <QPainter>
#include <math.h>
SMDonutPlot::SMDonutPlot(QWidget *parent) : QWidget(parent)
{
_ringPercent = QVector<qreal>({0.0});
_holePercent = 0.0;
}
void SMDonutPlot::paintEvent(QPaintEvent *)
{
QPainter painter(this);
qreal orad = std::min(this->width(), this->height()) / 2.0;
qreal irad = orad * _holePercent;
// construct painter path
QPointF center = QPointF(this->width()/2.0, this->height()/2.0);
QRectF obb = QRectF(center - QPointF(orad, orad), QSizeF(2*orad, 2*orad));
QRectF ibb = QRectF(center - QPointF(irad, irad), QSizeF(2*irad, 2*irad));
QPointF ostart = center - QPointF(0,orad);
for(int i = 0; i < _ringPercent.size(); i++) {
qreal rp = _ringPercent[i];
QPointF iend = center + QPointF(irad*cos(M_PI/2 - rp*2*M_PI),
-irad*sin(M_PI/2 - rp*2*M_PI));
qreal ringDegrees = 360*rp;
QPainterPath path;
path.moveTo(ostart);
// TODO make start orientation configurable
path.arcTo(obb, 90, -ringDegrees);
path.lineTo(iend);
path.arcTo(ibb, 90 - ringDegrees, ringDegrees);
path.closeSubpath();
painter.fillPath(path, _ringColor[i]); // TODO make this configurable
}
}
qreal SMDonutPlot::holePercent() const
{
return _holePercent;
}
void SMDonutPlot::setHolePercent(const qreal &holePercent)
{
if(holePercent < 0.0) {
_holePercent = 0.0;
} else if(holePercent > 1.0) {
_holePercent = 1.0;
} else {
_holePercent = holePercent;
}
this->update();
}
int SMDonutPlot::ringCount() const
{
return _ringPercent.length();
}
void SMDonutPlot::setRingCount(int i)
{
_ringPercent.resize(i);
_ringColor.resize(i);
}
QColor SMDonutPlot::ringColor(int i) const
{
return _ringColor[i];
}
void SMDonutPlot::setRingColor(int i, const QColor &c)
{
_ringColor[i] = c;
}
qreal SMDonutPlot::ringPercent(int i) const
{
return _ringPercent[i];
}
void SMDonutPlot::setRingPercent(int i, const qreal &ringPercent)
{
if(ringPercent < 0.0) {
_ringPercent[i] = 0.0;
} else if(ringPercent > 1.0) {
_ringPercent[i] = 1.0;
} else {
_ringPercent[i] = ringPercent;
}
this->update();
}