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