From cb72ee350dfa52c485a2e0d8e5a689df16ba2486 Mon Sep 17 00:00:00 2001 From: Devin Lin Date: Tue, 9 Mar 2021 19:04:29 +0000 Subject: [PATCH] Fix panel clock from not respecting 12/24 hour settings --- .../panel/package/contents/ui/main.qml | 2 +- containments/panel/phonepanel.cpp | 24 +++++++++++++++++++ containments/panel/phonepanel.h | 10 ++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/containments/panel/package/contents/ui/main.qml b/containments/panel/package/contents/ui/main.qml index 8dee55e7..c542df14 100644 --- a/containments/panel/package/contents/ui/main.qml +++ b/containments/panel/package/contents/ui/main.qml @@ -205,7 +205,7 @@ Item { PlasmaComponents.Label { id: clock - property bool is24HourTime: Qt.locale().timeFormat(Locale.ShortFormat).toLowerCase().indexOf("ap") === -1 + property bool is24HourTime: plasmoid.nativeInterface.isSystem24HourFormat anchors.fill: parent text: Qt.formatTime(timeSource.data.Local.DateTime, is24HourTime ? "h:mm" : "h:mm ap") diff --git a/containments/panel/phonepanel.cpp b/containments/panel/phonepanel.cpp index 4cb8438e..2bc54992 100644 --- a/containments/panel/phonepanel.cpp +++ b/containments/panel/phonepanel.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -23,6 +24,8 @@ #include #include +#define FORMAT24H "HH:mm:ss" + constexpr int SCREENSHOT_DELAY = 200; /* -- Static Helpers --------------------------------------------------------------------------- */ @@ -73,6 +76,19 @@ PhonePanel::PhonePanel(QObject *parent, const QVariantList &args) //setHasConfigurationInterface(true); m_kscreenInterface = new org::kde::KScreen(QStringLiteral("org.kde.kded5"), QStringLiteral("/modules/kscreen"), QDBusConnection::sessionBus(), this); m_screenshotInterface = new org::kde::kwin::Screenshot(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QDBusConnection::sessionBus(), this); + + m_localeConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::SimpleConfig); + m_localeConfigWatcher = KConfigWatcher::create(m_localeConfig); + + // watch for changes to locale config, to update 12/24 hour time + connect(m_localeConfigWatcher.data(), &KConfigWatcher::configChanged, + this, [this](const KConfigGroup &group, const QByteArrayList &names) -> void { + if (group.name() == "Locale") { + // we have to reparse for new changes (from system settings) + m_localeConfig->reparseConfiguration(); + Q_EMIT isSystem24HourFormatChanged(); + } + }); } PhonePanel::~PhonePanel() = default; @@ -183,6 +199,14 @@ void PhonePanel::takeScreenshot() }); } +bool PhonePanel::isSystem24HourFormat() +{ + KConfigGroup localeSettings = KConfigGroup(m_localeConfig, "Locale"); + + QString timeFormat = localeSettings.readEntry("TimeFormat", QStringLiteral(FORMAT24H)); + return timeFormat == QStringLiteral(FORMAT24H); +} + K_EXPORT_PLASMA_APPLET_WITH_JSON(quicksettings, PhonePanel, "metadata.json") #include "phonepanel.moc" diff --git a/containments/panel/phonepanel.h b/containments/panel/phonepanel.h index 1b59d031..65e0f623 100644 --- a/containments/panel/phonepanel.h +++ b/containments/panel/phonepanel.h @@ -10,6 +10,9 @@ #include +#include +#include + #include "kscreeninterface.h" #include "screenshotinterface.h" @@ -18,6 +21,7 @@ class PhonePanel : public Plasma::Containment Q_OBJECT Q_PROPERTY(bool autoRotateEnabled READ autoRotate WRITE setAutoRotate NOTIFY autoRotateChanged); Q_PROPERTY(bool torchEnabled READ torchEnabled NOTIFY torchChanged); + Q_PROPERTY(bool isSystem24HourFormat READ isSystem24HourFormat NOTIFY isSystem24HourFormatChanged); public: PhonePanel( QObject *parent, const QVariantList &args ); ~PhonePanel() override; @@ -31,13 +35,19 @@ public Q_SLOTS: void setAutoRotate(bool value); bool torchEnabled() const; + + bool isSystem24HourFormat(); signals: void autoRotateChanged(bool value); void torchChanged(bool value); + void isSystem24HourFormatChanged(); private: bool m_running = false; + + KConfigWatcher::Ptr m_localeConfigWatcher; + KSharedConfig::Ptr m_localeConfig; org::kde::KScreen *m_kscreenInterface; org::kde::kwin::Screenshot *m_screenshotInterface;