From 563a69e3f595251d18137e62afa5c3d4508a1f37 Mon Sep 17 00:00:00 2001 From: Roland Rossgotterer Date: Thu, 11 Apr 2019 10:08:58 +0200 Subject: [PATCH] Remember and ignore hotkey combination before capturing When capturing a new hotkey combination, we want to get only the delta of the keys and buttons being pressed during the capturing. ref T585 --- src/blackcore/inputmanager.cpp | 12 +++++-- src/blackcore/inputmanager.h | 1 + .../components/settingshotkeycomponent.cpp | 12 ++++++- src/blackmisc/input/hotkeycombination.cpp | 33 +++++++++++++++++-- src/blackmisc/input/hotkeycombination.h | 13 ++++++-- 5 files changed, 62 insertions(+), 9 deletions(-) diff --git a/src/blackcore/inputmanager.cpp b/src/blackcore/inputmanager.cpp index ecc0b81d8..de532bb99 100644 --- a/src/blackcore/inputmanager.cpp +++ b/src/blackcore/inputmanager.cpp @@ -93,6 +93,7 @@ namespace BlackCore { m_captureActive = true; m_capturedCombination = {}; + m_combinationBeforeCapture = m_lastCombination; } void CInputManager::callFunctionsBy(const QString &action, bool isKeyDown, bool shouldEmit) @@ -156,15 +157,20 @@ namespace BlackCore { if (m_captureActive) { - if (currentCombination.size() < m_capturedCombination.size()) + CHotkeyCombination deltaCombination = currentCombination.getDeltaComparedTo(m_combinationBeforeCapture); + + // Don't continue if there is no relevant combination yet + if (m_capturedCombination.isEmpty() && deltaCombination.isEmpty()) { return; } + + if (deltaCombination.size() < m_capturedCombination.size()) { emit combinationSelectionFinished(m_capturedCombination); m_captureActive = false; } else { - emit combinationSelectionChanged(currentCombination); - m_capturedCombination = currentCombination; + emit combinationSelectionChanged(deltaCombination); + m_capturedCombination = deltaCombination; } return; } diff --git a/src/blackcore/inputmanager.h b/src/blackcore/inputmanager.h index 2945be3d1..aaff459a7 100644 --- a/src/blackcore/inputmanager.h +++ b/src/blackcore/inputmanager.h @@ -147,6 +147,7 @@ namespace BlackCore bool m_captureActive = false; BlackMisc::Input::CHotkeyCombination m_lastCombination; BlackMisc::Input::CHotkeyCombination m_capturedCombination; + BlackMisc::Input::CHotkeyCombination m_combinationBeforeCapture; BlackMisc::CSetting m_actionHotkeys { this, &CInputManager::reloadHotkeySettings }; }; diff --git a/src/blackgui/components/settingshotkeycomponent.cpp b/src/blackgui/components/settingshotkeycomponent.cpp index b38bf1359..bb7a47d70 100644 --- a/src/blackgui/components/settingshotkeycomponent.cpp +++ b/src/blackgui/components/settingshotkeycomponent.cpp @@ -185,7 +185,17 @@ namespace BlackGui void CSettingsHotkeyComponent::hotkeySlot(bool keyDown) { - if (keyDown) { QMessageBox::information(this, "Test", "Hotkey test"); } + if (keyDown) + { + QMessageBox* msgBox = new QMessageBox(this); + msgBox->setAttribute(Qt::WA_DeleteOnClose); + msgBox->setStandardButtons(QMessageBox::Ok); + msgBox->setWindowTitle("Test"); + msgBox->setText("Hotkey test"); + msgBox->setIcon(QMessageBox::Information); + msgBox->setModal(false); + msgBox->open(); + } } bool CConfigHotkeyWizardPage::validatePage() diff --git a/src/blackmisc/input/hotkeycombination.cpp b/src/blackmisc/input/hotkeycombination.cpp index 68b3f9f18..bd982e765 100644 --- a/src/blackmisc/input/hotkeycombination.cpp +++ b/src/blackmisc/input/hotkeycombination.cpp @@ -25,20 +25,30 @@ namespace BlackMisc CHotkeyCombination::CHotkeyCombination(const CKeyboardKeyList &keys) : m_keyboardKeys(keys) { } - void CHotkeyCombination::addKeyboardKey(CKeyboardKey key) + void CHotkeyCombination::addKeyboardKey(const CKeyboardKey &key) { if (m_keyboardKeys.contains(key)) { return; } m_keyboardKeys.push_back(key); m_keyboardKeys.sortBy(&CKeyboardKey::getKey); } - void CHotkeyCombination::addJoystickButton(CJoystickButton button) + void CHotkeyCombination::addJoystickButton(const CJoystickButton &button) { if (m_joystickButtons.contains(button)) { return; } m_joystickButtons.push_back(button); m_joystickButtons.sortBy(&CJoystickButton::getButtonIndex); } + bool CHotkeyCombination::containsKeyboardKey(const CKeyboardKey &key) const + { + return m_keyboardKeys.contains(key); + } + + bool CHotkeyCombination::containsJoystickButton(const CJoystickButton &button) const + { + return m_joystickButtons.contains(button); + } + void CHotkeyCombination::replaceKey(CKeyboardKey oldKey, CKeyboardKey newKey) { if (oldKey.hasKey()) @@ -70,7 +80,24 @@ namespace BlackMisc bool CHotkeyCombination::isSubsetOf(const CHotkeyCombination &other) const { return std::all_of(m_keyboardKeys.begin(), m_keyboardKeys.end(), [&other](const CKeyboardKey & k) { return other.m_keyboardKeys.contains(k); }) && - std::all_of(m_joystickButtons.begin(), m_joystickButtons.end(), [&other](const CJoystickButton & b) { return other.m_joystickButtons.contains(b); }); + std::all_of(m_joystickButtons.begin(), m_joystickButtons.end(), [&other](const CJoystickButton & b) { return other.m_joystickButtons.contains(b); }); + } + + CHotkeyCombination CHotkeyCombination::getDeltaComparedTo(const CHotkeyCombination &other) const + { + CHotkeyCombination combination(*this); + const CKeyboardKeyList otherKeys = other.getKeyboardKeys(); + for (const CKeyboardKey &key : otherKeys) + { + if (containsKeyboardKey(key)) { combination.removeKeyboardKey(key); } + } + + const CJoystickButtonList otherButtons = other.getJoystickButtons(); + for (const CJoystickButton &button : otherButtons) + { + if (containsJoystickButton(button)) { combination.removeJoystickButton(button); } + } + return combination; } QString CHotkeyCombination::convertToQString(bool i18n) const diff --git a/src/blackmisc/input/hotkeycombination.h b/src/blackmisc/input/hotkeycombination.h index 1cdc4b15f..c18edb5f4 100644 --- a/src/blackmisc/input/hotkeycombination.h +++ b/src/blackmisc/input/hotkeycombination.h @@ -55,10 +55,16 @@ namespace BlackMisc CJoystickButtonList getJoystickButtons() const { return m_joystickButtons; } //! Add keyboard key - void addKeyboardKey(CKeyboardKey key); + void addKeyboardKey(const CKeyboardKey &key); //! Add joystick button - void addJoystickButton(CJoystickButton button); + void addJoystickButton(const CJoystickButton &button); + + //! Does combination contain key? + bool containsKeyboardKey(const CKeyboardKey &key) const; + + //! Does combination contain button? + bool containsJoystickButton(const CJoystickButton &button) const; //! Replace oldKey with newKey void replaceKey(CKeyboardKey oldKey, CKeyboardKey newKey); @@ -82,6 +88,9 @@ namespace BlackMisc //! E.g. CTRL would be a subset of CTRL+D bool isSubsetOf(const CHotkeyCombination &other) const; + //! Returns the delta (removing all keys and buttons contained in other) + CHotkeyCombination getDeltaComparedTo(const CHotkeyCombination &other) const; + //! \copydoc BlackMisc::Mixin::String::toQString QString convertToQString(bool i18n = false) const;