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