From 820dc0a553e50cc4560733d43fca2674a61d43b3 Mon Sep 17 00:00:00 2001 From: David Redondo Date: Thu, 13 Jan 2022 10:04:13 +0100 Subject: Introduce StandardShortcutWatcher to watch for runtime changes Currently an application needs to be restarted before it can see any changes made to the standard shortcut configuration. To notify about changes a new class is introduced that looks for those changes using KConfigWatcher and also updates the global map. CCBUG:426656 --- src/gui/kstandardshortcutwatcher.cpp | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/gui/kstandardshortcutwatcher.cpp (limited to 'src/gui/kstandardshortcutwatcher.cpp') diff --git a/src/gui/kstandardshortcutwatcher.cpp b/src/gui/kstandardshortcutwatcher.cpp new file mode 100644 index 00000000..1be5e503 --- /dev/null +++ b/src/gui/kstandardshortcutwatcher.cpp @@ -0,0 +1,46 @@ +/* + SPDX-FileCopyrightText: 2022 David Redondo + + SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL +*/ + +#include "kstandardshortcutwatcher.h" + +#include "kconfigwatcher.h" +#include "kstandardshortcut_p.h" + +namespace KStandardShortcut +{ +class StandardShortcutWatcherPrivate +{ +public: + KConfigWatcher::Ptr watcher = KConfigWatcher::create(KSharedConfig::openConfig()); +}; + +StandardShortcutWatcher::StandardShortcutWatcher(QObject *parent) + : QObject(parent) + , d(std::make_unique()) +{ + connect(d->watcher.get(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group, const QByteArrayList &keys) { + if (group.name() != QStringLiteral("Shortcuts")) { + return; + } + for (const auto &key : keys) { + const StandardShortcut shortcut = KStandardShortcut::findByName(QString::fromUtf8(key)); + if (shortcut != KStandardShortcut::AccelNone) { + initialize(shortcut); + Q_EMIT shortcutChanged(shortcut, KStandardShortcut::shortcut(shortcut)); + } + } + }); +} + +StandardShortcutWatcher::~StandardShortcutWatcher() = default; + +StandardShortcutWatcher *shortcutWatcher() +{ + static StandardShortcutWatcher watcher; + return &watcher; +} + +} -- cgit v1.2.1