From 2c63b4e3870622218ce1857244912b4719181962 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Thu, 11 Apr 2024 01:03:47 -0500 Subject: [PATCH] Add hotkey manager --- scwx-qt/scwx-qt.cmake | 2 + .../source/scwx/qt/manager/hotkey_manager.cpp | 105 ++++++++++++++++++ .../source/scwx/qt/manager/hotkey_manager.hpp | 43 +++++++ 3 files changed, 150 insertions(+) create mode 100644 scwx-qt/source/scwx/qt/manager/hotkey_manager.cpp create mode 100644 scwx-qt/source/scwx/qt/manager/hotkey_manager.hpp diff --git a/scwx-qt/scwx-qt.cmake b/scwx-qt/scwx-qt.cmake index 2b612997..5f58eded 100644 --- a/scwx-qt/scwx-qt.cmake +++ b/scwx-qt/scwx-qt.cmake @@ -88,6 +88,7 @@ set(SRC_GL_DRAW source/scwx/qt/gl/draw/draw_item.cpp set(HDR_MANAGER source/scwx/qt/manager/alert_manager.hpp source/scwx/qt/manager/download_manager.hpp source/scwx/qt/manager/font_manager.hpp + source/scwx/qt/manager/hotkey_manager.hpp source/scwx/qt/manager/media_manager.hpp source/scwx/qt/manager/placefile_manager.hpp source/scwx/qt/manager/position_manager.hpp @@ -101,6 +102,7 @@ set(HDR_MANAGER source/scwx/qt/manager/alert_manager.hpp set(SRC_MANAGER source/scwx/qt/manager/alert_manager.cpp source/scwx/qt/manager/download_manager.cpp source/scwx/qt/manager/font_manager.cpp + source/scwx/qt/manager/hotkey_manager.cpp source/scwx/qt/manager/media_manager.cpp source/scwx/qt/manager/placefile_manager.cpp source/scwx/qt/manager/position_manager.cpp diff --git a/scwx-qt/source/scwx/qt/manager/hotkey_manager.cpp b/scwx-qt/source/scwx/qt/manager/hotkey_manager.cpp new file mode 100644 index 00000000..e47f7b24 --- /dev/null +++ b/scwx-qt/source/scwx/qt/manager/hotkey_manager.cpp @@ -0,0 +1,105 @@ +#include +#include +#include + +#include + +#include +#include +#include + +namespace scwx +{ +namespace qt +{ +namespace manager +{ + +static const std::string logPrefix_ = "scwx::qt::manager::hotkey_manager"; +static const auto logger_ = scwx::util::Logger::Create(logPrefix_); + +class HotkeyManager::Impl +{ +public: + explicit Impl() + { + auto& hotkeySettings = settings::HotkeySettings::Instance(); + + for (auto hotkey : types::HotkeyIterator()) + { + auto& hotkeyVariable = hotkeySettings.hotkey(hotkey); + + UpdateHotkey(hotkey, hotkeyVariable.GetValue()); + + callbacks_.emplace_back(hotkeyVariable, + hotkeyVariable.RegisterValueChangedCallback( + [this, hotkey](const std::string& value) + { UpdateHotkey(hotkey, value); })); + } + } + + ~Impl() + { + for (auto& callback : callbacks_) + { + callback.first.UnregisterValueChangedCallback(callback.second); + } + } + + void UpdateHotkey(types::Hotkey hotkey, const std::string& value); + + std::vector< + std::pair&, boost::uuids::uuid>> + callbacks_ {}; + boost::container::flat_map hotkeys_ {}; +}; + +HotkeyManager::HotkeyManager() : p(std::make_unique()) {} +HotkeyManager::~HotkeyManager() = default; + +void HotkeyManager::Impl::UpdateHotkey(types::Hotkey hotkey, + const std::string& value) +{ + hotkeys_.insert_or_assign(hotkey, + QKeySequence {QString::fromStdString(value)}); +} + +void HotkeyManager::HandleKeyPress(QKeyEvent* ev) +{ + for (auto& hotkey : p->hotkeys_) + { + if (hotkey.second.count() == 1 && + hotkey.second[0] == ev->keyCombination()) + { + Q_EMIT HotkeyPressed(hotkey.first); + } + } +} + +void HotkeyManager::HandleKeyRelease(QKeyEvent* ev) +{ + Q_UNUSED(ev); +} + +std::shared_ptr HotkeyManager::Instance() +{ + static std::weak_ptr hotkeyManagerReference_ {}; + static std::mutex instanceMutex_ {}; + + std::unique_lock lock(instanceMutex_); + + std::shared_ptr hotkeyManager = + hotkeyManagerReference_.lock(); + + if (hotkeyManager == nullptr) + { + hotkeyManager = std::make_shared(); + hotkeyManagerReference_ = hotkeyManager; + } + + return hotkeyManager; +} + +} // namespace manager +} // namespace qt +} // namespace scwx diff --git a/scwx-qt/source/scwx/qt/manager/hotkey_manager.hpp b/scwx-qt/source/scwx/qt/manager/hotkey_manager.hpp new file mode 100644 index 00000000..49f7576d --- /dev/null +++ b/scwx-qt/source/scwx/qt/manager/hotkey_manager.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include + +#include + +#include + +class QKeyEvent; + +namespace scwx +{ +namespace qt +{ +namespace manager +{ + +class HotkeyManager : public QObject +{ + Q_OBJECT + Q_DISABLE_COPY_MOVE(HotkeyManager) + +public: + explicit HotkeyManager(); + ~HotkeyManager(); + + void HandleKeyPress(QKeyEvent* event); + void HandleKeyRelease(QKeyEvent* event); + + static std::shared_ptr Instance(); + +signals: + void HotkeyPressed(scwx::qt::types::Hotkey hotkey); + void HotkeyReleased(scwx::qt::types::Hotkey hotkey); + +private: + class Impl; + std::unique_ptr p; +}; + +} // namespace manager +} // namespace qt +} // namespace scwx