From cc33ff2fe212da4f20bb30060ee9996b3c357adc Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Wed, 8 Jan 2014 14:55:47 +0800 Subject: Add a string_to_depth() helper function to match our string_to_weight one It's currently only used for the setting of the cylinder switching depth, but now that one should work with user-specified units (so you can set a max depth in feet even if you use metric, and vice versa). In the future, if we also make the unit preferences something you can pass in (with user preferences as a default argument value), we might want to use this for parsing the XML too, so that we'd honor explicit units in the XML strings. But the XML input unit preferences are not necessarily at all the same as the user preferences, so that does require us to extend the conversion functions to do possibly explicit unit preference selection. Signed-off-by: Linus Torvalds Signed-off-by: Dirk Hohndel --- dive.h | 3 +++ qt-ui/models.cpp | 33 +++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/dive.h b/dive.h index 7a90bf49e..042073b5f 100644 --- a/dive.h +++ b/dive.h @@ -816,6 +816,9 @@ extern double strtod_flags(const char *str, const char **ptr, unsigned int flags } #endif +extern weight_t string_to_weight(const char *str); +extern depth_t string_to_depth(const char *str); + #include "pref.h" #endif /* DIVE_H */ diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp index b0a0dadb2..fb62c39bb 100644 --- a/qt-ui/models.cpp +++ b/qt-ui/models.cpp @@ -287,14 +287,8 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in } break; case DEPTH: - if (CHANGED(toDouble, "ft", "m")) { - if (vString.toInt() != 0) { - if (prefs.units.length == prefs.units.FEET) - cyl->depth.mm = feet_to_mm(vString.toInt()); - else - cyl->depth.mm = vString.toInt() * 1000; - } - } + if (CHANGED(data, "", "")) + cyl->depth = string_to_depth(vString.toUtf8().data()); } dataChanged(index, index); if (addDiveMode) @@ -488,6 +482,29 @@ lbs: return weight; } +depth_t string_to_depth(const char *str) +{ + const char *end; + double value = strtod_flags(str, &end, 0); + QString rest = QString(end).trimmed(); + QString local_ft = WeightModel::tr("ft"); + QString local_m = WeightModel::tr("m"); + depth_t depth; + + if (rest.startsWith("m") || rest.startsWith(local_m)) + goto m; + if (rest.startsWith("ft") || rest.startsWith(local_ft)) + goto ft; + if (prefs.units.length == prefs.units.FEET) + goto ft; +m: + depth.mm = rint(value * 1000); + return depth; +ft: + depth.mm = feet_to_mm(value); + return depth; +} + bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role) { QString vString = value.toString(); -- cgit v1.2.3-70-g09d2