2014-08-15 00:22:27 +00:00
|
|
|
#include "tankitem.h"
|
|
|
|
#include "diveplotdatamodel.h"
|
2014-08-15 03:40:47 +00:00
|
|
|
#include "divetextitem.h"
|
2014-08-15 00:22:27 +00:00
|
|
|
#include "profile.h"
|
2014-08-18 16:47:18 +00:00
|
|
|
#include <QPen>
|
2014-08-15 00:22:27 +00:00
|
|
|
|
|
|
|
TankItem::TankItem(QObject *parent) :
|
|
|
|
QGraphicsRectItem(),
|
|
|
|
dataModel(0),
|
2014-08-23 14:13:37 +00:00
|
|
|
pInfoEntry(0),
|
|
|
|
pInfoNr(0)
|
2014-08-15 00:22:27 +00:00
|
|
|
{
|
2014-08-15 03:40:47 +00:00
|
|
|
height = 3;
|
|
|
|
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;
|
2014-08-23 14:13:37 +00:00
|
|
|
memset(&diveCylinderStore, 0, sizeof(diveCylinderStore));
|
2014-08-15 00:22:27 +00:00
|
|
|
}
|
|
|
|
|
2014-12-18 07:47:53 +00:00
|
|
|
TankItem::~TankItem()
|
|
|
|
{
|
|
|
|
// Should this be clear_dive(diveCylinderStore)?
|
|
|
|
for (int i = 0; i < MAX_CYLINDERS; i++)
|
|
|
|
free((void *)diveCylinderStore.cylinder[i].type.description);
|
|
|
|
}
|
|
|
|
|
2014-08-15 00:22:27 +00:00
|
|
|
void TankItem::setData(DivePlotDataModel *model, struct plot_info *plotInfo, struct dive *d)
|
|
|
|
{
|
2014-08-23 14:13:37 +00:00
|
|
|
free(pInfoEntry);
|
|
|
|
// the plotInfo and dive structures passed in could become invalid before we stop using them,
|
|
|
|
// so copy the data that we need
|
|
|
|
int size = plotInfo->nr * sizeof(plotInfo->entry[0]);
|
|
|
|
pInfoEntry = (struct plot_data *)malloc(size);
|
|
|
|
pInfoNr = plotInfo->nr;
|
|
|
|
memcpy(pInfoEntry, plotInfo->entry, size);
|
|
|
|
copy_cylinders(d, &diveCylinderStore, false);
|
2014-08-15 00:22:27 +00:00
|
|
|
dataModel = model;
|
2015-01-18 21:17:24 +00:00
|
|
|
connect(dataModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(modelDataChanged(QModelIndex, QModelIndex)), Qt::UniqueConnection);
|
2014-08-15 00:22:27 +00:00
|
|
|
modelDataChanged();
|
|
|
|
}
|
|
|
|
|
2014-08-15 03:40:47 +00:00
|
|
|
void TankItem::createBar(qreal x, qreal w, struct gasmix *gas)
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
else if (gas->he.permille)
|
|
|
|
rect->setBrush(trimix);
|
2015-01-05 19:24:34 +00:00
|
|
|
else if (gas->o2.permille == 1000)
|
|
|
|
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);
|
|
|
|
label->setZValue(101);
|
|
|
|
}
|
|
|
|
|
2014-08-15 00:22:27 +00:00
|
|
|
void TankItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
|
|
|
|
{
|
|
|
|
// We don't have enougth data to calculate things, quit.
|
|
|
|
|
2014-08-23 14:13:37 +00:00
|
|
|
if (!dataModel || !pInfoEntry || !pInfoNr)
|
2014-08-15 00:22:27 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// remove the old rectangles
|
|
|
|
foreach (QGraphicsRectItem *r, rects) {
|
|
|
|
delete(r);
|
|
|
|
}
|
|
|
|
rects.clear();
|
|
|
|
|
|
|
|
// walk the list and figure out which tanks go where
|
2014-08-23 14:13:37 +00:00
|
|
|
struct plot_data *entry = pInfoEntry;
|
2014-08-15 00:22:27 +00:00
|
|
|
int cylIdx = entry->cylinderindex;
|
|
|
|
int i = -1;
|
|
|
|
int startTime = 0;
|
2014-08-23 14:13:37 +00:00
|
|
|
struct gasmix *gas = &diveCylinderStore.cylinder[cylIdx].gasmix;
|
2014-08-15 03:40:47 +00:00
|
|
|
qreal width, left;
|
2014-08-23 14:13:37 +00:00
|
|
|
while (++i < pInfoNr) {
|
|
|
|
entry = &pInfoEntry[i];
|
2014-08-15 00:22:27 +00:00
|
|
|
if (entry->cylinderindex == cylIdx)
|
|
|
|
continue;
|
|
|
|
width = hAxis->posAtValue(entry->sec) - hAxis->posAtValue(startTime);
|
|
|
|
left = hAxis->posAtValue(startTime);
|
2014-08-15 03:40:47 +00:00
|
|
|
createBar(left, width, gas);
|
2014-08-15 00:22:27 +00:00
|
|
|
cylIdx = entry->cylinderindex;
|
2014-08-23 14:13:37 +00:00
|
|
|
gas = &diveCylinderStore.cylinder[cylIdx].gasmix;
|
2014-08-15 00:22:27 +00:00
|
|
|
startTime = entry->sec;
|
|
|
|
}
|
|
|
|
width = hAxis->posAtValue(entry->sec) - hAxis->posAtValue(startTime);
|
|
|
|
left = hAxis->posAtValue(startTime);
|
2014-08-15 03:40:47 +00:00
|
|
|
createBar(left, width, gas);
|
2014-08-15 00:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TankItem::setHorizontalAxis(DiveCartesianAxis *horizontal)
|
|
|
|
{
|
|
|
|
hAxis = horizontal;
|
|
|
|
connect(hAxis, SIGNAL(sizeChanged()), this, SLOT(modelDataChanged()));
|
|
|
|
modelDataChanged();
|
|
|
|
}
|