Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwidgetlinecontrol_p.h
Go to the documentation of this file.
1// Copyright (C) 2020 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// Qt-Security score:significant reason:default
4
5#ifndef QWIDGETLINECONTROL_P_H
6#define QWIDGETLINECONTROL_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtWidgets/private/qtwidgetsglobal_p.h>
20
21#include "private/qwidget_p.h"
22#include "QtWidgets/qlineedit.h"
23#include "QtGui/qtextlayout.h"
24#include "QtCore/qpointer.h"
25#include "QtGui/qclipboard.h"
26#include "QtGui/qinputmethod.h"
27#include "QtCore/qpoint.h"
28#if QT_CONFIG(completer)
29#include "QtWidgets/qcompleter.h"
30#endif
31#include "QtCore/qbasictimer.h"
32#include "QtGui/private/qinputcontrol_p.h"
33
34#include "qplatformdefs.h"
35
36#include <vector>
37#include <memory>
38
39#ifdef DrawText
40# undef DrawText
41#endif
42
44
46
47class Q_WIDGETS_EXPORT QWidgetLineControl : public QInputControl
48{
49 Q_OBJECT
50
51public:
52 QWidgetLineControl(const QString &txt = QString())
53 : QInputControl(LineEdit)
54 , m_cursor(0), m_preeditCursor(0), m_cursorWidth(0), m_layoutDirection(Qt::LayoutDirectionAuto),
55 m_hideCursor(false), m_separator(0), m_readOnly(0),
56 m_dragEnabled(0), m_echoMode(0), m_textDirty(0), m_selDirty(0),
57 m_validInput(1), m_blinkStatus(0), m_blinkEnabled(false),
58 m_ascent(0), m_maxLength(32767), m_lastCursorPos(-1),
59 m_maskData(nullptr), m_modifiedState(0), m_undoState(0),
60 m_selstart(0), m_selend(0), m_passwordEchoEditing(false)
61 , m_passwordMaskDelay(-1)
62#if defined(QT_BUILD_INTERNAL)
63 , m_passwordMaskDelayOverride(-1)
64#endif
65 , m_keyboardScheme(0)
66 , m_accessibleObject(nullptr)
67 {
68 init(txt);
69 }
70
71 ~QWidgetLineControl()
72 {
73 // If this control is used for password input, we don't want the
74 // password data to stay in the process memory, therefore we need
75 // to zero it out
76 if (m_echoMode != QLineEdit::Normal)
77 m_text.fill(u'\0');
78 }
79
80 void setAccessibleObject(QObject *object)
81 {
82 Q_ASSERT(object);
83 m_accessibleObject = object;
84 }
85
86 QObject *accessibleObject()
87 {
88 if (m_accessibleObject)
89 return m_accessibleObject;
90 return parent();
91 }
92
93 int nextMaskBlank(int pos)
94 {
95 int c = findInMask(pos, true, false);
96 m_separator |= (c != pos);
97 return (c != -1 ? c : m_maxLength);
98 }
99
100 int prevMaskBlank(int pos)
101 {
102 int c = findInMask(pos, false, false);
103 m_separator |= (c != pos);
104 return (c != -1 ? c : 0);
105 }
106
107 bool isUndoAvailable() const;
108 bool isRedoAvailable() const;
109 void clearUndo() { m_history.clear(); m_modifiedState = m_undoState = 0; }
110
111 bool isModified() const { return m_modifiedState != m_undoState; }
112 void setModified(bool modified) { m_modifiedState = modified ? -1 : m_undoState; }
113
114 bool allSelected() const { return !m_text.isEmpty() && m_selstart == 0 && m_selend == (int)m_text.size(); }
115 bool hasSelectedText() const { return !m_text.isEmpty() && m_selend > m_selstart; }
116
117 int width() const { return qRound(m_textLayout.lineAt(0).width()) + 1; }
118 int height() const { return qRound(m_textLayout.lineAt(0).height()) + 1; }
119 int ascent() const { return m_ascent; }
120 qreal naturalTextWidth() const { return m_textLayout.lineAt(0).naturalTextWidth(); }
121
122 void setSelection(int start, int length);
123
124 inline QString selectedText() const { return hasSelectedText() ? m_text.mid(m_selstart, m_selend - m_selstart) : QString(); }
125 QString textBeforeSelection() const { return hasSelectedText() ? m_text.left(m_selstart) : QString(); }
126 QString textAfterSelection() const { return hasSelectedText() ? m_text.mid(m_selend) : QString(); }
127
128 int selectionStart() const { return hasSelectedText() ? m_selstart : -1; }
129 int selectionEnd() const { return hasSelectedText() ? m_selend : -1; }
130#if defined (Q_OS_ANDROID)
131 bool isSelectableByMouse() const { return true; }
132#endif
133 bool inSelection(int x) const
134 {
135 if (m_selstart >= m_selend)
136 return false;
137 int pos = xToPos(x, QTextLine::CursorOnCharacter);
138 return pos >= m_selstart && pos < m_selend;
139 }
140
141 void removeSelection()
142 {
143 int priorState = m_undoState;
144 removeSelectedText();
145 finishChange(priorState);
146 }
147
148 int start() const { return 0; }
149 int end() const { return m_text.size(); }
150
151#ifndef QT_NO_CLIPBOARD
152 void copy(QClipboard::Mode mode = QClipboard::Clipboard) const;
153 void paste(QClipboard::Mode mode = QClipboard::Clipboard);
154#endif
155
156 int cursor() const{ return m_cursor; }
157 int preeditCursor() const { return m_preeditCursor; }
158
159 int cursorWidth() const { return m_cursorWidth; }
160 void setCursorWidth(int value) { m_cursorWidth = value; }
161
162 Qt::CursorMoveStyle cursorMoveStyle() const { return m_textLayout.cursorMoveStyle(); }
163 void setCursorMoveStyle(Qt::CursorMoveStyle style) { m_textLayout.setCursorMoveStyle(style); }
164
165 void moveCursor(int pos, bool mark = false);
166 void cursorForward(bool mark, int steps)
167 {
168 int c = m_cursor;
169 if (steps > 0) {
170 while (steps--)
171 c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.rightCursorPosition(c)
172 : m_textLayout.nextCursorPosition(c);
173 } else if (steps < 0) {
174 while (steps++)
175 c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.leftCursorPosition(c)
176 : m_textLayout.previousCursorPosition(c);
177 }
178 moveCursor(c, mark);
179 }
180
181 void cursorWordForward(bool mark) { moveCursor(m_textLayout.nextCursorPosition(m_cursor, QTextLayout::SkipWords), mark); }
182 void cursorWordBackward(bool mark) { moveCursor(m_textLayout.previousCursorPosition(m_cursor, QTextLayout::SkipWords), mark); }
183
184 void home(bool mark) { moveCursor(0, mark); }
185 void end(bool mark) { moveCursor(m_text.size(), mark); }
186
187 int xToPos(int x, QTextLine::CursorPosition = QTextLine::CursorBetweenCharacters) const;
188 QRect rectForPos(int pos) const;
189 QRect cursorRect() const;
190 QRect anchorRect() const;
191
192 qreal cursorToX(int cursor) const { return m_textLayout.lineAt(0).cursorToX(cursor); }
193 qreal cursorToX() const
194 {
195 int cursor = m_cursor;
196 if (m_preeditCursor != -1)
197 cursor += m_preeditCursor;
198 return cursorToX(cursor);
199 }
200
201 bool isReadOnly() const { return m_readOnly; }
202 void setReadOnly(bool enable);
203
204 QString text() const
205 {
206 QString content = m_text;
207 QString res = m_maskData ? stripString(content) : content;
208 return (res.isNull() ? QString::fromLatin1("") : res);
209 }
210 void setText(const QString &txt)
211 {
212#ifndef QT_NO_IM
213 if (composeMode())
214 QGuiApplication::inputMethod()->reset();
215#endif
216 internalSetText(txt, -1, false);
217 }
218 void commitPreedit();
219
220 QString displayText() const { return m_textLayout.text(); }
221
222 QString surroundingText() const
223 {
224 return m_text.isNull() ? QString::fromLatin1("") : m_text;
225 }
226
227 void backspace();
228 void del();
229 void deselect() { internalDeselect(); finishChange(); }
230 void selectAll() { m_selstart = m_selend = m_cursor = 0; moveCursor(m_text.size(), true); }
231
232 void insert(const QString &);
233 void clear();
234 void undo();
235 void redo() { internalRedo(); finishChange(); }
236 void selectWordAtPos(int);
237
238 uint echoMode() const { return m_echoMode; }
239 void setEchoMode(uint mode)
240 {
241 cancelPasswordEchoTimer();
242 m_echoMode = mode;
243 m_passwordEchoEditing = false;
244
245 // If this control is used for password input, we want to minimize
246 // the possibility of string reallocation not to leak (parts of)
247 // the password.
248 if (m_echoMode != QLineEdit::Normal)
249 m_text.reserve(30);
250
251 updateDisplayText();
252 }
253
254 int maxLength() const { return m_maxLength; }
255 void setMaxLength(int maxLength)
256 {
257 if (m_maskData)
258 return;
259 m_maxLength = maxLength;
260 setText(m_text);
261 }
262
263#ifndef QT_NO_VALIDATOR
264 const QValidator *validator() const { return m_validator; }
265 void setValidator(const QValidator *v) { m_validator = const_cast<QValidator*>(v); }
266#endif
267
268#if QT_CONFIG(completer)
269 QCompleter *completer() const { return m_completer; }
270 /* Note that you must set the widget for the completer separately */
271 void setCompleter(const QCompleter *c) { m_completer = const_cast<QCompleter*>(c); }
272 void complete(int key);
273#endif
274
275 int cursorPosition() const { return m_cursor; }
276 void setCursorPosition(int pos) { if (pos <= m_text.size()) moveCursor(qMax(0, pos)); }
277
278 bool hasAcceptableInput() const { return hasAcceptableInput(m_text); }
279 bool fixup();
280
281 QString inputMask() const
282 {
283 QString mask;
284 if (m_maskData) {
285 mask = m_inputMask;
286 if (m_blank != u' ') {
287 mask += u';';
288 mask += m_blank;
289 }
290 }
291 return mask;
292 }
293 void setInputMask(const QString &mask)
294 {
295 parseInputMask(mask);
296 if (m_maskData)
297 moveCursor(nextMaskBlank(0));
298 }
299
300 // input methods
301#ifndef QT_NO_IM
302 bool composeMode() const { return !m_textLayout.preeditAreaText().isEmpty(); }
303 void setPreeditArea(int cursor, const QString &text) { m_textLayout.setPreeditArea(cursor, text); }
304#endif
305
306 QString preeditAreaText() const { return m_textLayout.preeditAreaText(); }
307
308 void updatePasswordEchoEditing(bool editing);
309 bool passwordEchoEditing() const {
310 if (m_passwordEchoTimer.isActive())
311 return true;
312 return m_passwordEchoEditing ;
313 }
314
315 QChar passwordCharacter() const { return m_passwordCharacter; }
316 void setPasswordCharacter(QChar character) { m_passwordCharacter = character; updateDisplayText(); }
317
318 int passwordMaskDelay() const { return m_passwordMaskDelay; }
319 void setPasswordMaskDelay(int delay) { m_passwordMaskDelay = delay; }
320
321 Qt::LayoutDirection layoutDirection() const {
322 if (m_layoutDirection == Qt::LayoutDirectionAuto && !m_text.isEmpty())
323 return m_text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight;
324 return m_layoutDirection;
325 }
326 void setLayoutDirection(Qt::LayoutDirection direction)
327 {
328 if (direction != m_layoutDirection) {
329 m_layoutDirection = direction;
330 updateDisplayText();
331 }
332 }
333
334 void setFont(const QFont &font) { m_textLayout.setFont(font); updateDisplayText(); }
335
336 void processInputMethodEvent(QInputMethodEvent *event);
337 void processKeyEvent(QKeyEvent* ev);
338
339 void setBlinkingCursorEnabled(bool enable);
340 void updateCursorBlinking();
341 void resetCursorBlinkTimer();
342
343 bool cursorBlinkStatus() const { return m_blinkStatus; }
344
345 QString cancelText() const { return m_cancelText; }
346 void setCancelText(const QString &text) { m_cancelText = text; }
347
348 const QPalette &palette() const { return m_palette; }
349 void setPalette(const QPalette &p) { m_palette = p; }
350
351 enum DrawFlags {
352 DrawText = 0x01,
353 DrawSelections = 0x02,
354 DrawCursor = 0x04,
355 DrawAll = DrawText | DrawSelections | DrawCursor
356 };
357 void draw(QPainter *, const QPoint &, const QRect &, int flags = DrawAll);
358
359#ifndef QT_NO_SHORTCUT
360 void processShortcutOverrideEvent(QKeyEvent *ke);
361#endif
362
363 QTextLayout *textLayout() const
364 {
365 return &m_textLayout;
366 }
367
368private:
369 void init(const QString &txt);
370 void removeSelectedText();
371 void internalSetText(const QString &txt, int pos = -1, bool edited = true);
372 void updateDisplayText(bool forceUpdate = false);
373
374 void internalInsert(const QString &s);
375 void internalDelete(bool wasBackspace = false);
376 void internalRemove(int pos);
377
378 inline void internalDeselect()
379 {
380 m_selDirty |= (m_selend > m_selstart);
381 m_selstart = m_selend = 0;
382 }
383
384 void internalUndo(int until = -1);
385 void internalRedo();
386
387 QString m_text;
388 QPalette m_palette;
389 int m_cursor;
390 int m_preeditCursor;
391 int m_cursorWidth;
392 Qt::LayoutDirection m_layoutDirection;
393 uint m_hideCursor : 1; // used to hide the m_cursor inside preedit areas
394 uint m_separator : 1;
395 uint m_readOnly : 1;
396 uint m_dragEnabled : 1;
397 uint m_echoMode : 2;
398 uint m_textDirty : 1;
399 uint m_selDirty : 1;
400 uint m_validInput : 1;
401 uint m_blinkStatus : 1;
402 uint m_blinkEnabled : 1;
403 QBasicTimer m_blinkTimer;
404 QBasicTimer m_deleteAllTimer;
405 int m_ascent;
406 int m_maxLength;
407 int m_lastCursorPos;
408 QList<int> m_transactions;
409 QPoint m_tripleClick;
410 QBasicTimer m_tripleClickTimer;
411 QString m_cancelText;
412
413 void emitCursorPositionChanged();
414
415 bool finishChange(int validateFromState = -1, bool update = false, bool edited = true);
416
417#ifndef QT_NO_VALIDATOR
418 QPointer<QValidator> m_validator;
419#endif
420 QPointer<QCompleter> m_completer;
421#if QT_CONFIG(completer)
422 bool advanceToEnabledItem(int dir);
423#endif
424
425 struct MaskInputData {
426 enum Casemode { NoCaseMode, Upper, Lower };
427 QChar maskChar; // either the separator char or the inputmask
428 bool separator;
429 Casemode caseMode;
430 };
431 QString m_inputMask;
432 QChar m_blank;
433 std::unique_ptr<MaskInputData[]> m_maskData;
434
435 // undo/redo handling
436 enum CommandType { Separator, Insert, Remove, Delete, RemoveSelection, DeleteSelection, SetSelection };
437 struct Command {
438 inline Command(CommandType t, int p, QChar c, int ss, int se) : type(t),uc(c),pos(p),selStart(ss),selEnd(se) {}
439 uint type : 4;
440 QChar uc;
441 int pos, selStart, selEnd;
442 };
443 int m_modifiedState;
444 int m_undoState;
445 std::vector<Command> m_history;
446 void addCommand(const Command& cmd);
447
448 inline void separate() { m_separator = true; }
449
450 // selection
451 int m_selstart;
452 int m_selend;
453
454 // masking
455 void parseInputMask(const QString &maskFields);
456 bool isValidInput(QChar key, QChar mask) const;
457 bool hasAcceptableInput(const QString &text) const;
458 QString maskString(int pos, const QString &str, bool clear = false) const;
459 QString clearString(int pos, int len) const;
460 QString stripString(const QString &str) const;
461 int findInMask(int pos, bool forward, bool findSeparator, QChar searchChar = QChar()) const;
462
463 // complex text layout (must be mutable so it can be reshaped at will)
464 mutable QTextLayout m_textLayout;
465
466 bool m_passwordEchoEditing;
467 QChar m_passwordCharacter;
468 QBasicTimer m_passwordEchoTimer;
469 int m_passwordMaskDelay;
470 void cancelPasswordEchoTimer()
471 {
472 m_passwordEchoTimer.stop();
473 }
474
475 int redoTextLayout() const;
476
477public:
478#if defined(QT_BUILD_INTERNAL)
479 int m_passwordMaskDelayOverride;
480#endif
481
482Q_SIGNALS:
483 void cursorPositionChanged(int, int);
484 void selectionChanged();
485
486 void displayTextChanged(const QString &);
487 void textChanged(const QString &);
488 void textEdited(const QString &);
489
490 void resetInputContext();
491 void updateMicroFocus();
492
493 void accepted();
494 void editingFinished();
495 void updateNeeded(const QRect &);
496 void inputRejected();
497
498protected:
499 virtual void timerEvent(QTimerEvent *event) override;
500
501private Q_SLOTS:
502 void _q_deleteSelected();
503
504private:
505 int m_keyboardScheme;
506
507 // accessibility events are sent for this object
508 QObject *m_accessibleObject;
509
510 friend class QLineEdit;
511};
512
513QT_END_NAMESPACE
514
515#endif // QWIDGETLINECONTROL_P_H
QT_REQUIRE_CONFIG(lineedit)