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"
|
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs
were calling into the dive-list models, which then reset the
core data structures, themselves and the unrelated
locationinformation model. The UI code then reset various other
things, such as the TankInformation model or the map. . This was
unsatisfying from a control-flow perspective, as the models should
display the core data, not act on it. Moreover, this meant lots
of intricate intermodule-dependencies.
Thus, straighten up the control flow: give the C core the
possibility to send a "all data reset" event. And do that
in those functions that reset the core data structures.
Let each module react to this event by itself. This removes
inter-module dependencies. For example, the MainWindow now
doesn't have to reset the TankInfoModel or the MapWidget.
Then, to reset the core data structures, let the UI code
simply directly call the respective core functions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 22:12:36 +00:00
|
|
|
#include "core/subsurface-qt/divelistnotifier.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-12-11 21:34:35 +00:00
|
|
|
beginInsertRows(parent, rowCount(), rowCount() + count - 1);
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
add_tank_info_metric(&tank_info_table, "", 0, 0);
|
2015-05-28 19:39:15 +00:00
|
|
|
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
|
|
|
|
2020-12-11 21:34:35 +00:00
|
|
|
if (index.row() < 0 || index.row() >= tank_info_table.nr )
|
2017-06-13 22:45:18 +00:00
|
|
|
return false;
|
|
|
|
|
2020-12-11 21:34:35 +00:00
|
|
|
struct tank_info &info = tank_info_table.infos[index.row()];
|
2015-05-28 19:39:15 +00:00
|
|
|
switch (index.column()) {
|
|
|
|
case DESCRIPTION:
|
2020-12-11 21:34:35 +00:00
|
|
|
free((void *)info.name);
|
|
|
|
info.name = strdup(value.toByteArray().data());
|
2015-05-28 19:39:15 +00:00
|
|
|
break;
|
|
|
|
case ML:
|
2020-12-11 21:34:35 +00:00
|
|
|
info.ml = value.toInt();
|
2015-05-28 19:39:15 +00:00
|
|
|
break;
|
|
|
|
case BAR:
|
2020-12-11 21:34:35 +00:00
|
|
|
info.bar = value.toInt();
|
2015-05-28 19:39:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant TankInfoModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2020-12-11 21:34:35 +00:00
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= tank_info_table.nr)
|
|
|
|
return QVariant();
|
|
|
|
if (role == Qt::FontRole)
|
2015-05-28 19:39:15 +00:00
|
|
|
return defaultModelFont();
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
2020-12-11 21:34:35 +00:00
|
|
|
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;
|
2015-05-28 19:39:15 +00:00
|
|
|
|
2020-12-11 21:34:35 +00:00
|
|
|
if (info.cuft && info.psi)
|
|
|
|
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:
|
2020-12-11 21:34:35 +00:00
|
|
|
return bar * 1000;
|
2015-05-28 19:39:15 +00:00
|
|
|
case ML:
|
2020-12-11 21:34:35 +00:00
|
|
|
return ml;
|
2015-05-28 19:39:15 +00:00
|
|
|
case DESCRIPTION:
|
2020-12-11 21:34:35 +00:00
|
|
|
return info.name;
|
2015-05-28 19:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 21:34:35 +00:00
|
|
|
return QVariant();
|
2015-05-28 19:39:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int TankInfoModel::rowCount(const QModelIndex&) const
|
2015-05-28 19:39:15 +00:00
|
|
|
{
|
2020-12-11 21:34:35 +00:00
|
|
|
return tank_info_table.nr;
|
2015-05-28 19:39:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 14:45:50 +00:00
|
|
|
TankInfoModel::TankInfoModel()
|
2015-05-28 19:39:15 +00:00
|
|
|
{
|
|
|
|
setHeaderDataStrings(QStringList() << tr("Description") << tr("ml") << tr("bar"));
|
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs
were calling into the dive-list models, which then reset the
core data structures, themselves and the unrelated
locationinformation model. The UI code then reset various other
things, such as the TankInformation model or the map. . This was
unsatisfying from a control-flow perspective, as the models should
display the core data, not act on it. Moreover, this meant lots
of intricate intermodule-dependencies.
Thus, straighten up the control flow: give the C core the
possibility to send a "all data reset" event. And do that
in those functions that reset the core data structures.
Let each module react to this event by itself. This removes
inter-module dependencies. For example, the MainWindow now
doesn't have to reset the TankInfoModel or the MapWidget.
Then, to reset the core data structures, let the UI code
simply directly call the respective core functions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 22:12:36 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &TankInfoModel::update);
|
2020-11-24 11:50:52 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::settingsChanged, this, &TankInfoModel::update);
|
2019-04-27 14:45:50 +00:00
|
|
|
update();
|
2015-05-28 19:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TankInfoModel::update()
|
|
|
|
{
|
2019-04-27 14:45:50 +00:00
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
2015-05-28 19:39:15 +00:00
|
|
|
}
|