inputcontext.cpp 34.1 KB
Newer Older
Kalle Viironen's avatar
Kalle Viironen committed
/****************************************************************************
Jarkko Koivikko's avatar
Jarkko Koivikko committed
**
Kalle Viironen's avatar
Kalle Viironen committed
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
**
Kalle Viironen's avatar
Kalle Viironen committed
** This file is part of the Qt Virtual Keyboard module of the Qt Toolkit.
Jarkko Koivikko's avatar
Jarkko Koivikko committed
**
Kalle Viironen's avatar
Kalle Viironen committed
** $QT_BEGIN_LICENSE:GPL$
Kalle Viironen's avatar
Kalle Viironen committed
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
Kalle Viironen's avatar
Kalle Viironen committed
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Kalle Viironen's avatar
Kalle Viironen committed
**
** $QT_END_LICENSE$
Jarkko Koivikko's avatar
Jarkko Koivikko committed
**
Kalle Viironen's avatar
Kalle Viironen committed
****************************************************************************/
Jarkko Koivikko's avatar
Jarkko Koivikko committed

Jarkko Koivikko's avatar
Jarkko Koivikko committed
#include "inputcontext.h"
#include "inputengine.h"
#include "shifthandler.h"
Jarkko Koivikko's avatar
Jarkko Koivikko committed
#include "platforminputcontext.h"
#include "shadowinputcontext.h"
Mitch Curtis's avatar
Mitch Curtis committed
#include "virtualkeyboarddebug.h"
#include "enterkeyaction.h"
#include "settings.h"
Jarkko Koivikko's avatar
Jarkko Koivikko committed

#include <QTextFormat>
#include <QGuiApplication>
#include <QtCore/private/qobject_p.h>
Jarkko Koivikko's avatar
Jarkko Koivikko committed

QT_BEGIN_NAMESPACE
bool operator==(const QInputMethodEvent::Attribute &attribute1, const QInputMethodEvent::Attribute &attribute2)
{
    return attribute1.start == attribute2.start &&
           attribute1.length == attribute2.length &&
           attribute1.type == attribute2.type &&
           attribute1.value == attribute2.value;
}

/*!
    \namespace QtVirtualKeyboard
    \inmodule QtVirtualKeyboard

    \brief Namespace for the Qt Virtual Keyboard C++ API.
*/

namespace QtVirtualKeyboard {

Jarkko Koivikko's avatar
Jarkko Koivikko committed
class InputContextPrivate : public QObjectPrivate
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
public:
    enum StateFlag {
        ReselectEventState = 0x1,
        InputMethodEventState = 0x2,
        KeyEventState = 0x4,
        InputMethodClickState = 0x8,
        SyncShadowInputState = 0x10
    };
    Q_DECLARE_FLAGS(StateFlags, StateFlag)

Jarkko Koivikko's avatar
Jarkko Koivikko committed
    InputContextPrivate() :
        QObjectPrivate(),
        inputContext(0),
        inputEngine(0),
        shiftHandler(0),
        keyboardRect(),
        previewRect(),
        previewVisible(false),
        animating(false),
        focus(false),
        shift(false),
        capsLock(false),
        cursorPosition(0),
        anchorPosition(0),
        forceAnchorPosition(-1),
        forceCursorPosition(-1),
        inputMethodHints(Qt::ImhNone),
        preeditText(),
        preeditTextAttributes(),
        surroundingText(),
        selectedText(),
        anchorRectangle(),
        cursorRectangle(),
        selectionControlVisible(false),
        anchorRectIntersectsClipRect(false),
        cursorRectIntersectsClipRect(false)
#ifdef QT_VIRTUALKEYBOARD_ARROW_KEY_NAVIGATION
        , activeNavigationKeys()
#endif
    {
    }

