aboutsummaryrefslogtreecommitdiffstats
path: root/src/virtualkeyboard/shadowinputcontext.cpp
blob: c7a5387c94b3e315df828e8c7a65ef0a64c77221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtVirtualKeyboard/private/shadowinputcontext_p.h>
#include <QtVirtualKeyboard/qvirtualkeyboardinputcontext.h>
#include <QtVirtualKeyboard/private/virtualkeyboarddebug_p.h>

#include <QtCore/private/qobject_p.h>
#include <QGuiApplication>
#include <QQuickItem>

QT_BEGIN_NAMESPACE

namespace QtVirtualKeyboard {

class ShadowInputContextPrivate : public QObjectPrivate
{
public:
    ShadowInputContextPrivate() :
        QObjectPrivate(),
        inputContext(nullptr),
        anchorRectIntersectsClipRect(false),
        cursorRectIntersectsClipRect(false),
        selectionControlVisible(false)
    {
    }

    QVirtualKeyboardInputContext *inputContext;
    QPointer<QObject> inputItem;
    QString preeditText;
    QList<QInputMethodEvent::Attribute> preeditTextAttributes;
    QRectF anchorRectangle;
    QRectF cursorRectangle;
    bool anchorRectIntersectsClipRect;
    bool cursorRectIntersectsClipRect;
    bool selectionControlVisible;
};

ShadowInputContext::ShadowInputContext(QObject *parent) :
    QObject(*new ShadowInputContextPrivate(), parent)
{
}

void ShadowInputContext::setInputContext(QVirtualKeyboardInputContext *inputContext)
{
    Q_D(ShadowInputContext);
    d->inputContext = inputContext;
}

QObject *ShadowInputContext::inputItem() const
{
    Q_D(const ShadowInputContext);
    return d->inputItem.data();
}

void ShadowInputContext::setInputItem(QObject *inputItem)
{
    Q_D(ShadowInputContext);
    if (d->inputItem != inputItem) {
        d->inputItem = inputItem;
        emit inputItemChanged();
        update(Qt::ImQueryAll);
    }
}

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

QRectF ShadowInputContext::cursorRectangle() const
{
    Q_D(const ShadowInputContext);
    return d->cursorRectangle;
}

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

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

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

void ShadowInputContext::setSelectionOnFocusObject(const QPointF &anchorPos, const QPointF &cursorPos)
{
    Q_D(ShadowInputContext);
    QObject *focus = d->inputItem;
    if (!focus)
        return;

    QQuickItem *quickItem = qobject_cast<QQuickItem *>(d->inputItem);
    bool success;
    int anchor = queryFocusObject(Qt::ImCursorPosition, quickItem ? quickItem->mapFromScene(anchorPos) : anchorPos).toInt(&success);
    if (success) {
        int cursor = queryFocusObject(Qt::ImCursorPosition, quickItem ? quickItem->mapFromScene(cursorPos) : cursorPos).toInt(&success);
        if (success) {
            if (anchor == cursor && anchorPos != cursorPos)
                return;
            QList<QInputMethodEvent::Attribute> imAttributes;
            imAttributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Selection, anchor, cursor - anchor, QVariant()));
            QInputMethodEvent event(QString(), imAttributes);
            QGuiApplication::sendEvent(QGuiApplication::focusObject(), &event);
        }
    }
}

