Add a string_to_depth() helper function to match our string_to_weight one

It's currently only used for the setting of the cylinder switching
depth, but now that one should work with user-specified units (so you
can set a max depth in feet even if you use metric, and vice versa).

In the future, if we also make the unit preferences something you can
pass in (with user preferences as a default argument value), we might
want to use this for parsing the XML too, so that we'd honor explicit
units in the XML strings.  But the XML input unit preferences are not
necessarily at all the same as the user preferences, so that does
require us to extend the conversion functions to do possibly explicit
unit preference selection.

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-08 14:55:47 +08:00 committed by Dirk Hohndel
parent 1c72b8b054
commit 262bc9c84b
2 changed files with 28 additions and 8 deletions

3
dive.h
View file

@ -799,6 +799,9 @@ extern double strtod_flags(const char *str, const char **ptr, unsigned int flags
}
#endif
extern weight_t string_to_weight(const char *str);
extern depth_t string_to_depth(const char *str);
#include "pref.h"
#endif /* DIVE_H */

View file

@ -287,14 +287,8 @@ bool CylindersModel::setData(const QModelIndex& index, const QVariant& value, in
}
break;
case DEPTH:
if (CHANGED(toDouble, "ft", "m")) {
if (vString.toInt() != 0) {
if (prefs.units.length == prefs.units.FEET)
cyl->depth.mm = feet_to_mm(vString.toInt());
else
cyl->depth.mm = vString.toInt() * 1000;
}
}
if (CHANGED(data, "", ""))
cyl->depth = string_to_depth(vString.toUtf8().data());
}
dataChanged(index, index);
if (addDiveMode)
@ -488,6 +482,29 @@ lbs:
return weight;
}
depth_t string_to_depth(const char *str)
{
const char *end;
double value = strtod_flags(str, &end, 0);
QString rest = QString(end).trimmed();
QString local_ft = WeightModel::tr("ft");
QString local_m = WeightModel::tr("m");
depth_t depth;
if (rest.startsWith("m") || rest.startsWith(local_m))
goto m;
if (rest.startsWith("ft") || rest.startsWith(local_ft))
goto ft;
if (prefs.units.length == prefs.units.FEET)
goto ft;
m:
depth.mm = rint(value * 1000);
return depth;
ft:
depth.mm = feet_to_mm(value);
return depth;
}
bool WeightModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
QString vString = value.toString();