    PlatformInputContext *inputContext;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    InputEngine *inputEngine;
    ShiftHandler *shiftHandler;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    QRectF keyboardRect;
    QRectF previewRect;
    bool previewVisible;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    bool animating;
    bool focus;
    bool shift;
    bool capsLock;
    StateFlags stateFlags;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    int cursorPosition;
    int anchorPosition;
    int forceAnchorPosition;
    int forceCursorPosition;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Qt::InputMethodHints inputMethodHints;
    QString preeditText;
    QList<QInputMethodEvent::Attribute> preeditTextAttributes;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    QString surroundingText;
    QString selectedText;
    QRectF anchorRectangle;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    QRectF cursorRectangle;
    bool selectionControlVisible;
    bool anchorRectIntersectsClipRect;
    bool cursorRectIntersectsClipRect;
#ifdef QT_VIRTUALKEYBOARD_ARROW_KEY_NAVIGATION
    QSet<int> activeNavigationKeys;
#endif
    ShadowInputContext shadow;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
};

Jarkko Koivikko's avatar
Jarkko Koivikko committed
Q_DECLARE_OPERATORS_FOR_FLAGS(InputContextPrivate::StateFlags)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
/*!
    \qmltype InputContext
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \instantiates QtVirtualKeyboard::InputContext
    \inqmlmodule QtQuick.VirtualKeyboard
    \ingroup qtvirtualkeyboard-qml
    \brief Provides access to an input context.
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    The InputContext can be accessed as singleton instance.
*/

/*!
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \class QtVirtualKeyboard::InputContext
    \inmodule QtVirtualKeyboard
    \brief Provides access to an input context.
Jarkko Koivikko's avatar
Jarkko Koivikko committed
*/

/*!
    \internal
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Constructs an input context with \a parent as the platform input
Jarkko Koivikko's avatar
Jarkko Koivikko committed
*/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
InputContext::InputContext(PlatformInputContext *parent) :
    QObject(*new InputContextPrivate(), parent)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    d->inputContext = parent;
    d->shadow.setInputContext(this);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    if (d->inputContext) {
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->inputContext->setInputContext(this);
        connect(d->inputContext, SIGNAL(focusObjectChanged()), SLOT(onInputItemChanged()));
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    }
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    d->inputEngine = new InputEngine(this);
    d->shiftHandler = new ShiftHandler(this);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
}

/*!
    \internal
    Destroys the input context and frees all allocated resources.
*/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
InputContext::~InputContext()
Jarkko Koivikko's avatar
Jarkko Koivikko committed
bool InputContext::focus() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->focus;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
bool InputContext::shift() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->shift;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::setShift(bool enable)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    if (d->shift != enable) {
        d->shift = enable;
        emit shiftChanged();
        if (!d->capsLock)
            emit uppercaseChanged();
Jarkko Koivikko's avatar
Jarkko Koivikko committed
bool InputContext::capsLock() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->capsLock;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::setCapsLock(bool enable)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    if (d->capsLock != enable) {
        d->capsLock = enable;
        emit capsLockChanged();
        if (!d->shift)
            emit uppercaseChanged();
bool InputContext::uppercase() const
{
    Q_D(const InputContext);
    return d->shift || d->capsLock;
}

int InputContext::anchorPosition() const
{
    Q_D(const InputContext);
    return d->anchorPosition;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
int InputContext::cursorPosition() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->cursorPosition;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
Qt::InputMethodHints InputContext::inputMethodHints() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->inputMethodHints;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
QString InputContext::preeditText() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->preeditText;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::setPreeditText(const QString &text, QList<QInputMethodEvent::Attribute> attributes, int replaceFrom, int replaceLength)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
    // Add default attributes
    if (!text.isEmpty()) {
        if (!testAttribute(attributes, QInputMethodEvent::TextFormat)) {
            QTextCharFormat textFormat;
            textFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline);
            attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, 0, text.length(), textFormat));
        }
    } else {
        addSelectionAttribute(attributes);
    sendPreedit(text, attributes, replaceFrom, replaceLength);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
}

QList<QInputMethodEvent::Attribute> InputContext::preeditTextAttributes() const
{
    Q_D(const InputContext);
    return d->preeditTextAttributes;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
QString InputContext::surroundingText() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->surroundingText;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
QString InputContext::selectedText() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->selectedText;
}

