Add unit-aware conversion of pressure data

This just adds (and uses) a string_to_pressure() to parse pressure units
correctly when filling in cylinder pressures.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Linus Torvalds 2014-01-09 08:49:21 +08:00 committed by Dirk Hohndel
parent 0d93470c50
commit 110c07e27b
2 changed files with 37 additions and 28 deletions

1
dive.h
View file

@ -801,6 +801,7 @@ extern double strtod_flags(const char *str, const char **ptr, unsigned int flags
extern weight_t string_to_weight(const char *str);
extern depth_t string_to_depth(const char *str);
extern pressure_t string_to_pressure(const char *str);
#include "pref.h"

View file

@ -232,40 +232,25 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in
}
break;
case WORKINGPRESS:
if (CHANGED(toDouble, "psi", "bar")) {
if (vString.toDouble() != 0.0) {
TankInfoModel *tanks = TankInfoModel::instance();
QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, cyl->type.description);
if (prefs.units.pressure == prefs.units.PSI)
cyl->type.workingpressure.mbar = psi_to_mbar(vString.toDouble());
else
cyl->type.workingpressure.mbar = vString.toDouble() * 1000;
if (!matches.isEmpty())
tanks->setData(tanks->index(matches.first().row(), TankInfoModel::BAR), cyl->type.workingpressure.mbar / 1000.0);
changed = true;
}
if (CHANGED(data, "", "")) {
TankInfoModel *tanks = TankInfoModel::instance();
QModelIndexList matches = tanks->match(tanks->index(0,0), Qt::DisplayRole, cyl->type.description);
cyl->type.workingpressure = string_to_pressure(vString.toUtf8().data());
if (!matches.isEmpty())
tanks->setData(tanks->index(matches.first().row(), TankInfoModel::BAR), cyl->type.workingpressure.mbar / 1000.0);
changed = true;
}
break;
case START:
if (CHANGED(toDouble, "psi", "bar")) {
if (vString.toDouble() != 0.0) {
if (prefs.units.pressure == prefs.units.PSI)
cyl->start.mbar = psi_to_mbar(vString.toDouble());
else
cyl->start.mbar = vString.toDouble() * 1000;
changed = true;
}
if (CHANGED(data, "", "")) {
cyl->start = string_to_pressure(vString.toUtf8().data());
changed = true;
}
break;
case END:
if (CHANGED(toDouble, "psi", "bar")) {
if (vString.toDouble() != 0.0) {
if (prefs.units.pressure == prefs.units.PSI)
cyl->end.mbar = psi_to_mbar(vString.toDouble());
else
cyl->end.mbar = vString.toDouble() * 1000;
changed = true;
}
if (CHANGED(data, "", "")) {
cyl->end = string_to_pressure(vString.toUtf8().data());
changed = true;
}
break;
case O2:
@ -505,6 +490,29 @@ ft:
return depth;
}
pressure_t string_to_pressure(const char *str)
{
const char *end;
double value = strtod_flags(str, &end, 0);
QString rest = QString(end).trimmed();
QString local_psi = CylindersModel::tr("psi");
QString local_bar = CylindersModel::tr("bar");
pressure_t pressure;
if (rest.startsWith("bar") || rest.startsWith(local_bar))
goto bar;
if (rest.startsWith("psi") || rest.startsWith(local_psi))
goto psi;
if (prefs.units.pressure == prefs.units.PSI)
goto psi;
bar:
pressure.mbar = rint(value * 1000);
return pressure;
psi:
pressure.mbar = psi_to_mbar(value);
return pressure;
}
bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
QString vString = value.toString();