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

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