diff --git a/scwx-qt/source/scwx/qt/main/main_window.cpp b/scwx-qt/source/scwx/qt/main/main_window.cpp index 3249577a..93ee8f4b 100644 --- a/scwx-qt/source/scwx/qt/main/main_window.cpp +++ b/scwx-qt/source/scwx/qt/main/main_window.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -197,7 +199,9 @@ public: QTimer clockTimer_ {}; - std::shared_ptr alertManager_; + std::shared_ptr alertManager_; + std::shared_ptr hotkeyManager_ { + manager::HotkeyManager::Instance()}; std::shared_ptr placefileManager_; std::shared_ptr positionManager_; std::shared_ptr textEventManager_; @@ -398,6 +402,24 @@ MainWindow::~MainWindow() delete ui; } +void MainWindow::keyPressEvent(QKeyEvent* ev) +{ + if (p->hotkeyManager_->HandleKeyPress(ev)) + { + p->activeMap_->update(); + ev->accept(); + } +} + +void MainWindow::keyReleaseEvent(QKeyEvent* ev) +{ + if (p->hotkeyManager_->HandleKeyRelease(ev)) + { + p->activeMap_->update(); + ev->accept(); + } +} + void MainWindow::showEvent(QShowEvent* event) { QMainWindow::showEvent(event); diff --git a/scwx-qt/source/scwx/qt/main/main_window.hpp b/scwx-qt/source/scwx/qt/main/main_window.hpp index 21592538..d0adf225 100644 --- a/scwx-qt/source/scwx/qt/main/main_window.hpp +++ b/scwx-qt/source/scwx/qt/main/main_window.hpp @@ -26,6 +26,8 @@ public: MainWindow(QWidget* parent = nullptr); ~MainWindow(); + void keyPressEvent(QKeyEvent* ev) override final; + void keyReleaseEvent(QKeyEvent* ev) override final; void showEvent(QShowEvent* event) override; signals: diff --git a/scwx-qt/source/scwx/qt/manager/hotkey_manager.cpp b/scwx-qt/source/scwx/qt/manager/hotkey_manager.cpp index 5ae17ed8..28575438 100644 --- a/scwx-qt/source/scwx/qt/manager/hotkey_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/hotkey_manager.cpp @@ -64,34 +64,44 @@ void HotkeyManager::Impl::UpdateHotkey(types::Hotkey hotkey, QKeySequence {QString::fromStdString(value)}); } -void HotkeyManager::HandleKeyPress(QKeyEvent* ev) +bool HotkeyManager::HandleKeyPress(QKeyEvent* ev) { logger_->trace("HandleKeyPress: {}, {}", ev->keyCombination().toCombined(), ev->isAutoRepeat()); + bool hotkeyPressed = false; + for (auto& hotkey : p->hotkeys_) { if (hotkey.second.count() == 1 && hotkey.second[0] == ev->keyCombination()) { + hotkeyPressed = true; Q_EMIT HotkeyPressed(hotkey.first, ev->isAutoRepeat()); } } + + return hotkeyPressed; } -void HotkeyManager::HandleKeyRelease(QKeyEvent* ev) +bool HotkeyManager::HandleKeyRelease(QKeyEvent* ev) { logger_->trace("HandleKeyRelease: {}", ev->keyCombination().toCombined()); + bool hotkeyReleased = false; + for (auto& hotkey : p->hotkeys_) { if (hotkey.second.count() == 1 && hotkey.second[0] == ev->keyCombination()) { + hotkeyReleased = true; Q_EMIT HotkeyReleased(hotkey.first); } } + + return hotkeyReleased; } std::shared_ptr HotkeyManager::Instance() diff --git a/scwx-qt/source/scwx/qt/manager/hotkey_manager.hpp b/scwx-qt/source/scwx/qt/manager/hotkey_manager.hpp index 0a38709d..b4780714 100644 --- a/scwx-qt/source/scwx/qt/manager/hotkey_manager.hpp +++ b/scwx-qt/source/scwx/qt/manager/hotkey_manager.hpp @@ -24,8 +24,8 @@ public: explicit HotkeyManager(); ~HotkeyManager(); - void HandleKeyPress(QKeyEvent* event); - void HandleKeyRelease(QKeyEvent* event); + bool HandleKeyPress(QKeyEvent* event); + bool HandleKeyRelease(QKeyEvent* event); static std::shared_ptr Instance(); diff --git a/scwx-qt/source/scwx/qt/map/map_widget.cpp b/scwx-qt/source/scwx/qt/map/map_widget.cpp index 5a3aa696..9fb5c253 100644 --- a/scwx-qt/source/scwx/qt/map/map_widget.cpp +++ b/scwx-qt/source/scwx/qt/map/map_widget.cpp @@ -359,28 +359,33 @@ void MapWidgetImpl::HandleHotkeyPressed(types::Hotkey hotkey, bool isAutoRepeat) switch (hotkey) { case types::Hotkey::ChangeMapStyle: - widget_->changeStyle(); + if (context_->settings().isActive_) + { + widget_->changeStyle(); + } break; case types::Hotkey::CopyCursorCoordinates: - { - QClipboard* clipboard = QGuiApplication::clipboard(); - auto coordinate = map_->coordinateForPixel(lastPos_); - std::string text = - fmt::format("{}, {}", coordinate.first, coordinate.second); - clipboard->setText(QString::fromStdString(text)); + if (hasMouse_) + { + QClipboard* clipboard = QGuiApplication::clipboard(); + auto coordinate = map_->coordinateForPixel(lastPos_); + std::string text = + fmt::format("{}, {}", coordinate.first, coordinate.second); + clipboard->setText(QString::fromStdString(text)); + } break; - } case types::Hotkey::CopyMapCoordinates: - { - QClipboard* clipboard = QGuiApplication::clipboard(); - auto coordinate = map_->coordinate(); - std::string text = - fmt::format("{}, {}", coordinate.first, coordinate.second); - clipboard->setText(QString::fromStdString(text)); + if (context_->settings().isActive_) + { + QClipboard* clipboard = QGuiApplication::clipboard(); + auto coordinate = map_->coordinate(); + std::string text = + fmt::format("{}, {}", coordinate.first, coordinate.second); + clipboard->setText(QString::fromStdString(text)); + } break; - } default: break; @@ -391,6 +396,8 @@ void MapWidgetImpl::HandleHotkeyPressed(types::Hotkey hotkey, bool isAutoRepeat) void MapWidgetImpl::HandleHotkeyReleased(types::Hotkey hotkey) { + // Erase the hotkey from the active set regardless of whether this is the + // active map activeHotkeys_.erase(hotkey); } @@ -409,6 +416,14 @@ void MapWidgetImpl::HandleHotkeyUpdates() hotkeyTime - prevHotkeyTime_), 100ms); + prevHotkeyTime_ = hotkeyTime; + + if (!context_->settings().isActive_) + { + // Don't attempt to handle a hotkey if this is not the active map + return; + } + for (auto& hotkey : activeHotkeys_) { switch (hotkey) @@ -480,8 +495,6 @@ void MapWidgetImpl::HandleHotkeyUpdates() break; } } - - prevHotkeyTime_ = hotkeyTime; } common::Level3ProductCategoryMap MapWidget::GetAvailableLevel3Categories() @@ -1212,16 +1225,18 @@ void MapWidget::leaveEvent(QEvent* /* ev */) void MapWidget::keyPressEvent(QKeyEvent* ev) { - p->hotkeyManager_->HandleKeyPress(ev); - - ev->accept(); + if (p->hotkeyManager_->HandleKeyPress(ev)) + { + ev->accept(); + } } void MapWidget::keyReleaseEvent(QKeyEvent* ev) { - p->hotkeyManager_->HandleKeyRelease(ev); - - ev->accept(); + if (p->hotkeyManager_->HandleKeyRelease(ev)) + { + ev->accept(); + } } void MapWidget::mousePressEvent(QMouseEvent* ev)