Use correct numeric format based on selected locale (Qt domain part)

This changes the numeric format of many values printed to the UI to
reflect the correct numeric format of the selected locale:
- dot or comma as decimal separator
- comma or dot as thousands separator

In the Qt domain the `L` flag is used case specific mostly
in qthelper.cpp.
Then the helper functions get_xxx_string() are used more consistently.

Signed-off-by: Stefan Fuchs <sfuchs@gmx.de>
This commit is contained in:
Stefan Fuchs 2018-02-19 21:55:18 +01:00 committed by Robert C. Helling
parent 18c034ea37
commit aacc688670
6 changed files with 29 additions and 38 deletions

View file

@ -20,6 +20,7 @@ QString get_weight_unit();
QString get_temperature_string(temperature_t temp, bool showunit = false);
QString get_temp_unit();
QString get_volume_string(volume_t volume, bool showunit = false);
QString get_volume_string(int mliter, bool showunit = false);
QString get_volume_unit();
QString get_pressure_string(pressure_t pressure, bool showunit = false);
QString get_pressure_unit();

View file

@ -46,10 +46,10 @@ QString weight_string(int weight_in_grams)
QString str;
if (get_units()->weight == units::KG) {
double kg = (double) weight_in_grams / 1000.0;
str = QString("%1").arg(kg, 0, 'f', kg >= 20.0 ? 0 : 1);
str = QString("%L1").arg(kg, 0, 'f', kg >= 20.0 ? 0 : 1);
} else {
double lbs = grams_to_lbs(weight_in_grams);
str = QString("%1").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1);
str = QString("%L1").arg(lbs, 0, 'f', lbs >= 40.0 ? 0 : 1);
}
return str;
}
@ -582,10 +582,10 @@ QString get_depth_string(int mm, bool showunit, bool showdecimal)
{
if (prefs.units.length == units::METERS) {
double meters = mm / 1000.0;
return QString("%1%2").arg(meters, 0, 'f', (showdecimal && meters < 20.0) ? 1 : 0).arg(showunit ? translate("gettextFromC", "m") : "");
return QString("%L1%2").arg(meters, 0, 'f', (showdecimal && meters < 20.0) ? 1 : 0).arg(showunit ? translate("gettextFromC", "m") : "");
} else {
double feet = mm_to_feet(mm);
return QString("%1%2").arg(feet, 0, 'f', 0).arg(showunit ? translate("gettextFromC", "ft") : "");
return QString("%L1%2").arg(feet, 0, 'f', 0).arg(showunit ? translate("gettextFromC", "ft") : "");
}
}
@ -627,10 +627,10 @@ QString get_temperature_string(temperature_t temp, bool showunit)
return ""; //temperature not defined
} else if (prefs.units.temperature == units::CELSIUS) {
double celsius = mkelvin_to_C(temp.mkelvin);
return QString("%1%2%3").arg(celsius, 0, 'f', 1).arg(showunit ? (UTF8_DEGREE) : "").arg(showunit ? translate("gettextFromC", "C") : "");
return QString("%L1%2%3").arg(celsius, 0, 'f', 1).arg(showunit ? (UTF8_DEGREE) : "").arg(showunit ? translate("gettextFromC", "C") : "");
} else {
double fahrenheit = mkelvin_to_F(temp.mkelvin);
return QString("%1%2%3").arg(fahrenheit, 0, 'f', 1).arg(showunit ? (UTF8_DEGREE) : "").arg(showunit ? translate("gettextFromC", "F") : "");
return QString("%L1%2%3").arg(fahrenheit, 0, 'f', 1).arg(showunit ? (UTF8_DEGREE) : "").arg(showunit ? translate("gettextFromC", "F") : "");
}
}
@ -642,12 +642,17 @@ QString get_temp_unit()
return QString(UTF8_DEGREE "F");
}
QString get_volume_string(volume_t volume, bool showunit)
QString get_volume_string(int mliter, bool showunit)
{
const char *unit;
int decimals;
double value = get_volume_units(volume.mliter, &decimals, &unit);
return QString("%1%2").arg(value, 0, 'f', decimals).arg(showunit ? unit : "");
double value = get_volume_units(mliter, &decimals, &unit);
return QString("%L1%2").arg(value, 0, 'f', decimals).arg(showunit ? unit : "");
}
QString get_volume_string(volume_t volume, bool showunit)
{
return get_volume_string(volume.mliter, showunit);
}
QString get_volume_unit()
@ -661,10 +666,10 @@ QString get_pressure_string(pressure_t pressure, bool showunit)
{
if (prefs.units.pressure == units::BAR) {
double bar = pressure.mbar / 1000.0;
return QString("%1%2").arg(bar, 0, 'f', 1).arg(showunit ? translate("gettextFromC", "bar") : "");
return QString("%L1%2").arg(bar, 0, 'f', 1).arg(showunit ? translate("gettextFromC", "bar") : "");
} else {
double psi = mbar_to_PSI(pressure.mbar);
return QString("%1%2").arg(psi, 0, 'f', 0).arg(showunit ? translate("gettextFromC", "psi") : "");
return QString("%L1%2").arg(psi, 0, 'f', 0).arg(showunit ? translate("gettextFromC", "psi") : "");
}
}

