Preferences: hook up the UI page with the preferences structure

With this the new date and time formats mostly work

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-11-02 11:32:46 -08:00
parent f85883c707
commit 8a3fb854c2
2 changed files with 48 additions and 20 deletions

View file

@ -32,6 +32,11 @@ void PreferencesLanguage::refreshSettings()
QSettings s;
s.beginGroup("Language");
ui->languageSystemDefault->setChecked(s.value("UseSystemLanguage", true).toBool());
ui->timeFormatSystemDefault->setChecked(!s.value("time_format_override", false).toBool());
ui->dateFormatSystemDefault->setChecked(!s.value("date_format_override", false).toBool());
ui->timeFormatEntry->setText(s.value("time_format").toString());
ui->dateFormatEntry->setText(s.value("date_format").toString());
ui->shortDateFormatEntry->setText(s.value("date_format_short").toString());
QAbstractItemModel *m = ui->languageDropdown->model();
QModelIndexList languages = m->match(m->index(0, 0), Qt::UserRole, s.value("UiLanguage").toString());
if (languages.count())
@ -51,7 +56,15 @@ void PreferencesLanguage::syncSettings()
QMessageBox::warning(this, tr("Restart required"),
tr("To correctly load a new language you must restart Subsurface."));
}
s.setValue("UseSystemLanguage", ui->languageSystemDefault->isChecked());
s.setValue("UiLanguage", currentText);
s.setValue("UseSystemLanguage", ui->languageSystemDefault->isChecked());
s.setValue("time_format_override", !ui->timeFormatSystemDefault->isChecked());
s.setValue("date_format_override", !ui->dateFormatSystemDefault->isChecked());
if (!ui->timeFormatSystemDefault->isChecked())
s.setValue("time_format", ui->timeFormatEntry->text());
if (!ui->dateFormatSystemDefault->isChecked()) {
s.setValue("date_format", ui->dateFormatEntry->text());
s.setValue("date_format_short", ui->shortDateFormatEntry->text());
}
s.endGroup();
}

View file

@ -722,6 +722,7 @@ QString uiLanguage(QLocale *callerLoc)
QString dateFormat;
QString timeFormat;
QSettings s;
QVariant v;
s.beginGroup("Language");
if (!s.value("UseSystemLanguage", true).toBool()) {
@ -731,6 +732,11 @@ QString uiLanguage(QLocale *callerLoc)
}
QString uiLang = loc.uiLanguages().first();
GET_BOOL("time_format_override", time_format_override);
GET_BOOL("date_format_override", date_format_override);
GET_TXT("time_format", time_format);
GET_TXT("date_format", date_format);
GET_TXT("date_format_short", date_format_short);
s.endGroup();
// there's a stupid Qt bug on MacOS where uiLanguages doesn't give us the country info
@ -742,24 +748,33 @@ QString uiLanguage(QLocale *callerLoc)
if (callerLoc)
*callerLoc = loc;
// the short format is fine
// the long format uses long weekday and month names, so replace those with the short ones
// for time we don't want the time zone designator and don't want leading zeroes on the hours
shortDateFormat = loc.dateFormat(QLocale::ShortFormat);
dateFormat = loc.dateFormat(QLocale::LongFormat);
dateFormat.replace("dddd,", "ddd").replace("dddd", "ddd").replace("MMMM", "MMM");
// special hack for Swedish as our switching from long weekday names to short weekday names
// messes things up there
dateFormat.replace("'en' 'den' d:'e'", " d");
timeFormat = loc.timeFormat();
timeFormat.replace("(t)", "").replace(" t", "").replace("t", "").replace("hh", "h").replace("HH", "H").replace("'kl'.", "");
timeFormat.replace(".ss", "").replace(":ss", "").replace("ss", "");
free((void*)prefs.time_format);
prefs.time_format = strdup(qPrintable(timeFormat));
free((void*)prefs.date_format);
prefs.date_format = strdup(qPrintable(dateFormat));
free((void*)prefs.date_format_short);
prefs.date_format_short = strdup(qPrintable(shortDateFormat));
if (!prefs.date_format_override || same_string(prefs.date_format_short, "") || same_string(prefs.date_format, "")) {
// derive our standard date format from what the locale gives us
// the short format is fine
// the long format uses long weekday and month names, so replace those with the short ones
// for time we don't want the time zone designator and don't want leading zeroes on the hours
shortDateFormat = loc.dateFormat(QLocale::ShortFormat);
dateFormat = loc.dateFormat(QLocale::LongFormat);
dateFormat.replace("dddd,", "ddd").replace("dddd", "ddd").replace("MMMM", "MMM");
// special hack for Swedish as our switching from long weekday names to short weekday names
// messes things up there
dateFormat.replace("'en' 'den' d:'e'", " d");
if (!prefs.date_format_override || same_string(prefs.date_format, "")) {
free((void*)prefs.date_format);
prefs.date_format = strdup(qPrintable(dateFormat));
}
if (!prefs.date_format_override || same_string(prefs.date_format_short, "")) {
free((void*)prefs.date_format_short);
prefs.date_format_short = strdup(qPrintable(shortDateFormat));
}
}
if (!prefs.time_format_override || same_string(prefs.time_format, "")) {
timeFormat = loc.timeFormat();
timeFormat.replace("(t)", "").replace(" t", "").replace("t", "").replace("hh", "h").replace("HH", "H").replace("'kl'.", "");
timeFormat.replace(".ss", "").replace(":ss", "").replace("ss", "");
free((void*)prefs.time_format);
prefs.time_format = strdup(qPrintable(timeFormat));
}
return uiLang;
}
@ -1078,7 +1093,7 @@ QString get_trip_date_string(timestamp_t when, int nr, bool getday)
QString suffix = " " + QObject::tr("(%n dive(s))", "", nr);
if (getday) {
ret = localTime.date().toString(dateFormat) + suffix;
ret = localTime.date().toString(prefs.date_format) + suffix;
} else {
ret = localTime.date().toString("MMM yy") + suffix;
}