* @brief Draws the Y axis (time or Campbell-mode dataset value). */
| 986 | * @brief Draws the Y axis (time or Campbell-mode dataset value). |
| 987 | */ |
| 988 | void Widgets::Waterfall::drawYAxis(QPainter* painter, const QRectF& plotRect) const |
| 989 | { |
| 990 | double dataMin = 0.0; |
| 991 | double dataMax = 0.0; |
| 992 | if (m_campbellMode) { |
| 993 | dataMin = m_yMin; |
| 994 | dataMax = m_yMax; |
| 995 | } else { |
| 996 | static auto& timer = Misc::TimerEvents::instance(); |
| 997 | const double fps = timer.fps() > 0 ? timer.fps() : 24.0; |
| 998 | dataMin = 0.0; |
| 999 | dataMax = m_historySize / fps; |
| 1000 | } |
| 1001 | const double dataRange = dataMax - dataMin; |
| 1002 | if (dataRange <= 0.0) |
| 1003 | return; |
| 1004 | |
| 1005 | const double srcH = dataRange / m_yZoom; |
| 1006 | const double maxPan = qMax(0.0, (dataRange - srcH) * 0.5); |
| 1007 | const double centerD = (dataMin + dataMax) * 0.5 + qBound(-maxPan, m_yPan * dataRange, maxPan); |
| 1008 | const double yMin = centerD - srcH * 0.5; |
| 1009 | const double yMax = centerD + srcH * 0.5; |
| 1010 | |
| 1011 | const AxisTicks ticks = computeTimeTicks(srcH, kAxisTickCount); |
| 1012 | |
| 1013 | const QFontMetrics fm(painter->font()); |
| 1014 | const double tickRightX = plotRect.left(); |
| 1015 | const double tickLeftX = plotRect.left() - kAxisTickPx; |
| 1016 | const double labelRight = tickLeftX - kAxisLabelPad; |
| 1017 | |
| 1018 | const double step = ticks.step; |
| 1019 | const double invStep = 1.0 / step; |
| 1020 | const double first = std::ceil(yMin * invStep - 1e-9) * step; |
| 1021 | const double yRange = yMax - yMin; |
| 1022 | const double invYRange = yRange > 0.0 ? 1.0 / yRange : 0.0; |
| 1023 | |
| 1024 | for (double v = first; v <= yMax + 1e-6; v += step) { |
| 1025 | const double t = (v - yMin) * invYRange; |
| 1026 | if (t < 0.0 || t > 1.0) |
| 1027 | continue; |
| 1028 | |
| 1029 | const double y = m_campbellMode ? plotRect.bottom() - t * plotRect.height() |
| 1030 | : plotRect.top() + t * plotRect.height(); |
| 1031 | |
| 1032 | painter->setPen(QPen(m_gridColor, 1, Qt::DotLine)); |
| 1033 | painter->drawLine(QPointF(plotRect.left(), y), QPointF(plotRect.right(), y)); |
| 1034 | |
| 1035 | painter->setPen(QPen(m_borderColor, 1)); |
| 1036 | painter->drawLine(QPointF(tickLeftX, y), QPointF(tickRightX, y)); |
| 1037 | |
| 1038 | const QString label = formatTimeTick(v, step); |
| 1039 | const int labelWidth = fm.horizontalAdvance(label); |
| 1040 | const double textCy = qBound(plotRect.top() + fm.ascent() * 0.5, |
| 1041 | y + fm.ascent() * 0.5, |
| 1042 | plotRect.bottom() + fm.ascent() * 0.5); |
| 1043 | painter->setPen(m_textColor); |
| 1044 | painter->drawText(QPointF(labelRight - labelWidth, textCy), label); |
| 1045 | } |