| 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/qvirtualkeyboardabstractinputmethod.h> |
| 31 | #include <QtCore/private/qobject_p.h> |
| 32 | |
| 33 | QT_BEGIN_NAMESPACE |
| 34 | |
| 35 | class QVirtualKeyboardAbstractInputMethodPrivate : public QObjectPrivate |
| 36 | { |
| 37 | public: |
| 38 | QVirtualKeyboardAbstractInputMethodPrivate(); |
| 39 | |
| 40 | QVirtualKeyboardInputEngine *inputEngine; |
| 41 | }; |
| 42 | |
| 43 | /*! |
| 44 | \class AbstractInputMethodPrivate |
| 45 | \internal |
| 46 | */ |
| 47 | |
| 48 | QVirtualKeyboardAbstractInputMethodPrivate::QVirtualKeyboardAbstractInputMethodPrivate() : |
| 49 | inputEngine(nullptr) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | /*! |
| 54 | \class QVirtualKeyboardAbstractInputMethod |
| 55 | |
| 56 | \inmodule QtVirtualKeyboard |
| 57 | |
| 58 | \brief The base class for input methods. |
| 59 | |
| 60 | Use this class if you want to implement a custom input |
| 61 | method using C/C++ language. |
| 62 | */ |
| 63 | |
| 64 | /*! |
| 65 | Constructs an input method with \a parent. |
| 66 | */ |
| 67 | QVirtualKeyboardAbstractInputMethod::QVirtualKeyboardAbstractInputMethod(QObject *parent) : |
| 68 | QObject(*new QVirtualKeyboardAbstractInputMethodPrivate(), parent) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | /*! |
| 73 | Destroys the input method and frees all allocated resources. |
| 74 | */ |
| 75 | QVirtualKeyboardAbstractInputMethod::~QVirtualKeyboardAbstractInputMethod() |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | /*! |
| 80 | Returns the input context associated with the input method. |
| 81 | This method returns \c NULL if the input method is not active. |
| 82 | */ |
| 83 | QVirtualKeyboardInputContext *QVirtualKeyboardAbstractInputMethod::inputContext() const |
| 84 | { |
| 85 | Q_D(const QVirtualKeyboardAbstractInputMethod); |
| 86 | return d->inputEngine ? d->inputEngine->inputContext() : nullptr; |
| 87 | } |
| 88 | |
| 89 | /*! |
| 90 | Returns the input engine associated with the input method. |
| 91 | This method returns \c NULL if the input method is not active. |
| 92 | */ |
| 93 | QVirtualKeyboardInputEngine *QVirtualKeyboardAbstractInputMethod::inputEngine() const |
| 94 | { |
| 95 | Q_D(const QVirtualKeyboardAbstractInputMethod); |
| 96 | return d->inputEngine; |
| 97 | } |
| 98 | |
| 99 | /*! |
| 100 | This method is called by the input engine when the input method needs |
| 101 | to be reset. The input method must reset its internal state only. The main |
| 102 | difference to the update() method is that reset() modifies only |
| 103 | the input method state, i.e. it must not modify the input context. |
| 104 | */ |
| 105 | void QVirtualKeyboardAbstractInputMethod::reset() |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | /*! |
| 110 | This method is called by the input engine when the input method needs to be |
| 111 | updated. The input method must close the current pre-edit text and |
| 112 | restore its internal state to the default. |
| 113 | */ |
| 114 | void QVirtualKeyboardAbstractInputMethod::update() |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | /*! |
| 119 | \internal |
| 120 | Called by the input engine when the input method is activated and |
| 121 | deactivated. |
| 122 | */ |
| 123 | void QVirtualKeyboardAbstractInputMethod::setInputEngine(QVirtualKeyboardInputEngine *inputEngine) |
| 124 | { |
| 125 | Q_D(QVirtualKeyboardAbstractInputMethod); |
| 126 | if (d->inputEngine) { |
| 127 | d->inputEngine->disconnect(receiver: this, SLOT(reset())); |
| 128 | d->inputEngine->disconnect(receiver: this, SLOT(update())); |
| 129 | } |
| 130 | d->inputEngine = inputEngine; |
| 131 | if (d->inputEngine) { |
| 132 | connect(asender: d->inputEngine, SIGNAL(inputMethodReset()), SLOT(reset())); |
| 133 | connect(asender: d->inputEngine, SIGNAL(inputMethodUpdate()), SLOT(update())); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | QList<QVirtualKeyboardSelectionListModel::Type> QVirtualKeyboardAbstractInputMethod::selectionLists() |
| 138 | { |
| 139 | return QList<QVirtualKeyboardSelectionListModel::Type>(); |
| 140 | } |
| 141 | |
| 142 | int QVirtualKeyboardAbstractInputMethod::selectionListItemCount(QVirtualKeyboardSelectionListModel::Type type) |
| 143 | { |
| 144 | Q_UNUSED(type) |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | QVariant QVirtualKeyboardAbstractInputMethod::selectionListData(QVirtualKeyboardSelectionListModel::Type type, int index, QVirtualKeyboardSelectionListModel::Role role) |
| 149 | { |
| 150 | Q_UNUSED(type) |
| 151 | Q_UNUSED(index) |
| 152 | switch (role) { |
| 153 | case QVirtualKeyboardSelectionListModel::Role::Display: |
| 154 | return QVariant(QString()); |
| 155 | case QVirtualKeyboardSelectionListModel::Role::WordCompletionLength: |
| 156 | return QVariant(0); |
| 157 | case QVirtualKeyboardSelectionListModel::Role::Dictionary: |
| 158 | return QVariant(static_cast<int>(QVirtualKeyboardSelectionListModel::DictionaryType::Default)); |
| 159 | case QVirtualKeyboardSelectionListModel::Role::CanRemoveSuggestion: |
| 160 | return QVariant(false); |
| 161 | } |
| 162 | return QVariant(); |
| 163 | } |
| 164 | |
| 165 | void QVirtualKeyboardAbstractInputMethod::selectionListItemSelected(QVirtualKeyboardSelectionListModel::Type type, int index) |
| 166 | { |
| 167 | Q_UNUSED(type) |
| 168 | Q_UNUSED(index) |
| 169 | } |
| 170 | |
| 171 | bool QVirtualKeyboardAbstractInputMethod::selectionListRemoveItem(QVirtualKeyboardSelectionListModel::Type type, int index) |
| 172 | { |
| 173 | Q_UNUSED(type) |
| 174 | Q_UNUSED(index) |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | /*! |
| 179 | \since QtQuick.VirtualKeyboard 2.0 |
| 180 | |
| 181 | Returns list of supported pattern recognition modes. |
| 182 | |
| 183 | This method is called by the input engine to query the list of |
| 184 | supported pattern recognition modes. |
| 185 | */ |
| 186 | QList<QVirtualKeyboardInputEngine::PatternRecognitionMode> QVirtualKeyboardAbstractInputMethod::patternRecognitionModes() const |
| 187 | { |
| 188 | return QList<QVirtualKeyboardInputEngine::PatternRecognitionMode>(); |
| 189 | } |
| 190 | |
| 191 | /*! |
| 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 QVirtualKeyboardTrace object. This object must remain valid until the traceEnd() method is called. If the |
| 201 | QVirtualKeyboardTrace is rendered on screen, it remains there until the QVirtualKeyboardTrace object is destroyed. |
| 202 | */ |
| 203 | QVirtualKeyboardTrace *QVirtualKeyboardAbstractInputMethod::traceBegin( |
| 204 | int traceId, QVirtualKeyboardInputEngine::PatternRecognitionMode patternRecognitionMode, |
| 205 | const QVariantMap &traceCaptureDeviceInfo, const QVariantMap &traceScreenInfo) |
| 206 | { |
| 207 | Q_UNUSED(traceId) |
| 208 | Q_UNUSED(patternRecognitionMode) |
| 209 | Q_UNUSED(traceCaptureDeviceInfo) |
| 210 | Q_UNUSED(traceScreenInfo) |
| 211 | return nullptr; |
| 212 | } |
| 213 | |
| 214 | /*! |
| 215 | \since QtQuick.VirtualKeyboard 2.0 |
| 216 | |
| 217 | This method is called when the trace interaction ends. The input method should destroy the \a trace object |
| 218 | at some point after this function is called. See the \l {Trace API for Input Methods} how to access the gathered |
| 219 | data. |
| 220 | |
| 221 | The method returns \c true if the trace interaction is accepted. |
| 222 | */ |
| 223 | bool QVirtualKeyboardAbstractInputMethod::traceEnd(QVirtualKeyboardTrace *trace) |
| 224 | { |
| 225 | Q_UNUSED(trace) |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | /*! |
| 230 | \since QtQuick.VirtualKeyboard 2.0 |
| 231 | |
| 232 | This function attempts to reselect a word located at the \a cursorPosition. |
| 233 | The \a reselectFlags define the rules for how the word should be selected in |
| 234 | relation to the cursor position. |
| 235 | |
| 236 | The function returns \c true if the word was successfully reselected. |
| 237 | */ |
| 238 | bool QVirtualKeyboardAbstractInputMethod::reselect(int cursorPosition, const QVirtualKeyboardInputEngine::ReselectFlags &reselectFlags) |
| 239 | { |
| 240 | Q_UNUSED(cursorPosition) |
| 241 | Q_UNUSED(reselectFlags) |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | /*! |
| 246 | \since QtQuick.VirtualKeyboard 2.4 |
| 247 | |
| 248 | Called when the user clicks on pre-edit text at \a cursorPosition. |
| 249 | |
| 250 | The function should return \c true if it handles the event. Otherwise the input |
| 251 | falls back to \l reselect() for further processing. |
| 252 | */ |
| 253 | bool QVirtualKeyboardAbstractInputMethod::clickPreeditText(int cursorPosition) |
| 254 | { |
| 255 | Q_UNUSED(cursorPosition) |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | /*! |
| 260 | \fn QList<QVirtualKeyboardInputEngine::InputMode> QVirtualKeyboardAbstractInputMethod::inputModes(const QString& locale) |
| 261 | |
| 262 | Returns the list of input modes for \a locale. |
| 263 | */ |
| 264 | |
| 265 | /*! |
| 266 | \fn bool QVirtualKeyboardAbstractInputMethod::setInputMode(const QString& locale, QVirtualKeyboardInputEngine::InputMode inputMode) |
| 267 | |
| 268 | Sets the \a inputMode and \a locale for this input method. Returns \c true |
| 269 | if successful. |
| 270 | */ |
| 271 | |
| 272 | /*! |
| 273 | \fn bool QVirtualKeyboardAbstractInputMethod::setTextCase(QVirtualKeyboardInputEngine::TextCase textCase) |
| 274 | |
| 275 | Updates the \a textCase for this input method. The method returns \c true |
| 276 | if successful. |
| 277 | */ |
| 278 | |
| 279 | /*! |
| 280 | \fn bool QVirtualKeyboardAbstractInputMethod::keyEvent(Qt::Key key, const QString& text, Qt::KeyboardModifiers modifiers) |
| 281 | |
| 282 | The purpose of this method is to handle the key events generated by the the |
| 283 | input engine. |
| 284 | |
| 285 | The \a key parameter specifies the code of the key to handle. The key code |
| 286 | does not distinguish between capital and non-capital letters. The \a |
| 287 | text parameter contains the Unicode text for the key. The \a modifiers |
| 288 | parameter contains the key modifiers that apply to key. |
| 289 | |
| 290 | This method returns \c true if the key event was successfully handled. |
| 291 | If the return value is \c false, the key event is redirected to the default |
| 292 | input method for further processing. |
| 293 | */ |
| 294 | |
| 295 | /*! |
| 296 | \fn QList<QVirtualKeyboardSelectionListModel::Type> QVirtualKeyboardAbstractInputMethod::selectionLists() |
| 297 | |
| 298 | Returns the list of selection lists used by this input method. |
| 299 | |
| 300 | This method is called by input engine when the input method is being |
| 301 | activated and every time the input method hints are updated. The input method |
| 302 | can reserve selection lists by returning the desired selection list types. |
| 303 | |
| 304 | The input method may request the input engine to update the selection lists |
| 305 | at any time by emitting selectionListsChanged() signal. This signal will |
| 306 | trigger a call to this method, allowing the input method to update the selection |
| 307 | list types. |
| 308 | */ |
| 309 | |
| 310 | /*! |
| 311 | \fn int QVirtualKeyboardAbstractInputMethod::selectionListItemCount(QVirtualKeyboardSelectionListModel::Type type) |
| 312 | |
| 313 | Returns the number of items in the selection list identified by \a type. |
| 314 | */ |
| 315 | |
| 316 | /*! |
| 317 | \fn QVariant QVirtualKeyboardAbstractInputMethod::selectionListData(QVirtualKeyboardSelectionListModel::Type type, int index, QVirtualKeyboardSelectionListModel::Role role) |
| 318 | |
| 319 | Returns item data for the selection list identified by \a type. The \a role |
| 320 | parameter specifies which data is requested. The \a index parameter is a |
| 321 | zero based index into the list. |
| 322 | */ |
| 323 | |
| 324 | /*! |
| 325 | \fn void QVirtualKeyboardAbstractInputMethod::selectionListItemSelected(QVirtualKeyboardSelectionListModel::Type type, int index) |
| 326 | |
| 327 | This method is called when an item at \a index has been selected by the |
| 328 | user. The selection list is identified by the \a type parameter. |
| 329 | */ |
| 330 | |
| 331 | /*! |
| 332 | \fn bool QVirtualKeyboardAbstractInputMethod::selectionListRemoveItem(QVirtualKeyboardSelectionListModel::Type type, int index) |
| 333 | |
| 334 | This method is called when an item at \a index must be removed from dictionary. |
| 335 | The selection list is identified by the \a type parameter. |
| 336 | The function returns \c true if the word was successfully removed. |
| 337 | */ |
| 338 | |
| 339 | /*! |
| 340 | \fn void QVirtualKeyboardAbstractInputMethod::selectionListChanged(QVirtualKeyboardSelectionListModel::Type type) |
| 341 | |
| 342 | The input method emits this signal when the contents of the selection list |
| 343 | has changed. The \a type parameter specifies which selection list has |
| 344 | changed. |
| 345 | */ |
| 346 | |
| 347 | /*! |
| 348 | \fn void QVirtualKeyboardAbstractInputMethod::selectionListActiveItemChanged(QVirtualKeyboardSelectionListModel::Type type, int index) |
| 349 | |
| 350 | The input method emits this signal when the current \a index has changed |
| 351 | in the selection list identified by \a type. |
| 352 | */ |
| 353 | |
| 354 | /*! |
| 355 | \fn void QVirtualKeyboardAbstractInputMethod::selectionListsChanged() |
| 356 | \since QtQuick.VirtualKeyboard 2.2 |
| 357 | |
| 358 | The input method emits this signal when the selection list types have |
| 359 | changed. This signal will trigger a call to selectionLists() method, |
| 360 | allowing the input method to update the selection list types. |
| 361 | */ |
| 362 | |
| 363 | QT_END_NAMESPACE |
| 364 | |