Fix panel clock from not respecting 12/24 hour settings

This commit is contained in:
Devin Lin 2021-03-09 19:04:29 +00:00
parent 7f7bea091e
commit cb72ee350d
3 changed files with 35 additions and 1 deletions

View file

@ -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")

View file

@ -13,6 +13,7 @@
#include <KNotification>
#include <KLocalizedString>
#include <KConfigGroup>
#include <QDateTime>
#include <QDBusPendingReply>
@ -23,6 +24,8 @@
#include <QtConcurrent/QtConcurrent>
#include <QScreen>
#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"

View file

@ -10,6 +10,9 @@
#include <Plasma/Containment>
#include <KSharedConfig>
#include <KConfigWatcher>
#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;