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"
|
|
|
|
#include "qt-models/diveplotdatamodel.h"
|
|
|
|
#include "profile-widget/divetextitem.h"
|
|
|
|
#include "core/profile.h"
|
2014-08-18 16:47:18 +00:00
|
|
|
#include <QPen>
|
2014-08-15 00:22:27 +00:00
|
|
|
|
2019-10-27 17:32:04 +00:00
|
|
|
static const qreal height = 3.0;
|
|
|
|
|
2014-08-15 00:22:27 +00:00
|
|
|
TankItem::TankItem(QObject *parent) :
|
2016-03-08 05:23:54 +00:00
|
|
|
QObject(parent),
|
2019-10-27 17:08:07 +00:00
|
|
|
plotEndTime(-1)
|
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);
|
2014-08-15 13:30:31 +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;
|
2014-08-15 13:30:31 +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;
|
2019-04-01 21:11:19 +00:00
|
|
|
hAxis = nullptr;
|
2014-12-18 07:47:53 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 00:22:27 +00:00
|
|
|
void TankItem::setData(DivePlotDataModel *model, struct plot_info *plotInfo, struct dive *d)
|
|
|
|
{
|
2019-10-27 17:08:07 +00:00
|
|
|
// If there is nothing to plot, quit early.
|
|
|
|
if (plotInfo->nr <= 0) {
|
|
|
|
plotEndTime = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find correct end of the dive plot for correct end of the tankbar.
|
|
|
|
struct plot_data *last_entry = &plotInfo->entry[plotInfo->nr - 1];
|
|
|
|
plotEndTime = last_entry->sec;
|
|
|
|
|
|
|
|
// Stay informed of changes to the tanks.
|
2019-10-27 14:22:34 +00:00
|
|
|
connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(modelDataChanged(QModelIndex, QModelIndex)), Qt::UniqueConnection);
|
2019-10-27 17:08:07 +00:00
|
|
|
|
2014-08-15 00:22:27 +00:00
|
|
|
modelDataChanged();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2019-10-27 16:48:04 +00:00
|
|
|
qreal x = hAxis->posAtValue(startTime);
|
|
|
|
qreal w = hAxis->posAtValue(stopTime) - hAxis->posAtValue(startTime);
|
|
|
|
|
2014-08-15 03:40:47 +00:00
|
|
|
// pick the right gradient, size, position and text
|
2014-08-15 13:30:31 +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);
|
|
|
|
DiveTextItem *label = new DiveTextItem(rect);
|
|
|
|
label->setText(gasname(gas));
|
|
|
|
label->setBrush(Qt::black);
|
2014-08-15 13:30:31 +00:00
|
|
|
label->setPos(x + 1, 0);
|
2014-08-15 03:40:47 +00:00
|
|
|
label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
|
2017-02-04 09:46:02 +00:00
|
|
|
#ifdef SUBSURFACE_MOBILE
|
|
|
|
label->setPos(x + 1, -2.5);
|
|
|
|
#endif
|
2014-08-15 03:40:47 +00:00
|
|
|
label->setZValue(101);
|
|
|
|
}
|
|
|
|
|
2018-05-21 16:20:29 +00:00
|
|
|
void TankItem::modelDataChanged(const QModelIndex&, const QModelIndex&)
|
2014-08-15 00:22:27 +00:00
|
|
|
{
|
|
|
|
// We don't have enougth data to calculate things, quit.
|
2019-10-27 17:08:07 +00:00
|
|
|
if (plotEndTime < 0)
|
2014-08-15 00:22:27 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// remove the old rectangles
|
2019-04-03 18:25:35 +00:00
|
|
|
qDeleteAll(rects);
|
2014-08-15 00:22:27 +00:00
|
|
|
rects.clear();
|
|
|
|
|
2019-08-04 17:39:47 +00:00
|
|
|
// Bail if there are no cylinders
|
|
|
|
if (displayed_dive.cylinders.nr <= 0)
|
|
|
|
return;
|
|
|
|
|
2017-09-14 03:29:20 +00:00
|
|
|
// get the information directly from the displayed_dive (the dc always exists)
|
|
|
|
struct divecomputer *dc = get_dive_dc(&displayed_dive, dc_number);
|
|
|
|
|
|
|
|
// start with the first gasmix and at the start of the dive
|
|
|
|
int cyl = explicit_first_cylinder(&displayed_dive, dc);
|
2019-08-04 20:13:49 +00:00
|
|
|
struct gasmix gasmix = get_cylinder(&displayed_dive, cyl)->gasmix;
|
2017-09-14 03:29:20 +00:00
|
|
|
int startTime = 0;
|
|
|
|
|
|
|
|
// work through all the gas changes and add the rectangle for each gas while it was used
|
2018-08-16 22:58:30 +00:00
|
|
|
const struct event *ev = get_next_event(dc->events, "gaschange");
|
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;
|
|
|
|
gasmix = get_gasmix_from_event(&displayed_dive, ev);
|
|
|
|
ev = get_next_event(ev->next, "gaschange");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void TankItem::setHorizontalAxis(DiveCartesianAxis *horizontal)
|
|
|
|
{
|
|
|
|
hAxis = horizontal;
|
|
|
|
connect(hAxis, SIGNAL(sizeChanged()), this, SLOT(modelDataChanged()));
|
|
|
|
modelDataChanged();
|
|
|
|
}
|