Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qcomposeplatforminputcontext.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include <QtCore/QCoreApplication>
6#include <QtCore/qvarlengtharray.h>
7#include <QtGui/QKeyEvent>
8#include <QtGui/QGuiApplication>
9
10#include <locale.h>
11
13
14Q_LOGGING_CATEGORY(lcXkbCompose, "qt.xkb.compose")
15
17{
18 setObjectName(QStringLiteral("QComposeInputContext"));
19 qCDebug(lcXkbCompose, "using xkb compose input context");
20}
21
23{
24 xkb_compose_state_unref(m_composeState);
25 xkb_compose_table_unref(m_composeTable);
26}
27
29{
30 if (m_initialized)
31 return;
32
33 if (!m_XkbContext) {
34 qCWarning(lcXkbCompose) << "error: xkb context has not been set on" << metaObject()->className();
35 return;
36 }
37
38 m_initialized = true;
39 // Get locale from user env settings, see also
40 // https://xkbcommon.org/doc/current/group__compose.html#compose-locale
41 const char *locale = getenv("LC_ALL");
42 if (!locale || !*locale)
43 locale = getenv("LC_CTYPE");
44 if (!locale || !*locale)
45 locale = getenv("LANG");
46 if (!locale || !*locale)
47 locale = "C";
48 qCDebug(lcXkbCompose) << "detected locale:" << locale;
49
50 m_composeTable = xkb_compose_table_new_from_locale(m_XkbContext, locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
51 if (m_composeTable)
52 m_composeState = xkb_compose_state_new(m_composeTable, XKB_COMPOSE_STATE_NO_FLAGS);
53
54 if (!m_composeTable) {
55 qCWarning(lcXkbCompose, "failed to create compose table");
56 return;
57 }
58 if (!m_composeState) {
59 qCWarning(lcXkbCompose, "failed to create compose state");
60 return;
61 }
62}
63
65{
66 auto keyEvent = static_cast<const QKeyEvent *>(event);
67 if (keyEvent->type() != QEvent::KeyPress)
68 return false;
69
71 return false;
72
73 // lazy initialization - we don't want to do this on an app startup
75
76 if (!m_composeTable || !m_composeState)
77 return false;
78
79 xkb_compose_state_feed(m_composeState, keyEvent->nativeVirtualKey());
80
81 switch (xkb_compose_state_get_status(m_composeState)) {
82 case XKB_COMPOSE_COMPOSING:
83 return true;
84 case XKB_COMPOSE_CANCELLED:
85 reset();
86 return false;
87 case XKB_COMPOSE_COMPOSED:
88 {
89 const int size = xkb_compose_state_get_utf8(m_composeState, nullptr, 0);
90 QVarLengthArray<char, 32> buffer(size + 1);
91 xkb_compose_state_get_utf8(m_composeState, buffer.data(), buffer.size());
92 QString composedText = QString::fromUtf8(buffer.constData());
93
95 event.setCommitString(composedText);
96
97 if (!m_focusObject && qApp)
98 m_focusObject = qApp->focusObject();
99
100 if (m_focusObject)
101 QCoreApplication::sendEvent(m_focusObject, &event);
102 else
103 qCWarning(lcXkbCompose, "no focus object");
104
105 reset();
106 return true;
107 }
108 case XKB_COMPOSE_NOTHING:
109 return false;
110 default:
111 Q_UNREACHABLE_RETURN(false);
112 }
113}
114
116{
117 return true;
118}
119
121{
122 m_focusObject = object;
123}
124
126{
127 if (m_composeState)
128 xkb_compose_state_reset(m_composeState);
129}
130
131void QComposeInputContext::update(Qt::InputMethodQueries q)
132{
134}
135
137
138#include "moc_qcomposeplatforminputcontext.cpp"
void reset() override
Method to be called when input method needs to be reset.
bool filterEvent(const QEvent *event) override
This function can be reimplemented to filter input events.
void setFocusObject(QObject *object) override
This virtual method gets called to notify updated focus to object.
bool isValid() const override
Returns input context validity.
void update(Qt::InputMethodQueries) override
Notification on editor updates.
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
\inmodule QtCore
Definition qcoreevent.h:45
@ KeyPress
Definition qcoreevent.h:64
The QInputMethodEvent class provides parameters for input method events.
Definition qevent.h:625
The QKeyEvent class describes a key event.
Definition qevent.h:424
\inmodule QtCore
Definition qobject.h:103
bool inputMethodAccepted() const
Returns true if current focus object supports input method events.
virtual QLocale locale() const
virtual void update(Qt::InputMethodQueries)
Notification on editor updates.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
object setObjectName("A new object name")
Combined button and popup list for selecting options.
#define qApp
#define Q_LOGGING_CATEGORY(name,...)
#define qCWarning(category,...)
#define qCDebug(category,...)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint object
[3]
GLenum GLuint buffer
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
#define QStringLiteral(str)
obj metaObject() -> className()