summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2014-01-02 20:52:47 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-01-02 21:17:51 -0800
commitc49d3885f51a9f2ae3fec9252b75fe4b5d7ee503 (patch)
tree13d37a0d2ac966916df3c741055972ec7bb6a2db
parentcb53a7867486c89397f1f83eb89d19511ec215ae (diff)
downloadsubsurface-c49d3885f51a9f2ae3fec9252b75fe4b5d7ee503.tar.gz
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@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r--qt-ui/models.cpp26
1 files 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();