1
0
Fork 0
mirror of https://github.com/subsurface/subsurface.git synced 2025-02-19 22:16:15 +00:00

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>
This commit is contained in:
Linus Torvalds 2014-01-02 20:52:47 -08:00 committed by Dirk Hohndel
parent cb53a78674
commit c49d3885f5

View file

@ -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();