QRectF InputContext::anchorRectangle() const
{
    Q_D(const InputContext);
    return d->anchorRectangle;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
QRectF InputContext::cursorRectangle() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->cursorRectangle;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
QRectF InputContext::keyboardRectangle() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->keyboardRect;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::setKeyboardRectangle(QRectF rectangle)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    if (d->keyboardRect != rectangle) {
        d->keyboardRect = rectangle;
        emit keyboardRectangleChanged();
        d->inputContext->emitKeyboardRectChanged();
    }
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
QRectF InputContext::previewRectangle() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
    return d->previewRect;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::setPreviewRectangle(QRectF rectangle)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
    if (d->previewRect != rectangle) {
        d->previewRect = rectangle;
        emit previewRectangleChanged();
    }
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
bool InputContext::previewVisible() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
    return d->previewVisible;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::setPreviewVisible(bool visible)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
    if (d->previewVisible != visible) {
        d->previewVisible = visible;
        emit previewVisibleChanged();
    }
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
bool InputContext::animating() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->animating;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::setAnimating(bool animating)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    if (d->animating != animating) {
        VIRTUALKEYBOARD_DEBUG() << "InputContext::setAnimating():" << animating;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->animating = animating;
        emit animatingChanged();
        d->inputContext->emitAnimatingChanged();
    }
}


Jarkko Koivikko's avatar
Jarkko Koivikko committed
QString InputContext::locale() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->inputContext->locale().name();
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::setLocale(const QString &locale)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
    VIRTUALKEYBOARD_DEBUG() << "InputContext::setLocale():" << locale;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    QLocale newLocale(locale);
    if (newLocale != d->inputContext->locale()) {
        d->inputContext->setLocale(newLocale);
        d->inputContext->setInputDirection(newLocale.textDirection());
        emit localeChanged();
    }
}

/*!
    \internal
*/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::updateAvailableLocales(const QStringList &availableLocales)
{
    Settings *settings = Settings::instance();
    if (settings)
        settings->setAvailableLocales(availableLocales);
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
QObject *InputContext::inputItem() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->inputContext ? d->inputContext->focusObject() : 0;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
ShiftHandler *InputContext::shiftHandler() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->shiftHandler;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
InputEngine *InputContext::inputEngine() const
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(const InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    return d->inputEngine;
}

/*!
    \qmlmethod void InputContext::hideInputPanel()

    This method hides the input panel. This method should only be called
    when the user initiates the hide, e.g. by pressing a dedicated button
    on the keyboard.
Jarkko Koivikko's avatar
Jarkko Koivikko committed
*/
/*!
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \fn void QtVirtualKeyboard::InputContext::hideInputPanel()

Jarkko Koivikko's avatar
Jarkko Koivikko committed
    This method hides the input panel. This method should only be called
    when the user initiates the hide, e.g. by pressing a dedicated button
    on the keyboard.
Jarkko Koivikko's avatar
Jarkko Koivikko committed
*/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::hideInputPanel()
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    d->inputContext->hideInputPanel();
}

/*!
    \qmlmethod void InputContext::sendKeyClick(int key, string text, int modifiers = 0)

    Sends a key click event with the given \a key, \a text and \a modifiers to
    the input item that currently has focus.
Jarkko Koivikko's avatar
Jarkko Koivikko committed
*/
/*!
    Sends a key click event with the given \a key, \a text and \a modifiers to
    the input item that currently has focus.
Jarkko Koivikko's avatar
Jarkko Koivikko committed
*/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::sendKeyClick(int key, const QString &text, int modifiers)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    if (d->focus && d->inputContext) {
        QKeyEvent pressEvent(QEvent::KeyPress, key, Qt::KeyboardModifiers(modifiers), text);
        QKeyEvent releaseEvent(QEvent::KeyRelease, key, Qt::KeyboardModifiers(modifiers), text);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        VIRTUALKEYBOARD_DEBUG() << "InputContext::::sendKeyClick():" << key;
Jarkko Koivikko's avatar
Jarkko Koivikko committed

Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->stateFlags |= InputContextPrivate::KeyEventState;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->inputContext->sendKeyEvent(&pressEvent);
        d->inputContext->sendKeyEvent(&releaseEvent);
        if (d->activeKeys.isEmpty())
Jarkko Koivikko's avatar
Jarkko Koivikko committed
            d->stateFlags &= ~InputContextPrivate::KeyEventState;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    } else {
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        qWarning() << "InputContext::::sendKeyClick():" << key << "no focus";
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    }
}

