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