Skip to content

Commit

Permalink
Improve timezone and language handling
Browse files Browse the repository at this point in the history
Ensure that changes performed in the Settings UI cause writes to
the appropriate backend setting value.

Ensure that backend setting value changes trigger updates to the
loaded translation catalogue and/or clock.

Note that currently Qt for WebAssembly doesn't support proper
QTimezone enumeration or tzdb.  Thus, we only support UTC or
local timezone (i.e. timezone of the browser, not the device).
Future resolution required, see issue #299 for more info.

Contributes to issue #406
  • Loading branch information
chriadam committed Jul 27, 2023
1 parent 8bdaa45 commit 7e9b5b0
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 3 deletions.
18 changes: 18 additions & 0 deletions data/SystemSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ QtObject {
}
}

property DataPoint timeZone: DataPoint {
source: "com.victronenergy.settings/Settings/System/TimeZone"
onValueChanged: {
if (value !== undefined) {
ClockTime.systemTimeZone = value
}
}
}

property DataPoint language: DataPoint {
source: "com.victronenergy.settings/Settings/Gui/Language"
onValueChanged: {
if (value !== undefined) {
Language.setCurrentLanguage(value)
}
}
}

function reset() {
// no-op
}
Expand Down
5 changes: 5 additions & 0 deletions pages/settings/PageSettingsDisplay.qml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ Page {

onOptionClicked: function(index) {
Language.current = optionModel.languageAt(index)
languageDataPoint.setValue(Language.toString(Language.current))
}

property DataPoint languageDataPoint: DataPoint {
source: "com.victronenergy.settings/Settings/Gui/Language"
}
}

Expand Down
2 changes: 1 addition & 1 deletion pages/settings/PageTzInfo.qml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Page {
ListTextItem {
//% "Date/Time UTC"
text: qsTrId("settings_tz_date_time_utc")
secondaryText: Qt.formatDateTime(ClockTime.currentDateTimeUtc, "yyyy-MM-dd hh:mm")
secondaryText: ClockTime.currentTimeUtcText
}

ListButton {
Expand Down
36 changes: 34 additions & 2 deletions src/clocktime.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "clocktime.h"

#if !defined(VENUS_WEBASSEMBLY_BUILD)
#include <QTimeZone>
#endif

using namespace Victron::VenusOS;

ClockTime::ClockTime(QObject *parent)
Expand Down Expand Up @@ -32,13 +36,27 @@ void ClockTime::timerEvent(QTimerEvent *)

void ClockTime::updateTime()
{
m_currentDateTime = QDateTime::currentDateTime();
m_currentDateTimeUtc = m_currentDateTime.toUTC();
if (m_systemTimeZone.compare(QStringLiteral("/UTC"), Qt::CaseInsensitive) == 0
|| m_systemTimeZone.compare(QStringLiteral("UTC"), Qt::CaseInsensitive) == 0) {
m_currentDateTime = QDateTime::currentDateTimeUtc();
} else {
#if defined(VENUS_WEBASSEMBLY_BUILD)
// Cannot use QTimeZone in Emscripten builds.
// We thus cannot convert to the specified timezone offset.
// The local time will be the local time of the browser.
m_currentDateTime = QDateTime::currentDateTime();
#else
m_currentDateTime = QDateTime::currentDateTime().toTimeZone(QTimeZone(m_systemTimeZone.toUtf8()));
#endif
}
m_currentDateTimeUtc = QDateTime::currentDateTimeUtc();
m_currentTimeText = m_currentDateTime.toString("hh:mm");
m_currentTimeUtcText = m_currentDateTimeUtc.toString("yyyy-MM-dd hh:mm");

emit currentDateTimeChanged();
emit currentDateTimeUtcChanged();
emit currentTimeTextChanged();
emit currentTimeUtcTextChanged();
}

void ClockTime::scheduleNextTimeCheck(const QTime &now)
Expand All @@ -57,3 +75,17 @@ ClockTime* ClockTime::instance(QObject* parent)
return new ClockTime(parent);
}

QString ClockTime::systemTimeZone() const
{
return m_systemTimeZone;
}

void ClockTime::setSystemTimeZone(const QString &timezone)
{
if (m_systemTimeZone != timezone) {
m_systemTimeZone = timezone;
updateTime();
emit systemTimeZoneChanged();
}
}

9 changes: 9 additions & 0 deletions src/clocktime.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ class ClockTime : public QObject
Q_PROPERTY(QDateTime currentDateTime MEMBER m_currentDateTime NOTIFY currentDateTimeChanged)
Q_PROPERTY(QDateTime currentDateTimeUtc MEMBER m_currentDateTimeUtc NOTIFY currentDateTimeUtcChanged)
Q_PROPERTY(QString currentTimeText MEMBER m_currentTimeText NOTIFY currentTimeTextChanged)
Q_PROPERTY(QString currentTimeUtcText MEMBER m_currentTimeUtcText NOTIFY currentTimeUtcTextChanged)
Q_PROPERTY(QString systemTimeZone READ systemTimeZone WRITE setSystemTimeZone NOTIFY systemTimeZoneChanged)

public:
ClockTime(QObject *parent);

static ClockTime* instance(QObject* parent = nullptr);

QString systemTimeZone() const;
void setSystemTimeZone(const QString &timezone); // "region/city" format.

public Q_SLOTS:
QString formatTime(int hour, int minute) const;
bool isDateValid(int year, int month, int day) const; // month is 1-12
Expand All @@ -29,6 +34,8 @@ public Q_SLOTS:
void currentDateTimeChanged();
void currentDateTimeUtcChanged();
void currentTimeTextChanged();
void currentTimeUtcTextChanged();
void systemTimeZoneChanged();

protected:
void timerEvent(QTimerEvent *) override;
Expand All @@ -40,6 +47,8 @@ public Q_SLOTS:
QDateTime m_currentDateTime;
QDateTime m_currentDateTimeUtc;
QString m_currentTimeText;
QString m_currentTimeUtcText;
QString m_systemTimeZone;
int m_timerId = 0;
};

Expand Down
10 changes: 10 additions & 0 deletions src/language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ void Language::setCurrentLanguage(QLocale::Language language)
}
}

void Language::setCurrentLanguage(const QString &language)
{
const QLocale::Language lang = QLocale::codeToLanguage(language);
if (lang != QLocale::AnyLanguage) {
setCurrentLanguage(lang);
} else {
qCWarning(venusGui) << "Unknown language code specified:" << language;
}
}

bool Language::installTranslatorForLanguage(QLocale::Language language)
{
const bool alreadyLoaded = m_loadedTranslators.contains(language);
Expand Down
1 change: 1 addition & 0 deletions src/language.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Language : public QObject
Q_INVOKABLE QString toString(QLocale::Language language) const;
QLocale::Language getCurrentLanguage() const;
void setCurrentLanguage(QLocale::Language language);
Q_INVOKABLE void setCurrentLanguage(const QString &language);

Q_SIGNALS:
void currentLanguageChanged();
Expand Down

0 comments on commit 7e9b5b0

Please sign in to comment.