2017-04-27 18:26:36 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "profile-widget/divecartesianaxis.h"
|
|
|
|
#include "profile-widget/divetextitem.h"
|
2022-03-12 10:11:04 +00:00
|
|
|
#include "core/profile.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "core/qthelper.h"
|
2022-08-30 16:13:13 +00:00
|
|
|
#include "core/subsurface-float.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "profile-widget/animationfunctions.h"
|
|
|
|
#include "profile-widget/divelineitem.h"
|
2021-06-28 18:29:46 +00:00
|
|
|
#include "profile-widget/profilescene.h"
|
2014-01-14 18:20:15 +00:00
|
|
|
|
2021-08-28 12:25:04 +00:00
|
|
|
static const double labelSpaceHorizontal = 2.0; // space between label and ticks
|
|
|
|
static const double labelSpaceVertical = 2.0; // space between label and ticks
|
2021-08-11 08:57:42 +00:00
|
|
|
|
2021-09-11 19:57:49 +00:00
|
|
|
void DiveCartesianAxis::setBounds(double minimum, double maximum)
|
2014-01-14 18:20:15 +00:00
|
|
|
{
|
2021-09-26 17:10:39 +00:00
|
|
|
dataMin = min = minimum;
|
|
|
|
dataMax = max = maximum;
|
2014-01-14 18:20:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 20:29:18 +00:00
|
|
|
DiveCartesianAxis::DiveCartesianAxis(Position position, bool inverted, int integralDigits, int fractionalDigits, color_index_t gridColor,
|
2021-10-03 20:10:00 +00:00
|
|
|
QColor textColor, bool textVisible, bool linesVisible,
|
2021-10-03 19:51:56 +00:00
|
|
|
double dpr, double labelScale, bool printMode, bool isGrayscale, ProfileScene &scene) :
|
2021-08-03 13:39:25 +00:00
|
|
|
printMode(printMode),
|
2021-08-28 18:12:36 +00:00
|
|
|
position(position),
|
2021-10-03 20:29:18 +00:00
|
|
|
inverted(inverted),
|
2021-09-22 18:56:13 +00:00
|
|
|
fractionalDigits(fractionalDigits),
|
2021-10-03 20:10:00 +00:00
|
|
|
textColor(textColor),
|
2021-06-28 18:29:46 +00:00
|
|
|
scene(scene),
|
2014-02-09 17:37:40 +00:00
|
|
|
min(0),
|
|
|
|
max(0),
|
2021-10-03 19:51:56 +00:00
|
|
|
textVisibility(textVisible),
|
|
|
|
lineVisibility(linesVisible),
|
2022-05-20 07:28:49 +00:00
|
|
|
gridIsMultipleOfThree(false),
|
2021-09-11 20:14:45 +00:00
|
|
|
labelScale(labelScale),
|
2021-09-22 18:56:13 +00:00
|
|
|
dpr(dpr),
|
|
|
|
transform({1.0, 0.0})
|
2014-01-14 18:20:15 +00:00
|
|
|
{
|
2021-09-01 19:18:38 +00:00
|
|
|
QPen pen;
|
|
|
|
pen.setColor(getColor(TIME_GRID, isGrayscale));
|
|
|
|
/* cosmetic width() == 0 for lines in printMode
|
|
|
|
* having setCosmetic(true) and width() > 0 does not work when
|
|
|
|
* printing on OSX and Linux */
|
|
|
|
pen.setWidth(DiveCartesianAxis::printMode ? 0 : 2);
|
|
|
|
pen.setCosmetic(true);
|
|
|
|
setPen(pen);
|
|
|
|
|
|
|
|
pen.setBrush(getColor(gridColor, isGrayscale));
|
|
|
|
gridPen = pen;
|
2021-09-20 19:13:19 +00:00
|
|
|
|
|
|
|
/* Create the longest expected label, e.g. 999.99. */
|
|
|
|
QString label;
|
|
|
|
label.reserve(integralDigits + fractionalDigits + 1);
|
|
|
|
for (int i = 0; i < integralDigits; ++i)
|
|
|
|
label.append('9');
|
|
|
|
if (fractionalDigits > 0) {
|
|
|
|
label.append('.');
|
|
|
|
for (int i = 0; i < fractionalDigits; ++i)
|
|
|
|
label.append('9');
|
|
|
|
}
|
|
|
|
|
2021-11-13 17:05:31 +00:00
|
|
|
std::tie(labelWidth, labelHeight) = DiveTextItem::getLabelSize(dpr, labelScale, label);
|
2021-12-04 20:52:25 +00:00
|
|
|
|
|
|
|
// The axis is implemented by a line, which gives the position.
|
|
|
|
// If we don't show labels or grid, we don't want to show the line,
|
|
|
|
// as this gives a strange artifact. TODO: Don't derive from a
|
|
|
|
// line object in the first place.
|
|
|
|
setVisible(textVisible || linesVisible);
|
2014-01-14 18:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DiveCartesianAxis::~DiveCartesianAxis()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:56:13 +00:00
|
|
|
void DiveCartesianAxis::setTransform(double a, double b)
|
|
|
|
{
|
|
|
|
transform.a = a;
|
|
|
|
transform.b = b;
|
|
|
|
}
|
|
|
|
|
2021-08-11 08:57:42 +00:00
|
|
|
double DiveCartesianAxis::width() const
|
|
|
|
{
|
2021-09-20 19:13:19 +00:00
|
|
|
return labelWidth + labelSpaceHorizontal * dpr;
|
2021-08-11 08:57:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 12:25:04 +00:00
|
|
|
double DiveCartesianAxis::height() const
|
|
|
|
{
|
2021-09-20 19:13:19 +00:00
|
|
|
return labelHeight + labelSpaceVertical * dpr;
|
2021-08-28 12:25:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 17:05:31 +00:00
|
|
|
double DiveCartesianAxis::horizontalOverhang() const
|
|
|
|
{
|
|
|
|
return labelWidth / 2.0;
|
|
|
|
}
|
|
|
|
|
2021-10-16 18:43:19 +00:00
|
|
|
int DiveCartesianAxis::getMinLabelDistance(const DiveCartesianAxis &timeAxis) const
|
|
|
|
{
|
|
|
|
// For the plot not being to crowded we want at least two
|
|
|
|
// labels to fit between each pair of displayed labels.
|
|
|
|
// May need some optimization.
|
|
|
|
QLineF m = timeAxis.line();
|
|
|
|
double interval = labelWidth * 3.0 * (timeAxis.maximum() - timeAxis.minimum()) / (m.x2() - m.x1());
|
|
|
|
return int(ceil(interval));
|
|
|
|
}
|
|
|
|
|
2022-05-20 07:28:49 +00:00
|
|
|
static double sensibleInterval(double inc, int decimals, bool is_time_axis, bool is_multiple_of_three)
|
2021-09-28 11:21:12 +00:00
|
|
|
{
|
2021-11-12 20:37:33 +00:00
|
|
|
if (is_time_axis && inc < 60.0) {
|
|
|
|
// for time axes and less than one hour increments, round to
|
|
|
|
// 1, 2, 3, 4, 5, 6 or 12 parts of an hour or a minute
|
|
|
|
// (that is 60, 30, 20, 15, 12, 10 or 5 min/sec).
|
|
|
|
bool fraction_of_hour = inc > 1.0;
|
|
|
|
if (fraction_of_hour)
|
|
|
|
inc /= 60.0;
|
|
|
|
inc = inc <= 1.0 / 12.0 ? 1.0 / 12.0 :
|
|
|
|
inc <= 1.0 / 6.0 ? 1.0 / 6.0 :
|
|
|
|
1.0 / floor(1.0/inc);
|
|
|
|
if (fraction_of_hour)
|
|
|
|
inc *= 60.0;
|
|
|
|
return inc;
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:21:12 +00:00
|
|
|
// Use full decimal increments
|
|
|
|
double digits = floor(log10(inc));
|
|
|
|
int digits_int = lrint(digits);
|
|
|
|
|
|
|
|
// Don't do increments smaller than the displayed decimals.
|
|
|
|
if (digits_int < -decimals) {
|
|
|
|
digits_int = -decimals;
|
|
|
|
digits = static_cast<double>(digits_int);
|
|
|
|
}
|
|
|
|
|
|
|
|
double digits_factor = pow(10.0, digits);
|
|
|
|
int inc_int = std::max((int)ceil(inc / digits_factor), 1);
|
2022-05-20 07:28:49 +00:00
|
|
|
if (is_multiple_of_three)
|
|
|
|
{
|
|
|
|
// Do increments quantized to 3. In general: 1, 3, 6, 15
|
|
|
|
if (inc_int > 6)
|
|
|
|
inc_int = 15;
|
|
|
|
else if (inc_int > 3)
|
|
|
|
inc_int = 6;
|
|
|
|
else if (inc_int == 2)
|
|
|
|
inc_int = 3;
|
|
|
|
} else {
|
|
|
|
// Do "nice" increments of the leading digit. In general: 1, 2, 4, 5.
|
|
|
|
if (inc_int > 5)
|
|
|
|
inc_int = 10;
|
|
|
|
if (inc_int == 3)
|
|
|
|
inc_int = 4;
|
|
|
|
}
|
2021-09-28 11:21:12 +00:00
|
|
|
inc = inc_int * digits_factor;
|
|
|
|
|
|
|
|
return inc;
|
|
|
|
}
|
|
|
|
|
2021-08-28 18:15:57 +00:00
|
|
|
void DiveCartesianAxis::updateTicks(int animSpeed)
|
2014-01-14 18:20:15 +00:00
|
|
|
{
|
2021-09-26 17:10:39 +00:00
|
|
|
if (dataMax - dataMin < 1e-5)
|
|
|
|
return;
|
|
|
|
|
2021-10-03 19:51:56 +00:00
|
|
|
if (!textVisibility && !lineVisibility)
|
|
|
|
return; // Nothing to display...
|
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
// Remember the old range for animations.
|
|
|
|
double dataMaxOld = dataMax;
|
|
|
|
double dataMinOld = dataMin;
|
|
|
|
|
2021-09-26 17:10:39 +00:00
|
|
|
// Guess the number of tick marks.
|
2014-01-14 18:20:15 +00:00
|
|
|
QLineF m = line();
|
2021-09-26 17:10:39 +00:00
|
|
|
double spaceNeeded = position == Position::Bottom ? labelWidth * 3.0 / 2.0
|
|
|
|
: labelHeight * 2.0;
|
|
|
|
double size = position == Position::Bottom ? fabs(m.x2() - m.x1())
|
|
|
|
: fabs(m.y2() - m.y1());
|
|
|
|
int numTicks = lrint(size / spaceNeeded);
|
|
|
|
|
|
|
|
numTicks = std::clamp(numTicks, 2, 50);
|
2021-10-03 19:51:56 +00:00
|
|
|
double stepValue = (dataMax - dataMin) / numTicks;
|
2021-09-26 17:10:39 +00:00
|
|
|
|
|
|
|
// Round the interval to a sensible size in display units
|
2021-10-03 19:51:56 +00:00
|
|
|
double intervalDisplay = stepValue * transform.a;
|
2022-05-20 07:28:49 +00:00
|
|
|
intervalDisplay = sensibleInterval(intervalDisplay, fractionalDigits, position == Position::Bottom, gridIsMultipleOfThree);
|
2021-09-26 17:10:39 +00:00
|
|
|
|
|
|
|
// Choose full multiples of the interval as minumum and maximum values
|
|
|
|
double minDisplay = transform.to(dataMin);
|
|
|
|
double maxDisplay = transform.to(dataMax);
|
2021-10-03 19:15:36 +00:00
|
|
|
|
|
|
|
// The time axis is special: use the full width in that case.
|
|
|
|
// Other axes round to the next "nice" number
|
|
|
|
double firstDisplay, lastDisplay;
|
2021-10-03 19:51:56 +00:00
|
|
|
double firstValue;
|
2021-10-03 19:15:36 +00:00
|
|
|
if (position == Position::Bottom) {
|
|
|
|
firstDisplay = ceil(minDisplay / intervalDisplay * (1.0 - 1e-5)) * intervalDisplay;
|
|
|
|
lastDisplay = floor(maxDisplay / intervalDisplay * (1.0 + 1e-5)) * intervalDisplay;
|
2021-10-03 19:51:56 +00:00
|
|
|
firstValue = transform.from(firstDisplay);
|
2021-10-03 19:15:36 +00:00
|
|
|
} 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);
|
2021-10-03 19:51:56 +00:00
|
|
|
firstValue = min;
|
2021-10-03 19:15:36 +00:00
|
|
|
}
|
2021-09-26 17:10:39 +00:00
|
|
|
numTicks = lrint((lastDisplay - firstDisplay) / intervalDisplay) + 1;
|
|
|
|
numTicks = std::max(numTicks, 0);
|
|
|
|
|
|
|
|
if (numTicks == 0)
|
2014-01-27 17:14:42 +00:00
|
|
|
return;
|
2014-01-27 19:09:08 +00:00
|
|
|
|
2021-10-03 20:29:18 +00:00
|
|
|
double internalToScreen = size / (max - min);
|
2021-10-03 19:51:56 +00:00
|
|
|
stepValue = position == Position::Bottom ?
|
2021-10-03 19:15:36 +00:00
|
|
|
intervalDisplay / transform.a : // special case for time axis.
|
|
|
|
numTicks > 1 ? (max - min) / (numTicks - 1) : 0;
|
2021-10-03 20:29:18 +00:00
|
|
|
double stepScreen = stepValue * internalToScreen;
|
|
|
|
|
|
|
|
// The ticks of the time axis don't necessarily start at the beginning.
|
|
|
|
double offsetScreen = position == Position::Bottom ?
|
|
|
|
(firstValue - min) * internalToScreen : 0.0;
|
2014-02-15 23:55:31 +00:00
|
|
|
|
2021-08-28 21:36:09 +00:00
|
|
|
// Move the remaining grid lines / labels to their correct positions
|
2021-10-03 20:29:18 +00:00
|
|
|
// regarding the possible new values for the axis.
|
|
|
|
double firstPosScreen = position == Position::Bottom ?
|
|
|
|
(inverted ? m.x2() - offsetScreen : m.x1() + offsetScreen) :
|
|
|
|
(inverted ? m.y1() + offsetScreen : m.y2() - offsetScreen);
|
2014-01-16 21:28:33 +00:00
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
updateLabels(numTicks, firstPosScreen, firstValue, stepScreen, stepValue, animSpeed, dataMinOld, dataMaxOld);
|
2021-10-03 19:51:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
|
|
|
|
QPointF DiveCartesianAxis::labelPos(double pos) const
|
2021-10-03 19:51:56 +00:00
|
|
|
{
|
2021-12-02 22:53:17 +00:00
|
|
|
return position == Position::Bottom ? QPointF(pos, rect.bottom() + labelSpaceVertical * dpr) :
|
|
|
|
position == Position::Left ? QPointF(rect.left() - labelSpaceHorizontal * dpr, pos) :
|
|
|
|
QPointF(rect.right() + labelSpaceHorizontal * dpr, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
QLineF DiveCartesianAxis::linePos(double pos) const
|
|
|
|
{
|
|
|
|
return position == Position::Bottom ? QLineF(pos, rect.top(), pos, rect.bottom()) :
|
|
|
|
QLineF(rect.left(), pos, rect.right(), pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveCartesianAxis::updateLabel(Label &label, double opacityEnd, double pos) const
|
|
|
|
{
|
2021-12-04 20:45:46 +00:00
|
|
|
label.opacityStart = label.label ? label.label->opacity()
|
|
|
|
: label.line->opacity();
|
2021-12-02 22:53:17 +00:00
|
|
|
label.opacityEnd = opacityEnd;
|
2021-12-04 20:45:46 +00:00
|
|
|
if (label.label) {
|
2021-12-02 22:53:17 +00:00
|
|
|
label.labelPosStart = label.label->pos();
|
|
|
|
label.labelPosEnd = labelPos(pos);
|
2021-12-11 07:38:56 +00:00
|
|
|
|
|
|
|
// For the time-axis, the format might change from "mm" to "mm:ss",
|
|
|
|
// or vice versa. Currently, we don't animate that, i.e. it will
|
|
|
|
// switch instantaneously.
|
|
|
|
if (position == Position::Bottom)
|
|
|
|
label.label->set(textForValue(label.value), textColor);
|
2014-01-14 18:24:33 +00:00
|
|
|
}
|
2021-12-04 20:45:46 +00:00
|
|
|
if (label.line) {
|
2021-12-02 22:53:17 +00:00
|
|
|
label.lineStart = label.line->line();
|
|
|
|
label.lineEnd = linePos(pos);
|
|
|
|
}
|
|
|
|
}
|
2014-01-14 18:24:33 +00:00
|
|
|
|
2021-12-04 20:45:46 +00:00
|
|
|
DiveCartesianAxis::Label DiveCartesianAxis::createLabel(double value, double pos, double dataMinOld, double dataMaxOld, int animSpeed, bool noLabel)
|
2021-12-02 22:53:17 +00:00
|
|
|
{
|
|
|
|
Label label { value, 0.0, 1.0 };
|
|
|
|
double posStart = posAtValue(value, dataMaxOld, dataMinOld);
|
2021-12-04 20:45:46 +00:00
|
|
|
if (textVisibility && !noLabel) {
|
2021-12-02 22:53:17 +00:00
|
|
|
label.labelPosStart = labelPos(posStart);
|
|
|
|
label.labelPosEnd = labelPos(pos);
|
2021-08-28 21:36:09 +00:00
|
|
|
int alignFlags = position == Position::Bottom ? Qt::AlignTop | Qt::AlignHCenter :
|
|
|
|
position == Position::Left ? Qt::AlignVCenter | Qt::AlignLeft:
|
|
|
|
Qt::AlignVCenter | Qt::AlignRight;
|
2021-12-02 22:53:17 +00:00
|
|
|
label.label = std::make_unique<DiveTextItem>(dpr, labelScale, alignFlags, this);
|
|
|
|
label.label->set(textForValue(value), textColor);
|
|
|
|
label.label->setZValue(1);
|
|
|
|
label.label->setPos(animSpeed <= 0 ? label.labelPosEnd : label.labelPosStart);
|
|
|
|
label.label->setOpacity(animSpeed <= 0 ? 1.0 : 0.0);
|
2014-01-14 18:28:07 +00:00
|
|
|
}
|
2021-12-02 22:53:17 +00:00
|
|
|
if (lineVisibility) {
|
|
|
|
label.lineStart = linePos(posStart);
|
|
|
|
label.lineEnd = linePos(pos);
|
|
|
|
label.line = std::make_unique<DiveLineItem>(this);
|
|
|
|
label.line->setPen(gridPen);
|
|
|
|
label.line->setZValue(0);
|
|
|
|
label.line->setLine(animSpeed <= 0 ? label.lineEnd : label.lineStart);
|
|
|
|
label.line->setOpacity(animSpeed <= 0 ? 1.0 : 0.0);
|
|
|
|
}
|
|
|
|
return label;
|
2021-10-03 19:51:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
void DiveCartesianAxis::updateLabels(int numTicks, double firstPosScreen, double firstValue, double stepScreen, double stepValue,
|
|
|
|
int animSpeed, double dataMinOld, double dataMaxOld)
|
2021-10-03 19:51:56 +00:00
|
|
|
{
|
2021-12-02 22:53:17 +00:00
|
|
|
if (animSpeed <= 0)
|
|
|
|
labels.clear(); // No animation? Simply redo the labels.
|
|
|
|
|
|
|
|
std::vector<Label> newLabels;
|
|
|
|
newLabels.reserve(numTicks);
|
|
|
|
auto actOld = labels.begin();
|
|
|
|
double value = firstValue;
|
2021-12-04 20:45:46 +00:00
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
for (int i = 0; i < numTicks; i++, value += stepValue) {
|
|
|
|
|
|
|
|
// Check if we already got that label. If we find unused labels, mark them for deletion.
|
|
|
|
// Labels to be deleted are recognized by an end-opacity of 0.0.
|
|
|
|
// Note: floating point comparisons should be fine owing to our rounding to integers above.
|
|
|
|
for ( ; actOld != labels.end() && actOld->value < value; ++actOld) {
|
|
|
|
double pos = posAtValue(actOld->value);
|
|
|
|
updateLabel(*actOld, 0.0, pos);
|
|
|
|
newLabels.push_back(std::move(*actOld));
|
|
|
|
}
|
2021-10-03 19:51:56 +00:00
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
double pos = ((position == Position::Bottom) != inverted) ?
|
|
|
|
firstPosScreen + i * stepScreen :
|
|
|
|
firstPosScreen - i * stepScreen;
|
|
|
|
if (actOld != labels.end() && actOld->value == value) {
|
|
|
|
// Update label, but don't delete it
|
|
|
|
updateLabel(*actOld, 1.0, pos);
|
|
|
|
newLabels.push_back(std::move(*actOld));
|
|
|
|
++actOld;
|
2021-10-03 19:51:56 +00:00
|
|
|
} else {
|
2021-12-04 20:45:46 +00:00
|
|
|
// This is horrible: for the depth axis, we don't want to show the first label (0).
|
|
|
|
// We recognize this by the fact that the depth axis is the only "inverted" axis.
|
|
|
|
// This really should be replaced by a general flag to avoid surprises!
|
|
|
|
bool noLabel = inverted && i == 0;
|
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
// Create new label
|
2021-12-04 20:45:46 +00:00
|
|
|
newLabels.push_back(createLabel(value, pos, dataMinOld, dataMaxOld, animSpeed, noLabel));
|
2021-10-03 19:51:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-12 16:24:19 +00:00
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
// If there are any labels left, mark them for deletion.
|
|
|
|
for ( ; actOld != labels.end(); ++actOld) {
|
|
|
|
double pos = posAtValue(actOld->value);
|
|
|
|
updateLabel(*actOld, 0.0, pos);
|
|
|
|
newLabels.push_back(std::move(*actOld));
|
|
|
|
}
|
|
|
|
|
|
|
|
labels = std::move(newLabels);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arithmetics with lines. Needed for animations. Operates pointwise.
|
|
|
|
static QLineF operator-(const QLineF &l1, const QLineF &l2)
|
|
|
|
{
|
|
|
|
return QLineF(l1.p1() - l2.p1(), l1.p2() - l2.p2());
|
|
|
|
}
|
|
|
|
static QLineF operator+(const QLineF &l1, const QLineF &l2)
|
|
|
|
{
|
|
|
|
return QLineF(l1.p1() + l2.p1(), l1.p2() + l2.p2());
|
|
|
|
}
|
|
|
|
static QLineF operator*(double f, const QLineF &l)
|
|
|
|
{
|
|
|
|
return QLineF(f*l.p1(), f*l.p2());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper template: get point in interval (0.0: start, 1.0: end)
|
|
|
|
template <typename T>
|
|
|
|
T mid(const T &start, const T &end, double fraction)
|
|
|
|
{
|
|
|
|
return start + fraction * (end - start);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveCartesianAxis::anim(double fraction)
|
|
|
|
{
|
|
|
|
if (fraction == 1.0) {
|
|
|
|
// The animation has finished.
|
|
|
|
// Remove labels that have been marked for deletion by setting the opacity to 0.0.
|
|
|
|
// Use the erase-remove idiom (yes, it is a weird idiom).
|
|
|
|
labels.erase(std::remove_if(labels.begin(), labels.end(),
|
|
|
|
[](const Label &l) { return l.opacityEnd == 0.0; }),
|
|
|
|
labels.end());
|
|
|
|
}
|
|
|
|
for (Label &label: labels) {
|
|
|
|
double opacity = mid(label.opacityStart, label.opacityEnd, fraction);
|
|
|
|
if (label.label) {
|
|
|
|
label.label->setOpacity(opacity);
|
|
|
|
label.label->setPos(mid(label.labelPosStart, label.labelPosEnd, fraction));
|
|
|
|
}
|
|
|
|
if (label.line) {
|
|
|
|
label.line->setOpacity(opacity);
|
|
|
|
label.line->setLine(mid(label.lineStart, label.lineEnd, fraction));
|
2014-02-16 00:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-14 18:20:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 08:32:41 +00:00
|
|
|
void DiveCartesianAxis::setPosition(const QRectF &rectIn)
|
2021-08-28 21:36:09 +00:00
|
|
|
{
|
|
|
|
rect = rectIn;
|
|
|
|
switch (position) {
|
|
|
|
case Position::Left:
|
|
|
|
setLine(QLineF(rect.topLeft(), rect.bottomLeft()));
|
|
|
|
break;
|
|
|
|
case Position::Right:
|
|
|
|
setLine(QLineF(rect.topRight(), rect.bottomRight()));
|
|
|
|
break;
|
|
|
|
case Position::Bottom:
|
|
|
|
default:
|
|
|
|
setLine(QLineF(rect.bottomLeft(), rect.bottomRight()));
|
|
|
|
break;
|
|
|
|
}
|
2014-01-22 19:10:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 18:56:13 +00:00
|
|
|
double DiveCartesianAxis::Transform::to(double x) const
|
|
|
|
{
|
|
|
|
return a*x + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
double DiveCartesianAxis::Transform::from(double y) const
|
|
|
|
{
|
|
|
|
return (y - b) / a;
|
|
|
|
}
|
|
|
|
|
2020-12-20 12:01:21 +00:00
|
|
|
QString DiveCartesianAxis::textForValue(double value) const
|
2014-01-14 18:20:15 +00:00
|
|
|
{
|
2021-10-03 19:15:36 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-01-14 18:20:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
double DiveCartesianAxis::valueAt(const QPointF &p) const
|
2014-01-14 18:20:15 +00:00
|
|
|
{
|
|
|
|
QLineF m = line();
|
2014-02-11 20:05:33 +00:00
|
|
|
QPointF relativePosition = p;
|
|
|
|
relativePosition -= pos(); // normalize p based on the axis' offset on screen
|
|
|
|
|
2021-10-03 20:29:18 +00:00
|
|
|
double fraction = position == Position::Bottom ?
|
|
|
|
(relativePosition.x() - m.x1()) / (m.x2() - m.x1()) :
|
|
|
|
(relativePosition.y() - m.y1()) / (m.y2() - m.y1());
|
2019-08-08 09:30:41 +00:00
|
|
|
|
2021-10-03 20:29:18 +00:00
|
|
|
if ((position == Position::Bottom) == inverted)
|
|
|
|
fraction = 1.0 - fraction;
|
2019-08-08 09:30:41 +00:00
|
|
|
return fraction * (max - min) + min;
|
2014-01-14 18:20:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
double DiveCartesianAxis::posAtValue(double value, double max, double min) const
|
2014-01-14 18:20:15 +00:00
|
|
|
{
|
|
|
|
QLineF m = line();
|
|
|
|
|
2021-11-14 16:39:33 +00:00
|
|
|
double screenFrom = position == Position::Bottom ? m.x1() : m.y1();
|
|
|
|
double screenTo = position == Position::Bottom ? m.x2() : m.y2();
|
2022-08-30 15:55:43 +00:00
|
|
|
if (nearly_equal(min, max))
|
2021-11-14 16:39:33 +00:00
|
|
|
return (screenFrom + screenTo) / 2.0;
|
2021-10-03 20:29:18 +00:00
|
|
|
if ((position == Position::Bottom) == inverted)
|
2021-11-14 16:39:33 +00:00
|
|
|
std::swap(screenFrom, screenTo);
|
|
|
|
return (value - min) / (max - min) *
|
|
|
|
(screenTo - screenFrom) + screenFrom;
|
2014-01-14 18:20:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
double DiveCartesianAxis::posAtValue(double value) const
|
|
|
|
{
|
|
|
|
return posAtValue(value, max, min);
|
|
|
|
}
|
|
|
|
|
2021-10-26 14:37:38 +00:00
|
|
|
static std::pair<double, double> getLineFromTo(const QLineF &l, bool horizontal)
|
2021-10-26 07:40:38 +00:00
|
|
|
{
|
2021-10-26 14:37:38 +00:00
|
|
|
if (horizontal)
|
|
|
|
return std::make_pair(l.x1(), l.x2());
|
|
|
|
else
|
|
|
|
return std::make_pair(l.y1(), l.y2());
|
|
|
|
}
|
2021-10-26 07:40:38 +00:00
|
|
|
|
2021-10-26 14:37:38 +00:00
|
|
|
double DiveCartesianAxis::screenPosition(double pos) const
|
|
|
|
{
|
2021-10-26 07:40:38 +00:00
|
|
|
if ((position == Position::Bottom) == inverted)
|
|
|
|
pos = 1.0 - pos;
|
|
|
|
|
2021-10-26 14:37:38 +00:00
|
|
|
auto [from, to] = getLineFromTo(line(), position == Position::Bottom);
|
2021-10-26 07:40:38 +00:00
|
|
|
return (to - from) * pos + from;
|
|
|
|
}
|
|
|
|
|
2021-10-26 14:37:38 +00:00
|
|
|
double DiveCartesianAxis::pointInRange(double pos) const
|
|
|
|
{
|
|
|
|
auto [from, to] = getLineFromTo(line(), position == Position::Bottom);
|
|
|
|
if (from > to)
|
|
|
|
std::swap(from, to);
|
|
|
|
return pos >= from && pos <= to;
|
|
|
|
}
|
|
|
|
|
2014-01-14 18:20:15 +00:00
|
|
|
double DiveCartesianAxis::maximum() const
|
|
|
|
{
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
double DiveCartesianAxis::minimum() const
|
|
|
|
{
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
2022-05-20 07:28:49 +00:00
|
|
|
void DiveCartesianAxis::setGridIsMultipleOfThree(bool arg1)
|
|
|
|
{
|
|
|
|
gridIsMultipleOfThree = arg1;
|
|
|
|
}
|
|
|
|
|
2021-08-29 20:13:26 +00:00
|
|
|
std::pair<double, double> DiveCartesianAxis::screenMinMax() const
|
|
|
|
{
|
|
|
|
return position == Position::Bottom ? std::make_pair(rect.left(), rect.right())
|
|
|
|
: std::make_pair(rect.top(), rect.bottom());
|
|
|
|
}
|