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/tankitem.h"
|
2020-12-23 11:39:07 +00:00
|
|
|
#include "profile-widget/divecartesianaxis.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "profile-widget/divetextitem.h"
|
2020-12-23 11:39:07 +00:00
|
|
|
#include "core/dive.h"
|
2020-10-25 08:14:16 +00:00
|
|
|
#include "core/event.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/profile.h"
|
2014-08-18 16:47:18 +00:00
|
|
|
#include <QPen>
|
2021-08-28 21:36:09 +00:00
|
|
|
#include <QFontMetrics>
|
2014-08-15 00:22:27 +00:00
|
|
|
|
2021-08-28 21:36:09 +00:00
|
|
|
static const double border = 1.0;
|
2019-10-27 17:32:04 +00:00
|
|
|
|
2021-08-09 14:48:08 +00:00
|
|
|
TankItem::TankItem(const DiveCartesianAxis &axis, double dpr) :
|
2020-12-23 11:33:05 +00:00
|
|
|
hAxis(axis),
|
2021-08-09 14:48:08 +00:00
|
|
|
dpr(dpr)
|
2014-08-15 00:22:27 +00:00
|
|
|
{
|
2014-08-15 03:40:47 +00:00
|
|
|
QColor red(PERSIANRED1);
|
|
|
|
QColor blue(AIR_BLUE);
|
|
|
|
QColor yellow(NITROX_YELLOW);
|
|
|
|
QColor green(NITROX_GREEN);
|
2021-08-28 21:36:09 +00:00
|
|
|
QLinearGradient nitroxGradient(QPointF(0, 0), QPointF(0, height()));
|
2014-08-15 03:40:47 +00:00
|
|
|
nitroxGradient.setColorAt(0.0, green);
|
|
|
|
nitroxGradient.setColorAt(0.49, green);
|
|
|
|
nitroxGradient.setColorAt(0.5, yellow);
|
|
|
|
nitroxGradient.setColorAt(1.0, yellow);
|
|
|
|
nitrox = nitroxGradient;
|
2015-01-05 19:24:34 +00:00
|
|
|
oxygen = green;
|
2021-08-28 21:36:09 +00:00
|
|
|
QLinearGradient trimixGradient(QPointF(0, 0), QPointF(0, height()));
|
2014-08-15 03:40:47 +00:00
|
|
|
trimixGradient.setColorAt(0.0, green);
|
|
|
|
trimixGradient.setColorAt(0.49, green);
|
|
|
|
trimixGradient.setColorAt(0.5, red);
|
|
|
|
trimixGradient.setColorAt(1.0, red);
|
|
|
|
trimix = trimixGradient;
|
|
|
|
air = blue;
|
2014-12-18 07:47:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 21:36:09 +00:00
|
|
|
double TankItem::height() const
|
|
|
|
{
|
2021-11-13 17:05:31 +00:00
|
|
|
return DiveTextItem::fontHeight(dpr, 1.0) + 2.0 * border * dpr;
|
2021-08-28 21:36:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 16:48:04 +00:00
|
|
|
void TankItem::createBar(int startTime, int stopTime, struct gasmix gas)
|
2014-08-15 03:40:47 +00:00
|
|
|
{
|
2020-12-23 11:33:05 +00:00
|
|
|
qreal x = hAxis.posAtValue(startTime);
|
|
|
|
qreal w = hAxis.posAtValue(stopTime) - hAxis.posAtValue(startTime);
|
2019-10-27 16:48:04 +00:00
|
|
|
|
2014-08-15 03:40:47 +00:00
|
|
|
// pick the right gradient, size, position and text
|
2021-08-28 21:36:09 +00:00
|
|
|
QGraphicsRectItem *rect = new QGraphicsRectItem(x, 0, w, height(), this);
|
2014-08-15 03:40:47 +00:00
|
|
|
if (gasmix_is_air(gas))
|
|
|
|
rect->setBrush(air);
|
2018-08-16 17:10:10 +00:00
|
|
|
else if (gas.he.permille)
|
2014-08-15 03:40:47 +00:00
|
|
|
rect->setBrush(trimix);
|
2018-08-16 17:10:10 +00:00
|
|
|
else if (gas.o2.permille == 1000)
|
2015-01-05 19:24:34 +00:00
|
|
|
rect->setBrush(oxygen);
|
2014-08-15 03:40:47 +00:00
|
|
|
else
|
|
|
|
rect->setBrush(nitrox);
|
2014-08-18 16:47:18 +00:00
|
|
|
rect->setPen(QPen(QBrush(), 0.0)); // get rid of the thick line around the rectangle
|
2014-08-15 03:40:47 +00:00
|
|
|
rects.push_back(rect);
|
2021-08-28 21:36:09 +00:00
|
|
|
DiveTextItem *label = new DiveTextItem(dpr, 1.0, Qt::AlignVCenter | Qt::AlignRight, rect);
|
2021-08-13 07:56:46 +00:00
|
|
|
label->set(gasname(gas), Qt::black);
|
2021-08-28 21:36:09 +00:00
|
|
|
label->setPos(x + 2.0 * dpr, height() / 2.0);
|
2014-08-15 03:40:47 +00:00
|
|
|
label->setZValue(101);
|
|
|
|
}
|
|
|
|
|
2022-04-03 09:23:51 +00:00
|
|
|
void TankItem::setData(const struct dive *d, const struct divecomputer *dc, int plotStartTime, int plotEndTime)
|
2014-08-15 00:22:27 +00:00
|
|
|
{
|
2021-10-08 19:28:32 +00:00
|
|
|
// remove the old rectangles
|
|
|
|
qDeleteAll(rects);
|
|
|
|
rects.clear();
|
2021-01-03 20:57:59 +00:00
|
|
|
|
2022-04-03 09:23:51 +00:00
|
|
|
if (!d || !dc)
|
2021-01-03 20:57:59 +00:00
|
|
|
return;
|
|
|
|
|
2014-08-15 00:22:27 +00:00
|
|
|
// We don't have enougth data to calculate things, quit.
|
2021-10-08 19:28:32 +00:00
|
|
|
if (plotEndTime < 0 || plotEndTime <= plotStartTime)
|
2014-08-15 00:22:27 +00:00
|
|
|
return;
|
|
|
|
|
2019-08-04 17:39:47 +00:00
|
|
|
// Bail if there are no cylinders
|
2024-05-28 19:31:11 +00:00
|
|
|
if (d->cylinders.empty())
|
2019-08-04 17:39:47 +00:00
|
|
|
return;
|
|
|
|
|
2023-02-03 06:09:54 +00:00
|
|
|
// start with the first gasmix and at the start of the plotted range
|
2024-05-25 06:16:57 +00:00
|
|
|
event_loop loop("gaschange");
|
|
|
|
struct gasmix gasmix = gasmix_invalid;
|
|
|
|
const struct event *ev;
|
|
|
|
while ((ev = loop.next(*dc)) != nullptr && ev->time.seconds <= plotStartTime)
|
2024-06-25 05:43:32 +00:00
|
|
|
gasmix = d->get_gasmix_from_event(*ev);
|
2021-10-08 19:28:32 +00:00
|
|
|
|
|
|
|
// work through all the gas changes and add the rectangle for each gas while it was used
|
|
|
|
int startTime = plotStartTime;
|
2019-10-27 17:08:07 +00:00
|
|
|
while (ev && (int)ev->time.seconds < plotEndTime) {
|
2019-10-27 16:48:04 +00:00
|
|
|
createBar(startTime, ev->time.seconds, gasmix);
|
2017-09-14 03:29:20 +00:00
|
|
|
startTime = ev->time.seconds;
|
2024-06-25 05:43:32 +00:00
|
|
|
gasmix = d->get_gasmix_from_event(*ev);
|
2024-05-25 06:16:57 +00:00
|
|
|
ev = loop.next(*dc);
|
2014-08-15 00:22:27 +00:00
|
|
|
}
|
2019-10-27 17:08:07 +00:00
|
|
|
createBar(startTime, plotEndTime, gasmix);
|
2014-08-15 00:22:27 +00:00
|
|
|
}
|