/*!
    \qmlmethod void InputContext::commit()

    Commits the current pre-edit text.
*/
/*!
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \fn void QtVirtualKeyboard::InputContext::commit()

Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Commits the current pre-edit text.
*/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::commit()
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    QString text = d->preeditText;
    commit(text);
}

/*!
    \qmlmethod void InputContext::commit(string text, int replaceFrom = 0, int replaceLength = 0)
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    Commits the final \a text to the input item and optionally
    modifies the text relative to the start of the pre-edit text.
    If \a replaceFrom is non-zero, the \a text replaces the
    contents relative to \a replaceFrom with a length of
    \a replaceLength.
Jarkko Koivikko's avatar
Jarkko Koivikko committed
*/
/*!
    Commits the final \a text to the input item and optionally
    modifies the text relative to the start of the pre-edit text.
    If \a replaceFrom is non-zero, the \a text replaces the
    contents relative to \a replaceFrom with a length of
    \a replaceLength.
Jarkko Koivikko's avatar
Jarkko Koivikko committed
*/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::commit(const QString &text, int replaceFrom, int replaceLength)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
    VIRTUALKEYBOARD_DEBUG() << "InputContext::commit():" << text << replaceFrom << replaceLength;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    bool preeditChanged = !d->preeditText.isEmpty();
    d->preeditText.clear();
    d->preeditTextAttributes.clear();
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    if (d->inputContext) {
        QList<QInputMethodEvent::Attribute> attributes;
        addSelectionAttribute(attributes);
        QInputMethodEvent inputEvent(QString(), attributes);
        inputEvent.setCommitString(text, replaceFrom, replaceLength);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->stateFlags |= InputContextPrivate::InputMethodEventState;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->inputContext->sendEvent(&inputEvent);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->stateFlags &= ~InputContextPrivate::InputMethodEventState;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    }

    if (preeditChanged)
        emit preeditTextChanged();
}

/*!
    \qmlmethod void InputContext::clear()

    Clears the pre-edit text.
Jarkko Koivikko's avatar
Jarkko Koivikko committed
*/
/*!
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \fn void QtVirtualKeyboard::InputContext::clear()

    Clears the pre-edit text.
Jarkko Koivikko's avatar
Jarkko Koivikko committed
*/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::clear()
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    bool preeditChanged = !d->preeditText.isEmpty();
    d->preeditText.clear();
    d->preeditTextAttributes.clear();
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    if (d->inputContext) {
        QList<QInputMethodEvent::Attribute> attributes;
        addSelectionAttribute(attributes);
        QInputMethodEvent event(QString(), attributes);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->stateFlags |= InputContextPrivate::InputMethodEventState;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->inputContext->sendEvent(&event);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->stateFlags &= ~InputContextPrivate::InputMethodEventState;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    }

    if (preeditChanged)
        emit preeditTextChanged();
}

/*!
    \internal
*/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
bool InputContext::fileExists(const QUrl &fileUrl)
    QString fileName;
    if (fileUrl.scheme() == QLatin1String("qrc")) {
        fileName = QLatin1Char(':') + fileUrl.path();
    } else {
        fileName = fileUrl.toLocalFile();
    }
    return !fileName.isEmpty() && QFile::exists(fileName);
/*!
    \internal
*/
Jarkko Koivikko's avatar
Jarkko Koivikko committed
bool InputContext::hasEnterKeyAction(QObject *item) const
{
    return item != 0 && qmlAttachedPropertiesObject<EnterKeyAction>(item, false);
}

void InputContext::setSelectionOnFocusObject(const QPointF &anchorPos, const QPointF &cursorPos)
{
    QPlatformInputContext::setSelectionOnFocusObject(anchorPos, cursorPos);
}

