diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2014-01-09 08:49:21 +0800 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2014-01-09 09:31:19 +0800 |
commit | 110c07e27b61cacacf4daba3a4606974c995721d (patch) | |
tree | 305e887c92238b58ad67f896e013394368a112f4 | |
parent | 0d93470c50e6ebaa0128ba182a94090d6d000278 (diff) | |
download | subsurface-110c07e27b61cacacf4daba3a4606974c995721d.tar.gz |
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>
-rw-r--r-- | dive.h | 1 | ||||
-rw-r--r-- | qt-ui/models.cpp | 64 |
2 files changed, 37 insertions, 28 deletions
@@ -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" diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index 7d08be288..db180a27a 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -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(); |