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

source code of qtdeclarative/src/quick/items/qquicktextinput_p.h