Simplify QCocoaKeyMapper by always returning a valid keymap

QCocoaKeyMapper::updateKeyMap would always end up creating a
valid key map, so the logic in CocoaKeyMapper::possibleKeys
for dealing with missing keymaps was not needed, and was
likely copied from one of the other key map implementations.

Since we know that we have a key map we might as well return
it after possibly updating it.

Change-Id: If83974f4ddedae8b1acefbadef48da3ee326eadd
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Tor Arne Vestbø 2020-06-28 16:04:51 +02:00
parent d962de314b
commit 3021b1ecb7
2 changed files with 16 additions and 17 deletions

View File

@ -85,7 +85,7 @@ public:
private:
bool updateKeyboard();
void deleteLayouts();
void updateKeyMap(unsigned short macVirtualKey, QChar unicodeKey);
KeyboardLayoutItem *keyMapForKey(unsigned short macVirtualKey, QChar unicodeKey) const;
void clearMappings();
QCFType<TISInputSourceRef> m_currentInputSource = nullptr;
@ -93,8 +93,8 @@ private:
enum { NullMode, UnicodeMode, OtherMode } m_keyboardMode = NullMode;
const UCKeyboardLayout *m_keyboardLayoutFormat = nullptr;
KeyboardLayoutKind m_keyboardKind = kKLKCHRuchrKind;
UInt32 m_deadKeyState = 0; // Maintains dead key state beween calls to UCKeyTranslate
KeyboardLayoutItem *m_keyLayout[256];
mutable UInt32 m_deadKeyState = 0; // Maintains dead key state beween calls to UCKeyTranslate
mutable KeyboardLayoutItem *m_keyLayout[256];
};
QT_END_NAMESPACE

View File

@ -363,15 +363,16 @@ static constexpr Qt::KeyboardModifiers modifierCombinations[] = {
};
/*
Updates the key map for the given \macVirtualKey by applying
all possible modifier combinations.
Returns a key map for the given \macVirtualKey based on all
possible modifier combinations.
*/
void QCocoaKeyMapper::updateKeyMap(unsigned short macVirtualKey, QChar unicodeKey)
KeyboardLayoutItem *QCocoaKeyMapper::keyMapForKey(unsigned short macVirtualKey, QChar unicodeKey) const
{
updateKeyboard();
const_cast<QCocoaKeyMapper *>(this)->updateKeyboard();
if (m_keyLayout[macVirtualKey])
return;
Q_ASSERT(macVirtualKey < 256);
if (auto *existingKeyMap = m_keyLayout[macVirtualKey])
return existingKeyMap;
qCDebug(lcQpaKeyMapper, "Updating key map for virtual key = 0x%02x!", (uint)macVirtualKey);
@ -402,27 +403,25 @@ void QCocoaKeyMapper::updateKeyMap(unsigned short macVirtualKey, QChar unicodeKe
qCDebug(lcQpaKeyMapper, " [%d] (%d,0x%02x,'%c')", i, qtkey, qtkey, qtkey);
}
return m_keyLayout[macVirtualKey];
}
QList<int> QCocoaKeyMapper::possibleKeys(const QKeyEvent *event) const
{
QList<int> ret;
auto virtualKey = event->nativeVirtualKey();
const_cast<QCocoaKeyMapper *>(this)->updateKeyMap(virtualKey, QChar(event->key()));
KeyboardLayoutItem *keyLayout = m_keyLayout[virtualKey];
auto *keyMap = keyMapForKey(event->nativeVirtualKey(), QChar(event->key()));
Q_ASSERT(keyMap);
if (!keyLayout) // Key is not in any keyboard layout (e.g. eisu-key on Japanese keyboard)
return ret;
int baseKey = keyLayout->qtKey[Qt::NoModifier];
int baseKey = keyMap->qtKey[Qt::NoModifier];
auto eventModifiers = event->modifiers();
// The base key is always valid
ret << int(baseKey + eventModifiers);
for (int i = 1; i < 8; ++i) {
int keyAfterApplyingModifiers = keyLayout->qtKey[i];
int keyAfterApplyingModifiers = keyMap->qtKey[i];
if (!keyAfterApplyingModifiers)
continue;
if (keyAfterApplyingModifiers == baseKey)