/*!
    \internal
*/
void InputContext::forceCursorPosition(int anchorPosition, int cursorPosition)
{
    Q_D(InputContext);
    if (!d->shadow.inputItem())
        return;
    if (!d->inputContext->m_visible)
        return;
    if (d->stateFlags.testFlag(InputContextPrivate::ReselectEventState))
        return;
    if (d->stateFlags.testFlag(InputContextPrivate::SyncShadowInputState))
        return;

    VIRTUALKEYBOARD_DEBUG() << "InputContext::forceCursorPosition():" << cursorPosition << "anchorPosition:" << anchorPosition;
    if (!d->preeditText.isEmpty()) {
        d->forceAnchorPosition = -1;
        d->forceCursorPosition = cursorPosition;
        if (cursorPosition > d->cursorPosition)
            d->forceCursorPosition += d->preeditText.length();
        d->inputEngine->update();
    } else {
        d->forceAnchorPosition = anchorPosition;
        d->forceCursorPosition = cursorPosition;
        setPreeditText("");
        if (!d->inputMethodHints.testFlag(Qt::ImhNoPredictiveText) &&
                   cursorPosition > 0 && d->selectedText.isEmpty()) {
            d->stateFlags |= InputContextPrivate::ReselectEventState;
            if (d->inputEngine->reselect(cursorPosition, InputEngine::WordAtCursor))
                d->stateFlags |= InputContextPrivate::InputMethodClickState;
            d->stateFlags &= ~InputContextPrivate::ReselectEventState;
        }
    }
}

bool InputContext::anchorRectIntersectsClipRect() const
{
    Q_D(const InputContext);
    return d->anchorRectIntersectsClipRect;
}

bool InputContext::cursorRectIntersectsClipRect() const
{
    Q_D(const InputContext);
    return d->cursorRectIntersectsClipRect;
}

bool InputContext::selectionControlVisible() const
{
    Q_D(const InputContext);
    return d->selectionControlVisible;
}

