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 weight_t string_to_weight(const char *str);
extern depth_t string_to_depth(const char *str); extern depth_t string_to_depth(const char *str);
extern pressure_t string_to_pressure(const char *str);
#include "pref.h" #include "pref.h"

View file

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