|
1 | 1 | #include "chart.h" |
2 | 2 | #include <QPainter> |
3 | 3 | #include <QBrush> |
| 4 | +#include <QPen> |
| 5 | +#include <QPolygonF> |
4 | 6 | #include <QTimer> |
5 | 7 | #include <QShowEvent> |
6 | 8 | #include <QHideEvent> |
@@ -36,6 +38,7 @@ void Chart::snapshotHistory() { |
36 | 38 | mFrameTimer->stop(); |
37 | 39 | mAnimationClock.invalidate(); |
38 | 40 | mScrollStart = 0.0; |
| 41 | + mNewestSample = mBufferSize > 0 ? quint64(mBufferSize - 1) : 0; |
39 | 42 | mUpload.clear(); |
40 | 43 | mDownload.clear(); |
41 | 44 | // Copy the circular history in chronological order, oldest to newest. |
@@ -63,6 +66,7 @@ void Chart::sampleUpdated(bool reset) { |
63 | 66 | mScaleStart = displayedMaximumSpeed(); |
64 | 67 | mScaleTarget = *mMaxSpeed; |
65 | 68 | mScrollStart = remaining + 1.0; |
| 69 | + ++mNewestSample; |
66 | 70 | mUpload.append(mUplBuffer[*mPtr]); |
67 | 71 | mDownload.append(mDldBuffer[*mPtr]); |
68 | 72 | const int retained = mBufferSize + int(std::ceil(mScrollStart)); |
@@ -96,26 +100,38 @@ void Chart::paintEvent(QPaintEvent *event) { |
96 | 100 | QBrush brush(QColor(0x33, 0x33, 0x33), Qt::BrushStyle::CrossPattern); |
97 | 101 | paint.fillRect(0, 0, width(), height(), brush); |
98 | 102 |
|
99 | | - const double maximum = displayedMaximumSpeed(); |
| 103 | + const double progress = animationProgress(); |
| 104 | + const double maximum = mScaleStart + (mScaleTarget - mScaleStart) * progress; |
100 | 105 | if (mBufferSize < 2 || mUpload.size() < 2 || width() <= 1 || height() <= 1 || maximum <= 0.0) |
101 | 106 | return; |
102 | 107 |
|
103 | 108 | const double right = width() - 1; |
104 | 109 | const double chartHeight = height() - 1; |
105 | 110 | const double spacing = right / (mBufferSize - 1); |
106 | | - const double remaining = mScrollStart * (1.0 - animationProgress()); |
107 | | - paint.setClipRect(rect()); |
108 | | - paint.setRenderHint(QPainter::Antialiasing); |
109 | | - for (int i = 1; i < mUpload.size(); ++i) { |
110 | | - const double x = right + spacing * (remaining - (mUpload.size() - 1 - i)); |
111 | | - const double previousX = x - spacing; |
112 | | - if (x < 0.0 || previousX > right) |
113 | | - continue; |
114 | | - paint.setPen(QColor(mInterfaceOptions->mChartDldColor)); |
115 | | - paint.drawLine(QPointF(previousX, chartHeight * (1.0 - mDownload[i - 1] / maximum)), |
116 | | - QPointF(x, chartHeight * (1.0 - mDownload[i] / maximum))); |
117 | | - paint.setPen(QColor(mInterfaceOptions->mChartUplColor)); |
118 | | - paint.drawLine(QPointF(previousX, chartHeight * (1.0 - mUpload[i - 1] / maximum)), |
119 | | - QPointF(x, chartHeight * (1.0 - mUpload[i] / maximum))); |
| 111 | + const double remaining = mScrollStart * (1.0 - progress); |
| 112 | + const double ratio = devicePixelRatioF(); |
| 113 | + // Give each sample a stable pixel position. Scroll the whole trace by an |
| 114 | + // integer number of device pixels, instead of changing every segment's |
| 115 | + // subpixel coverage on every frame. This also works with fractional DPI. |
| 116 | + const double origin = std::round((double(mNewestSample) - remaining) * spacing * ratio); |
| 117 | + QPolygonF upload, download; |
| 118 | + upload.reserve(mUpload.size()); |
| 119 | + download.reserve(mDownload.size()); |
| 120 | + for (int i = 0; i < mUpload.size(); ++i) { |
| 121 | + const double sample = double(mNewestSample) - (mUpload.size() - 1 - i); |
| 122 | + const double x = std::round(right * ratio) + std::round(sample * spacing * ratio) - origin; |
| 123 | + upload.append(QPointF(x, std::round(chartHeight * (1.0 - mUpload[i] / maximum) * ratio))); |
| 124 | + download.append(QPointF(x, std::round(chartHeight * (1.0 - mDownload[i] / maximum) * ratio))); |
120 | 125 | } |
| 126 | + paint.setClipRect(rect()); |
| 127 | + paint.setRenderHint(QPainter::Antialiasing, false); |
| 128 | + paint.scale(1.0 / ratio, 1.0 / ratio); |
| 129 | + QPen pen(QColor(mInterfaceOptions->mChartDldColor)); |
| 130 | + pen.setWidth(1); |
| 131 | + pen.setCosmetic(true); |
| 132 | + paint.setPen(pen); |
| 133 | + paint.drawPolyline(download); |
| 134 | + pen.setColor(QColor(mInterfaceOptions->mChartUplColor)); |
| 135 | + paint.setPen(pen); |
| 136 | + paint.drawPolyline(upload); |
121 | 137 | } |
0 commit comments