1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQuick module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
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 Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QQUICKTEXTINPUT_P_H |
41 | #define QQUICKTEXTINPUT_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include "qquickimplicitsizeitem_p.h" |
55 | #include <QtGui/qtextoption.h> |
56 | #include <QtGui/qvalidator.h> |
57 | |
58 | QT_BEGIN_NAMESPACE |
59 | |
60 | class QQuickTextInputPrivate; |
61 | class QValidator; |
62 | class Q_QUICK_PRIVATE_EXPORT QQuickTextInput : public QQuickImplicitSizeItem |
63 | { |
64 | Q_OBJECT |
65 | |
66 | Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) |
67 | Q_PROPERTY(int length READ length NOTIFY textChanged) |
68 | Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) |
69 | Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor NOTIFY selectionColorChanged) |
70 | Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor NOTIFY selectedTextColorChanged) |
71 | Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) |
72 | Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged) |
73 | Q_PROPERTY(HAlignment effectiveHorizontalAlignment READ effectiveHAlign NOTIFY effectiveHorizontalAlignmentChanged) |
74 | Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged) |
75 | Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged) |
76 | |
77 | Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly NOTIFY readOnlyChanged) |
78 | Q_PROPERTY(bool cursorVisible READ isCursorVisible WRITE setCursorVisible NOTIFY cursorVisibleChanged) |
79 | Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged) |
80 | Q_PROPERTY(QRectF cursorRectangle READ cursorRectangle NOTIFY cursorRectangleChanged) |
81 | Q_PROPERTY(QQmlComponent *cursorDelegate READ cursorDelegate WRITE setCursorDelegate NOTIFY cursorDelegateChanged) |
82 | Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode NOTIFY overwriteModeChanged) |
83 | Q_PROPERTY(int selectionStart READ selectionStart NOTIFY selectionStartChanged) |
84 | Q_PROPERTY(int selectionEnd READ selectionEnd NOTIFY selectionEndChanged) |
85 | Q_PROPERTY(QString selectedText READ selectedText NOTIFY selectedTextChanged) |
86 | |
87 | Q_PROPERTY(int maximumLength READ maxLength WRITE setMaxLength NOTIFY maximumLengthChanged) |
88 | Q_PROPERTY(QValidator* validator READ validator WRITE setValidator NOTIFY validatorChanged) |
89 | Q_PROPERTY(QString inputMask READ inputMask WRITE setInputMask NOTIFY inputMaskChanged) |
90 | Q_PROPERTY(Qt::InputMethodHints inputMethodHints READ inputMethodHints WRITE setInputMethodHints NOTIFY inputMethodHintsChanged) |
91 | |
92 | Q_PROPERTY(bool acceptableInput READ hasAcceptableInput NOTIFY acceptableInputChanged) |
93 | Q_PROPERTY(EchoMode echoMode READ echoMode WRITE setEchoMode NOTIFY echoModeChanged) |
94 | Q_PROPERTY(bool activeFocusOnPress READ focusOnPress WRITE setFocusOnPress NOTIFY activeFocusOnPressChanged) |
95 | Q_PROPERTY(QString passwordCharacter READ passwordCharacter WRITE setPasswordCharacter NOTIFY passwordCharacterChanged) |
96 | Q_PROPERTY(int passwordMaskDelay READ passwordMaskDelay WRITE setPasswordMaskDelay RESET resetPasswordMaskDelay NOTIFY passwordMaskDelayChanged REVISION 3) |
97 | Q_PROPERTY(QString displayText READ displayText NOTIFY displayTextChanged) |
98 | Q_PROPERTY(QString preeditText READ preeditText NOTIFY preeditTextChanged REVISION 7) |
99 | Q_PROPERTY(bool autoScroll READ autoScroll WRITE setAutoScroll NOTIFY autoScrollChanged) |
100 | Q_PROPERTY(bool selectByMouse READ selectByMouse WRITE setSelectByMouse NOTIFY selectByMouseChanged) |
101 | Q_PROPERTY(SelectionMode mouseSelectionMode READ mouseSelectionMode WRITE setMouseSelectionMode NOTIFY mouseSelectionModeChanged) |
102 | Q_PROPERTY(bool persistentSelection READ persistentSelection WRITE setPersistentSelection NOTIFY persistentSelectionChanged) |
103 | Q_PROPERTY(bool canPaste READ canPaste NOTIFY canPasteChanged) |
104 | Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged) |
105 | Q_PROPERTY(bool canRedo READ canRedo NOTIFY canRedoChanged) |
106 | Q_PROPERTY(bool inputMethodComposing READ isInputMethodComposing NOTIFY inputMethodComposingChanged) |
107 | Q_PROPERTY(qreal contentWidth READ contentWidth NOTIFY contentSizeChanged) |
108 | Q_PROPERTY(qreal contentHeight READ contentHeight NOTIFY contentSizeChanged) |
109 | Q_PROPERTY(RenderType renderType READ renderType WRITE setRenderType NOTIFY renderTypeChanged) |
110 | |
111 | Q_PROPERTY(qreal padding READ padding WRITE setPadding RESET resetPadding NOTIFY paddingChanged REVISION 6) |
112 | Q_PROPERTY(qreal topPadding READ topPadding WRITE setTopPadding RESET resetTopPadding NOTIFY topPaddingChanged REVISION 6) |
113 | Q_PROPERTY(qreal leftPadding READ leftPadding WRITE setLeftPadding RESET resetLeftPadding NOTIFY leftPaddingChanged REVISION 6) |
114 | Q_PROPERTY(qreal rightPadding READ rightPadding WRITE setRightPadding RESET resetRightPadding NOTIFY rightPaddingChanged REVISION 6) |
115 | Q_PROPERTY(qreal bottomPadding READ bottomPadding WRITE setBottomPadding RESET resetBottomPadding NOTIFY bottomPaddingChanged REVISION 6) |
116 | |
117 | public: |
118 | QQuickTextInput(QQuickItem * parent=nullptr); |
119 | ~QQuickTextInput(); |
120 | |
121 | void componentComplete() override; |
122 | |
123 | enum EchoMode {//To match QLineEdit::EchoMode |
124 | Normal, |
125 | NoEcho, |
126 | Password, |
127 | PasswordEchoOnEdit |
128 | }; |
129 | Q_ENUM(EchoMode) |
130 | |
131 | enum HAlignment { |
132 | AlignLeft = Qt::AlignLeft, |
133 | AlignRight = Qt::AlignRight, |
134 | AlignHCenter = Qt::AlignHCenter |
135 | }; |
136 | Q_ENUM(HAlignment) |
137 | |
138 | enum VAlignment { |
139 | AlignTop = Qt::AlignTop, |
140 | AlignBottom = Qt::AlignBottom, |
141 | AlignVCenter = Qt::AlignVCenter |
142 | }; |
143 | Q_ENUM(VAlignment) |
144 | |
145 | enum WrapMode { |
146 | NoWrap = QTextOption::NoWrap, |
147 | WordWrap = QTextOption::WordWrap, |
148 | WrapAnywhere = QTextOption::WrapAnywhere, |
149 | WrapAtWordBoundaryOrAnywhere = QTextOption::WrapAtWordBoundaryOrAnywhere, // COMPAT |
150 | Wrap = QTextOption::WrapAtWordBoundaryOrAnywhere |
151 | }; |
152 | Q_ENUM(WrapMode) |
153 | |
154 | enum SelectionMode { |
155 | SelectCharacters, |
156 | SelectWords |
157 | }; |
158 | Q_ENUM(SelectionMode) |
159 | |
160 | enum CursorPosition { |
161 | CursorBetweenCharacters, |
162 | CursorOnCharacter |
163 | }; |
164 | Q_ENUM(CursorPosition) |
165 | |
166 | enum RenderType { QtRendering, |
167 | NativeRendering |
168 | }; |
169 | Q_ENUM(RenderType) |
170 | |
171 | //Auxilliary functions needed to control the TextInput from QML |
172 | Q_INVOKABLE void positionAt(QQmlV4Function *args) const; |
173 | Q_INVOKABLE QRectF positionToRectangle(int pos) const; |
174 | Q_INVOKABLE void moveCursorSelection(int pos); |
175 | Q_INVOKABLE void moveCursorSelection(int pos, SelectionMode mode); |
176 | |
177 | RenderType renderType() const; |
178 | void setRenderType(RenderType renderType); |
179 | |
180 | QString text() const; |
181 | void setText(const QString &); |
182 | |
183 | int length() const; |
184 | |
185 | QFont font() const; |
186 | void setFont(const QFont &font); |
187 | |
188 | QColor color() const; |
189 | void setColor(const QColor &c); |
190 | |
191 | QColor selectionColor() const; |
192 | void setSelectionColor(const QColor &c); |
193 | |
194 | QColor selectedTextColor() const; |
195 | void setSelectedTextColor(const QColor &c); |
196 | |
197 | HAlignment hAlign() const; |
198 | void setHAlign(HAlignment align); |
199 | void resetHAlign(); |
200 | HAlignment effectiveHAlign() const; |
201 | |
202 | VAlignment vAlign() const; |
203 | void setVAlign(VAlignment align); |
204 | |
205 | WrapMode wrapMode() const; |
206 | void setWrapMode(WrapMode w); |
207 | |
208 | bool isReadOnly() const; |
209 | void setReadOnly(bool); |
210 | |
211 | bool isCursorVisible() const; |
212 | void setCursorVisible(bool on); |
213 | |
214 | int cursorPosition() const; |
215 | void setCursorPosition(int cp); |
216 | |
217 | QRectF cursorRectangle() const; |
218 | |
219 | int selectionStart() const; |
220 | int selectionEnd() const; |
221 | |
222 | QString selectedText() const; |
223 | |
224 | int maxLength() const; |
225 | void setMaxLength(int ml); |
226 | |
227 | QValidator * validator() const; |
228 | void setValidator(QValidator* v); |
229 | |
230 | QString inputMask() const; |
231 | void setInputMask(const QString &im); |
232 | |
233 | EchoMode echoMode() const; |
234 | void setEchoMode(EchoMode echo); |
235 | |
236 | QString passwordCharacter() const; |
237 | void setPasswordCharacter(const QString &str); |
238 | |
239 | int passwordMaskDelay() const; |
240 | void setPasswordMaskDelay(int delay); |
241 | void resetPasswordMaskDelay(); |
242 | |
243 | QString displayText() const; |
244 | Q_REVISION(7) QString preeditText() const; |
245 | |
246 | QQmlComponent* cursorDelegate() const; |
247 | void setCursorDelegate(QQmlComponent*); |
248 | |
249 | bool overwriteMode() const; |
250 | void setOverwriteMode(bool overwrite); |
251 | |
252 | bool focusOnPress() const; |
253 | void setFocusOnPress(bool); |
254 | |
255 | bool autoScroll() const; |
256 | void setAutoScroll(bool); |
257 | |
258 | bool selectByMouse() const; |
259 | void setSelectByMouse(bool); |
260 | |
261 | SelectionMode mouseSelectionMode() const; |
262 | void setMouseSelectionMode(SelectionMode mode); |
263 | |
264 | bool persistentSelection() const; |
265 | void setPersistentSelection(bool persist); |
266 | |
267 | bool hasAcceptableInput() const; |
268 | |
269 | #if QT_CONFIG(im) |
270 | QVariant inputMethodQuery(Qt::InputMethodQuery property) const override; |
271 | Q_REVISION(3) Q_INVOKABLE QVariant inputMethodQuery(Qt::InputMethodQuery query, QVariant argument) const; |
272 | #endif |
273 | |
274 | QRectF boundingRect() const override; |
275 | QRectF clipRect() const override; |
276 | |
277 | bool canPaste() const; |
278 | |
279 | bool canUndo() const; |
280 | bool canRedo() const; |
281 | |
282 | bool isInputMethodComposing() const; |
283 | |
284 | Qt::InputMethodHints inputMethodHints() const; |
285 | void setInputMethodHints(Qt::InputMethodHints hints); |
286 | |
287 | Q_INVOKABLE QString getText(int start, int end) const; |
288 | |
289 | qreal contentWidth() const; |
290 | qreal contentHeight() const; |
291 | |
292 | qreal padding() const; |
293 | void setPadding(qreal padding); |
294 | void resetPadding(); |
295 | |
296 | qreal topPadding() const; |
297 | void setTopPadding(qreal padding); |
298 | void resetTopPadding(); |
299 | |
300 | qreal leftPadding() const; |
301 | void setLeftPadding(qreal padding); |
302 | void resetLeftPadding(); |
303 | |
304 | qreal rightPadding() const; |
305 | void setRightPadding(qreal padding); |
306 | void resetRightPadding(); |
307 | |
308 | qreal bottomPadding() const; |
309 | void setBottomPadding(qreal padding); |
310 | void resetBottomPadding(); |
311 | |
312 | Q_SIGNALS: |
313 | void textChanged(); |
314 | void cursorPositionChanged(); |
315 | void cursorRectangleChanged(); |
316 | void selectionStartChanged(); |
317 | void selectionEndChanged(); |
318 | void selectedTextChanged(); |
319 | void accepted(); |
320 | void acceptableInputChanged(); |
321 | Q_REVISION(2) void editingFinished(); |
322 | Q_REVISION(9) void textEdited(); |
323 | void colorChanged(); |
324 | void selectionColorChanged(); |
325 | void selectedTextColorChanged(); |
326 | void fontChanged(const QFont &font); |
327 | void horizontalAlignmentChanged(QQuickTextInput::HAlignment alignment); |
328 | void verticalAlignmentChanged(QQuickTextInput::VAlignment alignment); |
329 | void wrapModeChanged(); |
330 | void readOnlyChanged(bool isReadOnly); |
331 | void cursorVisibleChanged(bool isCursorVisible); |
332 | void cursorDelegateChanged(); |
333 | void overwriteModeChanged(bool overwriteMode); |
334 | void maximumLengthChanged(int maximumLength); |
335 | void validatorChanged(); |
336 | void inputMaskChanged(const QString &inputMask); |
337 | void echoModeChanged(QQuickTextInput::EchoMode echoMode); |
338 | void passwordCharacterChanged(); |
339 | Q_REVISION(3) void passwordMaskDelayChanged(int delay); |
340 | void displayTextChanged(); |
341 | Q_REVISION(7) void preeditTextChanged(); |
342 | void activeFocusOnPressChanged(bool activeFocusOnPress); |
343 | void autoScrollChanged(bool autoScroll); |
344 | void selectByMouseChanged(bool selectByMouse); |
345 | void mouseSelectionModeChanged(QQuickTextInput::SelectionMode mode); |
346 | void persistentSelectionChanged(); |
347 | void canPasteChanged(); |
348 | void canUndoChanged(); |
349 | void canRedoChanged(); |
350 | void inputMethodComposingChanged(); |
351 | void effectiveHorizontalAlignmentChanged(); |
352 | void contentSizeChanged(); |
353 | void inputMethodHintsChanged(); |
354 | void renderTypeChanged(); |
355 | Q_REVISION(6) void paddingChanged(); |
356 | Q_REVISION(6) void topPaddingChanged(); |
357 | Q_REVISION(6) void leftPaddingChanged(); |
358 | Q_REVISION(6) void rightPaddingChanged(); |
359 | Q_REVISION(6) void bottomPaddingChanged(); |
360 | |
361 | private: |
362 | void invalidateFontCaches(); |
363 | void ensureActiveFocus(); |
364 | |
365 | protected: |
366 | QQuickTextInput(QQuickTextInputPrivate &dd, QQuickItem *parent = nullptr); |
367 | |
368 | void geometryChanged(const QRectF &newGeometry, |
369 | const QRectF &oldGeometry) override; |
370 | |
371 | void mousePressEvent(QMouseEvent *event) override; |
372 | void mouseMoveEvent(QMouseEvent *event) override; |
373 | void mouseReleaseEvent(QMouseEvent *event) override; |
374 | void mouseDoubleClickEvent(QMouseEvent *event) override; |
375 | void keyPressEvent(QKeyEvent* ev) override; |
376 | #if QT_CONFIG(im) |
377 | void inputMethodEvent(QInputMethodEvent *) override; |
378 | #endif |
379 | void mouseUngrabEvent() override; |
380 | bool event(QEvent *e) override; |
381 | void focusOutEvent(QFocusEvent *event) override; |
382 | void focusInEvent(QFocusEvent *event) override; |
383 | void timerEvent(QTimerEvent *event) override; |
384 | QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) override; |
385 | void updatePolish() override; |
386 | |
387 | public Q_SLOTS: |
388 | void selectAll(); |
389 | void selectWord(); |
390 | void select(int start, int end); |
391 | void deselect(); |
392 | bool isRightToLeft(int start, int end); |
393 | #if QT_CONFIG(clipboard) |
394 | void cut(); |
395 | void copy(); |
396 | void paste(); |
397 | #endif |
398 | void undo(); |
399 | void redo(); |
400 | void insert(int position, const QString &text); |
401 | void remove(int start, int end); |
402 | Q_REVISION(3) void ensureVisible(int position); |
403 | Q_REVISION(7) void clear(); |
404 | |
405 | private Q_SLOTS: |
406 | void selectionChanged(); |
407 | void createCursor(); |
408 | void updateCursorRectangle(bool scroll = true); |
409 | void q_canPasteChanged(); |
410 | void q_updateAlignment(); |
411 | void triggerPreprocess(); |
412 | |
413 | #if QT_CONFIG(validator) |
414 | void q_validatorChanged(); |
415 | #endif |
416 | |
417 | private: |
418 | friend class QQuickTextUtil; |
419 | |
420 | Q_DECLARE_PRIVATE(QQuickTextInput) |
421 | }; |
422 | |
423 | QT_END_NAMESPACE |
424 | |
425 | QML_DECLARE_TYPE(QQuickTextInput) |
426 | |
427 | #endif // QQUICKTEXTINPUT_P_H |
428 | |