fix: graph jitter on load + format
This commit is contained in:
@@ -17,8 +17,7 @@ namespace {
|
||||
constexpr qreal kNaN = std::numeric_limits<qreal>::quiet_NaN();
|
||||
|
||||
bool sameReal(qreal a, qreal b) {
|
||||
if (std::isnan(a) || std::isnan(b))
|
||||
return std::isnan(a) && std::isnan(b);
|
||||
if (std::isnan(a) || std::isnan(b)) return std::isnan(a) && std::isnan(b);
|
||||
return qFuzzyCompare(a, b);
|
||||
}
|
||||
|
||||
@@ -34,8 +33,7 @@ qreal parseValue(const QVariant& v) {
|
||||
// Index of the segment whose right endpoint is at or past targetX.
|
||||
int indexAt(const QVector<QPointF>& points, qreal targetX) {
|
||||
for (int i = 0; i < points.size() - 1; ++i) {
|
||||
if (targetX <= points[i + 1].x())
|
||||
return i;
|
||||
if (targetX <= points[i + 1].x()) return i;
|
||||
}
|
||||
const int last = static_cast<int>(points.size()) - 2;
|
||||
return last > 0 ? last : 0;
|
||||
@@ -44,13 +42,9 @@ int indexAt(const QVector<QPointF>& points, qreal targetX) {
|
||||
// The part of the polyline left/right of targetX, ending or beginning exactly at
|
||||
// the curve point there. Used to carve a gap around the marker dot.
|
||||
QVector<QPointF> cutLeft(
|
||||
const QVector<QPointF>& points,
|
||||
qreal targetX,
|
||||
bool curveSmoothing) {
|
||||
if (targetX <= points.first().x())
|
||||
return {};
|
||||
if (targetX >= points.last().x())
|
||||
return points;
|
||||
const QVector<QPointF>& points, qreal targetX, bool curveSmoothing) {
|
||||
if (targetX <= points.first().x()) return {};
|
||||
if (targetX >= points.last().x()) return points;
|
||||
|
||||
const int j = indexAt(points, targetX);
|
||||
QVector<QPointF> left = points.first(j + 1);
|
||||
@@ -60,13 +54,9 @@ QVector<QPointF> cutLeft(
|
||||
}
|
||||
|
||||
QVector<QPointF> cutRight(
|
||||
const QVector<QPointF>& points,
|
||||
qreal targetX,
|
||||
bool curveSmoothing) {
|
||||
if (targetX >= points.last().x())
|
||||
return {};
|
||||
if (targetX <= points.first().x())
|
||||
return points;
|
||||
const QVector<QPointF>& points, qreal targetX, bool curveSmoothing) {
|
||||
if (targetX >= points.last().x()) return {};
|
||||
if (targetX <= points.first().x()) return points;
|
||||
|
||||
const int j = indexAt(points, targetX);
|
||||
QVector<QPointF> right = points.mid(j + 1);
|
||||
@@ -76,27 +66,20 @@ QVector<QPointF> cutRight(
|
||||
}
|
||||
|
||||
QString formatXLabel(LineGraph::XLabelMode mode, int i, bool use12) {
|
||||
if (mode == LineGraph::XLabelMode::None)
|
||||
return {};
|
||||
if (mode == LineGraph::XLabelMode::Number)
|
||||
return QString::number(i);
|
||||
if (mode == LineGraph::XLabelMode::None) return {};
|
||||
if (mode == LineGraph::XLabelMode::Number) return QString::number(i);
|
||||
// Hour: an hour of day 0..24, on a 12- or 24-hour clock.
|
||||
if (use12) {
|
||||
const int hr = (i % 12 == 0) ? 12 : (i % 12);
|
||||
return QString::number(hr)
|
||||
+ (i < 12 ? QStringLiteral(" AM")
|
||||
: QStringLiteral(" PM"));
|
||||
return QString::number(hr) +
|
||||
(i < 12 ? QStringLiteral(" AM") : QStringLiteral(" PM"));
|
||||
}
|
||||
return QString::number(i);
|
||||
}
|
||||
|
||||
QString formatYLabel(
|
||||
LineGraph::YLabelMode mode,
|
||||
qreal v,
|
||||
int decimals,
|
||||
const QString& suffix) {
|
||||
if (mode == LineGraph::YLabelMode::Off)
|
||||
return {};
|
||||
LineGraph::YLabelMode mode, qreal v, int decimals, const QString& suffix) {
|
||||
if (mode == LineGraph::YLabelMode::Off) return {};
|
||||
return QString::number(v, 'f', qMax(0, decimals)) + suffix;
|
||||
}
|
||||
} // namespace
|
||||
@@ -194,7 +177,7 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
|
||||
const qreal padLeft = graph->m_labelFontSize * 5.0;
|
||||
const qreal padBottom = graph->m_labelFontSize + 14.0;
|
||||
model.plot = Plot {padLeft, 0.0, W - padLeft, H - padBottom};
|
||||
model.plot = Plot{padLeft, 0.0, W - padLeft, H - padBottom};
|
||||
if (model.plot.w < 2 || model.plot.h < 2) {
|
||||
m_model = model;
|
||||
return;
|
||||
@@ -222,8 +205,7 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
int n = 0;
|
||||
for (int j = -radius; j <= radius; ++j) {
|
||||
const int u = i + j;
|
||||
if (u < 0 || u >= raw.size())
|
||||
continue;
|
||||
if (u < 0 || u >= raw.size()) continue;
|
||||
if (!std::isnan(raw[u])) {
|
||||
sum += raw[u];
|
||||
++n;
|
||||
@@ -242,8 +224,7 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
qreal dmin = std::numeric_limits<qreal>::infinity();
|
||||
qreal dmax = -std::numeric_limits<qreal>::infinity();
|
||||
for (const qreal v : series) {
|
||||
if (std::isnan(v))
|
||||
continue;
|
||||
if (std::isnan(v)) continue;
|
||||
dmin = std::min(dmin, v);
|
||||
dmax = std::max(dmax, v);
|
||||
}
|
||||
@@ -254,12 +235,12 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
|
||||
const int step = graph->m_horizontalLineStep;
|
||||
const int minSteps = graph->m_minHorizontalLines > 1
|
||||
? graph->m_minHorizontalLines - 1
|
||||
: 0;
|
||||
? graph->m_minHorizontalLines - 1
|
||||
: 0;
|
||||
if (step > 0) {
|
||||
if (hiAuto) {
|
||||
const qreal baseLo =
|
||||
loAuto ? std::floor(dmin / step) * step : lo;
|
||||
const qreal baseLo = loAuto ? std::floor(dmin / step) * step
|
||||
: lo;
|
||||
const int span = std::max(
|
||||
{static_cast<int>(std::ceil((dmax - baseLo) / step)),
|
||||
minSteps,
|
||||
@@ -274,12 +255,9 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
lo = hi - qreal(span) * step;
|
||||
}
|
||||
} else {
|
||||
if (loAuto)
|
||||
lo = dmin;
|
||||
if (hiAuto)
|
||||
hi = dmax;
|
||||
if (hi <= lo)
|
||||
hi = lo + 1.0;
|
||||
if (loAuto) lo = dmin;
|
||||
if (hiAuto) hi = dmax;
|
||||
if (hi <= lo) hi = lo + 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,8 +273,7 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
return model.plot.x + (i / qreal(graph->m_xSteps)) * model.plot.w;
|
||||
};
|
||||
const auto yAt = [&](qreal v) {
|
||||
return model.plot.y
|
||||
+ (1.0 - (v - scaleBottom) / range) * model.plot.h;
|
||||
return model.plot.y + (1.0 - (v - scaleBottom) / range) * model.plot.h;
|
||||
};
|
||||
|
||||
const auto devW = [&](qreal w) {
|
||||
@@ -310,53 +287,48 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
const int vStep = graph->m_verticalLineStep;
|
||||
const int hStep = graph->m_horizontalLineStep;
|
||||
const auto tickVisible = [&](int i) {
|
||||
if (i == 0 && !graph->m_firstXLine)
|
||||
return false;
|
||||
if (i == graph->m_xSteps && !graph->m_lastXLine)
|
||||
return false;
|
||||
if (i == 0 && !graph->m_firstXLine) return false;
|
||||
if (i == graph->m_xSteps && !graph->m_lastXLine) return false;
|
||||
return true;
|
||||
};
|
||||
|
||||
if (vStep > 0) {
|
||||
for (int i = 0; i <= graph->m_xSteps; i += vStep) {
|
||||
if (!tickVisible(i))
|
||||
continue;
|
||||
model.vGrid.append(
|
||||
internal::snapLine(xAt(i), gridWDev, dpr));
|
||||
if (!tickVisible(i)) continue;
|
||||
model.vGrid.append(internal::snapLine(xAt(i), gridWDev, dpr));
|
||||
}
|
||||
}
|
||||
|
||||
const qreal vStart = lo
|
||||
- (graph->m_yPaddingBottom > 0.0
|
||||
? std::floor(graph->m_yPaddingBottom / hStep)
|
||||
* hStep
|
||||
: 0.0);
|
||||
const qreal vStart =
|
||||
lo - (graph->m_yPaddingBottom > 0.0
|
||||
? std::floor(graph->m_yPaddingBottom / hStep) * hStep
|
||||
: 0.0);
|
||||
if (hStep > 0) {
|
||||
for (qreal v = vStart; v <= hi + 0.001; v += hStep)
|
||||
model.hGrid.append(
|
||||
internal::snapLine(yAt(v), gridWDev, dpr));
|
||||
model.hGrid.append(internal::snapLine(yAt(v), gridWDev, dpr));
|
||||
}
|
||||
|
||||
if (vStep > 0 && graph->m_xLabelMode != LineGraph::XLabelMode::None) {
|
||||
for (int i = 0; i <= graph->m_xSteps; i += vStep) {
|
||||
if (!tickVisible(i))
|
||||
continue;
|
||||
if (!tickVisible(i)) continue;
|
||||
const QString text = formatXLabel(
|
||||
graph->m_xLabelMode, i, graph->m_useTwelveHourClock);
|
||||
if (!text.isEmpty())
|
||||
model.xLabels.append(Label {
|
||||
text, xAt(i) + 4.0,
|
||||
model.plot.y + model.plot.h + 8.0});
|
||||
model.xLabels.append(
|
||||
Label{
|
||||
text, xAt(i) + 4.0, model.plot.y + model.plot.h + 8.0});
|
||||
}
|
||||
}
|
||||
|
||||
if (hStep > 0 && graph->m_yLabelMode != LineGraph::YLabelMode::Off) {
|
||||
for (qreal v = vStart; v <= hi + 0.001; v += hStep) {
|
||||
const QString text = formatYLabel(
|
||||
graph->m_yLabelMode, v, graph->m_yLabelDecimals,
|
||||
graph->m_yLabelMode,
|
||||
v,
|
||||
graph->m_yLabelDecimals,
|
||||
graph->m_yLabelSuffix);
|
||||
if (!text.isEmpty())
|
||||
model.yLabels.append(Label {text, 2.0, yAt(v) - 2.0});
|
||||
model.yLabels.append(Label{text, 2.0, yAt(v) - 2.0});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -378,36 +350,33 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
}
|
||||
current.append(QPointF(snapX(xAt(i)), yAt(series[i])));
|
||||
}
|
||||
if (!current.isEmpty())
|
||||
runs.append(current);
|
||||
if (!current.isEmpty()) runs.append(current);
|
||||
|
||||
const bool curve = graph->m_curveSmoothing;
|
||||
const bool hasMarker =
|
||||
graph->m_markerX >= 0.0 && graph->m_markerX <= graph->m_xSteps;
|
||||
const bool hasMarker = graph->m_markerX >= 0.0 &&
|
||||
graph->m_markerX <= graph->m_xSteps;
|
||||
qreal markerPx = 0.0;
|
||||
qreal gapPx = 0.0;
|
||||
if (hasMarker) {
|
||||
markerPx = xAt(graph->m_markerX);
|
||||
qreal slope = 0.0;
|
||||
for (const auto& run : runs) {
|
||||
if (run.size() > 2 && run.first().x() <= markerPx
|
||||
&& markerPx <= run.last().x()
|
||||
&& markerPx - 1.0 >= run.first().x()
|
||||
&& markerPx + 1.0 <= run.last().x()) {
|
||||
slope = (internal::pointAtX(run, markerPx + 1.0, curve).y()
|
||||
- internal::pointAtX(run, markerPx - 1.0, curve)
|
||||
.y())
|
||||
/ 2.0;
|
||||
if (run.size() > 2 && run.first().x() <= markerPx &&
|
||||
markerPx <= run.last().x() &&
|
||||
markerPx - 1.0 >= run.first().x() &&
|
||||
markerPx + 1.0 <= run.last().x()) {
|
||||
slope = (internal::pointAtX(run, markerPx + 1.0, curve).y() -
|
||||
internal::pointAtX(run, markerPx - 1.0, curve).y()) /
|
||||
2.0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
gapPx = (graph->m_markerDotRadius + graph->m_markerGap)
|
||||
/ std::sqrt(1.0 + slope * slope);
|
||||
gapPx = (graph->m_markerDotRadius + graph->m_markerGap) /
|
||||
std::sqrt(1.0 + slope * slope);
|
||||
}
|
||||
|
||||
for (const auto& run : runs) {
|
||||
if (run.size() < 2)
|
||||
continue;
|
||||
if (run.size() < 2) continue;
|
||||
|
||||
Segment seg;
|
||||
seg.fill = internal::curveVertices(run, dpr, curve);
|
||||
@@ -415,16 +384,13 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
seg.fill.append(QPointF(run.first().x(), model.plotBottom));
|
||||
|
||||
if (hasMarker) {
|
||||
const QVector<QPointF> left =
|
||||
cutLeft(run, markerPx - gapPx, curve);
|
||||
const QVector<QPointF> left = cutLeft(run, markerPx - gapPx, curve);
|
||||
const QVector<QPointF> right =
|
||||
cutRight(run, markerPx + gapPx, curve);
|
||||
if (left.size() >= 2)
|
||||
seg.strokes.append(
|
||||
internal::curveVertices(left, dpr, curve));
|
||||
seg.strokes.append(internal::curveVertices(left, dpr, curve));
|
||||
if (right.size() >= 2)
|
||||
seg.strokes.append(
|
||||
internal::curveVertices(right, dpr, curve));
|
||||
seg.strokes.append(internal::curveVertices(right, dpr, curve));
|
||||
} else {
|
||||
seg.strokes.append(internal::curveVertices(run, dpr, curve));
|
||||
}
|
||||
@@ -433,8 +399,7 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
|
||||
if (hasMarker) {
|
||||
model.marker.hasLine = true;
|
||||
model.marker.lineX =
|
||||
internal::snapLine(markerPx, gridWDev, dpr);
|
||||
model.marker.lineX = internal::snapLine(markerPx, gridWDev, dpr);
|
||||
|
||||
// Dashes are drawn as solid segments: QCanvasPainter strokes are always
|
||||
// solid, so a dashed line is a run of short strokes, as LineGraph.qml did.
|
||||
@@ -442,19 +407,15 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
bool on = true;
|
||||
while (dashY < H) {
|
||||
const qreal dashEnd = std::min(dashY + 4.0, H);
|
||||
if (on)
|
||||
model.marker.dash.append(
|
||||
{snapX(dashY), snapX(dashEnd)});
|
||||
if (on) model.marker.dash.append({snapX(dashY), snapX(dashEnd)});
|
||||
dashY = dashEnd;
|
||||
on = !on;
|
||||
}
|
||||
|
||||
const int i0 = static_cast<int>(std::floor(graph->m_markerX));
|
||||
const qreal v0 = (i0 >= 0 && i0 < series.size()) ? series[i0]
|
||||
: kNaN;
|
||||
const qreal v1 = (i0 + 1 >= 0 && i0 + 1 < series.size())
|
||||
? series[i0 + 1]
|
||||
: kNaN;
|
||||
const qreal v0 = (i0 >= 0 && i0 < series.size()) ? series[i0] : kNaN;
|
||||
const qreal v1 =
|
||||
(i0 + 1 >= 0 && i0 + 1 < series.size()) ? series[i0 + 1] : kNaN;
|
||||
qreal y = kNaN;
|
||||
if (!std::isnan(v0) && !std::isnan(v1))
|
||||
y = yAt(v0 + (v1 - v0) * (graph->m_markerX - qreal(i0)));
|
||||
@@ -475,8 +436,7 @@ void LineGraphRenderer::buildModel(const LineGraph* graph) {
|
||||
|
||||
void LineGraphRenderer::paint(QCanvasPainter* painter) {
|
||||
painter->clearRect(0, 0, width(), height());
|
||||
if (!m_model.valid)
|
||||
return;
|
||||
if (!m_model.valid) return;
|
||||
|
||||
const Model& m = m_model;
|
||||
|
||||
@@ -553,8 +513,7 @@ void LineGraphRenderer::paint(QCanvasPainter* painter) {
|
||||
if (m.marker.hasDot) {
|
||||
painter->setFillStyle(m_markerColor);
|
||||
painter->beginPath();
|
||||
painter->circle(
|
||||
m.marker.dot.x(), m.marker.dot.y(), m.marker.dotRadius);
|
||||
painter->circle(m.marker.dot.x(), m.marker.dot.y(), m.marker.dotRadius);
|
||||
painter->fill();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +46,8 @@ class LineGraph : public QCanvasPainterItem {
|
||||
bool firstXLine READ firstXLine WRITE setFirstXLine NOTIFY
|
||||
firstXLineChanged FINAL)
|
||||
Q_PROPERTY(
|
||||
bool lastXLine READ lastXLine WRITE setLastXLine NOTIFY
|
||||
lastXLineChanged FINAL)
|
||||
bool lastXLine READ lastXLine WRITE setLastXLine NOTIFY lastXLineChanged
|
||||
FINAL)
|
||||
Q_PROPERTY(
|
||||
int minHorizontalLines READ minHorizontalLines WRITE
|
||||
setMinHorizontalLines NOTIFY minHorizontalLinesChanged FINAL)
|
||||
|
||||
@@ -16,43 +16,21 @@ using internal::fromDev;
|
||||
using internal::snapLine;
|
||||
using internal::toDev;
|
||||
|
||||
// Device-pixel snapping, bezier sampling and Catmull-Rom controls come from
|
||||
// internal::linegeometry, shared with LineGraph so both graphs align and smooth
|
||||
// their lines identically.
|
||||
|
||||
namespace {
|
||||
// A QQuickPaintedItem only lands on the device grid when its logical size is a
|
||||
// whole number *and* that whole number times dpr is a whole number as well.
|
||||
// Qt derives the painter node from qRound(size) (qquickpainteditem.cpp), makes
|
||||
// the texture from that rounded size times dpr, scales the painter by
|
||||
// texture/size rather than by dpr, and draws the texture into a quad of the
|
||||
// rounded size (qsgdefaultpainternode.cpp). Any fraction therefore moves the
|
||||
// painter scale off dpr - a height of 75.3333 at dpr 1.5 becomes a node of 75
|
||||
// and a texture of qRound(112.5) = 113, a scale of 1.5067, so every y snapped
|
||||
// to the dpr 1.5 grid falls between texture rows and the whole texture is then
|
||||
// resampled to a 112.5 pixel tall quad. That is the fuzz. 74 -> 111 and
|
||||
// 76 -> 114 are both exact.
|
||||
//
|
||||
// `grow` picks the direction: shrink to stay inside the slot, grow to stay
|
||||
// outside it. The overhang on the right has to grow, or the texture's own edge
|
||||
// moves inside the parent's clip and slides left and right with the scroll.
|
||||
qreal snapPaintedExtent(qreal logical, qreal dpr, bool grow) {
|
||||
qreal whole = grow ? std::ceil(logical - 1e-6) : std::floor(logical + 1e-6);
|
||||
for (int guard = 0; guard < 16 && whole >= 1.0; ++guard) {
|
||||
const qreal device = whole * dpr;
|
||||
if (std::abs(device - std::round(device)) < 1e-6)
|
||||
return whole;
|
||||
if (std::abs(device - std::round(device)) < 1e-6) return whole;
|
||||
whole += grow ? 1.0 : -1.0;
|
||||
}
|
||||
// No whole extent within the guard window (an exotic dpr): a whole number is
|
||||
// still closer than a fractional one, which Qt rounds away anyway.
|
||||
return std::max(1.0, grow ? whole : std::floor(logical));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
SparklineContent::SparklineContent(QQuickItem* parent)
|
||||
: QQuickPaintedItem(parent) {
|
||||
m_sampleClock.start();
|
||||
setAntialiasing(true);
|
||||
}
|
||||
|
||||
@@ -75,13 +53,6 @@ qreal SparklineContent::stepX() const {
|
||||
2,
|
||||
(m_line1 && m_line1->count() >= 2) ? m_line1->count()
|
||||
: (m_line2 ? m_line2->count() : 2));
|
||||
// Exactly the slot divided by the history, in logical pixels. Rounding this
|
||||
// to a whole device pixel was tempting - it puts every sample on a pixel
|
||||
// column - but the rounding error accumulates over the whole window: history
|
||||
// - 1 steps then no longer span the slot, so the oldest value either leaves
|
||||
// the clip before its tick (rounding up) or sits at the edge until the buffer
|
||||
// evicts it, popping out at the tick (rounding down). crispness does not need
|
||||
// it: internal::curveVertices puts a vertex on every device column anyway.
|
||||
return m_sliding ? m_viewportW / static_cast<qreal>(m_historyLength - 1)
|
||||
: m_viewportW / static_cast<qreal>(len - 1);
|
||||
}
|
||||
@@ -99,23 +70,23 @@ void SparklineContent::setViewportSize(
|
||||
void SparklineContent::setSlideInterval(qreal intervalMs) {
|
||||
if (qFuzzyCompare(m_slideIntervalMs, intervalMs)) return;
|
||||
m_slideIntervalMs = intervalMs;
|
||||
m_sampleClock.restart();
|
||||
invalidate();
|
||||
}
|
||||
|
||||
qreal SparklineContent::slidePhase(qreal progress) const {
|
||||
if (m_slideIntervalMs <= 0.0) return qBound(0.0, progress, 1.0);
|
||||
// The sample clock, not the animation's timeline. The NumberAnimation runs on
|
||||
// the render-loop clock while the sample arrives from a QTimer of the same
|
||||
// duration, so it reaches 1 on the last frame before that timer fires and then
|
||||
// stops. For that frame the newest value is parked exactly on the parent's
|
||||
// clip edge - it shows up at the clipped edge instead of sliding in from
|
||||
// beyond it, and the following frame travels nothing. Holding is capped at one
|
||||
// step, so a stalled producer cannot scroll the curve into empty space.
|
||||
|
||||
if (!m_sampleClock.isValid()) return 1.0;
|
||||
|
||||
const qreal elapsed = static_cast<qreal>(m_sampleClock.elapsed());
|
||||
return qBound(0.0, elapsed / m_slideIntervalMs, 1.0);
|
||||
}
|
||||
|
||||
bool SparklineContent::slideActive() const {
|
||||
return m_slideIntervalMs > 0.0 && m_sampleClock.isValid() &&
|
||||
static_cast<qreal>(m_sampleClock.elapsed()) < m_slideIntervalMs;
|
||||
}
|
||||
|
||||
void SparklineContent::setSlideProgress(qreal progress) {
|
||||
setX(-stepX() * slidePhase(progress));
|
||||
}
|
||||
@@ -139,11 +110,6 @@ void SparklineContent::setLine2(internal::CircularBuffer* buffer) {
|
||||
void SparklineContent::onSampleArrived() {
|
||||
m_sampleClock.restart();
|
||||
invalidate();
|
||||
// The offset reset and the buffer shifting its values by one step are a
|
||||
// single change: applied here, both are pending before the next sync, so they
|
||||
// cannot land in different frames. Left to the animation to write, a frame
|
||||
// can paint the reindexed values with the previous cycle's full offset still
|
||||
// in place, which moves the whole graph one extra step left for that frame.
|
||||
setSlideProgress(0.0);
|
||||
}
|
||||
|
||||
@@ -223,16 +189,7 @@ void SparklineContent::invalidate() {
|
||||
|
||||
void SparklineContent::applyGeometry() {
|
||||
const qreal dpr = m_dpr > 0.0 ? m_dpr : 1.0;
|
||||
// The newest value is drawn one step past the slot's right edge, so the child
|
||||
// has to overhang the slot and the overhang is offset left as it slides. Two
|
||||
// steps, not one: the newest vertex sits at one step past the edge and its
|
||||
// stroke cap extends beyond that, so a one-step overhang puts the texture's
|
||||
// own right edge exactly on the clip boundary at the end of a cycle, where the
|
||||
// cut-off ink becomes visible for a frame.
|
||||
const qreal pad = m_sliding ? 2.0 * stepX() : 0.0;
|
||||
// Qt rounds the painted size anyway, so choose a whole value that keeps
|
||||
// device alignment. Width grows, to keep that overhang outside the clip;
|
||||
// height shrinks, to stay inside the slot.
|
||||
const qreal paintedW = snapPaintedExtent(m_viewportW + pad, dpr, true);
|
||||
m_paintedH = snapPaintedExtent(m_viewportH, dpr, false);
|
||||
setSize(QSizeF(paintedW, m_paintedH));
|
||||
@@ -241,13 +198,10 @@ void SparklineContent::applyGeometry() {
|
||||
SparklineContent::LineGeometry SparklineContent::buildGeometry(
|
||||
internal::CircularBuffer* buffer) const {
|
||||
LineGeometry geom;
|
||||
// Every input has to be a usable number: a NaN from QML (an animated maxValue
|
||||
// fed by an empty buffer, for instance) would otherwise reach llround and the
|
||||
// path stroker as garbage, which draws a shape that ignores the curve.
|
||||
if (!buffer || buffer->count() < 2 || !std::isfinite(m_maxValue) ||
|
||||
m_maxValue <= 0.0 || !std::isfinite(m_viewportW) ||
|
||||
m_viewportW <= 0.0 || !std::isfinite(m_viewportH) || m_viewportH <= 0.0 ||
|
||||
m_paintedH < 1.0)
|
||||
m_viewportW <= 0.0 || !std::isfinite(m_viewportH) ||
|
||||
m_viewportH <= 0.0 || m_paintedH < 1.0)
|
||||
return geom;
|
||||
if (m_historyLength < 2) return geom;
|
||||
|
||||
@@ -259,8 +213,6 @@ SparklineContent::LineGeometry SparklineContent::buildGeometry(
|
||||
const qint64 lineDev = std::isfinite(m_lineWidth)
|
||||
? (std::max)(qint64(1), toDev(m_lineWidth, dpr))
|
||||
: qint64(1);
|
||||
// The painted height, not the slot's: it is the number of texture rows that
|
||||
// actually exist, so the plot and the fill bottom match them exactly.
|
||||
const qint64 heightDev = toDev(m_paintedH, dpr);
|
||||
|
||||
qreal startX = 0.0;
|
||||
@@ -269,9 +221,6 @@ SparklineContent::LineGeometry SparklineContent::buildGeometry(
|
||||
else
|
||||
startX = 0.0;
|
||||
|
||||
// Plot bounds in device pixels: half a stroke plus the curve overshoot at
|
||||
// the top, a half stroke and one pixel of antialiasing feather at the
|
||||
// bottom, so neither edge of the stroke is cut by the item's own rect.
|
||||
const qint64 curvePadDev = toDev(m_curveSmoothing ? 3.0 : 0.0, dpr);
|
||||
const qint64 plotTopDev = lineDev / 2 + curvePadDev;
|
||||
const qint64 plotBottomDev =
|
||||
@@ -285,26 +234,18 @@ SparklineContent::LineGeometry SparklineContent::buildGeometry(
|
||||
for (int i = 0; i < len; ++i) {
|
||||
const qreal raw = buffer->smoothedAt(i);
|
||||
|
||||
// A non-finite sample poisons the whole path: llround of it is undefined,
|
||||
// and the stroke and fill then ignore everything after that vertex. Treat
|
||||
// it as no traffic instead.
|
||||
const qreal value =
|
||||
std::isfinite(raw) ? qBound<qreal>(0.0, raw, m_maxValue) : 0.0;
|
||||
|
||||
// The y of every sample goes on the grid the stroke width needs; x stays
|
||||
// exact so the window's span is exactly the slot's width.
|
||||
const qreal y = fromDev(
|
||||
plotTopDev + static_cast<qint64>(std::llround(
|
||||
(1.0 - value / m_maxValue) *
|
||||
static_cast<double>(plotHDev))),
|
||||
dpr);
|
||||
points.append(QPointF(startX + qreal(i) * step, snapLine(y, lineDev, dpr)));
|
||||
points.append(
|
||||
QPointF(startX + qreal(i) * step, snapLine(y, lineDev, dpr)));
|
||||
}
|
||||
|
||||
// One vertex per device-pixel column, shared with LineGraphItem through
|
||||
// internal::curveVertices: a segment spanning several columns would otherwise
|
||||
// interpolate its edge across all of them. y stays continuous, so a slope is
|
||||
// interpolated rather than stepped one pixel at a time.
|
||||
const QVector<QPointF> verts =
|
||||
internal::curveVertices(points, dpr, m_curveSmoothing);
|
||||
|
||||
|
||||
@@ -31,20 +31,15 @@ class SparklineContent : public QQuickPaintedItem {
|
||||
|
||||
void setViewportSize(qreal viewportWidth, qreal viewportHeight);
|
||||
|
||||
// How often a new sample arrives, in milliseconds. With this set the slide
|
||||
// phase is read from the sample clock rather than from `progress`, which
|
||||
// makes the wrap exact; without it the phase is the value QML supplies.
|
||||
void setSlideInterval(qreal intervalMs);
|
||||
|
||||
// Offsets the item for the slide. Kept here rather than in SparklineItem
|
||||
// because the quantization needs both stepX() and the window's dpr.
|
||||
void setSlideProgress(qreal progress);
|
||||
|
||||
[[nodiscard]] qreal stepX() const;
|
||||
|
||||
[[nodiscard]] bool slideActive() const;
|
||||
|
||||
protected:
|
||||
// The stroke width, sample spacing and plot bounds are quantized in device
|
||||
// pixels, so they have to be built with the ratio the window actually has.
|
||||
void itemChange(ItemChange change, const ItemChangeData& value) override;
|
||||
|
||||
private:
|
||||
@@ -78,13 +73,9 @@ class SparklineContent : public QQuickPaintedItem {
|
||||
|
||||
qreal m_viewportW = 0.0;
|
||||
qreal m_viewportH = 0.0;
|
||||
// The height actually painted, snapped so the texture maps 1:1 onto device
|
||||
// rows. The plot bounds are built from this, not from the slot's height.
|
||||
qreal m_paintedH = 0.0;
|
||||
qreal m_dpr = 1.0;
|
||||
qreal m_slideIntervalMs = 0.0;
|
||||
// Restarted whenever a sample arrives, so the slide phase follows the sample
|
||||
// cadence instead of the animation's timeline.
|
||||
QElapsedTimer m_sampleClock;
|
||||
|
||||
LineGeometry m_geom1;
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include "sparklinecontent.hpp"
|
||||
|
||||
#include <qquickwindow.h>
|
||||
|
||||
namespace ZShell::components {
|
||||
|
||||
SparklineItem::SparklineItem(QQuickItem* parent) : QQuickItem(parent) {
|
||||
@@ -18,6 +20,29 @@ void SparklineItem::geometryChange(
|
||||
updateContentOffset();
|
||||
}
|
||||
|
||||
void SparklineItem::itemChange(ItemChange change, const ItemChangeData& value) {
|
||||
QQuickItem::itemChange(change, value);
|
||||
if (change != ItemSceneChange) return;
|
||||
|
||||
if (m_syncConnection) QObject::disconnect(m_syncConnection);
|
||||
|
||||
if (value.window) {
|
||||
m_syncConnection = connect(
|
||||
value.window,
|
||||
&QQuickWindow::beforeFrameBegin,
|
||||
this,
|
||||
&SparklineItem::syncSlidePhase);
|
||||
}
|
||||
}
|
||||
|
||||
void SparklineItem::syncSlidePhase() {
|
||||
if (m_slideInterval <= 0.0) return;
|
||||
|
||||
updateContentOffset();
|
||||
|
||||
if (isVisible() && m_content->slideActive() && window()) window()->update();
|
||||
}
|
||||
|
||||
void SparklineItem::updateContentOffset() {
|
||||
m_content->setSlideProgress(m_slideProgress);
|
||||
}
|
||||
@@ -111,9 +136,6 @@ qreal SparklineItem::slideProgress() const {
|
||||
}
|
||||
|
||||
void SparklineItem::setSlideProgress(qreal progress) {
|
||||
// Always forwarded: with a slide interval set the phase comes from the sample
|
||||
// clock, so the offset can move even when the animation value repeats. An
|
||||
// unchanged position is a no-op in QQuickItem.
|
||||
m_slideProgress = progress;
|
||||
updateContentOffset();
|
||||
emit slideProgressChanged();
|
||||
|
||||
@@ -121,11 +121,15 @@ class SparklineItem : public QQuickItem {
|
||||
void geometryChange(
|
||||
const QRectF& newGeometry, const QRectF& oldGeometry) override;
|
||||
|
||||
void itemChange(ItemChange change, const ItemChangeData& value) override;
|
||||
|
||||
private:
|
||||
void updateContentOffset();
|
||||
void syncSlidePhase();
|
||||
void applySmoothingRadius(internal::CircularBuffer* buffer) const;
|
||||
|
||||
SparklineContent* m_content = nullptr;
|
||||
QMetaObject::Connection m_syncConnection;
|
||||
internal::CircularBuffer* m_line1 = nullptr;
|
||||
internal::CircularBuffer* m_line2 = nullptr;
|
||||
QColor m_line1Color;
|
||||
|
||||
@@ -41,8 +41,8 @@ qreal fromDev(qint64 device, qreal dpr) {
|
||||
// points depending on floating point noise.
|
||||
qreal snapCenterDev(qint64 sizeDev, qint64 widthDev) {
|
||||
const double halfParity = (widthDev % 2) == 0 ? 0.0 : 0.5;
|
||||
return std::floor(static_cast<double>(sizeDev) / 2.0 - halfParity)
|
||||
+ halfParity;
|
||||
return std::floor(static_cast<double>(sizeDev) / 2.0 - halfParity) +
|
||||
halfParity;
|
||||
}
|
||||
|
||||
qint64 snapAmplitude(qint64 widthDev, qreal multiplier) {
|
||||
|
||||
Reference in New Issue
Block a user