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