[PATCH 2/2] Allow the user to specify weight units explicitly

Linus Torvalds torvalds at linux-foundation.org
Thu Jan 2 20:58:20 UTC 2014


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Thu, 2 Jan 2014 20:52:47 -0800
Subject: [PATCH 2/2] Allow the user to specify weight units explicitly

Instead of always assuming that all numbers are in the users locale
weight units, allow the user to say "kg" or "lbs" explicitly.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

This uses our now useful "strtod_flags()" helper to actually look at what 
comes *after* the numerical part.

Qt people are crazy. Who the f*ck thought that their "toDouble()" was a 
good interface?

 qt-ui/models.cpp | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index 2e73682dcbd4..0199c9989a8c 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -467,6 +467,25 @@ void WeightModel::passInData(const QModelIndex& index, const QVariant& value)
 	}
 }
 
+double string_to_grams(char *str)
+{
+	char *end;
+	double value = strtod_flags(str, &end, 0);
+
+	while (isspace(*end))
+		end++;
+	if (!strncmp(end, "kg", 2))
+		goto kg;
+	if (!strncmp(end, "lbs", 3))
+		goto lbs;
+	if (prefs.units.weight == prefs.units.LBS)
+		goto lbs;
+kg:
+	return rint(value * 1000);
+lbs:
+	return lbs_to_grams(value);
+}
+
 bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role)
 {
 	QString vString = value.toString();
@@ -490,11 +509,8 @@ bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int r
 		}
 		break;
 	case WEIGHT:
-		if (CHANGED(toDouble, "kg", "lbs")) {
-			if (prefs.units.weight == prefs.units.LBS)
-				ws->weight.grams = lbs_to_grams(vString.toDouble());
-			else
-				ws->weight.grams = vString.toDouble() * 1000.0 + 0.5;
+		if (CHANGED(data, "", "")) {
+			ws->weight.grams = string_to_grams(vString.toUtf8().data());
 			// now update the ws_info
 			changed = true;
 			WSInfoModel *wsim = WSInfoModel::instance();
-- 
1.8.5.1.163.gd7aced9



More information about the subsurface mailing list