[PATCH 3/3] Add a string_to_depth() helper function to match our string_to_weight one

Linus Torvalds torvalds at linux-foundation.org
Tue Jan 7 23:40:32 UTC 2014


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Wed, 8 Jan 2014 14:55:47 +0800
Subject: [PATCH 3/3] 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 at linux-foundation.org>
---
This is kind of pointless, but it's a step towards the whole "let's always 
parse units properly" model. It just isn't likely to be all that important 
for the cylinder change depth.

  dive.h           |  3 +++
  qt-ui/models.cpp | 33 +++++++++++++++++++++++++--------
  2 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/dive.h b/dive.h
index 7a90bf49ef84..042073b5f0cf 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 b0a0dadb2b35..fb62c39bb9db 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();
-- 
1.8.4.2



More information about the subsurface mailing list