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