1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QVIRTUALKEYBOARDINPUTCONTEXT_P_H
5#define QVIRTUALKEYBOARDINPUTCONTEXT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QObject>
19#include <QRectF>
20#include <QSet>
21#include <QInputMethodEvent>
22#include <QQuickItem>
23#include <QtVirtualKeyboard/qvirtualkeyboardinputcontext.h>
24#include <QtVirtualKeyboard/private/shadowinputcontext_p.h>
25#include <QtVirtualKeyboard/qvirtualkeyboardobserver.h>
26#include <QtVirtualKeyboard/private/qvirtualkeyboardnamespace_p.h>
27
28QT_BEGIN_NAMESPACE
29
30namespace QtVirtualKeyboard {
31
32// Boolean variable QT_VIRTUALKEYBOARD_FORCE_EVENTS_WITHOUT_FOCUS will enable virtual keyboard
33// to send key events without having any text input in focus when an environment
34// variable QT_VIRTUALKEYBOARD_FORCE_EVENTS_WITHOUT_FOCUS is set. This will also enable virtual
35// keyboard shift key usage in the same no focus situation.
36extern const bool QT_VIRTUALKEYBOARD_FORCE_EVENTS_WITHOUT_FOCUS;
37
38class PlatformInputContext;
39class ShiftHandler;
40}
41class QVirtualKeyboardInputEngine;
42class QVirtualKeyboardInputContextPrivate;
43
44class Q_VIRTUALKEYBOARD_EXPORT QVirtualKeyboardInputContextPrivate : public QObject
45{
46 Q_OBJECT
47 Q_DECLARE_PUBLIC(QVirtualKeyboardInputContext)
48 Q_PROPERTY(bool focus READ focus WRITE setFocus NOTIFY focusChanged)
49 Q_PROPERTY(QRectF keyboardRectangle READ keyboardRectangle WRITE setKeyboardRectangle NOTIFY keyboardRectangleChanged)
50 Q_PROPERTY(QRectF previewRectangle READ previewRectangle WRITE setPreviewRectangle NOTIFY previewRectangleChanged)
51 Q_PROPERTY(bool previewVisible READ previewVisible WRITE setPreviewVisible NOTIFY previewVisibleChanged)
52 Q_PROPERTY(QString locale READ locale WRITE setLocale NOTIFY localeChanged)
53 Q_PROPERTY(QObject *inputItem READ inputItem NOTIFY inputItemChanged)
54 Q_PROPERTY(QtVirtualKeyboard::ShiftHandler *shiftHandler READ shiftHandler CONSTANT)
55 Q_PROPERTY(QtVirtualKeyboard::ShadowInputContext *shadow READ shadow CONSTANT)
56 Q_MOC_INCLUDE("shifthandler_p.h")
57 QML_NAMED_ELEMENT(InputContextPrivate)
58 QML_UNCREATABLE("InputContextPrivate is only available via InputContext.priv")
59 QML_ADDED_IN_VERSION(2, 0)
60
61 explicit QVirtualKeyboardInputContextPrivate(QVirtualKeyboardInputContext *q_ptr);
62 void init();
63
64public:
65 enum class State {
66 Reselect = 0x1,
67 InputMethodEvent = 0x2,
68 KeyEvent = 0x4,
69 InputMethodClick = 0x8,
70 SyncShadowInput = 0x10,
71 SetFocus = 0x20
72 };
73 Q_FLAG(State)
74 Q_DECLARE_FLAGS(StateFlags, QVirtualKeyboardInputContextPrivate::State)
75
76 ~QVirtualKeyboardInputContextPrivate();
77
78 bool focus() const;
79 void setFocus(bool focus);
80 QRectF keyboardRectangle() const;
81 void setKeyboardRectangle(QRectF rectangle);
82 QRectF previewRectangle() const;
83 void setPreviewRectangle(QRectF rectangle);
84 bool previewVisible() const;
85 void setPreviewVisible(bool visible);
86 QString locale() const;
87 void setLocale(const QString &locale);
88 QObject *inputItem() const;
89 QtVirtualKeyboard::ShiftHandler *shiftHandler() const;
90 QtVirtualKeyboard::ShadowInputContext *shadow() const;
91 Q_INVOKABLE void setKeyboardObserver(QVirtualKeyboardObserver *keyboardObserver);
92
93 // Helper functions
94 Q_INVOKABLE bool fileExists(const QUrl &fileUrl);
95 Q_INVOKABLE bool hasEnterKeyAction(QObject *item) const;
96 Q_INVOKABLE void registerInputPanel(QObject *inputPanel);
97 Q_INVOKABLE bool contains(const QPointF &point) const;
98 Q_INVOKABLE QtVirtualKeyboard::KeyboardFunctionKey keyboardFunctionKey(QtVirtualKeyboard::KeyboardFunction keyboardFunction) const;
99
100Q_SIGNALS:
101 void focusChanged();
102 void keyboardRectangleChanged();
103 void previewRectangleChanged();
104 void previewVisibleChanged();
105 void localeChanged();
106 void inputItemChanged();
107 void navigationKeyPressed(int key, bool isAutoRepeat);
108 void navigationKeyReleased(int key, bool isAutoRepeat);
109
110public Q_SLOTS:
111 void hideInputPanel();
112 void updateAvailableLocales(const QStringList &availableLocales);
113 void forceCursorPosition(int anchorPosition, int cursorPosition);
114
115private Q_SLOTS:
116 void onInputItemChanged();
117
118private:
119 void sendPreedit(const QString &text, const QList<QInputMethodEvent::Attribute> &attributes, int replaceFrom, int replaceLength);
120 void sendInputMethodEvent(QInputMethodEvent *event);
121 void reset();
122 void commit();
123 void update(Qt::InputMethodQueries queries);
124 void invokeAction(QInputMethod::Action action, int cursorPosition);
125 bool filterEvent(const QEvent *event);
126 void addSelectionAttribute(QList<QInputMethodEvent::Attribute> &attributes);
127 bool testAttribute(const QList<QInputMethodEvent::Attribute> &attributes, QInputMethodEvent::AttributeType attributeType) const;
128 int findAttribute(const QList<QInputMethodEvent::Attribute> &attributes, QInputMethodEvent::AttributeType attributeType) const;
129 inline void setState(const State &state) { stateFlags.setFlag(flag: state); }
130 inline void clearState(const State &state) { stateFlags &= ~StateFlags(state); }
131 inline bool testState(const State &state) const { return stateFlags.testFlag(flag: state); }
132 inline bool isEmptyState() const { return !stateFlags; }
133 void updateSelectionControlVisible(bool inputPanelVisible);
134
135private:
136 QVirtualKeyboardInputContext *q_ptr;
137 QtVirtualKeyboard::PlatformInputContext *platformInputContext;
138 QVirtualKeyboardInputEngine *inputEngine;
139 QtVirtualKeyboard::ShiftHandler *_shiftHandler;
140 QPointer<QObject> inputPanel;
141 QPointer<QQuickItem> dimmer;
142 QRectF keyboardRect;
143 QRectF previewRect;
144 bool _previewVisible;
145 bool animating;
146 bool _focus;
147 StateFlags stateFlags;
148 int cursorPosition;
149 int anchorPosition;
150 int forceAnchorPosition;
151 int _forceCursorPosition;
152 Qt::InputMethodHints inputMethodHints;
153 QString preeditText;
154 QList<QInputMethodEvent::Attribute> preeditTextAttributes;
155 QString surroundingText;
156 QString selectedText;
157 QRectF anchorRectangle;
158 QRectF cursorRectangle;
159 bool selectionControlVisible;
160 bool anchorRectIntersectsClipRect;
161 bool cursorRectIntersectsClipRect;
162#ifdef QT_VIRTUALKEYBOARD_ARROW_KEY_NAVIGATION
163 QSet<int> activeNavigationKeys;
164#endif
165 QSet<quint32> activeKeys;
166 QtVirtualKeyboard::ShadowInputContext _shadow;
167 QPointer<QVirtualKeyboardObserver> keyboardObserver;
168
169 friend class QtVirtualKeyboard::PlatformInputContext;
170 friend class QVirtualKeyboardScopedState;
171};
172
173class QVirtualKeyboardScopedState
174{
175 Q_DISABLE_COPY(QVirtualKeyboardScopedState)
176public:
177 QVirtualKeyboardScopedState(QVirtualKeyboardInputContextPrivate *d, QVirtualKeyboardInputContextPrivate::State state) :
178 d(d),
179 state(state)
180 {
181 d->setState(state);
182 }
183
184 ~QVirtualKeyboardScopedState()
185 {
186 d->clearState(state);
187 }
188
189private:
190 QVirtualKeyboardInputContextPrivate *d;
191 QVirtualKeyboardInputContextPrivate::State state;
192};
193
194Q_DECLARE_OPERATORS_FOR_FLAGS(QVirtualKeyboardInputContextPrivate::StateFlags)
195
196/*!
197 TODO: Remove this type and move the registration back into QVirtualKeyboardInputContext when
198 QML stops creating separate singleton instances for each version.
199 */
200struct QVirtualKeyboardInputContextForeign
201{
202 Q_GADGET
203 QML_FOREIGN(QVirtualKeyboardInputContext)
204 QML_NAMED_ELEMENT(InputContext)
205 QML_SINGLETON
206 QML_ADDED_IN_VERSION(1, 0)
207 QML_EXTRA_VERSION(2, 0)
208
209public:
210 static QVirtualKeyboardInputContext *create(QQmlEngine *qmlEngine, QJSEngine *);
211};
212
213QT_END_NAMESPACE
214
215Q_DECLARE_METATYPE(QVirtualKeyboardInputContextPrivate::State)
216
217#endif // QVIRTUALKEYBOARDINPUTCONTEXT_P_H
218

source code of qtvirtualkeyboard/src/virtualkeyboard/qvirtualkeyboardinputcontext_p.h