2021-06-05 16:42:56 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "profilescene.h"
|
2021-08-03 07:51:37 +00:00
|
|
|
#include "diveeventitem.h"
|
2021-06-28 18:29:46 +00:00
|
|
|
#include "divecartesianaxis.h"
|
2021-08-29 20:13:26 +00:00
|
|
|
#include "divepercentageitem.h"
|
2021-08-30 19:11:11 +00:00
|
|
|
#include "divepixmapcache.h"
|
2021-08-03 07:51:37 +00:00
|
|
|
#include "diveprofileitem.h"
|
|
|
|
#include "divetextitem.h"
|
|
|
|
#include "tankitem.h"
|
|
|
|
#include "core/device.h"
|
|
|
|
#include "core/event.h"
|
2021-06-28 18:29:46 +00:00
|
|
|
#include "core/pref.h"
|
2021-08-03 07:51:37 +00:00
|
|
|
#include "core/profile.h"
|
|
|
|
#include "core/qthelper.h" // for decoMode()
|
2022-08-28 20:42:54 +00:00
|
|
|
#include "core/subsurface-float.h"
|
2021-08-03 07:51:37 +00:00
|
|
|
#include "core/subsurface-string.h"
|
|
|
|
#include "core/settings/qPrefDisplay.h"
|
|
|
|
#include "qt-models/diveplannermodel.h"
|
2021-12-02 22:53:17 +00:00
|
|
|
#include <QAbstractAnimation>
|
2021-06-05 16:42:56 +00:00
|
|
|
|
2021-08-28 21:36:09 +00:00
|
|
|
static const double diveComputerTextBorder = 1.0;
|
2021-06-28 18:29:46 +00:00
|
|
|
|
2021-12-02 22:53:17 +00:00
|
|
|
// Class for animations (if any). Might want to do our own.
|
|
|
|
class ProfileAnimation : public QAbstractAnimation {
|
|
|
|
ProfileScene &scene;
|
|
|
|
// For historical reasons, speed is actually the duration
|
|
|
|
// (i.e. the reciprocal of speed). Ouch, that hurts.
|
|
|
|
int speed;
|
|
|
|
|
|
|
|
int duration() const override
|
|
|
|
{
|
|
|
|
return speed;
|
|
|
|
}
|
|
|
|
void updateCurrentTime(int time) override
|
|
|
|
{
|
|
|
|
// Note: we explicitly pass 1.0 at the end, so that
|
|
|
|
// the callee can do a simple float comparison for "end".
|
|
|
|
scene.anim(time == speed ? 1.0
|
|
|
|
: static_cast<double>(time) / speed);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
ProfileAnimation(ProfileScene &scene, int animSpeed) :
|
|
|
|
scene(scene),
|
|
|
|
speed(animSpeed)
|
|
|
|
{
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-03 07:51:37 +00:00
|
|
|
template<typename T, class... Args>
|
2021-12-03 17:49:49 +00:00
|
|
|
T *ProfileScene::createItem(const DiveCartesianAxis &vAxis, DataAccessor accessor, int z, Args&&... args)
|
2021-08-03 07:51:37 +00:00
|
|
|
{
|
2021-12-03 18:04:33 +00:00
|
|
|
T *res = new T(plotInfo, *timeAxis, vAxis, accessor, std::forward<Args>(args)...);
|
2021-08-03 07:51:37 +00:00
|
|
|
res->setZValue(static_cast<double>(z));
|
|
|
|
profileItems.push_back(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:49:49 +00:00
|
|
|
PartialPressureGasItem *ProfileScene::createPPGas(DataAccessor accessor, color_index_t color, color_index_t colorAlert,
|
|
|
|
const double *thresholdSettingsMin, const double *thresholdSettingsMax)
|
2021-08-03 07:51:37 +00:00
|
|
|
{
|
2021-12-03 17:49:49 +00:00
|
|
|
PartialPressureGasItem *item = createItem<PartialPressureGasItem>(*gasYAxis, accessor, 99, dpr);
|
2021-08-03 07:51:37 +00:00
|
|
|
item->setThresholdSettingsKey(thresholdSettingsMin, thresholdSettingsMax);
|
|
|
|
item->setColors(getColor(color, isGrayscale), getColor(colorAlert, isGrayscale));
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2021-12-03 17:49:49 +00:00
|
|
|
template <int IDX>
|
|
|
|
double accessTissue(const plot_data &item)
|
|
|
|
{
|
|
|
|
return item.ceilings[IDX];
|
|
|
|
}
|
|
|
|
|
|
|
|
// For now, the accessor functions for the profile data do not possess a payload.
|
|
|
|
// To generate the 16 tissue (ceiling) accessor functions, use iterative templates.
|
|
|
|
// Thanks to C++17's constexpr if, this is actually easy to read and follow.
|
|
|
|
template <int ACT, int MAX>
|
|
|
|
void ProfileScene::addTissueItems(double dpr)
|
|
|
|
{
|
|
|
|
if constexpr (ACT < MAX) {
|
|
|
|
DiveCalculatedTissue *tissueItem = createItem<DiveCalculatedTissue>(*profileYAxis, &accessTissue<ACT>, ACT + 1, dpr);
|
|
|
|
allTissues.push_back(tissueItem);
|
|
|
|
addTissueItems<ACT + 1, MAX>(dpr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:48:08 +00:00
|
|
|
ProfileScene::ProfileScene(double dpr, bool printMode, bool isGrayscale) :
|
2021-08-03 07:51:37 +00:00
|
|
|
d(nullptr),
|
|
|
|
dc(-1),
|
2021-08-09 14:48:08 +00:00
|
|
|
dpr(dpr),
|
2021-08-03 13:39:25 +00:00
|
|
|
printMode(printMode),
|
|
|
|
isGrayscale(isGrayscale),
|
2022-10-30 07:55:59 +00:00
|
|
|
empty(true),
|
2021-08-03 07:51:37 +00:00
|
|
|
maxtime(-1),
|
|
|
|
maxdepth(-1),
|
2021-10-03 20:29:18 +00:00
|
|
|
profileYAxis(new DiveCartesianAxis(DiveCartesianAxis::Position::Left, true, 3, 0, TIME_GRID, Qt::red, true, true,
|
2021-10-03 19:51:56 +00:00
|
|
|
dpr, 1.0, printMode, isGrayscale, *this)),
|
2021-10-03 20:29:18 +00:00
|
|
|
gasYAxis(new DiveCartesianAxis(DiveCartesianAxis::Position::Right, false, 1, 2, TIME_GRID, Qt::black, true, true,
|
2021-10-03 19:51:56 +00:00
|
|
|
dpr, 0.7, printMode, isGrayscale, *this)),
|
2021-10-03 20:29:18 +00:00
|
|
|
temperatureAxis(new DiveCartesianAxis(DiveCartesianAxis::Position::Right, false, 3, 0, TIME_GRID, Qt::black, false, false,
|
2021-10-03 19:51:56 +00:00
|
|
|
dpr, 1.0, printMode, isGrayscale, *this)),
|
2021-10-03 20:29:18 +00:00
|
|
|
timeAxis(new DiveCartesianAxis(DiveCartesianAxis::Position::Bottom, false, 2, 2, TIME_GRID, Qt::blue, true, true,
|
2021-10-03 19:51:56 +00:00
|
|
|
dpr, 1.0, printMode, isGrayscale, *this)),
|
2021-10-03 20:29:18 +00:00
|
|
|
cylinderPressureAxis(new DiveCartesianAxis(DiveCartesianAxis::Position::Right, false, 4, 0, TIME_GRID, Qt::black, false, false,
|
2021-10-03 19:51:56 +00:00
|
|
|
dpr, 1.0, printMode, isGrayscale, *this)),
|
2021-10-03 20:29:18 +00:00
|
|
|
heartBeatAxis(new DiveCartesianAxis(DiveCartesianAxis::Position::Left, false, 3, 0, HR_AXIS, Qt::black, true, true,
|
2021-10-03 19:51:56 +00:00
|
|
|
dpr, 0.7, printMode, isGrayscale, *this)),
|
2021-10-03 20:29:18 +00:00
|
|
|
percentageAxis(new DiveCartesianAxis(DiveCartesianAxis::Position::Right, false, 2, 0, TIME_GRID, Qt::black, false, false,
|
2021-10-03 19:51:56 +00:00
|
|
|
dpr, 0.7, printMode, isGrayscale, *this)),
|
2021-12-03 17:49:49 +00:00
|
|
|
diveProfileItem(createItem<DiveProfileItem>(*profileYAxis,
|
|
|
|
[](const plot_data &item) { return (double)item.depth; },
|
|
|
|
0, dpr)),
|
|
|
|
temperatureItem(createItem<DiveTemperatureItem>(*temperatureAxis,
|
|
|
|
[](const plot_data &item) { return (double)item.temperature; },
|
|
|
|
1, dpr)),
|
|
|
|
meanDepthItem(createItem<DiveMeanDepthItem>(*profileYAxis,
|
|
|
|
[](const plot_data &item) { return (double)item.running_sum; },
|
|
|
|
1, dpr)),
|
|
|
|
gasPressureItem(createItem<DiveGasPressureItem>(*cylinderPressureAxis,
|
|
|
|
[](const plot_data &item) { return 0.0; }, // unused
|
|
|
|
1, dpr)),
|
2021-08-12 20:57:57 +00:00
|
|
|
diveComputerText(new DiveTextItem(dpr, 1.0, Qt::AlignRight | Qt::AlignTop, nullptr)),
|
2021-12-03 17:49:49 +00:00
|
|
|
reportedCeiling(createItem<DiveReportedCeiling>(*profileYAxis,
|
|
|
|
[](const plot_data &item) { return (double)item.ceiling; },
|
|
|
|
1, dpr)),
|
|
|
|
pn2GasItem(createPPGas([](const plot_data &item) { return (double)item.pressures.n2; },
|
|
|
|
PN2, PN2_ALERT, NULL, &prefs.pp_graphs.pn2_threshold)),
|
|
|
|
pheGasItem(createPPGas([](const plot_data &item) { return (double)item.pressures.he; },
|
|
|
|
PHE, PHE_ALERT, NULL, &prefs.pp_graphs.phe_threshold)),
|
|
|
|
po2GasItem(createPPGas([](const plot_data &item) { return (double)item.pressures.o2; },
|
|
|
|
PO2, PO2_ALERT, &prefs.pp_graphs.po2_threshold_min, &prefs.pp_graphs.po2_threshold_max)),
|
|
|
|
o2SetpointGasItem(createPPGas([](const plot_data &item) { return item.o2setpoint.mbar / 1000.0; },
|
|
|
|
O2SETPOINT, PO2_ALERT, &prefs.pp_graphs.po2_threshold_min, &prefs.pp_graphs.po2_threshold_max)),
|
|
|
|
ccrsensor1GasItem(createPPGas([](const plot_data &item) { return item.o2sensor[0].mbar / 1000.0; },
|
|
|
|
CCRSENSOR1, PO2_ALERT, &prefs.pp_graphs.po2_threshold_min, &prefs.pp_graphs.po2_threshold_max)),
|
|
|
|
ccrsensor2GasItem(createPPGas([](const plot_data &item) { return item.o2sensor[1].mbar / 1000.0; },
|
|
|
|
CCRSENSOR2, PO2_ALERT, &prefs.pp_graphs.po2_threshold_min, &prefs.pp_graphs.po2_threshold_max)),
|
|
|
|
ccrsensor3GasItem(createPPGas([](const plot_data &item) { return item.o2sensor[2].mbar / 1000.0; },
|
|
|
|
CCRSENSOR3, PO2_ALERT, &prefs.pp_graphs.po2_threshold_min, &prefs.pp_graphs.po2_threshold_max)),
|
|
|
|
ocpo2GasItem(createPPGas([](const plot_data &item) { return item.scr_OC_pO2.mbar / 1000.0; },
|
|
|
|
SCR_OCPO2, PO2_ALERT, &prefs.pp_graphs.po2_threshold_min, &prefs.pp_graphs.po2_threshold_max)),
|
|
|
|
diveCeiling(createItem<DiveCalculatedCeiling>(*profileYAxis,
|
|
|
|
[](const plot_data &item) { return (double)item.ceiling; },
|
|
|
|
1, dpr)),
|
2021-08-28 21:36:09 +00:00
|
|
|
decoModelParameters(new DiveTextItem(dpr, 1.0, Qt::AlignHCenter | Qt::AlignTop, nullptr)),
|
2021-12-03 17:49:49 +00:00
|
|
|
heartBeatItem(createItem<DiveHeartrateItem>(*heartBeatAxis,
|
|
|
|
[](const plot_data &item) { return (double)item.heartbeat; },
|
|
|
|
1, dpr)),
|
2022-02-10 01:06:40 +00:00
|
|
|
percentageItem(new DivePercentageItem(*timeAxis, *percentageAxis)),
|
2021-08-30 19:11:11 +00:00
|
|
|
tankItem(new TankItem(*timeAxis, dpr)),
|
|
|
|
pixmaps(getDivePixmaps(dpr))
|
2021-06-05 16:42:56 +00:00
|
|
|
{
|
2021-08-03 07:51:37 +00:00
|
|
|
init_plot_info(&plotInfo);
|
|
|
|
|
2021-06-05 16:49:00 +00:00
|
|
|
setSceneRect(0, 0, 100, 100);
|
|
|
|
setItemIndexMethod(QGraphicsScene::NoIndex);
|
2021-06-28 18:29:46 +00:00
|
|
|
|
|
|
|
gasYAxis->setZValue(timeAxis->zValue() + 1);
|
2021-08-03 07:51:37 +00:00
|
|
|
tankItem->setZValue(100);
|
|
|
|
|
2021-09-22 18:56:13 +00:00
|
|
|
// These axes are not locale-dependent. Set their scale factor once here.
|
2021-09-26 17:10:39 +00:00
|
|
|
timeAxis->setTransform(1.0/60.0);
|
2021-09-22 18:56:13 +00:00
|
|
|
heartBeatAxis->setTransform(1.0);
|
|
|
|
gasYAxis->setTransform(1.0); // Non-metric countries likewise use bar (disguised as "percentage") for partial pressure.
|
|
|
|
|
2021-12-03 17:49:49 +00:00
|
|
|
addTissueItems<0,16>(dpr);
|
2021-06-28 18:29:46 +00:00
|
|
|
|
2021-08-29 20:13:26 +00:00
|
|
|
percentageItem->setZValue(1.0);
|
2022-05-20 07:28:49 +00:00
|
|
|
profileYAxis->setGridIsMultipleOfThree( qPrefDisplay::three_m_based_grid() );
|
2021-08-29 20:13:26 +00:00
|
|
|
|
2021-06-28 18:29:46 +00:00
|
|
|
// Add items to scene
|
2021-08-03 07:51:37 +00:00
|
|
|
addItem(diveComputerText);
|
|
|
|
addItem(tankItem);
|
|
|
|
addItem(decoModelParameters);
|
2021-06-28 18:29:46 +00:00
|
|
|
addItem(profileYAxis);
|
|
|
|
addItem(gasYAxis);
|
|
|
|
addItem(temperatureAxis);
|
|
|
|
addItem(timeAxis);
|
|
|
|
addItem(cylinderPressureAxis);
|
|
|
|
addItem(percentageAxis);
|
|
|
|
addItem(heartBeatAxis);
|
2021-08-29 20:13:26 +00:00
|
|
|
addItem(percentageItem);
|
2021-08-03 07:51:37 +00:00
|
|
|
|
|
|
|
for (AbstractProfilePolygonItem *item: profileItems)
|
|
|
|
addItem(item);
|
2021-06-05 16:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProfileScene::~ProfileScene()
|
|
|
|
{
|
2021-08-03 07:51:37 +00:00
|
|
|
free_plot_info_data(&plotInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileScene::clear()
|
|
|
|
{
|
|
|
|
for (AbstractProfilePolygonItem *item: profileItems)
|
|
|
|
item->clear();
|
|
|
|
|
|
|
|
// the events will have connected slots which can fire after
|
|
|
|
// the dive and its data have been deleted - so explictly delete
|
|
|
|
// the DiveEventItems
|
|
|
|
qDeleteAll(eventItems);
|
|
|
|
eventItems.clear();
|
2021-12-04 15:11:58 +00:00
|
|
|
free_plot_info_data(&plotInfo);
|
2022-10-30 07:55:59 +00:00
|
|
|
empty = true;
|
2021-06-05 16:42:56 +00:00
|
|
|
}
|
2021-06-28 18:29:46 +00:00
|
|
|
|
2021-10-09 21:22:42 +00:00
|
|
|
static bool ppGraphsEnabled(const struct divecomputer *dc, bool simplified)
|
2021-06-28 18:29:46 +00:00
|
|
|
{
|
2021-10-09 21:22:42 +00:00
|
|
|
return simplified ? (dc->divemode == CCR && prefs.pp_graphs.po2)
|
|
|
|
: (prefs.pp_graphs.po2 || prefs.pp_graphs.pn2 || prefs.pp_graphs.phe);
|
2021-06-28 18:29:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 07:51:37 +00:00
|
|
|
// Update visibility of non-interactive chart features according to preferences
|
2021-10-09 21:22:42 +00:00
|
|
|
void ProfileScene::updateVisibility(bool diveHasHeartBeat, bool simplified)
|
2021-08-03 07:51:37 +00:00
|
|
|
{
|
2021-10-09 21:22:42 +00:00
|
|
|
const struct divecomputer *currentdc = get_dive_dc_const(d, dc);
|
|
|
|
if (!currentdc)
|
|
|
|
return;
|
|
|
|
bool ppGraphs = ppGraphsEnabled(currentdc, simplified);
|
|
|
|
|
|
|
|
if (simplified) {
|
|
|
|
pn2GasItem->setVisible(false);
|
|
|
|
po2GasItem->setVisible(ppGraphs);
|
|
|
|
pheGasItem->setVisible(false);
|
|
|
|
|
|
|
|
temperatureItem->setVisible(!ppGraphs);
|
|
|
|
tankItem->setVisible(!ppGraphs && prefs.tankbar);
|
|
|
|
|
|
|
|
o2SetpointGasItem->setVisible(ppGraphs && prefs.show_ccr_setpoint);
|
|
|
|
ccrsensor1GasItem->setVisible(ppGraphs && prefs.show_ccr_sensors);
|
|
|
|
ccrsensor2GasItem->setVisible(ppGraphs && prefs.show_ccr_sensors && (currentdc->no_o2sensors > 1));
|
|
|
|
ccrsensor3GasItem->setVisible(ppGraphs && prefs.show_ccr_sensors && (currentdc->no_o2sensors > 1));
|
|
|
|
ocpo2GasItem->setVisible((currentdc->divemode == PSCR) && prefs.show_scr_ocpo2);
|
|
|
|
} else {
|
|
|
|
pn2GasItem->setVisible(prefs.pp_graphs.pn2);
|
|
|
|
po2GasItem->setVisible(prefs.pp_graphs.po2);
|
|
|
|
pheGasItem->setVisible(prefs.pp_graphs.phe);
|
|
|
|
|
|
|
|
bool setpointflag = currentdc->divemode == CCR && prefs.pp_graphs.po2;
|
|
|
|
bool sensorflag = setpointflag && prefs.show_ccr_sensors;
|
|
|
|
o2SetpointGasItem->setVisible(setpointflag && prefs.show_ccr_setpoint);
|
|
|
|
ccrsensor1GasItem->setVisible(sensorflag);
|
|
|
|
ccrsensor2GasItem->setVisible(sensorflag && currentdc->no_o2sensors > 1);
|
|
|
|
ccrsensor3GasItem->setVisible(sensorflag && currentdc->no_o2sensors > 2);
|
|
|
|
ocpo2GasItem->setVisible(currentdc->divemode == PSCR && prefs.show_scr_ocpo2);
|
|
|
|
|
|
|
|
heartBeatItem->setVisible(prefs.hrgraph && diveHasHeartBeat);
|
|
|
|
|
|
|
|
diveCeiling->setVisible(prefs.calcceiling);
|
|
|
|
decoModelParameters->setVisible(prefs.decoinfo);
|
|
|
|
|
|
|
|
for (DiveCalculatedTissue *tissue: allTissues)
|
|
|
|
tissue->setVisible(prefs.calcalltissues && prefs.calcceiling);
|
|
|
|
percentageItem->setVisible(prefs.percentagegraph);
|
|
|
|
|
|
|
|
meanDepthItem->setVisible(prefs.show_average_depth);
|
|
|
|
reportedCeiling->setVisible(prefs.dcceiling);
|
|
|
|
tankItem->setVisible(prefs.tankbar);
|
|
|
|
temperatureItem->setVisible(true);
|
|
|
|
}
|
2021-08-03 07:51:37 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 21:36:09 +00:00
|
|
|
void ProfileScene::resize(QSizeF size)
|
|
|
|
{
|
|
|
|
setSceneRect(QRectF(QPointF(), size));
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:56:13 +00:00
|
|
|
// Helper templates to determine slope and intersect of a linear function.
|
|
|
|
// The function arguments are supposed to be integral types.
|
|
|
|
template<typename Func>
|
|
|
|
static auto intercept(Func f)
|
|
|
|
{
|
|
|
|
return f(0);
|
|
|
|
}
|
|
|
|
template<typename Func>
|
|
|
|
static auto slope(Func f)
|
|
|
|
{
|
|
|
|
return f(1) - f(0);
|
|
|
|
}
|
|
|
|
|
2021-08-28 21:36:09 +00:00
|
|
|
// Helper structure for laying out secondary plots.
|
|
|
|
struct VerticalAxisLayout {
|
|
|
|
DiveCartesianAxis *axis;
|
2021-12-11 07:45:47 +00:00
|
|
|
double height; // in pixels (times dpr) or a weight if not enough space
|
|
|
|
double bottom_border; // in pixels (times dpr)
|
2021-08-28 21:36:09 +00:00
|
|
|
bool visible;
|
|
|
|
};
|
|
|
|
|
2021-10-09 21:22:42 +00:00
|
|
|
void ProfileScene::updateAxes(bool diveHasHeartBeat, bool simplified)
|
2021-06-28 18:29:46 +00:00
|
|
|
{
|
2021-10-09 21:22:42 +00:00
|
|
|
const struct divecomputer *currentdc = get_dive_dc_const(d, dc);
|
|
|
|
if (!currentdc)
|
|
|
|
return;
|
|
|
|
|
2021-11-13 17:05:31 +00:00
|
|
|
// Calculate left and right border needed for the axes and other chart items.
|
2021-08-28 21:36:09 +00:00
|
|
|
double leftBorder = profileYAxis->width();
|
|
|
|
if (prefs.hrgraph)
|
|
|
|
leftBorder = std::max(leftBorder, heartBeatAxis->width());
|
2021-11-13 17:05:31 +00:00
|
|
|
|
|
|
|
double rightWidth = timeAxis->horizontalOverhang();
|
|
|
|
if (prefs.show_average_depth)
|
|
|
|
rightWidth = std::max(rightWidth, meanDepthItem->labelWidth);
|
|
|
|
if (ppGraphsEnabled(currentdc, simplified))
|
|
|
|
rightWidth = std::max(rightWidth, gasYAxis->width());
|
2021-08-28 21:36:09 +00:00
|
|
|
double rightBorder = sceneRect().width() - rightWidth;
|
|
|
|
double width = rightBorder - leftBorder;
|
2021-06-28 18:29:46 +00:00
|
|
|
|
2021-08-28 21:36:09 +00:00
|
|
|
if (width <= 10.0 * dpr)
|
|
|
|
return clear();
|
|
|
|
|
2022-05-20 07:28:49 +00:00
|
|
|
profileYAxis->setGridIsMultipleOfThree( qPrefDisplay::three_m_based_grid() );
|
|
|
|
|
2021-08-28 21:36:09 +00:00
|
|
|
// Place the fixed dive computer text at the bottom
|
|
|
|
double bottomBorder = sceneRect().height() - diveComputerText->height() - 2.0 * dpr * diveComputerTextBorder;
|
|
|
|
diveComputerText->setPos(0.0, bottomBorder + dpr * diveComputerTextBorder);
|
|
|
|
|
|
|
|
double topBorder = 0.0;
|
|
|
|
|
|
|
|
// show the deco model parameters at the top in the center
|
|
|
|
if (prefs.decoinfo) {
|
|
|
|
decoModelParameters->setPos(leftBorder + width / 2.0, topBorder);
|
|
|
|
topBorder += decoModelParameters->height();
|
|
|
|
}
|
|
|
|
|
|
|
|
bottomBorder -= timeAxis->height();
|
2021-12-10 11:15:44 +00:00
|
|
|
profileRegion = QRectF(leftBorder, topBorder, width, bottomBorder - topBorder);
|
|
|
|
timeAxis->setPosition(profileRegion);
|
2021-08-28 21:36:09 +00:00
|
|
|
|
|
|
|
if (prefs.tankbar) {
|
|
|
|
bottomBorder -= tankItem->height();
|
|
|
|
// Note: we set x to 0.0, because the tank item uses the timeAxis to set the x-coordinate.
|
|
|
|
tankItem->setPos(0.0, bottomBorder);
|
|
|
|
}
|
|
|
|
|
|
|
|
double height = bottomBorder - topBorder;
|
|
|
|
if (height <= 50.0 * dpr)
|
|
|
|
return clear();
|
|
|
|
|
|
|
|
// The rest is laid out dynamically. Give at least 50% to the actual profile.
|
|
|
|
// The max heights are given for DPR=1, i.e. a ca. 800x600 pixels profile.
|
|
|
|
const double minProfileFraction = 0.5;
|
|
|
|
VerticalAxisLayout secondaryAxes[] = {
|
|
|
|
// Note: axes are listed from bottom to top, since they are added that way.
|
2021-12-11 07:45:47 +00:00
|
|
|
{ heartBeatAxis, 75.0, 0.0, prefs.hrgraph && diveHasHeartBeat },
|
|
|
|
{ percentageAxis, 50.0, 0.0, prefs.percentagegraph },
|
|
|
|
{ gasYAxis, 75.0, 0.0, ppGraphsEnabled(currentdc, simplified) },
|
|
|
|
{ temperatureAxis, 50.0, 2.0, true },
|
2021-08-28 21:36:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A loop is probably easier to read than std::accumulate.
|
|
|
|
double totalSecondaryHeight = 0.0;
|
|
|
|
for (const VerticalAxisLayout &l: secondaryAxes) {
|
|
|
|
if (l.visible)
|
|
|
|
totalSecondaryHeight += l.height * dpr;
|
2021-06-28 18:29:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 21:36:09 +00:00
|
|
|
if (totalSecondaryHeight > height * minProfileFraction) {
|
|
|
|
// Use 50% for the profile and the rest for the remaining graphs, scaled by their maximum height.
|
|
|
|
double remainingSpace = height * minProfileFraction;
|
|
|
|
for (VerticalAxisLayout &l: secondaryAxes)
|
|
|
|
l.height *= remainingSpace / totalSecondaryHeight;
|
|
|
|
}
|
2021-06-28 18:29:46 +00:00
|
|
|
|
2021-08-28 21:36:09 +00:00
|
|
|
for (const VerticalAxisLayout &l: secondaryAxes) {
|
|
|
|
l.axis->setVisible(l.visible);
|
|
|
|
if (!l.visible)
|
|
|
|
continue;
|
|
|
|
bottomBorder -= l.height * dpr;
|
2021-12-11 07:45:47 +00:00
|
|
|
l.axis->setPosition(QRectF(leftBorder, bottomBorder, width, (l.height - l.bottom_border) * dpr));
|
2021-08-28 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
height = bottomBorder - topBorder;
|
2021-09-28 08:32:41 +00:00
|
|
|
profileYAxis->setPosition(QRectF(leftBorder, topBorder, width, height));
|
2021-08-28 21:36:09 +00:00
|
|
|
|
|
|
|
// The cylinders are displayed in the 24-80% region of the profile
|
2021-09-28 08:32:41 +00:00
|
|
|
cylinderPressureAxis->setPosition(QRectF(leftBorder, topBorder + 0.24 * height, width, 0.56 * height));
|
2021-09-22 18:56:13 +00:00
|
|
|
|
|
|
|
// Set scale factors depending on locale.
|
|
|
|
// The conversion calls, such as mm_to_feet(), will be optimized away.
|
|
|
|
profileYAxis->setTransform(prefs.units.length == units::METERS ? 0.001 : slope(mm_to_feet));
|
|
|
|
cylinderPressureAxis->setTransform(prefs.units.pressure == units::BAR ? 0.001 : slope(mbar_to_PSI));
|
|
|
|
// Temperature is special: this is not a linear transformation, but requires a shift of origin.
|
|
|
|
if (prefs.units.temperature == units::CELSIUS)
|
|
|
|
temperatureAxis->setTransform(slope(mkelvin_to_C), intercept(mkelvin_to_C));
|
|
|
|
else
|
|
|
|
temperatureAxis->setTransform(slope(mkelvin_to_F), intercept(mkelvin_to_F));
|
2021-06-28 18:29:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 14:37:38 +00:00
|
|
|
bool ProfileScene::pointOnProfile(const QPointF &point) const
|
2021-06-28 18:29:46 +00:00
|
|
|
{
|
2021-10-26 14:37:38 +00:00
|
|
|
return timeAxis->pointInRange(point.x()) && profileYAxis->pointInRange(point.y());
|
2021-06-28 18:29:46 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 13:18:31 +00:00
|
|
|
static double max_gas(const plot_info &pi, double gas_pressures::*gas)
|
|
|
|
{
|
|
|
|
double ret = -1;
|
|
|
|
for (int i = 0; i < pi.nr; ++i) {
|
|
|
|
if (pi.entry[i].pressures.*gas > ret)
|
|
|
|
ret = pi.entry[i].pressures.*gas;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-10-06 19:01:58 +00:00
|
|
|
void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsModel *plannerModel,
|
2021-10-22 16:23:15 +00:00
|
|
|
bool inPlanner, bool instant, bool keepPlotInfo, bool calcMax, double zoom, double zoomedPosition)
|
2021-08-03 07:51:37 +00:00
|
|
|
{
|
|
|
|
d = dIn;
|
|
|
|
dc = dcIn;
|
2021-12-02 22:53:17 +00:00
|
|
|
animatedAxes.clear();
|
2021-08-03 07:51:37 +00:00
|
|
|
if (!d) {
|
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!plannerModel) {
|
|
|
|
if (decoMode(false) == VPMB)
|
2021-08-13 07:56:46 +00:00
|
|
|
decoModelParameters->set(QString("VPM-B +%1").arg(prefs.vpmb_conservatism), getColor(PRESSURE_TEXT));
|
2021-08-03 07:51:37 +00:00
|
|
|
else
|
2021-08-13 07:56:46 +00:00
|
|
|
decoModelParameters->set(QString("GF %1/%2").arg(prefs.gflow).arg(prefs.gfhigh), getColor(PRESSURE_TEXT));
|
2021-08-03 07:51:37 +00:00
|
|
|
} else {
|
|
|
|
struct diveplan &diveplan = plannerModel->getDiveplan();
|
|
|
|
if (decoMode(inPlanner) == VPMB)
|
2021-08-13 07:56:46 +00:00
|
|
|
decoModelParameters->set(QString("VPM-B +%1").arg(diveplan.vpmb_conservatism), getColor(PRESSURE_TEXT));
|
2021-08-03 07:51:37 +00:00
|
|
|
else
|
2021-08-13 07:56:46 +00:00
|
|
|
decoModelParameters->set(QString("GF %1/%2").arg(diveplan.gflow).arg(diveplan.gfhigh), getColor(PRESSURE_TEXT));
|
2021-08-03 07:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct divecomputer *currentdc = get_dive_dc_const(d, dc);
|
2021-12-04 15:15:38 +00:00
|
|
|
if (!currentdc || !currentdc->samples) {
|
|
|
|
clear();
|
2021-08-03 07:51:37 +00:00
|
|
|
return;
|
2021-12-04 15:15:38 +00:00
|
|
|
}
|
2021-08-03 07:51:37 +00:00
|
|
|
|
2022-10-30 07:55:59 +00:00
|
|
|
// If we come from the empty state, the plot info has to be recalculated.
|
|
|
|
if (empty)
|
|
|
|
keepPlotInfo = false;
|
|
|
|
empty = false;
|
|
|
|
|
2021-08-03 08:32:08 +00:00
|
|
|
int animSpeed = instant || printMode ? 0 : qPrefDisplay::animation_speed();
|
2021-08-03 07:51:37 +00:00
|
|
|
|
|
|
|
// A non-null planner_ds signals to create_plot_info_new that the dive is currently planned.
|
|
|
|
struct deco_state *planner_ds = inPlanner && plannerModel ? &plannerModel->final_deco_state : nullptr;
|
|
|
|
|
|
|
|
/* This struct holds all the data that's about to be plotted.
|
|
|
|
* I'm not sure this is the best approach ( but since we are
|
|
|
|
* interpolating some points of the Dive, maybe it is... )
|
|
|
|
* The Calculation of the points should be done per graph,
|
|
|
|
* so I'll *not* calculate everything if something is not being
|
|
|
|
* shown.
|
|
|
|
* create_plot_info_new() automatically frees old plot data.
|
|
|
|
*/
|
2021-10-22 16:23:15 +00:00
|
|
|
if (!keepPlotInfo)
|
2022-03-11 20:51:08 +00:00
|
|
|
create_plot_info_new(d, currentdc, &plotInfo, planner_ds);
|
2021-08-03 07:51:37 +00:00
|
|
|
|
2021-09-11 19:43:53 +00:00
|
|
|
bool hasHeartBeat = plotInfo.maxhr;
|
2021-10-09 21:22:42 +00:00
|
|
|
// For mobile we might want to turn of some features that are normally shown.
|
|
|
|
#ifdef SUBSURFACE_MOBILE
|
|
|
|
bool simplified = true;
|
|
|
|
#else
|
|
|
|
bool simplified = false;
|
|
|
|
#endif
|
|
|
|
updateVisibility(hasHeartBeat, simplified);
|
|
|
|
updateAxes(hasHeartBeat, simplified);
|
2021-09-11 19:43:53 +00:00
|
|
|
|
2021-08-03 07:51:37 +00:00
|
|
|
int newMaxtime = get_maxtime(&plotInfo);
|
|
|
|
if (calcMax || newMaxtime > maxtime)
|
|
|
|
maxtime = newMaxtime;
|
|
|
|
|
|
|
|
/* Only update the max. depth if it's bigger than the current ones
|
|
|
|
* when we are dragging the handler to plan / add dive.
|
|
|
|
* otherwhise, update normally.
|
|
|
|
*/
|
|
|
|
int newMaxDepth = get_maxdepth(&plotInfo);
|
|
|
|
if (!calcMax) {
|
2022-10-30 07:55:59 +00:00
|
|
|
if (maxdepth < newMaxDepth)
|
2021-08-03 07:51:37 +00:00
|
|
|
maxdepth = newMaxDepth;
|
|
|
|
} else {
|
|
|
|
maxdepth = newMaxDepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It seems that I'll have a lot of boilerplate setting the model / axis for
|
|
|
|
// each item, I'll mostly like to fix this in the future, but I'll keep at this for now.
|
2021-09-11 19:57:49 +00:00
|
|
|
profileYAxis->setBounds(0.0, maxdepth);
|
2021-08-03 08:32:08 +00:00
|
|
|
profileYAxis->updateTicks(animSpeed);
|
2021-12-02 22:53:17 +00:00
|
|
|
animatedAxes.push_back(profileYAxis);
|
2021-08-03 07:51:37 +00:00
|
|
|
|
2021-09-11 19:57:49 +00:00
|
|
|
temperatureAxis->setBounds(plotInfo.mintemp,
|
|
|
|
plotInfo.maxtemp - plotInfo.mintemp > 2000 ? plotInfo.maxtemp : plotInfo.mintemp + 2000);
|
2021-08-03 07:51:37 +00:00
|
|
|
|
2021-09-11 19:43:53 +00:00
|
|
|
if (hasHeartBeat) {
|
2021-09-26 17:10:39 +00:00
|
|
|
heartBeatAxis->setBounds(plotInfo.minhr, plotInfo.maxhr);
|
|
|
|
heartBeatAxis->updateTicks(animSpeed);
|
2021-12-02 22:53:17 +00:00
|
|
|
animatedAxes.push_back(heartBeatAxis);
|
2021-08-03 07:51:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 19:57:49 +00:00
|
|
|
percentageAxis->setBounds(0, 100);
|
2021-08-03 07:51:37 +00:00
|
|
|
percentageAxis->setVisible(false);
|
2021-08-28 18:15:57 +00:00
|
|
|
percentageAxis->updateTicks(animSpeed);
|
2021-12-02 22:53:17 +00:00
|
|
|
animatedAxes.push_back(percentageAxis);
|
2021-08-03 07:51:37 +00:00
|
|
|
|
2022-08-28 20:42:54 +00:00
|
|
|
double relStart = (1.0 - 1.0/zoom) * zoomedPosition;
|
|
|
|
double relEnd = relStart + 1.0/zoom;
|
|
|
|
timeAxis->setBounds(round(relStart * maxtime), round(relEnd * maxtime));
|
2021-08-03 07:51:37 +00:00
|
|
|
|
2021-10-08 19:00:37 +00:00
|
|
|
// Find first and last plotInfo entry
|
|
|
|
int firstSecond = lrint(timeAxis->minimum());
|
|
|
|
int lastSecond = lrint(timeAxis->maximum());
|
|
|
|
auto it1 = std::lower_bound(plotInfo.entry, plotInfo.entry + plotInfo.nr, firstSecond,
|
|
|
|
[](const plot_data &d, int s)
|
|
|
|
{ return d.sec < s; });
|
|
|
|
auto it2 = std::lower_bound(it1, plotInfo.entry + plotInfo.nr, lastSecond,
|
|
|
|
[](const plot_data &d, int s)
|
|
|
|
{ return d.sec < s; });
|
|
|
|
if (it1 > plotInfo.entry && it1->sec > firstSecond)
|
|
|
|
--it1;
|
|
|
|
if (it2 < plotInfo.entry + plotInfo.nr)
|
|
|
|
++it2;
|
|
|
|
int from = it1 - plotInfo.entry;
|
|
|
|
int to = it2 - plotInfo.entry;
|
|
|
|
|
2021-08-03 08:32:08 +00:00
|
|
|
timeAxis->updateTicks(animSpeed);
|
2021-12-02 22:53:17 +00:00
|
|
|
animatedAxes.push_back(timeAxis);
|
2021-09-11 19:57:49 +00:00
|
|
|
cylinderPressureAxis->setBounds(plotInfo.minpressure, plotInfo.maxpressure);
|
2021-08-03 07:51:37 +00:00
|
|
|
|
2022-04-03 09:23:51 +00:00
|
|
|
tankItem->setData(d, currentdc, firstSecond, lastSecond);
|
2021-08-03 07:51:37 +00:00
|
|
|
|
2021-11-13 17:05:31 +00:00
|
|
|
if (ppGraphsEnabled(currentdc, simplified)) {
|
2021-12-03 18:04:33 +00:00
|
|
|
double max = prefs.pp_graphs.phe ? max_gas(plotInfo, &gas_pressures::he) : -1;
|
2021-09-26 17:30:27 +00:00
|
|
|
if (prefs.pp_graphs.pn2)
|
2021-12-03 18:04:33 +00:00
|
|
|
max = std::max(max_gas(plotInfo, &gas_pressures::n2), max);
|
2021-09-26 17:30:27 +00:00
|
|
|
if (prefs.pp_graphs.po2)
|
2021-12-03 18:04:33 +00:00
|
|
|
max = std::max(max_gas(plotInfo, &gas_pressures::o2), max);
|
2021-09-26 17:30:27 +00:00
|
|
|
|
|
|
|
gasYAxis->setBounds(0.0, max);
|
|
|
|
gasYAxis->updateTicks(animSpeed);
|
2021-12-02 22:53:17 +00:00
|
|
|
animatedAxes.push_back(gasYAxis);
|
2021-09-26 17:30:27 +00:00
|
|
|
}
|
2021-08-03 07:51:37 +00:00
|
|
|
|
|
|
|
// Replot dive items
|
|
|
|
for (AbstractProfilePolygonItem *item: profileItems)
|
2021-10-08 19:00:37 +00:00
|
|
|
item->replot(d, from, to, inPlanner);
|
2021-08-03 07:51:37 +00:00
|
|
|
|
2021-08-29 20:13:26 +00:00
|
|
|
if (prefs.percentagegraph)
|
2021-12-03 18:04:33 +00:00
|
|
|
percentageItem->replot(d, currentdc, plotInfo);
|
2021-08-29 20:13:26 +00:00
|
|
|
|
2021-08-03 07:51:37 +00:00
|
|
|
// The event items are a bit special since we don't know how many events are going to
|
|
|
|
// exist on a dive, so I cant create cache items for that. that's why they are here
|
|
|
|
// while all other items are up there on the constructor.
|
|
|
|
qDeleteAll(eventItems);
|
|
|
|
eventItems.clear();
|
|
|
|
struct event *event = currentdc->events;
|
2022-03-11 20:51:08 +00:00
|
|
|
struct gasmix lastgasmix = get_gasmix_at_time(d, currentdc, duration_t{1});
|
2021-08-03 07:51:37 +00:00
|
|
|
|
|
|
|
while (event) {
|
|
|
|
// if print mode is selected only draw headings, SP change, gas events or bookmark event
|
|
|
|
if (printMode) {
|
|
|
|
if (empty_string(event->name) ||
|
|
|
|
!(strcmp(event->name, "heading") == 0 ||
|
|
|
|
(same_string(event->name, "SP change") && event->time.seconds == 0) ||
|
|
|
|
event_is_gaschange(event) ||
|
|
|
|
event->type == SAMPLE_EVENT_BOOKMARK)) {
|
|
|
|
event = event->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2021-10-09 13:05:27 +00:00
|
|
|
if (DiveEventItem::isInteresting(d, currentdc, event, plotInfo, firstSecond, lastSecond)) {
|
2021-08-31 18:44:37 +00:00
|
|
|
auto item = new DiveEventItem(d, event, lastgasmix, plotInfo,
|
2021-08-31 18:10:24 +00:00
|
|
|
timeAxis, profileYAxis, animSpeed, *pixmaps);
|
|
|
|
item->setZValue(2);
|
|
|
|
addItem(item);
|
|
|
|
eventItems.push_back(item);
|
|
|
|
}
|
2021-08-03 07:51:37 +00:00
|
|
|
if (event_is_gaschange(event))
|
|
|
|
lastgasmix = get_gasmix_from_event(d, event);
|
|
|
|
event = event->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString dcText = get_dc_nickname(currentdc);
|
|
|
|
if (dcText == "planned dive")
|
|
|
|
dcText = tr("Planned dive");
|
|
|
|
else if (dcText == "manually added dive")
|
|
|
|
dcText = tr("Manually added dive");
|
|
|
|
else if (dcText.isEmpty())
|
|
|
|
dcText = tr("Unknown dive computer");
|
2022-09-17 15:19:11 +00:00
|
|
|
int nr = number_of_computers(d);
|
|
|
|
if (nr > 1)
|
2021-08-03 07:51:37 +00:00
|
|
|
dcText += tr(" (#%1 of %2)").arg(dc + 1).arg(nr);
|
2021-08-13 07:56:46 +00:00
|
|
|
diveComputerText->set(dcText, getColor(TIME_TEXT, isGrayscale));
|
2021-12-02 22:53:17 +00:00
|
|
|
|
|
|
|
// Reset animation.
|
|
|
|
if (animSpeed <= 0)
|
|
|
|
animation.reset();
|
|
|
|
else
|
|
|
|
animation = std::make_unique<ProfileAnimation>(*this, animSpeed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileScene::anim(double fraction)
|
|
|
|
{
|
|
|
|
for (DiveCartesianAxis *axis: animatedAxes)
|
|
|
|
axis->anim(fraction);
|
2021-08-03 07:51:37 +00:00
|
|
|
}
|
2021-08-04 05:27:41 +00:00
|
|
|
|
2021-08-30 20:59:45 +00:00
|
|
|
void ProfileScene::draw(QPainter *painter, const QRect &pos,
|
|
|
|
const struct dive *d, int dc,
|
|
|
|
DivePlannerPointsModel *plannerModel, bool inPlanner)
|
2021-08-04 05:27:41 +00:00
|
|
|
{
|
2021-08-30 20:59:45 +00:00
|
|
|
QSize size = pos.size();
|
|
|
|
resize(QSizeF(size));
|
2022-01-20 14:42:22 +00:00
|
|
|
plotDive(d, dc, plannerModel, inPlanner, true, false, true);
|
2021-08-04 05:27:41 +00:00
|
|
|
|
2021-08-30 20:59:45 +00:00
|
|
|
QImage image(pos.size(), QImage::Format_ARGB32);
|
2021-08-04 05:27:41 +00:00
|
|
|
image.fill(getColor(::BACKGROUND, isGrayscale));
|
|
|
|
|
|
|
|
QPainter imgPainter(&image);
|
|
|
|
imgPainter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
imgPainter.setRenderHint(QPainter::SmoothPixmapTransform);
|
2021-08-30 20:59:45 +00:00
|
|
|
render(&imgPainter, QRect(QPoint(), size), sceneRect(), Qt::IgnoreAspectRatio);
|
2021-08-04 05:27:41 +00:00
|
|
|
imgPainter.end();
|
|
|
|
|
|
|
|
if (isGrayscale) {
|
|
|
|
// convert QImage to grayscale before rendering
|
|
|
|
for (int i = 0; i < image.height(); i++) {
|
|
|
|
QRgb *pixel = reinterpret_cast<QRgb *>(image.scanLine(i));
|
|
|
|
QRgb *end = pixel + image.width();
|
|
|
|
for (; pixel != end; pixel++) {
|
|
|
|
int gray_val = qGray(*pixel);
|
|
|
|
*pixel = QColor(gray_val, gray_val, gray_val).rgb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 20:59:45 +00:00
|
|
|
painter->drawImage(pos, image);
|
2021-08-04 05:27:41 +00:00
|
|
|
}
|
2022-08-28 20:42:54 +00:00
|
|
|
|
|
|
|
// Calculate the new zoom position when the mouse is dragged by delta.
|
|
|
|
// This is annoyingly complex, because the zoom position is given as
|
|
|
|
// a real between 0 and 1.
|
|
|
|
double ProfileScene::calcZoomPosition(double zoom, double originalPos, double delta)
|
|
|
|
{
|
|
|
|
double factor = 1.0 - 1.0/zoom;
|
|
|
|
if (nearly_0(factor))
|
|
|
|
return 0.0;
|
|
|
|
double relStart = factor * originalPos;
|
|
|
|
double start = relStart * maxtime;
|
|
|
|
double newStart = start + timeAxis->deltaToValue(delta);
|
|
|
|
double newRelStart = newStart / maxtime;
|
|
|
|
double newPos = newRelStart / factor;
|
|
|
|
return std::clamp(newPos, 0.0, 1.0);
|
|
|
|
}
|