[PATCH 2/3] Add and use 'string_to_fraction()' helper converter function

Linus Torvalds torvalds at linux-foundation.org
Wed Jan 8 18:52:40 UTC 2014


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Thu, 9 Jan 2014 10:43:28 +0800
Subject: [PATCH 2/3] Add and use 'string_to_fraction()' helper converter
 function

This matches the pattern of unit conversion, and will allow us to remove
all the code that uses the old complex "CHANGED()" macro that tries to
remove units or percent signs.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---
 dive.h           |  1 +
 qt-ui/models.cpp | 28 ++++++++++++++++------------
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/dive.h b/dive.h
index f7d1c04b3156..ccba7a09f25a 100644
--- a/dive.h
+++ b/dive.h
@@ -820,6 +820,7 @@ extern weight_t string_to_weight(const char *str);
 extern depth_t string_to_depth(const char *str);
 extern pressure_t string_to_pressure(const char *str);
 extern volume_t string_to_volume(const char *str, pressure_t workp);
+extern fraction_t string_to_fraction(const char *str);
 
 #include "pref.h"
 
diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index f70a81a94572..213475723f42 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -240,21 +240,15 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in
 		}
 		break;
 	case O2:
-		if (CHANGED(toDouble, "%", "%")) {
-			int o2 = vString.toDouble() * 10 + 0.5;
-			if (cyl->gasmix.he.permille + o2 <= 1000) {
-				cyl->gasmix.o2.permille = o2;
-				changed = true;
-			}
+		if (CHANGED(data, "", "")) {
+			cyl->gasmix.o2 = string_to_fraction(vString.toUtf8().data());
+			changed = true;
 		}
 		break;
 	case HE:
-		if (CHANGED(toDouble, "%", "%")) {
-			int he = vString.toDouble() * 10 + 0.5;
-			if (cyl->gasmix.o2.permille + he <= 1000) {
-				cyl->gasmix.he.permille = he;
-				changed = true;
-			}
+		if (CHANGED(data, "", "")) {
+			cyl->gasmix.he = string_to_fraction(vString.toUtf8().data());
+			changed = true;
 		}
 		break;
 	case DEPTH:
@@ -531,6 +525,16 @@ l:
 	return volume;
 }
 
+fraction_t string_to_fraction(const char *str)
+{
+	const char *end;
+	double value = strtod_flags(str, &end, 0);
+	fraction_t fraction;
+
+	fraction.permille = rint(value * 10);
+	return fraction;
+}
+
 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