ShadowInputContext *InputContext::shadow() const
{
    Q_D(const InputContext);
    return const_cast<ShadowInputContext *>(&d->shadow);
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::onInputItemChanged()
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
    if (!inputItem() && !d->activeKeys.isEmpty()) {
        // After losing keyboard focus it is impossible to track pressed keys
        d->activeKeys.clear();
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->stateFlags &= ~InputContextPrivate::KeyEventState;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    d->stateFlags &= ~InputContextPrivate::InputMethodClickState;

    emit inputItemChanged();
Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::setFocus(bool enable)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    if (d->focus != enable) {
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        VIRTUALKEYBOARD_DEBUG() << "InputContext::setFocus():" << enable;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->focus = enable;
        emit focusChanged();
    }
    emit focusEditorChanged();
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::sendPreedit(const QString &text, const QList<QInputMethodEvent::Attribute> &attributes, int replaceFrom, int replaceLength)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
    VIRTUALKEYBOARD_DEBUG() << "InputContext::sendPreedit():" << text << replaceFrom << replaceLength;
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    bool textChanged = d->preeditText != text;
    bool attributesChanged = d->preeditTextAttributes != attributes;
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    if (textChanged || attributesChanged) {
        d->preeditText = text;
        d->preeditTextAttributes = attributes;
Jarkko Koivikko's avatar
Jarkko Koivikko committed

        if (d->inputContext) {
            QInputMethodEvent event(text, attributes);
            const bool replace = replaceFrom != 0 || replaceLength > 0;
            if (replace)
                event.setCommitString(QString(), replaceFrom, replaceLength);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
            d->stateFlags |= InputContextPrivate::InputMethodEventState;
            d->inputContext->sendEvent(&event);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
            d->stateFlags &= ~InputContextPrivate::InputMethodEventState;

            // Send also to shadow input if only attributes changed.
            // In this case the update() may not be called, so the shadow
            // input may be out of sync.
            if (d->shadow.inputItem() && !replace && !text.isEmpty() &&
                    !textChanged && attributesChanged) {
                VIRTUALKEYBOARD_DEBUG() << "InputContext::sendPreedit(shadow):" << text << replaceFrom << replaceLength;
                event.setAccepted(true);
                QGuiApplication::sendEvent(d->shadow.inputItem(), &event);
            }
Jarkko Koivikko's avatar
Jarkko Koivikko committed

        if (textChanged)
            emit preeditTextChanged();
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    }

    if (d->preeditText.isEmpty())
        d->preeditTextAttributes.clear();
Jarkko Koivikko's avatar
Jarkko Koivikko committed
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::reset()
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    d->inputEngine->reset();
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::externalCommit()
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    d->inputEngine->update();
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::update(Qt::InputMethodQueries queries)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
{
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    // No need to fetch input clip rectangle during animation
    if (!(queries & ~Qt::ImInputItemClipRectangle) && d->animating)
        return;

Jarkko Koivikko's avatar
Jarkko Koivikko committed
    // fetch
    QInputMethodQueryEvent imQueryEvent(Qt::InputMethodQueries(Qt::ImHints |
                    Qt::ImQueryInput | Qt::ImInputItemClipRectangle));
    d->inputContext->sendEvent(&imQueryEvent);
    Qt::InputMethodHints inputMethodHints = Qt::InputMethodHints(imQueryEvent.value(Qt::ImHints).toInt());
    const int cursorPosition = imQueryEvent.value(Qt::ImCursorPosition).toInt();
    const int anchorPosition = imQueryEvent.value(Qt::ImAnchorPosition).toInt();
    QRectF anchorRectangle;
    QRectF cursorRectangle;
    if (const QGuiApplication *app = qApp) {
        anchorRectangle = app->inputMethod()->anchorRectangle();
        cursorRectangle = app->inputMethod()->cursorRectangle();
    } else {
        anchorRectangle = d->anchorRectangle;
        cursorRectangle = d->cursorRectangle;
    }
    QString surroundingText = imQueryEvent.value(Qt::ImSurroundingText).toString();
    QString selectedText = imQueryEvent.value(Qt::ImCurrentSelection).toString();
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    // check against changes
    bool newInputMethodHints = inputMethodHints != d->inputMethodHints;
    bool newSurroundingText = surroundingText != d->surroundingText;
    bool newSelectedText = selectedText != d->selectedText;
    bool newAnchorPosition = anchorPosition != d->anchorPosition;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    bool newCursorPosition = cursorPosition != d->cursorPosition;
    bool newAnchorRectangle = anchorRectangle != d->anchorRectangle;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    bool newCursorRectangle = cursorRectangle != d->cursorRectangle;
J-P Nurmi's avatar
J-P Nurmi committed
    bool selectionControlVisible = d->inputContext->isInputPanelVisible() && (cursorPosition != anchorPosition) && !inputMethodHints.testFlag(Qt::ImhNoTextHandles);
    bool newSelectionControlVisible = selectionControlVisible != d->selectionControlVisible;
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    QRectF inputItemClipRect = imQueryEvent.value(Qt::ImInputItemClipRectangle).toRectF();
    QRectF anchorRect = imQueryEvent.value(Qt::ImAnchorRectangle).toRectF();
    QRectF cursorRect = imQueryEvent.value(Qt::ImCursorRectangle).toRectF();

    bool anchorRectIntersectsClipRect = inputItemClipRect.intersects(anchorRect);
    bool newAnchorRectIntersectsClipRect = anchorRectIntersectsClipRect != d->anchorRectIntersectsClipRect;

    bool cursorRectIntersectsClipRect = inputItemClipRect.intersects(cursorRect);
    bool newCursorRectIntersectsClipRect = cursorRectIntersectsClipRect != d->cursorRectIntersectsClipRect;

Jarkko Koivikko's avatar
Jarkko Koivikko committed
    // update
    d->inputMethodHints = inputMethodHints;
    d->surroundingText = surroundingText;
    d->selectedText = selectedText;
    d->anchorPosition = anchorPosition;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    d->cursorPosition = cursorPosition;
    d->anchorRectangle = anchorRectangle;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    d->cursorRectangle = cursorRectangle;
    d->selectionControlVisible = selectionControlVisible;
    d->anchorRectIntersectsClipRect = anchorRectIntersectsClipRect;
    d->cursorRectIntersectsClipRect = cursorRectIntersectsClipRect;
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    // update input engine
    if ((newSurroundingText || newCursorPosition) &&
Jarkko Koivikko's avatar
Jarkko Koivikko committed
            !d->stateFlags.testFlag(InputContextPrivate::InputMethodEventState)) {
        d->inputEngine->update();
    }
    if (newInputMethodHints) {
        d->inputEngine->reset();
    }
Jarkko Koivikko's avatar
Jarkko Koivikko committed

    // notify
    if (newInputMethodHints) {
        emit inputMethodHintsChanged();
    }
    if (newSurroundingText) {
        emit surroundingTextChanged();
    }
    if (newSelectedText) {
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        emit selectedTextChanged();
    }
    if (newAnchorPosition) {
        emit anchorPositionChanged();
    }
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    if (newCursorPosition) {
        emit cursorPositionChanged();
    }
    if (newAnchorRectangle) {
        emit anchorRectangleChanged();
    }
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    if (newCursorRectangle) {
        emit cursorRectangleChanged();
    }
    if (newSelectionControlVisible) {
        emit selectionControlVisibleChanged();
    }
    if (newAnchorRectIntersectsClipRect) {
        emit anchorRectIntersectsClipRectChanged();
    }
    if (newCursorRectIntersectsClipRect) {
        emit cursorRectIntersectsClipRectChanged();
    }

    // word reselection
    if (newInputMethodHints || newSurroundingText || newSelectedText)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->stateFlags &= ~InputContextPrivate::InputMethodClickState;
    if ((newSurroundingText || newCursorPosition) && !newSelectedText && (int)d->stateFlags == 0 &&
            !d->inputMethodHints.testFlag(Qt::ImhNoPredictiveText) &&
            d->cursorPosition > 0 && d->selectedText.isEmpty()) {
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->stateFlags |= InputContextPrivate::ReselectEventState;
        if (d->inputEngine->reselect(d->cursorPosition, InputEngine::WordAtCursor))
            d->stateFlags |= InputContextPrivate::InputMethodClickState;
        d->stateFlags &= ~InputContextPrivate::ReselectEventState;

    if (!d->stateFlags.testFlag(InputContextPrivate::SyncShadowInputState)) {
        d->stateFlags |= InputContextPrivate::SyncShadowInputState;
        d->shadow.update(queries);
        d->stateFlags &= ~InputContextPrivate::SyncShadowInputState;
    }
Jarkko Koivikko's avatar
Jarkko Koivikko committed
void InputContext::invokeAction(QInputMethod::Action action, int cursorPosition)
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    Q_D(InputContext);
    switch (action) {
    case QInputMethod::Click:
        if ((int)d->stateFlags == 0) {
            bool reselect = !d->inputMethodHints.testFlag(Qt::ImhNoPredictiveText) && d->selectedText.isEmpty() && cursorPosition < d->preeditText.length();
            if (reselect) {
Jarkko Koivikko's avatar
Jarkko Koivikko committed
                d->stateFlags |= InputContextPrivate::ReselectEventState;
                d->forceCursorPosition = d->cursorPosition + cursorPosition;
                d->inputEngine->update();
Jarkko Koivikko's avatar
Jarkko Koivikko committed
                d->inputEngine->reselect(d->cursorPosition, InputEngine::WordBeforeCursor);
                d->stateFlags &= ~InputContextPrivate::ReselectEventState;
            } else if (!d->preeditText.isEmpty() && cursorPosition == d->preeditText.length()) {
                d->inputEngine->update();
            }
        }
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        d->stateFlags &= ~InputContextPrivate::InputMethodClickState;
        break;

    case QInputMethod::ContextMenu:
        break;
    }
Jarkko Koivikko's avatar
Jarkko Koivikko committed
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
bool InputContext::filterEvent(const QEvent *event)
    QEvent::Type type = event->type();
    if (type == QEvent::KeyPress || type == QEvent::KeyRelease) {
Jarkko Koivikko's avatar
Jarkko Koivikko committed
        Q_D(InputContext);
        const QKeyEvent *keyEvent = static_cast<const QKeyEvent *>(event);

        // Keep track of pressed keys update key event state
        if (type == QEvent::KeyPress)
            d->activeKeys += keyEvent->nativeScanCode();
        else if (type == QEvent::KeyRelease)
            d->activeKeys -= keyEvent->nativeScanCode();

        if (d->activeKeys.isEmpty())
Jarkko Koivikko's avatar
Jarkko Koivikko committed
            d->stateFlags &= ~InputContextPrivate::KeyEventState;
Jarkko Koivikko's avatar
Jarkko Koivikko committed
            d->stateFlags |= InputContextPrivate::KeyEventState;
#ifdef QT_VIRTUALKEYBOARD_ARROW_KEY_NAVIGATION
        int key = keyEvent->key();
        if ((key >= Qt::Key_Left && key <= Qt::Key_Down) || key == Qt::Key_Return) {
            if (type == QEvent::KeyPress && d->inputContext->isInputPanelVisible()) {
                d->activeNavigationKeys += key;
                emit navigationKeyPressed(key, keyEvent->isAutoRepeat());
                return true;
            } else if (type == QEvent::KeyRelease && d->activeNavigationKeys.contains(key)) {
                d->activeNavigationKeys -= key;
                emit navigationKeyReleased(key, keyEvent->isAutoRepeat());
                return true;
            }
        }
#endif

        // Break composing text since the virtual keyboard does not support hard keyboard events
        if (!d->preeditText.isEmpty())
            d->inputEngine->update();
    }
    return false;
}

void InputContext::addSelectionAttribute(QList<QInputMethodEvent::Attribute> &attributes)
{
    Q_D(InputContext);
    if (!testAttribute(attributes, QInputMethodEvent::Selection) && d->forceCursorPosition != -1) {
        if (d->forceAnchorPosition != -1)
            attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Selection, d->forceAnchorPosition, d->forceCursorPosition - d->forceAnchorPosition, QVariant()));
        else
            attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Selection, d->forceCursorPosition, 0, QVariant()));
    }
    d->forceAnchorPosition = -1;
    d->forceCursorPosition = -1;
}

