diff --git a/src/blackmisc/hwkeyboardkey.cpp b/src/blackmisc/hwkeyboardkey.cpp index cb8099c76..d7dda95d1 100644 --- a/src/blackmisc/hwkeyboardkey.cpp +++ b/src/blackmisc/hwkeyboardkey.cpp @@ -268,6 +268,7 @@ namespace BlackMisc added = true; } + if (added) this->cleanup(); return added; } @@ -291,7 +292,6 @@ namespace BlackMisc removed = true; } cleanup(); - return removed; } @@ -300,6 +300,20 @@ namespace BlackMisc return removeModifier(modifierFromString(modifier)); } + bool CKeyboardKey::equalsModifierReleaxed(CKeyboardKey::Modifier m1, CKeyboardKey::Modifier m2) + { + if (m1 == m2) return true; + if (m1 == ModifierAltAny && (m2 == ModifierAltLeft || m2 == ModifierAltRight)) return true; + if (m1 == ModifierCtrlAny && (m2 == ModifierCtrlLeft || m2 == ModifierCtrlRight)) return true; + if (m1 == ModifierShiftAny && (m2 == ModifierShiftLeft || m2 == ModifierShiftRight)) return true; + + if (m2 == ModifierAltAny && (m1 == ModifierAltLeft || m1 == ModifierAltRight)) return true; + if (m2 == ModifierCtrlAny && (m1 == ModifierCtrlLeft || m1 == ModifierCtrlRight)) return true; + if (m2 == ModifierShiftAny && (m1 == ModifierShiftLeft || m1 == ModifierShiftRight)) return true; + + return false; + } + QString CKeyboardKey::getFunctionAsString() const { switch (this->m_function) @@ -310,6 +324,7 @@ namespace BlackMisc case HotkeyOpacity100: return QCoreApplication::translate("Hotkey", "Opacity 100%"); case HotkeyToggleCom1: return QCoreApplication::translate("Hotkey", "Toggle COM1"); case HotkeyToggleCom2: return QCoreApplication::translate("Hotkey", "Toggle COM2"); + case HotkeyToogleWindowsStayOnTop: return QCoreApplication::translate("Hotkey", "Toogle Window on top"); default: qFatal("Wrong function"); return ""; @@ -321,6 +336,7 @@ namespace BlackMisc (void)QT_TRANSLATE_NOOP("Hotkey", "Opacity 100%"); (void)QT_TRANSLATE_NOOP("Hotkey", "Toggle COM1"); (void)QT_TRANSLATE_NOOP("Hotkey", "Toggle COM2"); + (void)QT_TRANSLATE_NOOP("Hotkey", "Toogle Window on top"); } void CKeyboardKey::setKeyObject(const CKeyboardKey &key) @@ -332,6 +348,33 @@ namespace BlackMisc this->m_qtKey = key.m_qtKey; } + bool CKeyboardKey::equalsWithRelaxedModifiers(const CKeyboardKey &key, bool ignoreFunction) const + { + if (key == (*this)) return true; // fully equal, not need to bother + + // this can never be true + if (key.m_qtKey != this->m_qtKey) return false; + if (!ignoreFunction && key.m_function != this->m_function) return false; + if (this->numberOfModifiers() != key.numberOfModifiers()) return false; + + // special modifiers + if (this->getModifier1() != key.getModifier1()) + { + if (!CKeyboardKey::equalsModifierReleaxed(this->getModifier1(), key.getModifier1())) return false; + } + return CKeyboardKey::equalsModifierReleaxed(this->getModifier1(), key.getModifier1()); + } + + bool CKeyboardKey::equalsWithoutFunction(const CKeyboardKey &key) const + { + if (key == (*this)) return true; + CKeyboardKey k1(*this); + CKeyboardKey k2(*this); + k1.setFunction(HotkeyNone); + k2.setFunction(HotkeyNone); + return k1 == k2; + } + QVariant CKeyboardKey::propertyByIndex(int index) const { switch (index) diff --git a/src/blackmisc/hwkeyboardkey.h b/src/blackmisc/hwkeyboardkey.h index 4784297a2..55fcfdceb 100644 --- a/src/blackmisc/hwkeyboardkey.h +++ b/src/blackmisc/hwkeyboardkey.h @@ -46,7 +46,8 @@ namespace BlackMisc HotkeyToggleCom1, HotkeyToggleCom2, HotkeyOpacity50, - HotkeyOpacity100 + HotkeyOpacity100, + HotkeyToogleWindowsStayOnTop }; //! Modifier @@ -75,7 +76,7 @@ namespace BlackMisc //! Constructor CKeyboardKey(Qt::Key keyCode, quint32 nativeVirtualKey, Modifier modifier1 = ModifierNone, Modifier modifier2 = ModifierNone, const HotkeyFunction &function = HotkeyNone); - //! \brief Destructor + //! Destructor ~CKeyboardKey() {} //! \copydoc CValueObject::toQVariant @@ -93,7 +94,7 @@ namespace BlackMisc //! \copydoc CValueObject::fromJson void fromJson(const QJsonObject &json) override; - //! \brief Register metadata + //! Register metadata static void registerMetadata(); //! \copydoc TupleConverter<>::jsonMembers() @@ -196,6 +197,9 @@ namespace BlackMisc //! Set modifier 2 void setModifier2(const Modifier &modifier) { this->m_modifier2 = modifier; } + //! Is empty, (no key, no modifier)? + bool isEmpty() const { return !this->hasModifier() && !this->hasKey(); } + //! Modifier? bool hasModifier() const { @@ -208,13 +212,16 @@ namespace BlackMisc return m_modifier1 == modifier || m_modifier2 == modifier; } - //! \brief with key? + //! ALT = ALT/R or ALT/L, CTRL = CTRL/R or left, ... + static bool equalsModifierReleaxed(Modifier m1, Modifier m2); + + //! with key? bool hasKey() const { return !(this->m_qtKey == Qt::Key_unknown); } - //! \brief Key + modifier? + //! Key + modifier? bool hasKeyAndModifier() const { return this->hasKey() && this->hasModifier(); @@ -232,6 +239,12 @@ namespace BlackMisc //! Set function void setFunction(const HotkeyFunction &function) { this->m_function = function; } + //! CTRL will be consider equal CTRL-left/reigt, ALT = ALT-left/right .. + bool equalsWithRelaxedModifiers(const CKeyboardKey &key, bool ignoreFunction = false) const; + + //! Equal, but function value ignored + bool equalsWithoutFunction(const CKeyboardKey &key) const; + //! \copydoc CValueObject::setPropertyByIndex virtual void setPropertyByIndex(const QVariant &variant, int index); diff --git a/src/blackmisc/hwkeyboardkeylist.cpp b/src/blackmisc/hwkeyboardkeylist.cpp index dcb1e51d6..7f8485728 100644 --- a/src/blackmisc/hwkeyboardkeylist.cpp +++ b/src/blackmisc/hwkeyboardkeylist.cpp @@ -30,6 +30,16 @@ namespace BlackMisc return CSequence::contains(&CKeyboardKey::getFunction, function); } + /* + * Key for function + */ + CKeyboardKey CKeyboardKeyList::keyForFunction(CKeyboardKey::HotkeyFunction function) const + { + CKeyboardKeyList keys = this->findBy(&CKeyboardKey::getFunction, function); + if (keys.isEmpty()) + return CKeyboardKey(CKeyboardKey::HotkeyNone); + else + return keys[0]; } /* @@ -56,6 +66,7 @@ namespace BlackMisc if (!this->containsFunction(CKeyboardKey::HotkeyOpacity100)) this->push_back(CKeyboardKey(CKeyboardKey::HotkeyOpacity100)); if (!this->containsFunction(CKeyboardKey::HotkeyToggleCom1)) this->push_back(CKeyboardKey(CKeyboardKey::HotkeyToggleCom1)); if (!this->containsFunction(CKeyboardKey::HotkeyToggleCom2)) this->push_back(CKeyboardKey(CKeyboardKey::HotkeyToggleCom2)); + if (!this->containsFunction(CKeyboardKey::HotkeyToogleWindowsStayOnTop)) this->push_back(CKeyboardKey(CKeyboardKey::HotkeyToogleWindowsStayOnTop)); } } // namespace diff --git a/src/blackmisc/hwkeyboardkeylist.h b/src/blackmisc/hwkeyboardkeylist.h index ea8e217e2..6bf2d7e8d 100644 --- a/src/blackmisc/hwkeyboardkeylist.h +++ b/src/blackmisc/hwkeyboardkeylist.h @@ -41,6 +41,8 @@ namespace BlackMisc bool containsFunction(CKeyboardKey::HotkeyFunction function) const; //! Key for given function + CKeyboardKey keyForFunction(CKeyboardKey::HotkeyFunction function) const; + //! Register metadata static void registerMetadata(); diff --git a/tests/blackmisc/testhardware.cpp b/tests/blackmisc/testhardware.cpp index d1dc6ff53..9c3333d1f 100644 --- a/tests/blackmisc/testhardware.cpp +++ b/tests/blackmisc/testhardware.cpp @@ -54,6 +54,20 @@ namespace BlackMiscTest QVERIFY2(key.numberOfModifiers() == 1, "Expected number of modifiers to be equal to 1"); QVERIFY2(key.getModifier1() == CKeyboardKey::ModifierAltLeft, "Expected modifier to be ModifierAltLeft"); QVERIFY2(key.getModifier2() == CKeyboardKey::ModifierNone, "Expected modifier to be ModifierAltLeft"); + + + // relaxed checks + key = CKeyboardKey(Qt::Key_1, 0, CKeyboardKey::ModifierCtrlLeft, CKeyboardKey::ModifierNone, CKeyboardKey::HotkeyPtt); + key2 = CKeyboardKey(Qt::Key_1, 0, CKeyboardKey::ModifierCtrlLeft, CKeyboardKey::ModifierNone, CKeyboardKey::HotkeyOpacity50); + + QVERIFY2(key != key2, "Function differs, values shall be unequal"); + QVERIFY2(key.equalsWithoutFunction(key2), "Function differs, values shall be equal without function"); + key2 = key; + key2.setModifier1(CKeyboardKey::ModifierCtrlAny); + QVERIFY2(key != key2, "Modifiers differs, values shall be unequal"); + QVERIFY2(key.equalsWithRelaxedModifiers(key2), "Modifiers are relaxed easy, values shall be equal"); + key2.setFunction(CKeyboardKey::HotkeyToggleCom2); + QVERIFY2(key.equalsWithRelaxedModifiers(key2, true), "Modifiers are relaxed easy, function ignored, value shall be equal without function"); } } // namespace