| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtVirtualKeyboard/private/inputmethod_p.h> |
| 5 | #include <QtVirtualKeyboard/qvirtualkeyboardtrace.h> |
| 6 | #include <QVariant> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | namespace QtVirtualKeyboard { |
| 10 | |
| 11 | /*! |
| 12 | \qmltype InputMethod |
| 13 | \nativetype QtVirtualKeyboard::InputMethod |
| 14 | \inqmlmodule QtQuick.VirtualKeyboard |
| 15 | \ingroup qtvirtualkeyboard-internal-qml |
| 16 | \brief Base type for creating input method in QML. |
| 17 | |
| 18 | The InputMethod type lets you create a custom input method |
| 19 | which can be assigned to InputEngine. |
| 20 | */ |
| 21 | |
| 22 | /*! |
| 23 | \qmlproperty InputContext InputMethod::inputContext |
| 24 | |
| 25 | The input context. |
| 26 | */ |
| 27 | |
| 28 | /*! |
| 29 | \qmlproperty InputEngine InputMethod::inputEngine |
| 30 | |
| 31 | The input engine. |
| 32 | */ |
| 33 | |
| 34 | /*! |
| 35 | \qmlmethod list<int> InputMethod::inputModes(string locale) |
| 36 | |
| 37 | Returns a list of input modes for \a locale. |
| 38 | */ |
| 39 | |
| 40 | /*! |
| 41 | \qmlmethod bool InputMethod::setInputMode(string locale, int inputMode) |
| 42 | |
| 43 | Changes \a inputMode and \a locale for this input method. The method returns |
| 44 | \c true if successful. |
| 45 | */ |
| 46 | |
| 47 | /*! |
| 48 | \qmlmethod bool InputMethod::setTextCase(int textCase) |
| 49 | |
| 50 | Updates \a textCase for this input method. The method returns \c true if |
| 51 | successful. |
| 52 | |
| 53 | The possible values for the text case are: |
| 54 | |
| 55 | \list |
| 56 | \li \c InputEngine.Lower Lower case text. |
| 57 | \li \c InputEngine.Upper Upper case text. |
| 58 | \endlist |
| 59 | */ |
| 60 | |
| 61 | /*! |
| 62 | \qmlmethod bool InputMethod::keyEvent(int key, string text, int modifiers) |
| 63 | |
| 64 | The purpose of this method is to handle the key events generated by the the |
| 65 | input engine. |
| 66 | |
| 67 | The \a key parameter specifies the code of the key to handle. The key code |
| 68 | does not distinguish between capital and non-capital letters. The \a |
| 69 | text parameter contains the Unicode text for the key. The \a modifiers |
| 70 | parameter contains the key modifiers that apply to \a key. |
| 71 | |
| 72 | This method returns \c true if the key event was successfully handled. |
| 73 | If the return value is \c false, the key event is redirected to the default |
| 74 | input method for further processing. |
| 75 | */ |
| 76 | |
| 77 | /*! |
| 78 | \qmlmethod InputMethod::reset() |
| 79 | |
| 80 | This method is called by the input engine when this input method needs to be |
| 81 | reset. The input method must reset its internal state only. The main |
| 82 | difference to the update() method is that reset() modifies only |
| 83 | the input method state; it must not modify the input context. |
| 84 | */ |
| 85 | |
| 86 | /*! |
| 87 | \qmlmethod InputMethod::update() |
| 88 | |
| 89 | This method is called by the input engine when the input method needs to be |
| 90 | updated. The input method must close the current pre-edit text and |
| 91 | restore the internal state to the default. |
| 92 | */ |
| 93 | |
| 94 | /*! |
| 95 | \qmlmethod list<int> InputMethod::selectionLists() |
| 96 | |
| 97 | Returns the list of selection types used for this input method. |
| 98 | |
| 99 | This method is called by input engine when the input method is being |
| 100 | activated and every time the input method hints are updated. The input method |
| 101 | can reserve selection lists by returning the desired selection list types. |
| 102 | |
| 103 | The input method may request the input engine to update the selection lists |
| 104 | at any time by emitting selectionListsChanged() signal. This signal will |
| 105 | trigger a call to this method, allowing the input method to update the selection |
| 106 | list types. |
| 107 | */ |
| 108 | |
| 109 | /*! |
| 110 | \qmlmethod int InputMethod::selectionListItemCount(int type) |
| 111 | |
| 112 | Returns the number of items in the selection list identified by \a type. |
| 113 | */ |
| 114 | |
| 115 | /*! |
| 116 | \qmlmethod var InputMethod::selectionListData(int type, int index, int role) |
| 117 | |
| 118 | Returns item data for a selection list identified by \a type. The \a role |
| 119 | parameter specifies which data is requested. The \a index parameter is a |
| 120 | zero based index into the selecteion list. |
| 121 | */ |
| 122 | |
| 123 | /*! |
| 124 | \qmlmethod void InputMethod::selectionListItemSelected(int type, int index) |
| 125 | |
| 126 | This method is called when an item at \a index has been selected by the |
| 127 | user. The selection list is identified by the \a type parameter. |
| 128 | */ |
| 129 | |
| 130 | /*! |
| 131 | \qmlsignal InputMethod::selectionListChanged(int type) |
| 132 | |
| 133 | The input method emits this signal when the contents of the selection list |
| 134 | are changed. The \a type parameter specifies which selection list has |
| 135 | changed. |
| 136 | */ |
| 137 | |
| 138 | /*! |
| 139 | \qmlsignal InputMethod::selectionListActiveItemChanged(int type, int index) |
| 140 | |
| 141 | The input method emits this signal when the current \a index has changed |
| 142 | in the selection list identified by \a type. |
| 143 | */ |
| 144 | |
| 145 | /*! |
| 146 | \qmlsignal InputMethod::selectionListsChanged() |
| 147 | \since QtQuick.VirtualKeyboard 2.2 |
| 148 | |
| 149 | The input method emits this signal when the selection list types have |
| 150 | changed. This signal will trigger a call to selectionLists() method, |
| 151 | allowing the input method to update the selection list types. |
| 152 | */ |
| 153 | |
| 154 | /*! |
| 155 | \qmlmethod list<int> InputMethod::patternRecognitionModes() |
| 156 | \since QtQuick.VirtualKeyboard 2.0 |
| 157 | |
| 158 | Returns list of supported pattern recognition modes. |
| 159 | |
| 160 | This method is invoked by the input engine to query the list of |
| 161 | supported pattern recognition modes. |
| 162 | */ |
| 163 | |
| 164 | /*! |
| 165 | \qmlmethod Trace InputMethod::traceBegin(int traceId, int patternRecognitionMode, var traceCaptureDeviceInfo, var traceScreenInfo) |
| 166 | \since QtQuick.VirtualKeyboard 2.0 |
| 167 | |
| 168 | This method is called when a trace interaction starts with the specified \a patternRecognitionMode. |
| 169 | The trace is uniquely identified by the \a traceId. |
| 170 | The \a traceCaptureDeviceInfo provides information about the source device and the |
| 171 | \a traceScreenInfo provides information about the screen context. |
| 172 | |
| 173 | If the input method accepts the event and wants to capture the trace input, it must return |
| 174 | a new Trace object. This object must remain valid until the \l {InputMethod::traceEnd()} |
| 175 | {InputMethod.traceEnd()} method is called. If the Trace is rendered on screen, it remains there |
| 176 | until the Trace object is destroyed. |
| 177 | */ |
| 178 | |
| 179 | /*! |
| 180 | \qmlmethod bool InputMethod::traceEnd(Trace trace) |
| 181 | \since QtQuick.VirtualKeyboard 2.0 |
| 182 | |
| 183 | This method is called when the trace interaction ends. The input method should destroy the \a trace object |
| 184 | at some point after this function is called. Returns \c true on success. |
| 185 | |
| 186 | See the \l {Trace API for Input Methods} how to access the gathered data. |
| 187 | */ |
| 188 | |
| 189 | /*! |
| 190 | \qmlmethod bool InputMethod::reselect(int cursorPosition, int reselectFlags) |
| 191 | \since QtQuick.VirtualKeyboard 2.0 |
| 192 | |
| 193 | This method attempts to reselect a word located at the \a cursorPosition. |
| 194 | The \a reselectFlags define the rules for how the word should be selected in |
| 195 | relation to the cursor position. |
| 196 | |
| 197 | \list |
| 198 | \li \c InputEngine.WordBeforeCursor Activate the word before the cursor. When this flag is used exclusively, the word must end exactly at the cursor. |
| 199 | \li \c InputEngine.WordAfterCursor Activate the word after the cursor. When this flag is used exclusively, the word must start exactly at the cursor. |
| 200 | \li \c InputEngine.WordAtCursor Activate the word at the cursor. This flag is a combination of the above flags with the exception that the word cannot start or stop at the cursor. |
| 201 | \endlist |
| 202 | |
| 203 | The method returns \c true if the word was successfully reselected. |
| 204 | */ |
| 205 | |
| 206 | /*! |
| 207 | \qmlmethod bool InputMethod::clickPreeditText(int cursorPosition) |
| 208 | \since QtQuick.VirtualKeyboard 2.4 |
| 209 | |
| 210 | Called when the user clicks on pre-edit text at \a cursorPosition. |
| 211 | |
| 212 | The function should return \c true if it handles the event. Otherwise the input |
| 213 | falls back to \l reselect() for further processing. |
| 214 | */ |
| 215 | |
| 216 | /*! |
| 217 | \class QtVirtualKeyboard::InputMethod |
| 218 | \internal |
| 219 | */ |
| 220 | |
| 221 | InputMethod::InputMethod(QObject *parent) : |
| 222 | QVirtualKeyboardAbstractInputMethod(parent) |
| 223 | { |
| 224 | } |
| 225 | |
| 226 | InputMethod::~InputMethod() |
| 227 | { |
| 228 | } |
| 229 | |
| 230 | QList<QVirtualKeyboardInputEngine::InputMode> InputMethod::inputModes(const QString &locale) |
| 231 | { |
| 232 | QVariant result; |
| 233 | QMetaObject::invokeMethod(obj: this, member: "inputModes" , |
| 234 | Q_RETURN_ARG(QVariant, result), |
| 235 | Q_ARG(QVariant, locale)); |
| 236 | QList<QVirtualKeyboardInputEngine::InputMode> inputModeList; |
| 237 | const auto resultList = result.toList(); |
| 238 | inputModeList.reserve(asize: resultList.size()); |
| 239 | for (const QVariant &inputMode : resultList) |
| 240 | inputModeList.append(t: static_cast<QVirtualKeyboardInputEngine::InputMode>(inputMode.toInt())); |
| 241 | return inputModeList; |
| 242 | } |
| 243 | |
| 244 | bool InputMethod::setInputMode(const QString &locale, QVirtualKeyboardInputEngine::InputMode inputMode) |
| 245 | { |
| 246 | QVariant result; |
| 247 | QMetaObject::invokeMethod(obj: this, member: "setInputMode" , |
| 248 | Q_RETURN_ARG(QVariant, result), |
| 249 | Q_ARG(QVariant, locale), |
| 250 | Q_ARG(QVariant, static_cast<int>(inputMode))); |
| 251 | return result.toBool(); |
| 252 | } |
| 253 | |
| 254 | bool InputMethod::setTextCase(QVirtualKeyboardInputEngine::TextCase textCase) |
| 255 | { |
| 256 | QVariant result; |
| 257 | QMetaObject::invokeMethod(obj: this, member: "setTextCase" , |
| 258 | Q_RETURN_ARG(QVariant, result), |
| 259 | Q_ARG(QVariant, static_cast<int>(textCase))); |
| 260 | return result.toBool(); |
| 261 | } |
| 262 | |
| 263 | bool InputMethod::keyEvent(Qt::Key key, const QString &text, Qt::KeyboardModifiers modifiers) |
| 264 | { |
| 265 | QVariant result; |
| 266 | QMetaObject::invokeMethod(obj: this, member: "keyEvent" , |
| 267 | Q_RETURN_ARG(QVariant, result), |
| 268 | Q_ARG(QVariant, key), |
| 269 | Q_ARG(QVariant, text), |
| 270 | Q_ARG(QVariant, (int)modifiers)); |
| 271 | return result.toBool(); |
| 272 | } |
| 273 | |
| 274 | QList<QVirtualKeyboardSelectionListModel::Type> InputMethod::selectionLists() |
| 275 | { |
| 276 | QVariant result; |
| 277 | QMetaObject::invokeMethod(obj: this, member: "selectionLists" , |
| 278 | Q_RETURN_ARG(QVariant, result)); |
| 279 | QList<QVirtualKeyboardSelectionListModel::Type> selectionListsList; |
| 280 | const auto resultList = result.toList(); |
| 281 | selectionListsList.reserve(asize: resultList.size()); |
| 282 | for (const QVariant &selectionListType : resultList) |
| 283 | selectionListsList.append(t: static_cast<QVirtualKeyboardSelectionListModel::Type>(selectionListType.toInt())); |
| 284 | |
| 285 | return selectionListsList; |
| 286 | } |
| 287 | |
| 288 | int InputMethod::selectionListItemCount(QVirtualKeyboardSelectionListModel::Type type) |
| 289 | { |
| 290 | QVariant result; |
| 291 | QMetaObject::invokeMethod(obj: this, member: "selectionListItemCount" , |
| 292 | Q_RETURN_ARG(QVariant, result), |
| 293 | Q_ARG(QVariant, static_cast<int>(type))); |
| 294 | return result.toInt(); |
| 295 | } |
| 296 | |
| 297 | QVariant InputMethod::selectionListData(QVirtualKeyboardSelectionListModel::Type type, int index, QVirtualKeyboardSelectionListModel::Role role) |
| 298 | { |
| 299 | QVariant result; |
| 300 | QMetaObject::invokeMethod(obj: this, member: "selectionListData" , |
| 301 | Q_RETURN_ARG(QVariant, result), |
| 302 | Q_ARG(QVariant, static_cast<int>(type)), |
| 303 | Q_ARG(QVariant, index), |
| 304 | Q_ARG(QVariant, static_cast<int>(role))); |
| 305 | if (result.isNull()) { |
| 306 | result = QVirtualKeyboardAbstractInputMethod::selectionListData(type, index, role); |
| 307 | } |
| 308 | return result; |
| 309 | } |
| 310 | |
| 311 | void InputMethod::selectionListItemSelected(QVirtualKeyboardSelectionListModel::Type type, int index) |
| 312 | { |
| 313 | QMetaObject::invokeMethod(obj: this, member: "selectionListItemSelected" , |
| 314 | Q_ARG(QVariant, static_cast<int>(type)), |
| 315 | Q_ARG(QVariant, index)); |
| 316 | } |
| 317 | |
| 318 | QList<QVirtualKeyboardInputEngine::PatternRecognitionMode> InputMethod::patternRecognitionModes() const |
| 319 | { |
| 320 | QVariant result; |
| 321 | QMetaObject::invokeMethod(obj: const_cast<InputMethod *>(this), member: "patternRecognitionModes" , |
| 322 | Q_RETURN_ARG(QVariant, result)); |
| 323 | QList<QVirtualKeyboardInputEngine::PatternRecognitionMode> patterRecognitionModeList; |
| 324 | const auto resultList = result.toList(); |
| 325 | patterRecognitionModeList.reserve(asize: resultList.size()); |
| 326 | for (const QVariant &patterRecognitionMode : resultList) |
| 327 | patterRecognitionModeList.append(t: static_cast<QVirtualKeyboardInputEngine::PatternRecognitionMode>(patterRecognitionMode.toInt())); |
| 328 | |
| 329 | return patterRecognitionModeList; |
| 330 | } |
| 331 | |
| 332 | QVirtualKeyboardTrace *InputMethod::traceBegin( |
| 333 | int traceId, QVirtualKeyboardInputEngine::PatternRecognitionMode patternRecognitionMode, |
| 334 | const QVariantMap &traceCaptureDeviceInfo, const QVariantMap &traceScreenInfo) |
| 335 | { |
| 336 | QVariant result; |
| 337 | QMetaObject::invokeMethod(obj: this, member: "traceBegin" , |
| 338 | Q_RETURN_ARG(QVariant, result), |
| 339 | Q_ARG(int, traceId), |
| 340 | Q_ARG(int, (int)patternRecognitionMode), |
| 341 | Q_ARG(QVariant, traceCaptureDeviceInfo), |
| 342 | Q_ARG(QVariant, traceScreenInfo)); |
| 343 | return result.value<QVirtualKeyboardTrace *>(); |
| 344 | } |
| 345 | |
| 346 | bool InputMethod::traceEnd(QVirtualKeyboardTrace *trace) |
| 347 | { |
| 348 | QVariant result; |
| 349 | QMetaObject::invokeMethod(obj: this, member: "traceEnd" , |
| 350 | Q_RETURN_ARG(QVariant, result), |
| 351 | Q_ARG(QVariant, QVariant::fromValue(trace))); |
| 352 | return result.toBool(); |
| 353 | } |
| 354 | |
| 355 | bool InputMethod::reselect(int cursorPosition, const QVirtualKeyboardInputEngine::ReselectFlags &reselectFlags) |
| 356 | { |
| 357 | QVariant result; |
| 358 | QMetaObject::invokeMethod(obj: this, member: "reselect" , |
| 359 | Q_RETURN_ARG(QVariant, result), |
| 360 | Q_ARG(int, cursorPosition), |
| 361 | Q_ARG(int, (int)reselectFlags)); |
| 362 | return result.toBool(); |
| 363 | } |
| 364 | |
| 365 | bool InputMethod::clickPreeditText(int cursorPosition) |
| 366 | { |
| 367 | QVariant result; |
| 368 | QMetaObject::invokeMethod(obj: this, member: "clickPreeditText" , |
| 369 | Q_RETURN_ARG(QVariant, result), |
| 370 | Q_ARG(int, cursorPosition)); |
| 371 | return result.toBool(); |
| 372 | } |
| 373 | |
| 374 | void InputMethod::reset() |
| 375 | { |
| 376 | QMetaObject::invokeMethod(obj: this, member: "reset" ); |
| 377 | } |
| 378 | |
| 379 | void InputMethod::update() |
| 380 | { |
| 381 | QMetaObject::invokeMethod(obj: this, member: "update" ); |
| 382 | } |
| 383 | |
| 384 | void InputMethod::clearInputMode() |
| 385 | { |
| 386 | QMetaObject::invokeMethod(obj: this, member: "clearInputMode" ); |
| 387 | } |
| 388 | |
| 389 | } // namespace QtVirtualKeyboard |
| 390 | QT_END_NAMESPACE |
| 391 | |