aboutsummaryrefslogtreecommitdiffstats
path: root/qt-models/tankinfomodel.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2020-12-11 22:34:35 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2020-12-13 11:49:59 -0800
commit50b11024d685129e78c36313b892dbc0e55c654c (patch)
tree96d56180e18ae2e11a7e4f3a6d86944883738797 /qt-models/tankinfomodel.cpp
parent2e328c7633c1de43af000fd6506385c651365a0d (diff)
downloadsubsurface-50b11024d685129e78c36313b892dbc0e55c654c.tar.gz
core: keep tank infos in a dynamic table
The list of known tank types were kept in a fixed size table. Instead, use a dynamic table with our horrendous table macros. This is more flexible and sensible. While doing this, clean up the TankInfoModel, which was leaking memory. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'qt-models/tankinfomodel.cpp')
-rw-r--r--qt-models/tankinfomodel.cpp55
1 files changed, 22 insertions, 33 deletions
diff --git a/qt-models/tankinfomodel.cpp b/qt-models/tankinfomodel.cpp
index 76d921c71..0a9379ec9 100644
--- a/qt-models/tankinfomodel.cpp
+++ b/qt-models/tankinfomodel.cpp
@@ -13,8 +13,9 @@ TankInfoModel *TankInfoModel::instance()
bool TankInfoModel::insertRows(int, int count, const QModelIndex &parent)
{
- beginInsertRows(parent, rowCount(), rowCount());
- rows += count;
+ beginInsertRows(parent, rowCount(), rowCount() + count - 1);
+ for (int i = 0; i < count; ++i)
+ add_tank_info_metric(&tank_info_table, "", 0, 0);
endInsertRows();
return true;
}
@@ -23,64 +24,55 @@ bool TankInfoModel::setData(const QModelIndex &index, const QVariant &value, int
{
//WARN Seems wrong, we need to check for role == Qt::EditRole
- if (index.row() < 0 || index.row() > MAX_TANK_INFO - 1)
+ if (index.row() < 0 || index.row() >= tank_info_table.nr )
return false;
- struct tank_info_t *info = &tank_info[index.row()];
+ struct tank_info &info = tank_info_table.infos[index.row()];
switch (index.column()) {
case DESCRIPTION:
- info->name = strdup(value.toByteArray().data());
+ free((void *)info.name);
+ info.name = strdup(value.toByteArray().data());
break;
case ML:
- info->ml = value.toInt();
+ info.ml = value.toInt();
break;
case BAR:
- info->bar = value.toInt();
+ 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;
- if (!index.isValid() || index.row() < 0 || index.row() > MAX_TANK_INFO - 1) {
- return ret;
- }
- if (role == Qt::FontRole) {
+ if (!index.isValid() || index.row() < 0 || index.row() >= tank_info_table.nr)
+ return QVariant();
+ 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;
+ const struct tank_info &info = tank_info_table.infos[index.row()];
+ int ml = info.ml;
+ double bar = (info.psi) ? psi_to_bar(info.psi) : info.bar;
- if (info->cuft && info->psi)
- ml = lrint(cuft_to_l(info->cuft) * 1000 / bar_to_atm(bar));
+ if (info.cuft && info.psi)
+ ml = lrint(cuft_to_l(info.cuft) * 1000 / bar_to_atm(bar));
switch (index.column()) {
case BAR:
- ret = bar * 1000;
- break;
+ return bar * 1000;
case ML:
- ret = ml;
- break;
+ return ml;
case DESCRIPTION:
- ret = QString(info->name);
- break;
+ return info.name;
}
}
- return ret;
+ return QVariant();
}
int TankInfoModel::rowCount(const QModelIndex&) const
{
- return rows;
+ return tank_info_table.nr;
}
TankInfoModel::TankInfoModel()
@@ -94,8 +86,5 @@ TankInfoModel::TankInfoModel()
void TankInfoModel::update()
{
beginResetModel();
- rows = 0;
- for (struct tank_info_t *info = tank_info; info->name && info < tank_info + MAX_TANK_INFO; info++, rows++)
- ;
endResetModel();
}