From a7a7bd182e18cc30227839bd0e2a1a859d743a2c Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 3 Oct 2021 21:15:36 +0200 Subject: [PATCH] profile: special case time axis Rounding the axes dimensions to "nice" number may have been a good idea, but for the time-axis it feels weird. Therefore revert the time axis to the previous behavior: range is set according to the data. To differentiate between time an other axes, use the position: the time axis is the only axis at the bottom. Yes, that's ugly but pragmatic. Since we have that flag also use it for the special casing of the text-display. Spares us one virtual function dispatch. Signed-off-by: Berthold Stoeger --- profile-widget/divecartesianaxis.cpp | 48 +++++++++++++++++----------- profile-widget/divecartesianaxis.h | 3 +- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/profile-widget/divecartesianaxis.cpp b/profile-widget/divecartesianaxis.cpp index 7d290fb19..4b9fc8623 100644 --- a/profile-widget/divecartesianaxis.cpp +++ b/profile-widget/divecartesianaxis.cpp @@ -181,24 +181,34 @@ void DiveCartesianAxis::updateTicks(int animSpeed) // Choose full multiples of the interval as minumum and maximum values double minDisplay = transform.to(dataMin); double maxDisplay = transform.to(dataMax); - double firstDisplay = floor(minDisplay / intervalDisplay * (1.0 + 1e-5)) * intervalDisplay; - double lastDisplay = ceil(maxDisplay / intervalDisplay * (1.0 - 1e-5)) * intervalDisplay; + + // The time axis is special: use the full width in that case. + // Other axes round to the next "nice" number + double firstDisplay, lastDisplay; + double currValueText, currValueLine; + if (position == Position::Bottom) { + firstDisplay = ceil(minDisplay / intervalDisplay * (1.0 - 1e-5)) * intervalDisplay; + lastDisplay = floor(maxDisplay / intervalDisplay * (1.0 + 1e-5)) * intervalDisplay; + currValueText = currValueLine = transform.from(firstDisplay); + } else { + firstDisplay = floor(minDisplay / intervalDisplay * (1.0 + 1e-5)) * intervalDisplay; + lastDisplay = ceil(maxDisplay / intervalDisplay * (1.0 - 1e-5)) * intervalDisplay; + min = transform.from(firstDisplay); + max = transform.from(lastDisplay); + currValueText = currValueLine = min; + } numTicks = lrint((lastDisplay - firstDisplay) / intervalDisplay) + 1; numTicks = std::max(numTicks, 0); - min = transform.from(firstDisplay); - max = transform.from(lastDisplay); - - double currValueText = min; - double currValueLine = min; - emptyList(labels, numTicks, animSpeed); emptyList(lines, numTicks, animSpeed); if (numTicks == 0) return; - interval = numTicks > 1 ? (max - min) / (numTicks - 1) : 0; - double stepSize = numTicks > 1 ? size / (numTicks - 1) : 0; + interval = position == Position::Bottom ? + intervalDisplay / transform.a : // special case for time axis. + numTicks > 1 ? (max - min) / (numTicks - 1) : 0; + double stepSize = interval * size / (max - min); // Move the remaining grid lines / labels to their correct positions // regarding the possible new values for the axis @@ -343,7 +353,15 @@ double DiveCartesianAxis::Transform::from(double y) const QString DiveCartesianAxis::textForValue(double value) const { - return QStringLiteral("%L1").arg(transform.to(value), 0, 'f', fractionalDigits); + if (position == Position::Bottom) { + // The bottom axis is the time axis and that needs special treatment. + int nr = lrint(value) / 60; + if (maximum() - minimum() < 600.0) + return QString("%1:%2").arg(nr).arg((int)value % 60, 2, 10, QChar('0')); + return QString::number(nr); + } else { + return QStringLiteral("%L1").arg(transform.to(value), 0, 'f', fractionalDigits); + } } qreal DiveCartesianAxis::valueAt(const QPointF &p) const @@ -416,11 +434,3 @@ QColor TimeAxis::colorForValue(double) const { return QColor(Qt::blue); } - -QString TimeAxis::textForValue(double value) const -{ - int nr = lrint(value) / 60; - if (maximum() < 600) - return QString("%1:%2").arg(nr).arg((int)value % 60, 2, 10, QChar('0')); - return QString::number(nr); -} diff --git a/profile-widget/divecartesianaxis.h b/profile-widget/divecartesianaxis.h index 1efc5b181..bb90f494e 100644 --- a/profile-widget/divecartesianaxis.h +++ b/profile-widget/divecartesianaxis.h @@ -58,7 +58,7 @@ protected: QPen gridPen; color_index_t gridColor; ProfileScene &scene; - virtual QString textForValue(double value) const; + QString textForValue(double value) const; virtual QColor colorForValue(double value) const; Orientation orientation; QList labels; @@ -96,7 +96,6 @@ class TimeAxis : public DiveCartesianAxis { public: using DiveCartesianAxis::DiveCartesianAxis; private: - QString textForValue(double value) const override; QColor colorForValue(double value) const override; };