From c49d3885f51a9f2ae3fec9252b75fe4b5d7ee503 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 2 Jan 2014 20:52:47 -0800 Subject: 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 Signed-off-by: Dirk Hohndel --- 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 2e73682dc..0199c9989 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(); -- cgit v1.2.3-70-g09d2