void ShadowInputContext::updateSelectionProperties()
{
    Q_D(ShadowInputContext);
    if (!d->inputItem)
        return;

    QInputMethodQueryEvent imQueryEvent(Qt::ImAnchorRectangle |
                                        Qt::ImCursorRectangle |
                                        Qt::ImInputItemClipRectangle);
    QGuiApplication::sendEvent(d->inputItem, &imQueryEvent);
    QQuickItem *quickItem = qobject_cast<QQuickItem *>(d->inputItem);
    const QRectF anchorRect = imQueryEvent.value(Qt::ImAnchorRectangle).toRectF();
    const QRectF cursorRect = imQueryEvent.value(Qt::ImCursorRectangle).toRectF();
    const QRectF anchorRectangle = quickItem ? quickItem->mapRectToScene(anchorRect) : anchorRect;
    const QRectF cursorRectangle = quickItem ? quickItem->mapRectToScene(cursorRect) : cursorRect;
    const QRectF inputItemClipRect = imQueryEvent.value(Qt::ImInputItemClipRectangle).toRectF();
    const bool anchorRectIntersectsClipRect = inputItemClipRect.intersects(anchorRect);
    const bool cursorRectIntersectsClipRect = inputItemClipRect.intersects(cursorRect);
    const bool selectionControlVisible = d->inputContext->isSelectionControlVisible();

    const bool newAnchorRectangle = anchorRectangle != d->anchorRectangle;
    const bool newCursorRectangle = cursorRectangle != d->cursorRectangle;
    const bool newAnchorRectIntersectsClipRect = anchorRectIntersectsClipRect != d->anchorRectIntersectsClipRect;
    const bool newCursorRectIntersectsClipRect = cursorRectIntersectsClipRect != d->cursorRectIntersectsClipRect;
    const bool newSelectionControlVisible = selectionControlVisible != d->selectionControlVisible;

    d->anchorRectangle = anchorRectangle;
    d->cursorRectangle = cursorRectangle;
    d->anchorRectIntersectsClipRect = anchorRectIntersectsClipRect;
    d->cursorRectIntersectsClipRect = cursorRectIntersectsClipRect;
    d->selectionControlVisible = selectionControlVisible;

    if (newAnchorRectangle)
        emit anchorRectangleChanged();
    if (newCursorRectangle)
        emit cursorRectangleChanged();
    if (newAnchorRectIntersectsClipRect)
        emit anchorRectIntersectsClipRectChanged();
    if (newCursorRectIntersectsClipRect)
        emit cursorRectIntersectsClipRectChanged();
    if (newSelectionControlVisible)
        emit selectionControlVisibleChanged();
}

void ShadowInputContext::update(Qt::InputMethodQueries queries)
{
    Q_UNUSED(queries);
    Q_D(ShadowInputContext);
    if (!d->inputItem)
        return;

    QInputMethodQueryEvent imQueryEvent(Qt::ImQueryInput);
    QGuiApplication::sendEvent(d->inputItem, &imQueryEvent);

    const QString surroundingText = imQueryEvent.value(Qt::ImSurroundingText).toString();
    const int cursorPosition = imQueryEvent.value(Qt::ImCursorPosition).toInt();
    const int anchorPosition = imQueryEvent.value(Qt::ImAnchorPosition).toInt();

    const QString newSurroundingText = d->inputContext->surroundingText();
    const int newCursorPosition = d->inputContext->cursorPosition();
    const int newAnchorPosition = d->inputContext->anchorPosition();

    const QString newPreeditText = d->inputContext->preeditText();
    const QList<QInputMethodEvent::Attribute> newPreeditAttributes = d->inputContext->preeditTextAttributes();

    bool updateSurroundingText = newSurroundingText != surroundingText;
    bool updateSelection = newCursorPosition != cursorPosition || newAnchorPosition != anchorPosition;
    if (updateSurroundingText) {
        QInputMethodEvent inputEvent;
        inputEvent.setCommitString(newSurroundingText, -cursorPosition, surroundingText.size());
        QGuiApplication::sendEvent(d->inputItem, &inputEvent);
    }

    if (updateSurroundingText || updateSelection) {
        QList<QInputMethodEvent::Attribute> attributes;
        attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Selection,
                                                       newAnchorPosition,
                                                       newCursorPosition - newAnchorPosition, QVariant()));
        QInputMethodEvent inputEvent(QString(), attributes);
        QGuiApplication::sendEvent(d->inputItem, &inputEvent);
    }

    const bool forcePreeditText = !newPreeditText.isEmpty() && (updateSurroundingText || updateSelection);
    if (forcePreeditText || d->preeditText != newPreeditText || d->preeditTextAttributes != newPreeditAttributes) {
        d->preeditText = newPreeditText;
        d->preeditTextAttributes = newPreeditAttributes;
        QInputMethodEvent inputEvent(d->preeditText, d->preeditTextAttributes);
        QGuiApplication::sendEvent(d->inputItem, &inputEvent);
    }

    updateSelectionProperties();
}

QVariant ShadowInputContext::queryFocusObject(Qt::InputMethodQuery query, QVariant argument)
{
    Q_D(ShadowInputContext);
    QVariant retval;
    QObject *focusObject = d->inputItem;
    if (!focusObject)
        return retval;

    bool newMethodWorks = QMetaObject::invokeMethod(focusObject, "inputMethodQuery",
                                                    Qt::DirectConnection,
                                                    Q_RETURN_ARG(QVariant, retval),
                                                    Q_ARG(Qt::InputMethodQuery, query),
                                                    Q_ARG(QVariant, argument));
    if (newMethodWorks)
        return retval;

    QInputMethodQueryEvent queryEvent(query);
    QCoreApplication::sendEvent(focusObject, &queryEvent);
    return queryEvent.value(query);
}

} // namespace QtVirtualKeyboard
QT_END_NAMESPACE