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)
66 , m_accessibleObject(
nullptr)
76 if (m_echoMode != QLineEdit::Normal)
80 void setAccessibleObject(QObject *object)
83 m_accessibleObject = object;
86 QObject *accessibleObject()
88 if (m_accessibleObject)
89 return m_accessibleObject;
93 int nextMaskBlank(
int pos)
95 int c = findInMask(pos,
true,
false);
96 m_separator |= (c != pos);
97 return (c != -1 ? c : m_maxLength);
100 int prevMaskBlank(
int pos)
102 int c = findInMask(pos,
false,
false);
103 m_separator |= (c != pos);
104 return (c != -1 ? c : 0);
107 bool isUndoAvailable()
const;
108 bool isRedoAvailable()
const;
109 void clearUndo() { m_history.clear(); m_modifiedState = m_undoState = 0; }
111 bool isModified()
const {
return m_modifiedState != m_undoState; }
112 void setModified(
bool modified) { m_modifiedState = modified ? -1 : m_undoState; }
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; }
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(); }
122 void setSelection(
int start,
int length);
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(); }
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; }
133 bool inSelection(
int x)
const
135 if (m_selstart >= m_selend)
137 int pos = xToPos(x, QTextLine::CursorOnCharacter);
138 return pos >= m_selstart && pos < m_selend;
141 void removeSelection()
143 int priorState = m_undoState;
144 removeSelectedText();
145 finishChange(priorState);
148 int start()
const {
return 0; }
149 int end()
const {
return m_text.size(); }
151#ifndef QT_NO_CLIPBOARD
152 void copy(QClipboard::Mode mode = QClipboard::Clipboard)
const;
153 void paste(QClipboard::Mode mode = QClipboard::Clipboard);
156 int cursor()
const{
return m_cursor; }
157 int preeditCursor()
const {
return m_preeditCursor; }
159 int cursorWidth()
const {
return m_cursorWidth; }
160 void setCursorWidth(
int value) { m_cursorWidth = value; }
162 Qt::CursorMoveStyle cursorMoveStyle()
const {
return m_textLayout.cursorMoveStyle(); }
163 void setCursorMoveStyle(Qt::CursorMoveStyle style) { m_textLayout.setCursorMoveStyle(style); }
165 void moveCursor(
int pos,
bool mark =
false);
166 void cursorForward(
bool mark,
int steps)
171 c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.rightCursorPosition(c)
172 : m_textLayout.nextCursorPosition(c);
173 }
else if (steps < 0) {
175 c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.leftCursorPosition(c)
176 : m_textLayout.previousCursorPosition(c);
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); }
184 void home(
bool mark) { moveCursor(0, mark); }
185 void end(
bool mark) { moveCursor(m_text.size(), mark); }
187 int xToPos(
int x, QTextLine::CursorPosition = QTextLine::CursorBetweenCharacters)
const;
188 QRect rectForPos(
int pos)
const;
189 QRect cursorRect()
const;
190 QRect anchorRect()
const;
192 qreal cursorToX(
int cursor)
const {
return m_textLayout.lineAt(0).cursorToX(cursor); }
193 qreal cursorToX()
const
195 int cursor = m_cursor;
196 if (m_preeditCursor != -1)
197 cursor += m_preeditCursor;
198 return cursorToX(cursor);
201 bool isReadOnly()
const {
return m_readOnly; }
202 void setReadOnly(
bool enable);
206 QString content = m_text;
207 QString res = m_maskData ? stripString(content) : content;
208 return (res.isNull() ? QString::fromLatin1(
"") : res);
210 void setText(
const QString &txt)
214 QGuiApplication::inputMethod()->reset();
216 internalSetText(txt, -1,
false);
218 void commitPreedit();
220 QString displayText()
const {
return m_textLayout.text(); }
222 QString surroundingText()
const
224 return m_text.isNull() ? QString::fromLatin1(
"") : m_text;
229 void deselect() { internalDeselect(); finishChange(); }
230 void selectAll() { m_selstart = m_selend = m_cursor = 0; moveCursor(m_text.size(),
true); }
232 void insert(
const QString &);
235 void redo() { internalRedo(); finishChange(); }
236 void selectWordAtPos(
int);
238 uint echoMode()
const {
return m_echoMode; }
239 void setEchoMode(uint mode)
241 cancelPasswordEchoTimer();
243 m_passwordEchoEditing =
false;
248 if (m_echoMode != QLineEdit::Normal)
254 int maxLength()
const {
return m_maxLength; }
255 void setMaxLength(
int maxLength)
259 m_maxLength = maxLength;
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); }
268#if QT_CONFIG(completer)
269 QCompleter *completer()
const {
return m_completer; }
271 void setCompleter(
const QCompleter *c) { m_completer =
const_cast<QCompleter*>(c); }
272 void complete(
int key);
275 int cursorPosition()
const {
return m_cursor; }
276 void setCursorPosition(
int pos) {
if (pos <= m_text.size()) moveCursor(qMax(0, pos)); }
278 bool hasAcceptableInput()
const {
return hasAcceptableInput(m_text); }
281 QString inputMask()
const
286 if (m_blank != u' ') {
293 void setInputMask(
const QString &mask)
295 parseInputMask(mask);
297 moveCursor(nextMaskBlank(0));
302 bool composeMode()
const {
return !m_textLayout.preeditAreaText().isEmpty(); }
303 void setPreeditArea(
int cursor,
const QString &text) { m_textLayout.setPreeditArea(cursor, text); }
306 QString preeditAreaText()
const {
return m_textLayout.preeditAreaText(); }
308 void updatePasswordEchoEditing(
bool editing);
309 bool passwordEchoEditing()
const {
310 if (m_passwordEchoTimer.isActive())
312 return m_passwordEchoEditing ;
315 QChar passwordCharacter()
const {
return m_passwordCharacter; }
316 void setPasswordCharacter(QChar character) { m_passwordCharacter = character; updateDisplayText(); }
318 int passwordMaskDelay()
const {
return m_passwordMaskDelay; }
319 void setPasswordMaskDelay(
int delay) { m_passwordMaskDelay = delay; }
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;
326 void setLayoutDirection(Qt::LayoutDirection direction)
328 if (direction != m_layoutDirection) {
329 m_layoutDirection = direction;
334 void setFont(
const QFont &font) { m_textLayout.setFont(font); updateDisplayText(); }
336 void processInputMethodEvent(QInputMethodEvent *event);
337 void processKeyEvent(QKeyEvent* ev);
339 void setBlinkingCursorEnabled(
bool enable);
340 void updateCursorBlinking();
341 void resetCursorBlinkTimer();
343 bool cursorBlinkStatus()
const {
return m_blinkStatus; }
345 QString cancelText()
const {
return m_cancelText; }
346 void setCancelText(
const QString &text) { m_cancelText = text; }
348 const QPalette &palette()
const {
return m_palette; }
349 void setPalette(
const QPalette &p) { m_palette = p; }
353 DrawSelections = 0x02,
355 DrawAll = DrawText | DrawSelections | DrawCursor
357 void draw(QPainter *,
const QPoint &,
const QRect &,
int flags = DrawAll);
359#ifndef QT_NO_SHORTCUT
360 void processShortcutOverrideEvent(QKeyEvent *ke);
363 QTextLayout *textLayout()
const
365 return &m_textLayout;
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);
374 void internalInsert(
const QString &s);
375 void internalDelete(
bool wasBackspace =
false);
376 void internalRemove(
int pos);
378 inline void internalDeselect()
380 m_selDirty |= (m_selend > m_selstart);
381 m_selstart = m_selend = 0;
384 void internalUndo(
int until = -1);
392 Qt::LayoutDirection m_layoutDirection;
393 uint m_hideCursor : 1;
394 uint m_separator : 1;
396 uint m_dragEnabled : 1;
398 uint m_textDirty : 1;
400 uint m_validInput : 1;
401 uint m_blinkStatus : 1;
402 uint m_blinkEnabled : 1;
403 QBasicTimer m_blinkTimer;
404 QBasicTimer m_deleteAllTimer;
408 QList<
int> m_transactions;
409 QPoint m_tripleClick;
410 QBasicTimer m_tripleClickTimer;
411 QString m_cancelText;
413 void emitCursorPositionChanged();
415 bool finishChange(
int validateFromState = -1,
bool update =
false,
bool edited =
true);
417#ifndef QT_NO_VALIDATOR
418 QPointer<QValidator> m_validator;
420 QPointer<QCompleter> m_completer;
421#if QT_CONFIG(completer)
422 bool advanceToEnabledItem(
int dir);
425 struct MaskInputData {
426 enum Casemode { NoCaseMode, Upper, Lower };
433 std::unique_ptr<MaskInputData[]> m_maskData;
436 enum CommandType { Separator, Insert, Remove, Delete, RemoveSelection, DeleteSelection, SetSelection };
438 inline Command(CommandType t,
int p, QChar c,
int ss,
int se) : type(t),uc(c),pos(p),selStart(ss),selEnd(se) {}
441 int pos, selStart, selEnd;
445 std::vector<Command> m_history;
446 void addCommand(
const Command& cmd);
448 inline void separate() { m_separator =
true; }
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;
464 mutable QTextLayout m_textLayout;
466 bool m_passwordEchoEditing;
467 QChar m_passwordCharacter;
468 QBasicTimer m_passwordEchoTimer;
469 int m_passwordMaskDelay;
470 void cancelPasswordEchoTimer()
472 m_passwordEchoTimer.stop();
475 int redoTextLayout()
const;
478#if defined(QT_BUILD_INTERNAL)
479 int m_passwordMaskDelayOverride;
483 void cursorPositionChanged(
int,
int);
484 void selectionChanged();
486 void displayTextChanged(
const QString &);
487 void textChanged(
const QString &);
488 void textEdited(
const QString &);
490 void resetInputContext();
491 void updateMicroFocus();
494 void editingFinished();
495 void updateNeeded(
const QRect &);
496 void inputRejected();
499 virtual void timerEvent(QTimerEvent *event) override;
502 void _q_deleteSelected();
505 int m_keyboardScheme;
508 QObject *m_accessibleObject;
510 friend class QLineEdit;