View file

@ -271,7 +271,7 @@ void DiveCartesianAxis::animateChangeLine(const QLineF &newLine)
QString DiveCartesianAxis::textForValue(double value)
{
return QString::number(value);
return QString("%L1").arg(value, 0, 'g', 4);
}
void DiveCartesianAxis::setTickSize(qreal size)

View file

@ -260,11 +260,9 @@ void DiveProfileItem::settingsChanged()
void DiveProfileItem::plot_depth_sample(struct plot_data *entry, QFlags<Qt::AlignmentFlag> flags, const QColor &color)
{
int decimals;
double d = get_depth_units(entry->depth, &decimals, NULL);
DiveTextItem *item = new DiveTextItem(this);
item->setPos(hAxis->posAtValue(entry->sec), vAxis->posAtValue(entry->depth));
item->setText(QString("%1").arg(d, 0, 'f', 1));
item->setText(get_depth_string(entry->depth, true));
item->setAlignment(flags);
item->setBrush(color);
texts.append(item);
@ -670,15 +668,12 @@ void DiveMeanDepthItem::createTextItem() {
int sec = entry[dataModel->rowCount()-1].sec;
qDeleteAll(texts);
texts.clear();
int decimals;
const char *unitText;
double d = get_depth_units(lrint(lastRunningSum), &decimals, &unitText);
DiveTextItem *text = new DiveTextItem(this);
text->setAlignment(Qt::AlignRight | Qt::AlignTop);
text->setBrush(getColor(TEMP_TEXT));
text->setPos(QPointF(hAxis->posAtValue(sec) + 1, vAxis->posAtValue(lastRunningSum)));
text->setScale(0.8); // need to call this BEFORE setText()
text->setText(QString("%1%2").arg(d, 0, 'f', 1).arg(unitText));
text->setText(get_depth_string(lrint(lastRunningSum), true));
texts.append(text);
}

View file

@ -57,7 +57,7 @@ static QString get_cylinder_string(cylinder_t *cyl)
unit = CylindersModel::tr("");
}
return QString("%1").arg(value, 0, 'f', decimals) + unit;
return QString("%L1").arg(value, 0, 'f', decimals) + unit;
}
static QString gas_volume_string(int ml, const char *tail)
@ -69,7 +69,7 @@ static QString gas_volume_string(int ml, const char *tail)
vol = get_volume_units(ml, NULL, &unit);
decimals = (vol > 20.0) ? 0 : (vol > 2.0) ? 1 : 2;
return QString("%1 %2 %3").arg(vol, 0, 'f', decimals).arg(unit).arg(tail);
return QString("%L1 %2 %3").arg(vol, 0, 'f', decimals).arg(unit).arg(tail);
}
static QVariant gas_wp_tooltip(cylinder_t *cyl);
@ -126,7 +126,7 @@ static QVariant percent_string(fraction_t fraction)
if (!permille)
return QVariant();
return QString("%1%").arg(permille / 10.0, 0, 'f', 1);
return QString("%L1%").arg(permille / 10.0, 0, 'f', 1);
}
QVariant CylindersModel::data(const QModelIndex &index, int role) const

View file

@ -387,28 +387,18 @@ QString DiveItem::displayTemperatureWithUnit() const
QString DiveItem::displaySac() const
{
QString str;
struct dive *dive = get_dive_by_uniq_id(diveId);
if (dive->sac) {
const char *unit;
int decimal;
double value = get_volume_units(dive->sac, &decimal, &unit);
return QString::number(value, 'f', decimal);
}
return QString("");
if (!dive->sac)
return QString();
return get_volume_string(dive->sac, false);
}
QString DiveItem::displaySacWithUnit() const
{
QString str;
struct dive *dive = get_dive_by_uniq_id(diveId);
if (dive->sac) {
const char *unit;
int decimal;
double value = get_volume_units(dive->sac, &decimal, &unit);
return QString::number(value, 'f', decimal) + QString(unit).append(tr("/min"));
}
return QString("");
if (!dive->sac)
return QString();
return get_volume_string(dive->sac, true).append(tr("/min"));
}
QString DiveItem::displayWeight() const