bool InputContext::testAttribute(const QList<QInputMethodEvent::Attribute> &attributes, QInputMethodEvent::AttributeType attributeType) const
{
    for (const QInputMethodEvent::Attribute &attribute : qAsConst(attributes)) {
        if (attribute.type == attributeType)
            return true;
    }
    return false;
}

Jarkko Koivikko's avatar
Jarkko Koivikko committed
/*!
    \qmlproperty bool InputContext::focus

    This property is changed when the input method receives or loses focus.
*/

/*!
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \property QtVirtualKeyboard::InputContext::focus
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \brief the focus status.

    This property is changed when the input method receives or loses focus.
*/

/*!
    \qmlproperty bool InputContext::shift

    This property is changed when the shift status changes.
*/

/*!
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \property QtVirtualKeyboard::InputContext::shift
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \brief the shift status.

    This property is changed when the shift status changes.
*/

/*!
    \qmlproperty bool InputContext::capsLock

    This property is changed when the caps lock status changes.
*/

/*!
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \property QtVirtualKeyboard::InputContext::capsLock
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \brief the caps lock status.

    This property is changed when the caps lock status changes.
*/

/*!
    \qmlproperty bool InputContext::uppercase
    \since QtQuick.VirtualKeyboard 2.2

    This property is \c true when either \l shift or \l capsLock is \c true.
*/

/*!
    \property QtVirtualKeyboard::InputContext::uppercase
    \brief the uppercase status.

    This property is \c true when either \l shift or \l capsLock is \c true.
*/

/*!
    \qmlproperty int InputContext::anchorPosition
    \since QtQuick.VirtualKeyboard 2.2

    This property is changed when the anchor position changes.
*/

/*!
    \property QtVirtualKeyboard::InputContext::anchorPosition
    \brief the anchor position.

    This property is changed when the anchor position changes.
*/

Jarkko Koivikko's avatar
Jarkko Koivikko committed
/*!
    \qmlproperty int InputContext::cursorPosition

    This property is changed when the cursor position changes.
*/

/*!
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \property QtVirtualKeyboard::InputContext::cursorPosition
Jarkko Koivikko's avatar
Jarkko Koivikko committed
    \brief the cursor position.

    This property is changed when the cursor position changes.
*/

/*!
    \qmlproperty int InputContext::inputMethodHints

    This property is changed when the input method hints changes.