refs #233 Keyboard key/key list

* helper methods for keylist (key for function)
* new Hotkey function (toggle window on top)
* improved comparisons for key
* unit tests
This commit is contained in:
Klaus Basan
2014-05-07 23:16:28 +02:00
parent 17f617f418
commit 421d2d5411
5 changed files with 89 additions and 6 deletions

View File

@@ -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)

View File

@@ -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);

View File

@@ -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

View File

@@ -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();