Long live QPlatformKeyMapper!

The QKeyMapper class never got a platform integration companion.

As we might be adding more functionality here in the future, let's
fix that now, instead of adding more hooks directly to the platform
integration class.

The QKeyMapper will soon update its possibleKeys signature to
return QKeyCombination, but for now transform the result.

Change-Id: I88ef498180b2a8a7937a74627b7eb6b5156e872a
Reviewed-by: Liang Qi <liang.qi@qt.io>
This commit is contained in:
Tor Arne Vestbø 2023-09-20 23:35:13 +02:00
parent d9bb8c0a17
commit c74cfae7a3
9 changed files with 115 additions and 9 deletions

View File

@ -114,6 +114,7 @@ qt_internal_add_module(Gui
kernel/qplatformintegration.cpp kernel/qplatformintegration.h
kernel/qplatformintegrationfactory.cpp kernel/qplatformintegrationfactory_p.h
kernel/qplatformintegrationplugin.cpp kernel/qplatformintegrationplugin.h
kernel/qplatformkeymapper.cpp kernel/qplatformkeymapper.h
kernel/qplatformmenu.cpp kernel/qplatformmenu.h kernel/qplatformmenu_p.h
kernel/qplatformnativeinterface.cpp kernel/qplatformnativeinterface.h
kernel/qplatformoffscreensurface.cpp kernel/qplatformoffscreensurface.h

View File

@ -17,6 +17,7 @@
#include <qpa/qplatformnativeinterface.h>
#include <qpa/qplatformtheme.h>
#include <qpa/qplatformintegration.h>
#include <qpa/qplatformkeymapper.h>
#include <QtCore/QAbstractEventDispatcher>
#include <QtCore/QFileInfo>
@ -1847,7 +1848,7 @@ Qt::KeyboardModifiers QGuiApplication::queryKeyboardModifiers()
{
CHECK_QAPP_INSTANCE(Qt::KeyboardModifiers{})
QPlatformIntegration *pi = QGuiApplicationPrivate::platformIntegration();
return pi->queryKeyboardModifiers();
return pi->keyMapper()->queryKeyboardModifiers();
}
/*!

View File

@ -9,6 +9,7 @@
#include <private/qguiapplication_p.h>
#include <qpa/qplatformintegration.h>
#include <qpa/qplatformkeymapper.h>
QT_BEGIN_NAMESPACE
@ -36,14 +37,21 @@ QKeyMapper::~QKeyMapper()
QList<int> QKeyMapper::possibleKeys(const QKeyEvent *e)
{
QList<int> result = QGuiApplicationPrivate::platformIntegration()->possibleKeys(e);
if (!result.isEmpty())
return result;
QList<int> result;
const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
const auto *platformKeyMapper = platformIntegration->keyMapper();
const auto keyCombinations = platformKeyMapper->possibleKeyCombinations(e);
for (auto keyCombination : keyCombinations)
result << keyCombination.toCombined();
if (result.isEmpty()) {
if (e->key() && (e->key() != Qt::Key_unknown))
result << e->keyCombination().toCombined();
else if (!e->text().isEmpty())
result << int(e->text().at(0).unicode() + (int)e->modifiers());
}
return result;
}

View File

@ -6,6 +6,7 @@
#include <qpa/qplatformfontdatabase.h>
#include <qpa/qplatformclipboard.h>
#include <qpa/qplatformaccessibility.h>
#include <qpa/qplatformkeymapper.h>
#include <qpa/qplatformtheme.h>
#include <QtGui/private/qguiapplication_p.h>
#include <QtGui/private/qpixmap_raster_p.h>
@ -342,6 +343,21 @@ QPlatformInputContext *QPlatformIntegration::inputContext() const
return nullptr;
}
/*!
Accessor for the platform integration's key mapper.
Default implementation returns a default QPlatformKeyMapper.
\sa QPlatformKeyMapper
*/
QPlatformKeyMapper *QPlatformIntegration::keyMapper() const
{
static QPlatformKeyMapper *keyMapper = nullptr;
if (!keyMapper)
keyMapper = new QPlatformKeyMapper;
return keyMapper;
}
#if QT_CONFIG(accessibility)
/*!

View File

@ -33,6 +33,7 @@ class QPlatformOpenGLContext;
class QGuiGLFormat;
class QAbstractEventDispatcher;
class QPlatformInputContext;
class QPlatformKeyMapper;
class QPlatformAccessibility;
class QPlatformTheme;
class QPlatformDialogHelper;
@ -171,8 +172,12 @@ public:
virtual QVariant styleHint(StyleHint hint) const;
virtual Qt::WindowState defaultWindowState(Qt::WindowFlags) const;
protected:
virtual Qt::KeyboardModifiers queryKeyboardModifiers() const;
virtual QList<int> possibleKeys(const QKeyEvent *) const;
friend class QPlatformKeyMapper;
public:
virtual QPlatformKeyMapper *keyMapper() const;
virtual QStringList themeNames() const;
virtual QPlatformTheme *createPlatformTheme(const QString &name) const;

View File

@ -0,0 +1,38 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qplatformkeymapper.h"
#include <private/qguiapplication_p.h>
#include <qpa/qplatformintegration.h>
QT_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(lcQpaKeyMapper, "qt.qpa.keymapper")
QPlatformKeyMapper::~QPlatformKeyMapper()
{
}
/*
Should return a list of possible key combinations for the given key event.
For example, given a US English keyboard layout, the key event Shift+5
can represent both a "Shift+5" key combination, as well as just "%".
*/
QList<QKeyCombination> QPlatformKeyMapper::possibleKeyCombinations(const QKeyEvent *event) const
{
auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
QList<int> possibleKeys = platformIntegration->possibleKeys(event);
QList<QKeyCombination> combinations;
for (int key : possibleKeys)
combinations << QKeyCombination::fromCombined(key);
return combinations;
}
Qt::KeyboardModifiers QPlatformKeyMapper::queryKeyboardModifiers() const
{
return QGuiApplicationPrivate::platformIntegration()->queryKeyboardModifiers();
}
QT_END_NAMESPACE

View File

@ -0,0 +1,36 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QPLATFORMKEYMAPPER_P
#define QPLATFORMKEYMAPPER_P
//
// W A R N I N G
// -------------
//
// This file is part of the QPA API and is not meant to be used
// in applications. Usage of this API may make your code
// source and binary incompatible with future versions of Qt.
//
#include <QtGui/qtguiglobal.h>
#include <QtCore/qloggingcategory.h>
QT_BEGIN_NAMESPACE
Q_DECLARE_EXPORTED_LOGGING_CATEGORY(lcQpaKeyMapper, Q_GUI_EXPORT)
class QKeyEvent;
class Q_GUI_EXPORT QPlatformKeyMapper
{
public:
virtual ~QPlatformKeyMapper();
virtual QList<QKeyCombination> possibleKeyCombinations(const QKeyEvent *event) const;
virtual Qt::KeyboardModifiers queryKeyboardModifiers() const;
};
QT_END_NAMESPACE
#endif // QPLATFORMKEYMAPPER_P

View File

@ -18,7 +18,6 @@
QT_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(lcQpaKeyMapper, "qt.qpa.keymapper");
Q_LOGGING_CATEGORY(lcQpaKeyMapperKeys, "qt.qpa.keymapper.keys");
static Qt::KeyboardModifiers swapModifiersIfNeeded(const Qt::KeyboardModifiers modifiers)

View File

@ -19,6 +19,8 @@
#include <Carbon/Carbon.h>
#endif
#include <qpa/qplatformkeymapper.h>
#include <QtCore/QList>
#include <QtCore/QHash>
#include <QtGui/QKeyEvent>