-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqplatforminputcontext.cpp
357 lines (305 loc) · 10.8 KB
/
qplatforminputcontext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (C) 2016 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 "qplatforminputcontext.h"
#include <qguiapplication.h>
#include <QRect>
#include "private/qkeymapper_p.h"
#include "private/qhighdpiscaling_p.h"
#include <qpa/qplatforminputcontext_p.h>
#include <QtGui/qtransform.h>
QT_BEGIN_NAMESPACE
/*!
\class QPlatformInputContext
\since 5.0
\internal
\preliminary
\ingroup qpa
\brief The QPlatformInputContext class abstracts the input method dependent data and composing state.
An input method is responsible for inputting complex text that cannot
be inputted via simple keymap. It converts a sequence of input
events (typically key events) into a text string through the input
method specific converting process. The class of the processes are
widely ranging from simple finite state machine to complex text
translator that pools a whole paragraph of a text with text
editing capability to perform grammar and semantic analysis.
To abstract such different input method specific intermediate
information, Qt offers the QPlatformInputContext as base class. The
concept is well known as 'input context' in the input method
domain. An input context is created for a text widget in response
to a demand. It is ensured that an input context is prepared for
an input method before input to a text widget.
QPlatformInputContext provides an interface the actual input methods
can derive from by reimplementing methods.
\sa QInputMethod
*/
/*!
\internal
*/
QPlatformInputContext::QPlatformInputContext()
: QObject(*(new QPlatformInputContextPrivate))
{
// Delay initialization of cached input direction
// until super class has finished constructing.
QMetaObject::invokeMethod(this, [this]{
m_inputDirection = inputDirection();
}, Qt::QueuedConnection);
}
/*!
\internal
*/
QPlatformInputContext::~QPlatformInputContext()
{
}
/*!
Returns input context validity. Deriving implementations should return true.
*/
bool QPlatformInputContext::isValid() const
{
return false;
}
/*!
Returns whether the implementation supports \a capability.
\internal
\since 5.4
*/
bool QPlatformInputContext::hasCapability(Capability capability) const
{
Q_UNUSED(capability);
return true;
}
/*!
Method to be called when input method needs to be reset. Called by QInputMethod::reset().
No further QInputMethodEvents should be sent as response.
*/
void QPlatformInputContext::reset()
{
}
void QPlatformInputContext::commit()
{
}
/*!
Notification on editor updates. Called by QInputMethod::update().
*/
void QPlatformInputContext::update(Qt::InputMethodQueries)
{
}
/*!
Called when the word currently being composed in the input item is tapped by
the user. Input methods often use this information to offer more word
suggestions to the user.
*/
void QPlatformInputContext::invokeAction(QInputMethod::Action action, int cursorPosition)
{
Q_UNUSED(cursorPosition);
// Default behavior for simple ephemeral input contexts. Some
// complex input contexts should not be reset here.
if (action == QInputMethod::Click)
reset();
}
/*!
This function can be reimplemented to filter input events.
Return true if the event has been consumed. Otherwise, the unfiltered event will
be forwarded to widgets as ordinary way. Although the input events have accept()
and ignore() methods, leave it untouched.
*/
bool QPlatformInputContext::filterEvent(const QEvent *event)
{
Q_UNUSED(event);
return false;
}
/*!
This function can be reimplemented to return virtual keyboard rectangle in currently active
window coordinates. Default implementation returns invalid rectangle.
*/
QRectF QPlatformInputContext::keyboardRect() const
{
return QRectF();
}
/*!
Active QPlatformInputContext is responsible for providing keyboardRectangle property to QInputMethod.
In addition of providing the value in keyboardRect function, it also needs to call this emit
function whenever the property changes.
*/
void QPlatformInputContext::emitKeyboardRectChanged()
{
emit QGuiApplication::inputMethod()->keyboardRectangleChanged();
}
/*!
This function can be reimplemented to return true whenever input method is animating
shown or hidden. Default implementation returns \c false.
*/
bool QPlatformInputContext::isAnimating() const
{
return false;
}
/*!
Active QPlatformInputContext is responsible for providing animating property to QInputMethod.
In addition of providing the value in isAnimation function, it also needs to call this emit
function whenever the property changes.
*/
void QPlatformInputContext::emitAnimatingChanged()
{
emit QGuiApplication::inputMethod()->animatingChanged();
}
/*!
Request to show input panel.
*/
void QPlatformInputContext::showInputPanel()
{
}
/*!
Request to hide input panel.
*/
void QPlatformInputContext::hideInputPanel()
{
}
/*!
Returns input panel visibility status. Default implementation returns \c false.
*/
bool QPlatformInputContext::isInputPanelVisible() const
{
return false;
}
/*!
Active QPlatformInputContext is responsible for providing visible property to QInputMethod.
In addition of providing the value in isInputPanelVisible function, it also needs to call this emit
function whenever the property changes.
*/
void QPlatformInputContext::emitInputPanelVisibleChanged()
{
emit QGuiApplication::inputMethod()->visibleChanged();
}
QLocale QPlatformInputContext::locale() const
{
return QLocale::system();
}
void QPlatformInputContext::emitLocaleChanged()
{
emit QGuiApplication::inputMethod()->localeChanged();
// Changing the locale might have updated the input direction
emitInputDirectionChanged(inputDirection());
}
Qt::LayoutDirection QPlatformInputContext::inputDirection() const
{
return locale().textDirection();
}
void QPlatformInputContext::emitInputDirectionChanged(Qt::LayoutDirection newDirection)
{
if (newDirection == m_inputDirection)
return;
emit QGuiApplication::inputMethod()->inputDirectionChanged(newDirection);
m_inputDirection = newDirection;
}
/*!
This virtual method gets called to notify updated focus to \a object.
\warning Input methods must not call this function directly.
*/
void QPlatformInputContext::setFocusObject(QObject *object)
{
Q_UNUSED(object);
}
/*!
Returns \c true if current focus object supports input method events.
*/
bool QPlatformInputContext::inputMethodAccepted() const
{
return QPlatformInputContextPrivate::s_inputMethodAccepted;
}
bool QPlatformInputContextPrivate::s_inputMethodAccepted = false;
void QPlatformInputContextPrivate::setInputMethodAccepted(bool accepted)
{
QPlatformInputContextPrivate::s_inputMethodAccepted = accepted;
}
/*!
\brief QPlatformInputContext::setSelectionOnFocusObject
\param anchorPos Beginning of selection in currently active window native coordinates
\param cursorPos End of selection in currently active window native coordinates
*/
void QPlatformInputContext::setSelectionOnFocusObject(const QPointF &nativeAnchorPos, const QPointF &nativeCursorPos)
{
QObject *focus = qApp->focusObject();
if (!focus)
return;
QWindow *window = qApp->focusWindow();
const QPointF &anchorPos = QHighDpi::fromNativePixels(nativeAnchorPos, window);
const QPointF &cursorPos = QHighDpi::fromNativePixels(nativeCursorPos, window);
QInputMethod *im = QGuiApplication::inputMethod();
const QTransform mapToLocal = im->inputItemTransform().inverted();
bool success;
int anchor = QInputMethod::queryFocusObject(Qt::ImCursorPosition, anchorPos * mapToLocal).toInt(&success);
if (success) {
int cursor = QInputMethod::queryFocusObject(Qt::ImCursorPosition, cursorPos * mapToLocal).toInt(&success);
if (success) {
if (anchor == cursor && anchorPos != cursorPos)
return;
QList<QInputMethodEvent::Attribute> imAttributes;
imAttributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Selection, anchor, cursor - anchor, QVariant()));
QInputMethodEvent event(QString(), imAttributes);
QGuiApplication::sendEvent(focus, &event);
}
}
}
/*!
\brief QPlatformInputContext::queryFocusObject
Queries the current foucus object with a window position in native pixels.
*/
QVariant QPlatformInputContext::queryFocusObject(Qt::InputMethodQuery query, QPointF nativePosition)
{
const QPointF position = QHighDpi::fromNativePixels(nativePosition, QGuiApplication::focusWindow());
const QInputMethod *im = QGuiApplication::inputMethod();
const QTransform mapToLocal = im->inputItemTransform().inverted();
return im->queryFocusObject(query, mapToLocal.map(position));
}
/*!
\brief QPlatformInputContext::inputItemRectangle
Returns the input item rectangle for the currently active window
and input methiod in native window coordinates.
*/
QRectF QPlatformInputContext::inputItemRectangle()
{
QInputMethod *im = QGuiApplication::inputMethod();
const QRectF deviceIndependentRectangle = im->inputItemTransform().mapRect(im->inputItemRectangle());
return QHighDpi::toNativePixels(deviceIndependentRectangle, QGuiApplication::focusWindow());
}
/*!
\brief QPlatformInputContext::inputItemClipRectangle
Returns the input item clip rectangle for the currently active window
and input methiod in native window coordinates.
*/
QRectF QPlatformInputContext::inputItemClipRectangle()
{
return QHighDpi::toNativePixels(
QGuiApplication::inputMethod()->inputItemClipRectangle(), QGuiApplication::focusWindow());
}
/*!
\brief QPlatformInputContext::cursorRectangle
Returns the cursor rectangle for the currently active window
and input methiod in native window coordinates.
*/
QRectF QPlatformInputContext::cursorRectangle()
{
return QHighDpi::toNativePixels(
QGuiApplication::inputMethod()->cursorRectangle(), QGuiApplication::focusWindow());
}
/*!
\brief QPlatformInputContext::anchorRectangle
Returns the anchor rectangle for the currently active window
and input methiod in native window coordinates.
*/
QRectF QPlatformInputContext::anchorRectangle()
{
return QHighDpi::toNativePixels(
QGuiApplication::inputMethod()->anchorRectangle(), QGuiApplication::focusWindow());
}
/*!
\brief QPlatformInputContext::keyboardRectangle
Returns the keyboard rectangle for the currently active window
and input methiod in native window coordinates.
*/
QRectF QPlatformInputContext::keyboardRectangle()
{
return QHighDpi::toNativePixels(
QGuiApplication::inputMethod()->keyboardRectangle(), QGuiApplication::focusWindow());
}
QT_END_NAMESPACE
#include "moc_qplatforminputcontext.cpp"