|
3 | 3 | #include <QBrush> |
4 | 4 | #include <QPen> |
5 | 5 | #include <QPolygonF> |
| 6 | +#include <QPainterPath> |
6 | 7 | #include <QTimer> |
7 | 8 | #include <QShowEvent> |
8 | 9 | #include <QHideEvent> |
9 | 10 | #include <algorithm> |
10 | 11 | #include <cmath> |
11 | 12 |
|
| 13 | +namespace { |
| 14 | +QPainterPath roundedTrace(const QPolygonF &points) { |
| 15 | + QPainterPath path; |
| 16 | + if (points.isEmpty()) |
| 17 | + return path; |
| 18 | + path.moveTo(points.first()); |
| 19 | + if (points.size() < 2) |
| 20 | + return path; |
| 21 | + |
| 22 | + QVector<double> slopes(points.size() - 1); |
| 23 | + for (int i = 0; i < slopes.size(); ++i) { |
| 24 | + const double width = points[i + 1].x() - points[i].x(); |
| 25 | + slopes[i] = width > 0.0 ? (points[i + 1].y() - points[i].y()) / width : 0.0; |
| 26 | + } |
| 27 | + QVector<double> tangents(points.size(), 0.0); |
| 28 | + tangents.first() = slopes.first(); |
| 29 | + tangents.last() = slopes.last(); |
| 30 | + for (int i = 1; i < points.size() - 1; ++i) { |
| 31 | + const double before = slopes[i - 1], after = slopes[i]; |
| 32 | + // Flatten peaks and troughs. Else limit the shared tangent to both |
| 33 | + // adjacent slopes, keeping each curve inside its measured endpoints. |
| 34 | + if ((before > 0.0 && after > 0.0) || (before < 0.0 && after < 0.0)) |
| 35 | + tangents[i] = std::copysign(std::min(std::abs(before), std::abs(after)), before); |
| 36 | + } |
| 37 | + for (int i = 0; i < points.size() - 1; ++i) { |
| 38 | + const QPointF &start = points[i], &end = points[i + 1]; |
| 39 | + const double third = (end.x() - start.x()) / 3.0; |
| 40 | + if (third <= 0.0) { |
| 41 | + // Very narrow charts can put adjacent samples on the same pixel. |
| 42 | + path.lineTo(end); |
| 43 | + continue; |
| 44 | + } |
| 45 | + path.cubicTo(QPointF(start.x() + third, start.y() + tangents[i] * third), |
| 46 | + QPointF(end.x() - third, end.y() - tangents[i + 1] * third), end); |
| 47 | + } |
| 48 | + return path; |
| 49 | +} |
| 50 | +} |
| 51 | + |
12 | 52 | Chart::Chart(const ViewOptions *interfaceOptions, const double *uploadBuffer, const double *downloadBuffer, |
13 | 53 | const double *maxspeed, const int *ptr, int bufferSize) |
14 | 54 | : QWidget(), mInterfaceOptions(interfaceOptions), mUplBuffer(uploadBuffer), |
@@ -130,8 +170,8 @@ void Chart::paintEvent(QPaintEvent *event) { |
130 | 170 | pen.setWidth(1); |
131 | 171 | pen.setCosmetic(true); |
132 | 172 | paint.setPen(pen); |
133 | | - paint.drawPolyline(download); |
| 173 | + paint.drawPath(roundedTrace(download)); |
134 | 174 | pen.setColor(QColor(mInterfaceOptions->mChartUplColor)); |
135 | 175 | paint.setPen(pen); |
136 | | - paint.drawPolyline(upload); |
| 176 | + paint.drawPath(roundedTrace(upload)); |
137 | 177 | } |
0 commit comments