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