summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2014-01-08 14:55:47 +0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-01-09 09:30:35 +0800
commit262bc9c84b1ab04934e84d6671e6a2b7c19c9af3 (patch)
treee84907b28de4960188a56cac8e8336f3a2b36263
parent1c72b8b0545e6a13ea3db917a1434152c73a3c3b (diff)
downloadsubsurface-262bc9c84b1ab04934e84d6671e6a2b7c19c9af3.tar.gz
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 <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r--dive.h3
-rw-r--r--qt-ui/models.cpp33
2 files changed, 28 insertions, 8 deletions
diff --git a/dive.h b/dive.h
index 11afa99b1..ecb0bfd50 100644
--- a/dive.h
+++ b/dive.h
@@ -799,6 +799,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 35696a778..7d08be288 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();