2017-04-27 18:25:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "qt-models/tankinfomodel.h"
|
|
|
|
#include "core/dive.h"
|
|
|
|
#include "core/gettextfromc.h"
|
|
|
|
#include "core/metrics.h"
|
2015-05-28 19:39:15 +00:00
|
|
|
|
|
|
|
TankInfoModel *TankInfoModel::instance()
|
|
|
|
{
|
2017-12-20 19:00:26 +00:00
|
|
|
static TankInfoModel self;
|
|
|
|
return &self;
|
2015-05-28 19:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString &TankInfoModel::biggerString() const
|
|
|
|
{
|
|
|
|
return biggerEntry;
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
bool TankInfoModel::insertRows(int, int count, const QModelIndex &parent)
|
2015-05-28 19:39:15 +00:00
|
|
|
{
|
|
|
|
beginInsertRows(parent, rowCount(), rowCount());
|
|
|
|
rows += count;
|
|
|
|
endInsertRows();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
bool TankInfoModel::setData(const QModelIndex &index, const QVariant &value, int)
|
2015-05-28 19:39:15 +00:00
|
|
|
{
|
2016-03-08 05:28:00 +00:00
|
|
|
//WARN Seems wrong, we need to check for role == Qt::EditRole
|
2017-06-13 22:45:18 +00:00
|
|
|
|
|
|
|
if (index.row() < 0 || index.row() > MAX_TANK_INFO - 1)
|
|
|
|
return false;
|
|
|
|
|
2015-05-28 19:39:15 +00:00
|
|
|
struct tank_info_t *info = &tank_info[index.row()];
|
|
|
|
switch (index.column()) {
|
|
|
|
case DESCRIPTION:
|
|
|
|
info->name = strdup(value.toByteArray().data());
|
|
|
|
break;
|
|
|
|
case ML:
|
|
|
|
info->ml = value.toInt();
|
|
|
|
break;
|
|
|
|
case BAR:
|
|
|
|
info->bar = value.toInt();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TankInfoModel::clear()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant TankInfoModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
QVariant ret;
|
2017-06-13 22:45:18 +00:00
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() > MAX_TANK_INFO - 1) {
|
2015-05-28 19:39:15 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (role == Qt::FontRole) {
|
|
|
|
return defaultModelFont();
|
|
|
|
}
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
|
|
|
struct tank_info_t *info = &tank_info[index.row()];
|
|
|
|
int ml = info->ml;
|
|
|
|
double bar = (info->psi) ? psi_to_bar(info->psi) : info->bar;
|
|
|
|
|
|
|
|
if (info->cuft && info->psi)
|
2017-03-23 01:13:49 +00:00
|
|
|
ml = lrint(cuft_to_l(info->cuft) * 1000 / bar_to_atm(bar));
|
2015-05-28 19:39:15 +00:00
|
|
|
|
|
|
|
switch (index.column()) {
|
|
|
|
case BAR:
|
|
|
|
ret = bar * 1000;
|
|
|
|
break;
|
|
|
|
case ML:
|
|
|
|
ret = ml;
|
|
|
|
break;
|
|
|
|
case DESCRIPTION:
|
|
|
|
ret = QString(info->name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int TankInfoModel::rowCount(const QModelIndex&) const
|
2015-05-28 19:39:15 +00:00
|
|
|
{
|
|
|
|
return rows + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TankInfoModel::TankInfoModel() : rows(-1)
|
|
|
|
{
|
|
|
|
setHeaderDataStrings(QStringList() << tr("Description") << tr("ml") << tr("bar"));
|
|
|
|
struct tank_info_t *info = tank_info;
|
|
|
|
for (info = tank_info; info->name; info++, rows++) {
|
2018-06-17 15:55:47 +00:00
|
|
|
QString infoName = gettextFromC::tr(info->name);
|
2015-05-28 19:39:15 +00:00
|
|
|
if (infoName.count() > biggerEntry.count())
|
|
|
|
biggerEntry = infoName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rows > -1) {
|
|
|
|
beginInsertRows(QModelIndex(), 0, rows);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TankInfoModel::update()
|
|
|
|
{
|
|
|
|
if (rows > -1) {
|
|
|
|
beginRemoveRows(QModelIndex(), 0, rows);
|
|
|
|
endRemoveRows();
|
|
|
|
rows = -1;
|
|
|
|
}
|
|
|
|
struct tank_info_t *info = tank_info;
|
|
|
|
for (info = tank_info; info->name; info++, rows++)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (rows > -1) {
|
|
|
|
beginInsertRows(QModelIndex(), 0, rows);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
}
|