summaryrefslogtreecommitdiffstats
path: root/qt-ui/models.cpp
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2014-01-09 08:49:21 +0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-01-09 09:10:29 +0800
commit9c06938adcc7670201ed5e94ca2c35ec18702373 (patch)
tree07dc0c541edd0e11314e969174487610bf843d0c /qt-ui/models.cpp
parentc32c28d40b627fa08051697f44a829c7f19854d0 (diff)
downloadsubsurface-9c06938adcc7670201ed5e94ca2c35ec18702373.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>
Diffstat (limited to 'qt-ui/models.cpp')
-rw-r--r--qt-ui/models.cpp64
1 files changed, 36 insertions, 28 deletions
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index fb62c39bb..0da5aa222 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();