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