Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/kernel/qinputmethod.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||
25 | ** | - | ||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||
29 | ** | - | ||||||
30 | ** $QT_END_LICENSE$ | - | ||||||
31 | ** | - | ||||||
32 | ****************************************************************************/ | - | ||||||
33 | - | |||||||
34 | #include <qinputmethod.h> | - | ||||||
35 | #include <private/qinputmethod_p.h> | - | ||||||
36 | #include <qguiapplication.h> | - | ||||||
37 | #include <qtimer.h> | - | ||||||
38 | #include <qpa/qplatforminputcontext_p.h> | - | ||||||
39 | - | |||||||
40 | #include <QDebug> | - | ||||||
41 | - | |||||||
42 | QT_BEGIN_NAMESPACE | - | ||||||
43 | - | |||||||
44 | /*! | - | ||||||
45 | \internal | - | ||||||
46 | */ | - | ||||||
47 | QInputMethod::QInputMethod() | - | ||||||
48 | : QObject(*new QInputMethodPrivate) | - | ||||||
49 | { | - | ||||||
50 | } never executed: end of block | 0 | ||||||
51 | - | |||||||
52 | /*! | - | ||||||
53 | \internal | - | ||||||
54 | */ | - | ||||||
55 | QInputMethod::~QInputMethod() | - | ||||||
56 | { | - | ||||||
57 | } | - | ||||||
58 | - | |||||||
59 | /*! | - | ||||||
60 | \class QInputMethod | - | ||||||
61 | \brief The QInputMethod class provides access to the active text input method. | - | ||||||
62 | \inmodule QtGui | - | ||||||
63 | - | |||||||
64 | QInputMethod is used by the text editors for integrating to the platform text input | - | ||||||
65 | methods and more commonly by application views for querying various text input method-related | - | ||||||
66 | information like virtual keyboard visibility and keyboard dimensions. | - | ||||||
67 | - | |||||||
68 | Qt Quick also provides access to QInputMethod in QML through \l{QmlGlobalQtObject}{Qt global object} | - | ||||||
69 | as \c Qt.inputMethod property. | - | ||||||
70 | */ | - | ||||||
71 | - | |||||||
72 | /*! | - | ||||||
73 | Returns the transformation from input item coordinates to the window coordinates. | - | ||||||
74 | */ | - | ||||||
75 | QTransform QInputMethod::inputItemTransform() const | - | ||||||
76 | { | - | ||||||
77 | Q_D(const QInputMethod); | - | ||||||
78 | return d->inputItemTransform; never executed: return d->inputItemTransform; | 0 | ||||||
79 | } | - | ||||||
80 | - | |||||||
81 | /*! | - | ||||||
82 | Sets the transformation from input item coordinates to window coordinates to be \a transform. | - | ||||||
83 | Item transform needs to be updated by the focused window like QQuickCanvas whenever | - | ||||||
84 | item is moved inside the scene. | - | ||||||
85 | */ | - | ||||||
86 | void QInputMethod::setInputItemTransform(const QTransform &transform) | - | ||||||
87 | { | - | ||||||
88 | Q_D(QInputMethod); | - | ||||||
89 | if (d->inputItemTransform == transform)
| 0 | ||||||
90 | return; never executed: return; | 0 | ||||||
91 | - | |||||||
92 | d->inputItemTransform = transform; | - | ||||||
93 | emit cursorRectangleChanged(); | - | ||||||
94 | } never executed: end of block | 0 | ||||||
95 | - | |||||||
96 | - | |||||||
97 | /*! | - | ||||||
98 | \since 5.1 | - | ||||||
99 | - | |||||||
100 | Returns the input item's geometry in input item coordinates. | - | ||||||
101 | - | |||||||
102 | \sa setInputItemRectangle() | - | ||||||
103 | */ | - | ||||||
104 | QRectF QInputMethod::inputItemRectangle() const | - | ||||||
105 | { | - | ||||||
106 | Q_D(const QInputMethod); | - | ||||||
107 | return d->inputRectangle; never executed: return d->inputRectangle; | 0 | ||||||
108 | } | - | ||||||
109 | - | |||||||
110 | /*! | - | ||||||
111 | \since 5.1 | - | ||||||
112 | - | |||||||
113 | Sets the input item's geometry to be \a rect, in input item coordinates. | - | ||||||
114 | This needs to be updated by the focused window like QQuickCanvas whenever | - | ||||||
115 | item is moved inside the scene, or focus is changed. | - | ||||||
116 | */ | - | ||||||
117 | void QInputMethod::setInputItemRectangle(const QRectF &rect) | - | ||||||
118 | { | - | ||||||
119 | Q_D(QInputMethod); | - | ||||||
120 | d->inputRectangle = rect; | - | ||||||
121 | } never executed: end of block | 0 | ||||||
122 | - | |||||||
123 | /*! | - | ||||||
124 | \property QInputMethod::cursorRectangle | - | ||||||
125 | \brief Input item's cursor rectangle in window coordinates. | - | ||||||
126 | - | |||||||
127 | Cursor rectangle is often used by various text editing controls | - | ||||||
128 | like text prediction popups for following the text being typed. | - | ||||||
129 | */ | - | ||||||
130 | QRectF QInputMethod::cursorRectangle() const | - | ||||||
131 | { | - | ||||||
132 | Q_D(const QInputMethod); | - | ||||||
133 | - | |||||||
134 | QObject *focusObject = qGuiApp->focusObject(); | - | ||||||
135 | if (!focusObject)
| 0 | ||||||
136 | return QRectF(); never executed: return QRectF(); | 0 | ||||||
137 | - | |||||||
138 | QInputMethodQueryEvent query(Qt::ImCursorRectangle); | - | ||||||
139 | QGuiApplication::sendEvent(focusObject, &query); | - | ||||||
140 | QRectF r = query.value(Qt::ImCursorRectangle).toRectF(); | - | ||||||
141 | if (!r.isValid())
| 0 | ||||||
142 | return QRectF(); never executed: return QRectF(); | 0 | ||||||
143 | - | |||||||
144 | return d->inputItemTransform.mapRect(r); never executed: return d->inputItemTransform.mapRect(r); | 0 | ||||||
145 | } | - | ||||||
146 | - | |||||||
147 | /*! | - | ||||||
148 | \property QInputMethod::keyboardRectangle | - | ||||||
149 | \brief Virtual keyboard's geometry in window coordinates. | - | ||||||
150 | - | |||||||
151 | This might be an empty rectangle if it is not possible to know the geometry | - | ||||||
152 | of the keyboard. This is the case for a floating keyboard on android. | - | ||||||
153 | */ | - | ||||||
154 | QRectF QInputMethod::keyboardRectangle() const | - | ||||||
155 | { | - | ||||||
156 | Q_D(const QInputMethod); | - | ||||||
157 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
158 | if (ic)
| 0 | ||||||
159 | return ic->keyboardRect(); never executed: return ic->keyboardRect(); | 0 | ||||||
160 | return QRectF(); never executed: return QRectF(); | 0 | ||||||
161 | } | - | ||||||
162 | - | |||||||
163 | /*! | - | ||||||
164 | Requests virtual keyboard to open. If the platform | - | ||||||
165 | doesn't provide virtual keyboard the visibility | - | ||||||
166 | remains false. | - | ||||||
167 | - | |||||||
168 | Normally applications should not need to call this | - | ||||||
169 | function, keyboard should automatically open when | - | ||||||
170 | the text editor gains focus. | - | ||||||
171 | */ | - | ||||||
172 | void QInputMethod::show() | - | ||||||
173 | { | - | ||||||
174 | Q_D(QInputMethod); | - | ||||||
175 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
176 | if (ic)
| 0 | ||||||
177 | ic->showInputPanel(); never executed: ic->showInputPanel(); | 0 | ||||||
178 | } never executed: end of block | 0 | ||||||
179 | - | |||||||
180 | /*! | - | ||||||
181 | Requests virtual keyboard to close. | - | ||||||
182 | - | |||||||
183 | Normally applications should not need to call this function, | - | ||||||
184 | keyboard should automatically close when the text editor loses | - | ||||||
185 | focus, for example when the parent view is closed. | - | ||||||
186 | */ | - | ||||||
187 | void QInputMethod::hide() | - | ||||||
188 | { | - | ||||||
189 | Q_D(QInputMethod); | - | ||||||
190 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
191 | if (ic)
| 0 | ||||||
192 | ic->hideInputPanel(); never executed: ic->hideInputPanel(); | 0 | ||||||
193 | } never executed: end of block | 0 | ||||||
194 | - | |||||||
195 | /*! | - | ||||||
196 | \property QInputMethod::visible | - | ||||||
197 | \brief Virtual keyboard's visibility on the screen | - | ||||||
198 | - | |||||||
199 | Input method visibility remains false for devices | - | ||||||
200 | with no virtual keyboards. | - | ||||||
201 | - | |||||||
202 | \sa show(), hide() | - | ||||||
203 | */ | - | ||||||
204 | bool QInputMethod::isVisible() const | - | ||||||
205 | { | - | ||||||
206 | Q_D(const QInputMethod); | - | ||||||
207 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
208 | if (ic)
| 0 | ||||||
209 | return ic->isInputPanelVisible(); never executed: return ic->isInputPanelVisible(); | 0 | ||||||
210 | return false; never executed: return false; | 0 | ||||||
211 | } | - | ||||||
212 | - | |||||||
213 | /*! | - | ||||||
214 | Controls the keyboard visibility. Equivalent | - | ||||||
215 | to calling show() (if \a visible is \c true) | - | ||||||
216 | or hide() (if \a visible is \c false). | - | ||||||
217 | - | |||||||
218 | \sa show(), hide() | - | ||||||
219 | */ | - | ||||||
220 | void QInputMethod::setVisible(bool visible) | - | ||||||
221 | { | - | ||||||
222 | visible ? show() : hide(); | - | ||||||
223 | } never executed: end of block | 0 | ||||||
224 | - | |||||||
225 | /*! | - | ||||||
226 | \property QInputMethod::animating | - | ||||||
227 | \brief True when the virtual keyboard is being opened or closed. | - | ||||||
228 | - | |||||||
229 | Animating is false when keyboard is fully open or closed. | - | ||||||
230 | When \c animating is \c true and \c visibility is \c true keyboard | - | ||||||
231 | is being opened. When \c animating is \c true and \c visibility is | - | ||||||
232 | false keyboard is being closed. | - | ||||||
233 | */ | - | ||||||
234 | - | |||||||
235 | bool QInputMethod::isAnimating() const | - | ||||||
236 | { | - | ||||||
237 | Q_D(const QInputMethod); | - | ||||||
238 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
239 | if (ic)
| 0 | ||||||
240 | return ic->isAnimating(); never executed: return ic->isAnimating(); | 0 | ||||||
241 | return false; never executed: return false; | 0 | ||||||
242 | } | - | ||||||
243 | - | |||||||
244 | /*! | - | ||||||
245 | \property QInputMethod::locale | - | ||||||
246 | \brief Current input locale. | - | ||||||
247 | */ | - | ||||||
248 | QLocale QInputMethod::locale() const | - | ||||||
249 | { | - | ||||||
250 | Q_D(const QInputMethod); | - | ||||||
251 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
252 | if (ic)
| 0 | ||||||
253 | return ic->locale(); never executed: return ic->locale(); | 0 | ||||||
254 | return QLocale::c(); never executed: return QLocale::c(); | 0 | ||||||
255 | } | - | ||||||
256 | - | |||||||
257 | /*! | - | ||||||
258 | \property QInputMethod::inputDirection | - | ||||||
259 | \brief Current input direction. | - | ||||||
260 | */ | - | ||||||
261 | Qt::LayoutDirection QInputMethod::inputDirection() const | - | ||||||
262 | { | - | ||||||
263 | Q_D(const QInputMethod); | - | ||||||
264 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
265 | if (ic)
| 0 | ||||||
266 | return ic->inputDirection(); never executed: return ic->inputDirection(); | 0 | ||||||
267 | return Qt::LeftToRight; never executed: return Qt::LeftToRight; | 0 | ||||||
268 | } | - | ||||||
269 | - | |||||||
270 | /*! | - | ||||||
271 | Called by the input item to inform the platform input methods when there has been | - | ||||||
272 | state changes in editor's input method query attributes. When calling the function | - | ||||||
273 | \a queries parameter has to be used to tell what has changes, which input method | - | ||||||
274 | can use to make queries for attributes it's interested with QInputMethodQueryEvent. | - | ||||||
275 | - | |||||||
276 | In particular calling update whenever the cursor position changes is important as | - | ||||||
277 | that often causes other query attributes like surrounding text and text selection | - | ||||||
278 | to change as well. The attributes that often change together with cursor position | - | ||||||
279 | have been grouped in Qt::ImQueryInput value for convenience. | - | ||||||
280 | */ | - | ||||||
281 | void QInputMethod::update(Qt::InputMethodQueries queries) | - | ||||||
282 | { | - | ||||||
283 | Q_D(QInputMethod); | - | ||||||
284 | - | |||||||
285 | if (queries & Qt::ImEnabled) {
| 0 | ||||||
286 | QObject *focus = qApp->focusObject(); | - | ||||||
287 | bool enabled = d->objectAcceptsInputMethod(focus); | - | ||||||
288 | QPlatformInputContextPrivate::setInputMethodAccepted(enabled); | - | ||||||
289 | } never executed: end of block | 0 | ||||||
290 | - | |||||||
291 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
292 | if (ic)
| 0 | ||||||
293 | ic->update(queries); never executed: ic->update(queries); | 0 | ||||||
294 | - | |||||||
295 | if (queries & Qt::ImCursorRectangle)
| 0 | ||||||
296 | emit cursorRectangleChanged(); never executed: cursorRectangleChanged(); | 0 | ||||||
297 | } never executed: end of block | 0 | ||||||
298 | - | |||||||
299 | /*! | - | ||||||
300 | Resets the input method state. For example, a text editor normally calls | - | ||||||
301 | this method before inserting a text to make widget ready to accept a text. | - | ||||||
302 | - | |||||||
303 | Input method resets automatically when the focused editor changes. | - | ||||||
304 | */ | - | ||||||
305 | void QInputMethod::reset() | - | ||||||
306 | { | - | ||||||
307 | Q_D(QInputMethod); | - | ||||||
308 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
309 | if (ic)
| 0 | ||||||
310 | ic->reset(); never executed: ic->reset(); | 0 | ||||||
311 | } never executed: end of block | 0 | ||||||
312 | - | |||||||
313 | /*! | - | ||||||
314 | Commits the word user is currently composing to the editor. The function is | - | ||||||
315 | mostly needed by the input methods with text prediction features and by the | - | ||||||
316 | methods where the script used for typing characters is different from the | - | ||||||
317 | script that actually gets appended to the editor. Any kind of action that | - | ||||||
318 | interrupts the text composing needs to flush the composing state by calling the | - | ||||||
319 | commit() function, for example when the cursor is moved elsewhere. | - | ||||||
320 | */ | - | ||||||
321 | void QInputMethod::commit() | - | ||||||
322 | { | - | ||||||
323 | Q_D(QInputMethod); | - | ||||||
324 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
325 | if (ic)
| 0 | ||||||
326 | ic->commit(); never executed: ic->commit(); | 0 | ||||||
327 | } never executed: end of block | 0 | ||||||
328 | - | |||||||
329 | /*! | - | ||||||
330 | \enum QInputMethod::Action | - | ||||||
331 | - | |||||||
332 | Indicates the kind of action performed by the user. | - | ||||||
333 | - | |||||||
334 | \value Click A normal click/tap | - | ||||||
335 | \value ContextMenu A context menu click/tap (e.g. right-button or tap-and-hold) | - | ||||||
336 | - | |||||||
337 | \sa invokeAction() | - | ||||||
338 | */ | - | ||||||
339 | - | |||||||
340 | /*! | - | ||||||
341 | Called by the input item when the word currently being composed is tapped by | - | ||||||
342 | the user, as indicated by the action \a a and the given \a cursorPosition. | - | ||||||
343 | Input methods often use this information to offer more word suggestions to the user. | - | ||||||
344 | */ | - | ||||||
345 | void QInputMethod::invokeAction(Action a, int cursorPosition) | - | ||||||
346 | { | - | ||||||
347 | Q_D(QInputMethod); | - | ||||||
348 | QPlatformInputContext *ic = d->platformInputContext(); | - | ||||||
349 | if (ic)
| 0 | ||||||
350 | ic->invokeAction(a, cursorPosition); never executed: ic->invokeAction(a, cursorPosition); | 0 | ||||||
351 | } never executed: end of block | 0 | ||||||
352 | - | |||||||
353 | bool QInputMethodPrivate::objectAcceptsInputMethod(QObject *object) | - | ||||||
354 | { | - | ||||||
355 | bool enabled = false; | - | ||||||
356 | if (object) {
| 0 | ||||||
357 | QInputMethodQueryEvent query(Qt::ImEnabled); | - | ||||||
358 | QGuiApplication::sendEvent(object, &query); | - | ||||||
359 | enabled = query.value(Qt::ImEnabled).toBool(); | - | ||||||
360 | } never executed: end of block | 0 | ||||||
361 | - | |||||||
362 | return enabled; never executed: return enabled; | 0 | ||||||
363 | } | - | ||||||
364 | - | |||||||
365 | /*! | - | ||||||
366 | Send \a query to the current focus object with parameters \a argument and return the result. | - | ||||||
367 | */ | - | ||||||
368 | QVariant QInputMethod::queryFocusObject(Qt::InputMethodQuery query, QVariant argument) | - | ||||||
369 | { | - | ||||||
370 | QVariant retval; | - | ||||||
371 | QObject *focusObject = qGuiApp->focusObject(); | - | ||||||
372 | if (!focusObject)
| 0 | ||||||
373 | return retval; never executed: return retval; | 0 | ||||||
374 | - | |||||||
375 | bool newMethodWorks = QMetaObject::invokeMethod(focusObject, "inputMethodQuery", | - | ||||||
376 | Qt::DirectConnection, | - | ||||||
377 | Q_RETURN_ARG(QVariant, retval), | - | ||||||
378 | Q_ARG(Qt::InputMethodQuery, query), | - | ||||||
379 | Q_ARG(QVariant, argument)); | - | ||||||
380 | if (newMethodWorks)
| 0 | ||||||
381 | return retval; never executed: return retval; | 0 | ||||||
382 | - | |||||||
383 | QInputMethodQueryEvent queryEvent(query); | - | ||||||
384 | QCoreApplication::sendEvent(focusObject, &queryEvent); | - | ||||||
385 | return queryEvent.value(query); never executed: return queryEvent.value(query); | 0 | ||||||
386 | } | - | ||||||
387 | - | |||||||
388 | QT_END_NAMESPACE | - | ||||||
389 | - | |||||||
390 | #include "moc_qinputmethod.cpp" | - | ||||||
Source code | Switch to Preprocessed file |