9#include <private/qqmlglobal_p.h>
10#include <private/qv4scopedvalue_p.h>
12#include <QtCore/qcoreapplication.h>
13#include <QtCore/qmimedata.h>
14#include <QtQml/qqmlinfo.h>
15#include <QtGui/qevent.h>
16#include <QTextBoundaryFinder>
18#include <QtQuick/qsgsimplerectnode.h>
20#include <QtGui/qstylehints.h>
21#include <QtGui/qinputmethod.h>
22#include <QtCore/qmath.h>
24#if QT_CONFIG(accessibility)
25#include "qaccessible.h"
26#include "qquickaccessibleattached_p.h"
29#include <QtGui/private/qtextengine_p.h>
30#include <QtGui/private/qinputcontrol_p.h>
34DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
35Q_STATIC_LOGGING_CATEGORY(lcQuickTextInput,
"qt.quick.textInput")
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65QQuickTextInput::QQuickTextInput(QQuickItem* parent)
66: QQuickImplicitSizeItem(*(
new QQuickTextInputPrivate), parent)
72QQuickTextInput::QQuickTextInput(QQuickTextInputPrivate &dd, QQuickItem *parent)
73: QQuickImplicitSizeItem(dd, parent)
79QQuickTextInput::~QQuickTextInput()
83void QQuickTextInput::componentComplete()
87 QQuickImplicitSizeItem::componentComplete();
91 updateCursorRectangle();
92 if (d->cursorComponent && isCursorVisible())
93 QQuickTextUtil::createCursor(d);
94#if QT_CONFIG(accessibility)
95 if (QAccessible::isActive())
96 d->accessibilityActiveChanged(
true);
101
102
103
104
105
106
107
108
109
110
111
112
113
114QString QQuickTextInput::text()
const
116 Q_D(
const QQuickTextInput);
118 QString content = d->m_text;
119 QString res = d->m_maskData ? d->stripString(content) : content;
120 return (res.isNull() ? QString::fromLatin1(
"") : res);
123void QQuickTextInput::invalidate()
125 Q_D(QQuickTextInput);
127 invalidateFontCaches();
130void QQuickTextInput::setText(
const QString &s)
132 Q_D(QQuickTextInput);
139 d->internalSetText(s, -1,
false);
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168QQuickTextInput::RenderType QQuickTextInput::renderType()
const
170 Q_D(
const QQuickTextInput);
171 return d->renderType;
174void QQuickTextInput::setRenderType(QQuickTextInput::RenderType renderType)
176 Q_D(QQuickTextInput);
177 if (d->renderType == renderType)
180 d->renderType = renderType;
181 emit renderTypeChanged();
183 if (isComponentComplete())
188
189
190
191
192
193
194
195
196
197
199int QQuickTextInput::length()
const
201 Q_D(
const QQuickTextInput);
202 return d->m_text.size();
206
207
208
209
210
211
213QString QQuickTextInput::getText(
int start,
int end)
const
215 Q_D(
const QQuickTextInput);
220 return d->m_text.mid(start, end - start);
223QString QQuickTextInputPrivate::realText()
const
225 QString res = m_maskData ? stripString(m_text) : m_text;
226 return (res.isNull() ? QString::fromLatin1(
"") : res);
230
231
232
233
234
235
238
239
240
241
242
243
244
245
248
249
250
251
254
255
256
257
260
261
262
263
266
267
268
269
272
273
274
275
278
279
280
281
284
285
286
287
288
289
290
293
294
295
296
297
298
301
302
303
304
305
306
309
310
311
312
313
314
317
318
319
320
321
322
323
326
327
328
329
330
333
334
335
336
337
340
341
342
343
344
347
348
349
350
351
354
355
356
357
358
361
362
363
364
365
366QFont QQuickTextInput::font()
const
368 Q_D(
const QQuickTextInput);
369 return d->sourceFont;
372void QQuickTextInput::setFont(
const QFont &font)
374 Q_D(QQuickTextInput);
375 if (d->sourceFont == font)
378 d->sourceFont = font;
379 QFont oldFont = d->font;
381 if (d->font.pointSizeF() != -1) {
383 qreal size = qRound(d->font.pointSizeF()*2.0);
384 d->font.setPointSizeF(size/2.0);
386 if (oldFont != d->font) {
388 updateCursorRectangle();
390 updateInputMethod(Qt::ImCursorRectangle | Qt::ImFont | Qt::ImAnchorRectangle);
393 emit fontChanged(d->sourceFont);
397
398
399
400
401QColor QQuickTextInput::color()
const
403 Q_D(
const QQuickTextInput);
407void QQuickTextInput::setColor(
const QColor &c)
409 Q_D(QQuickTextInput);
412 d->textLayoutDirty =
true;
413 d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
422
423
424
425
426QColor QQuickTextInput::selectionColor()
const
428 Q_D(
const QQuickTextInput);
429 return d->selectionColor;
432void QQuickTextInput::setSelectionColor(
const QColor &color)
434 Q_D(QQuickTextInput);
435 if (d->selectionColor == color)
438 d->selectionColor = color;
439 if (d->hasSelectedText()) {
440 d->textLayoutDirty =
true;
441 d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
445 emit selectionColorChanged();
448
449
450
451
452QColor QQuickTextInput::selectedTextColor()
const
454 Q_D(
const QQuickTextInput);
455 return d->selectedTextColor;
458void QQuickTextInput::setSelectedTextColor(
const QColor &color)
460 Q_D(QQuickTextInput);
461 if (d->selectedTextColor == color)
464 d->selectedTextColor = color;
465 if (d->hasSelectedText()) {
466 d->textLayoutDirty =
true;
467 d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
471 emit selectedTextColorChanged();
475
476
477
478
479
480
481
482
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509QQuickTextInput::HAlignment QQuickTextInput::hAlign()
const
511 Q_D(
const QQuickTextInput);
515void QQuickTextInput::setHAlign(HAlignment align)
517 Q_D(QQuickTextInput);
519 if (d->setHAlign(align,
true) && isComponentComplete()) {
521 updateCursorRectangle();
525void QQuickTextInput::resetHAlign()
527 Q_D(QQuickTextInput);
528 d->hAlignImplicit =
true;
529 if (d->determineHorizontalAlignment() && isComponentComplete()) {
531 updateCursorRectangle();
535QQuickTextInput::HAlignment QQuickTextInput::effectiveHAlign()
const
537 Q_D(
const QQuickTextInput);
538 QQuickTextInput::HAlignment effectiveAlignment = d->hAlign;
539 if (!d->hAlignImplicit && d->effectiveLayoutMirror) {
541 case QQuickTextInput::AlignLeft:
542 effectiveAlignment = QQuickTextInput::AlignRight;
544 case QQuickTextInput::AlignRight:
545 effectiveAlignment = QQuickTextInput::AlignLeft;
551 return effectiveAlignment;
554bool QQuickTextInputPrivate::setHAlign(QQuickTextInput::HAlignment align,
bool forceAlign)
556 Q_Q(QQuickTextInput);
557 if (align > QQuickTextInput::AlignHCenter)
560 if (hAlign == align && !forceAlign)
563 const bool wasImplicit = hAlignImplicit;
564 const auto oldEffectiveHAlign = q->effectiveHAlign();
566 hAlignImplicit = !forceAlign;
567 if (hAlign != align) {
569 emit q->horizontalAlignmentChanged(align);
572 if (q->effectiveHAlign() != oldEffectiveHAlign) {
573 emit q->effectiveHorizontalAlignmentChanged();
577 if (forceAlign && wasImplicit) {
580 emit q->effectiveHorizontalAlignmentChanged();
585Qt::LayoutDirection QQuickTextInputPrivate::textDirection()
const
587 QString text = m_text;
590 text = m_textLayout.preeditAreaText();
593 const QChar *character = text.constData();
594 while (!character->isNull()) {
595 switch (character->direction()) {
597 return Qt::LeftToRight;
601 return Qt::RightToLeft;
607 return Qt::LayoutDirectionAuto;
610Qt::LayoutDirection QQuickTextInputPrivate::layoutDirection()
const
612 Qt::LayoutDirection direction = m_layoutDirection;
613 if (direction == Qt::LayoutDirectionAuto) {
614 direction = textDirection();
616 if (direction == Qt::LayoutDirectionAuto)
617 direction = QGuiApplication::inputMethod()->inputDirection();
620 return (direction == Qt::LayoutDirectionAuto) ? Qt::LeftToRight : direction;
623bool QQuickTextInputPrivate::determineHorizontalAlignment()
629 Qt::LayoutDirection direction = textDirection();
631 if (direction == Qt::LayoutDirectionAuto)
632 direction = QGuiApplication::inputMethod()->inputDirection();
635 const auto implicitHAlign = direction == Qt::RightToLeft ?
636 QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft;
637 return setHAlign(implicitHAlign);
640QQuickTextInput::VAlignment QQuickTextInput::vAlign()
const
642 Q_D(
const QQuickTextInput);
646void QQuickTextInput::setVAlign(QQuickTextInput::VAlignment alignment)
648 Q_D(QQuickTextInput);
649 if (alignment == d->vAlign)
651 d->vAlign = alignment;
652 emit verticalAlignmentChanged(d->vAlign);
653 if (isComponentComplete()) {
654 updateCursorRectangle();
655 d->updateBaselineOffset();
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679QQuickTextInput::WrapMode QQuickTextInput::wrapMode()
const
681 Q_D(
const QQuickTextInput);
685void QQuickTextInput::setWrapMode(WrapMode mode)
687 Q_D(QQuickTextInput);
688 if (mode == d->wrapMode)
692 updateCursorRectangle();
693 emit wrapModeChanged();
696void QQuickTextInputPrivate::mirrorChange()
698 Q_Q(QQuickTextInput);
699 if (q->isComponentComplete()) {
700 if (!hAlignImplicit && (hAlign == QQuickTextInput::AlignRight || hAlign == QQuickTextInput::AlignLeft)) {
701 q->updateCursorRectangle();
702 emit q->effectiveHorizontalAlignmentChanged();
708
709
710
711
712
713
714
715
716bool QQuickTextInput::isReadOnly()
const
718 Q_D(
const QQuickTextInput);
719 return d->m_readOnly;
722void QQuickTextInput::setReadOnly(
bool ro)
724 Q_D(QQuickTextInput);
725 if (d->m_readOnly == ro)
729 setFlag(QQuickItem::ItemAcceptsInputMethod, !ro);
732 d->setCursorPosition(d->end());
734 updateInputMethod(Qt::ImEnabled);
737 d->emitUndoRedoChanged();
738 emit readOnlyChanged(ro);
740 setCursorVisible(
false);
741 }
else if (hasActiveFocus()) {
742 setCursorVisible(
true);
748
749
750
751
752
753
754
755int QQuickTextInput::maxLength()
const
757 Q_D(
const QQuickTextInput);
758 return d->m_maxLength;
761void QQuickTextInput::setMaxLength(
int ml)
763 Q_D(QQuickTextInput);
764 if (d->m_maxLength == ml || d->m_maskData)
768 d->internalSetText(d->m_text, -1,
false);
770 emit maximumLengthChanged(ml);
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799bool QQuickTextInput::isCursorVisible()
const
801 Q_D(
const QQuickTextInput);
802 return d->cursorVisible;
805void QQuickTextInput::setCursorVisible(
bool on)
807 Q_D(QQuickTextInput);
808 if (d->cursorVisible == on)
810 d->cursorVisible = on;
811 if (on && isComponentComplete())
812 QQuickTextUtil::createCursor(d);
814 d->updateCursorBlinking();
815 emit cursorVisibleChanged(d->cursorVisible);
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834int QQuickTextInput::cursorPosition()
const
836 Q_D(
const QQuickTextInput);
840void QQuickTextInput::setCursorPosition(
int cp)
842 Q_D(QQuickTextInput);
843 if (cp < 0 || cp > text().size())
849
850
851
852
853
854
855
856
857
859QRectF QQuickTextInput::cursorRectangle()
const
861 Q_D(
const QQuickTextInput);
865 c += d->m_preeditCursor;
867 if (d->m_echoMode == NoEcho)
869 QTextLine l = d->m_textLayout.lineForTextPosition(c);
872 qreal x = l.cursorToX(c) - d->hscroll + leftPadding();
873 qreal y = l.y() - d->vscroll + topPadding();
875 if (d->overwriteMode) {
876 if (c < text().size())
877 w = l.cursorToX(c + 1) - x;
879 w = QFontMetrics(font()).horizontalAdvance(QLatin1Char(
' '));
881 return QRectF(x, y, w, l.height());
885
886
887
888
889
890
891
892
893
894
895int QQuickTextInput::selectionStart()
const
897 Q_D(
const QQuickTextInput);
898 return d->lastSelectionStart;
901
902
903
904
905
906
907
908
909
910
911int QQuickTextInput::selectionEnd()
const
913 Q_D(
const QQuickTextInput);
914 return d->lastSelectionEnd;
917
918
919
920
921
922
923
924
925
926
927
928
929void QQuickTextInput::select(
int start,
int end)
931 Q_D(QQuickTextInput);
932 if (start < 0 || end < 0 || start > d->m_text.size() || end > d->m_text.size())
934 d->setSelection(start, end-start);
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952QString QQuickTextInput::selectedText()
const
954 Q_D(
const QQuickTextInput);
955 return d->selectedText();
959
960
961
962
963
964bool QQuickTextInput::focusOnPress()
const
966 Q_D(
const QQuickTextInput);
967 return d->focusOnPress;
970void QQuickTextInput::setFocusOnPress(
bool b)
972 Q_D(QQuickTextInput);
973 if (d->focusOnPress == b)
978 emit activeFocusOnPressChanged(d->focusOnPress);
981
982
983
984
985
986
987
988bool QQuickTextInput::autoScroll()
const
990 Q_D(
const QQuickTextInput);
991 return d->autoScroll;
994void QQuickTextInput::setAutoScroll(
bool b)
996 Q_D(QQuickTextInput);
997 if (d->autoScroll == b)
1002 updateCursorRectangle();
1003 emit autoScrollChanged(d->autoScroll);
1006#if QT_CONFIG(validator)
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1031QValidator* QQuickTextInput::validator()
const
1033 Q_D(
const QQuickTextInput);
1034 return d->m_validator;
1037void QQuickTextInput::setValidator(QValidator* v)
1039 Q_D(QQuickTextInput);
1040 if (d->m_validator == v)
1043 if (d->m_validator) {
1044 qmlobject_disconnect(
1045 d->m_validator, QValidator, SIGNAL(changed()),
1046 this, QQuickTextInput, SLOT(q_validatorChanged()));
1051 if (d->m_validator) {
1053 d->m_validator, QValidator, SIGNAL(changed()),
1054 this, QQuickTextInput, SLOT(q_validatorChanged()));
1057 if (isComponentComplete())
1060 emit validatorChanged();
1063void QQuickTextInput::q_validatorChanged()
1065 Q_D(QQuickTextInput);
1070QRectF QQuickTextInputPrivate::anchorRectangle()
const
1072 Q_Q(
const QQuickTextInput);
1079 if (m_selstart == m_selend)
1084 a = m_selstart == m_cursor ? m_selend : m_selstart;
1087 a += m_preeditCursor;
1089 if (m_echoMode == QQuickTextInput::NoEcho)
1091 QTextLine l = m_textLayout.lineForTextPosition(a);
1093 qreal x = l.cursorToX(a) - hscroll + q->leftPadding();
1094 qreal y = l.y() - vscroll + q->topPadding();
1095 rect.setRect(x, y, 1, l.height());
1101void QQuickTextInputPrivate::checkIsValid()
1103 Q_Q(QQuickTextInput);
1105 ValidatorState state = hasAcceptableInput(m_text);
1107 m_validInput = state != InvalidInput;
1108 if (state != AcceptableInput) {
1109 if (m_acceptableInput) {
1110 m_acceptableInput =
false;
1111 emit q->acceptableInputChanged();
1113 }
else if (!m_acceptableInput) {
1114 m_acceptableInput =
true;
1115 emit q->acceptableInputChanged();
1120
1121
1122
1123
1124
1125
1126
1127
1128QString QQuickTextInput::inputMask()
const
1130 Q_D(
const QQuickTextInput);
1131 return d->inputMask();
1134void QQuickTextInput::setInputMask(
const QString &im)
1136 Q_D(QQuickTextInput);
1137 QString canonicalInputMask = im;
1138 if (im.lastIndexOf(QLatin1Char(
';')) == -1)
1139 canonicalInputMask.append(QLatin1String(
"; "));
1140 if (d->inputMask() == canonicalInputMask)
1143 d->setInputMask(im);
1144 emit inputMaskChanged(d->inputMask());
1148
1149
1150
1151
1152
1153
1154
1155
1156bool QQuickTextInput::hasAcceptableInput()
const
1158 Q_D(
const QQuickTextInput);
1159 return d->m_acceptableInput;
1163
1164
1165
1166
1167
1168
1169
1170
1171
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1187
1188
1189
1190
1191
1192
1193
1194
1195
1198Qt::InputMethodHints QQuickTextInputPrivate::effectiveInputMethodHints()
const
1200 Qt::InputMethodHints hints = inputMethodHints;
1201 if (m_echoMode == QQuickTextInput::NoEcho)
1202 hints |= Qt::ImhHiddenText;
1203 else if (m_echoMode == QQuickTextInput::Password || m_echoMode == QQuickTextInput::PasswordEchoOnEdit)
1204 hints &= ~Qt::ImhHiddenText;
1205 if (m_echoMode != QQuickTextInput::Normal)
1206 hints |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText | Qt::ImhSensitiveData);
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225QQuickTextInput::EchoMode QQuickTextInput::echoMode()
const
1227 Q_D(
const QQuickTextInput);
1228 return QQuickTextInput::EchoMode(d->m_echoMode);
1231void QQuickTextInput::setEchoMode(QQuickTextInput::EchoMode echo)
1233 Q_D(QQuickTextInput);
1234 if (echoMode() == echo)
1236 d->cancelPasswordEchoTimer();
1237 d->m_echoMode = echo;
1238 d->m_passwordEchoEditing =
false;
1240 updateInputMethod(Qt::ImHints);
1242 d->updateDisplayText();
1243 updateCursorRectangle();
1248 if (d->m_echoMode != QQuickTextInput::Normal)
1249 d->m_text.reserve(30);
1251 emit echoModeChanged(echoMode());
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1298Qt::InputMethodHints QQuickTextInput::inputMethodHints()
const
1303 Q_D(
const QQuickTextInput);
1304 return d->inputMethodHints;
1308void QQuickTextInput::setInputMethodHints(Qt::InputMethodHints hints)
1313 Q_D(QQuickTextInput);
1315 if (hints == d->inputMethodHints)
1318 d->inputMethodHints = hints;
1319 updateInputMethod(Qt::ImHints);
1320 emit inputMethodHintsChanged();
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337QQmlComponent* QQuickTextInput::cursorDelegate()
const
1339 Q_D(
const QQuickTextInput);
1340 return d->cursorComponent;
1343void QQuickTextInput::setCursorDelegate(QQmlComponent* c)
1345 Q_D(QQuickTextInput);
1346 QQuickTextUtil::setCursorDelegate(d, c);
1349void QQuickTextInput::createCursor()
1351 Q_D(QQuickTextInput);
1352 d->cursorPending =
true;
1353 QQuickTextUtil::createCursor(d);
1357
1358
1359
1360
1361
1362
1363
1364
1365QRectF QQuickTextInput::positionToRectangle(
int pos)
const
1367 Q_D(
const QQuickTextInput);
1368 if (d->m_echoMode == NoEcho)
1371 else if (pos > d->m_cursor)
1372 pos += d->preeditAreaText().size();
1374 QTextLine l = d->m_textLayout.lineForTextPosition(pos);
1377 qreal x = l.cursorToX(pos) - d->hscroll;
1378 qreal y = l.y() - d->vscroll;
1380 if (d->overwriteMode) {
1381 if (pos < text().size())
1382 w = l.cursorToX(pos + 1) - x;
1384 w = QFontMetrics(font()).horizontalAdvance(QLatin1Char(
' '));
1386 return QRectF(x, y, w, l.height());
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1412int QQuickTextInput::positionAt(qreal x, qreal y, QQuickTextInput::CursorPosition positionQuick)
const
1414 Q_D(
const QQuickTextInput);
1416 QTextLine::CursorPosition position = QTextLine::CursorPosition(positionQuick);
1418 int pos = d->positionAt(x, y, position);
1419 const int cursor = d->m_cursor;
1422 const int preeditLength = d->preeditAreaText().size();
1423 pos = pos > cursor + preeditLength
1424 ? pos - preeditLength
1433int QQuickTextInputPrivate::positionAt(qreal x, qreal y, QTextLine::CursorPosition position)
const
1435 Q_Q(
const QQuickTextInput);
1436 x += hscroll - q->leftPadding();
1437 y += vscroll - q->topPadding();
1438 QTextLine line = m_textLayout.lineAt(0);
1439 for (
int i = 1; i < m_textLayout.lineCount(); ++i) {
1440 QTextLine nextLine = m_textLayout.lineAt(i);
1442 if (y < (line.rect().bottom() + nextLine.y()) / 2)
1446 return line.isValid() ? line.xToCursor(x, position) : 0;
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464bool QQuickTextInput::overwriteMode()
const
1466 Q_D(
const QQuickTextInput);
1467 return d->overwriteMode;
1470void QQuickTextInput::setOverwriteMode(
bool overwrite)
1472 Q_D(QQuickTextInput);
1473 if (d->overwriteMode == overwrite)
1475 d->overwriteMode = overwrite;
1476 emit overwriteModeChanged(overwrite);
1479void QQuickTextInput::keyPressEvent(QKeyEvent* ev)
1481 Q_D(QQuickTextInput);
1483 bool ignore = (ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) && ev->modifiers() == Qt::NoModifier;
1484 if (!ignore && (d->lastSelectionStart == d->lastSelectionEnd) && (ev->key() == Qt::Key_Right || ev->key() == Qt::Key_Left)) {
1487 int cursorPosition = d->m_cursor;
1488 if (cursorPosition == 0)
1489 ignore = ev->key() == (d->layoutDirection() == Qt::LeftToRight ? Qt::Key_Left : Qt::Key_Right);
1490 if (!ignore && cursorPosition == d->m_text.size())
1491 ignore = ev->key() == (d->layoutDirection() == Qt::LeftToRight ? Qt::Key_Right : Qt::Key_Left);
1496 d->processKeyEvent(ev);
1498 if (!ev->isAccepted())
1499 QQuickImplicitSizeItem::keyPressEvent(ev);
1503void QQuickTextInput::inputMethodEvent(QInputMethodEvent *ev)
1505 Q_D(QQuickTextInput);
1506 const bool wasComposing = d->hasImState;
1507 d->processInputMethodEvent(ev);
1508 if (!ev->isAccepted())
1509 QQuickImplicitSizeItem::inputMethodEvent(ev);
1511 if (wasComposing != d->hasImState)
1512 emit inputMethodComposingChanged();
1516void QQuickTextInput::mouseDoubleClickEvent(QMouseEvent *event)
1518 Q_D(QQuickTextInput);
1520 if (d->selectByMouse && event->button() == Qt::LeftButton &&
1521 QQuickDeliveryAgentPrivate::isEventFromMouseOrTouchpad(event)) {
1525 int cursor = d->positionAt(event->position());
1526 d->selectWordAtPos(cursor);
1527 event->setAccepted(
true);
1528 if (!d->hasPendingTripleClick()) {
1529 d->tripleClickStartPoint = event->position();
1530 d->tripleClickTimer.start();
1533 if (d->sendMouseEventToInputContext(event))
1535 QQuickImplicitSizeItem::mouseDoubleClickEvent(event);
1539void QQuickTextInput::mousePressEvent(QMouseEvent *event)
1541 Q_D(QQuickTextInput);
1543 d->pressPos = event->position();
1545 if (d->sendMouseEventToInputContext(event))
1548 d->hadSelectionOnMousePress = d->hasSelectedText();
1550 const bool isMouse = QQuickDeliveryAgentPrivate::isEventFromMouseOrTouchpad(event);
1551 if (d->selectByMouse &&
1553#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
1554 || d->selectByTouchDrag
1557 setKeepMouseGrab(
false);
1558 d->selectPressed =
true;
1559 QPointF distanceVector = d->pressPos - d->tripleClickStartPoint;
1560 if (d->hasPendingTripleClick()
1561 && distanceVector.manhattanLength() < QGuiApplication::styleHints()->startDragDistance()) {
1562 event->setAccepted(
true);
1569#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
1570 || d->selectByTouchDrag
1573 bool mark = (event->modifiers() & Qt::ShiftModifier) && d->selectByMouse;
1574 int cursor = d->positionAt(event->position());
1575 d->moveCursor(cursor, mark);
1578 if (d->focusOnPress && !qGuiApp->styleHints()->setFocusOnTouchRelease())
1579 ensureActiveFocus(Qt::MouseFocusReason);
1581 event->setAccepted(
true);
1584void QQuickTextInput::mouseMoveEvent(QMouseEvent *event)
1586 Q_D(QQuickTextInput);
1587 if (!QQuickDeliveryAgentPrivate::isEventFromMouseOrTouchpad(event)
1588#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
1589 && ! d->selectByTouchDrag
1594 if (d->selectPressed) {
1595 if (qAbs(
int(event->position().x() - d->pressPos.x())) > QGuiApplication::styleHints()->startDragDistance())
1596 setKeepMouseGrab(
true);
1599 if (d->composeMode()) {
1601 int startPos = d->positionAt(d->pressPos);
1602 int currentPos = d->positionAt(event->position());
1603 if (startPos != currentPos)
1604 d->setSelection(startPos, currentPos - startPos);
1608 moveCursorSelection(d->positionAt(event->position()), d->mouseSelectionMode);
1610 event->setAccepted(
true);
1612 QQuickImplicitSizeItem::mouseMoveEvent(event);
1616void QQuickTextInput::mouseReleaseEvent(QMouseEvent *event)
1618 Q_D(QQuickTextInput);
1619 if (d->sendMouseEventToInputContext(event))
1621 if (d->selectPressed) {
1622 d->selectPressed =
false;
1623 setKeepMouseGrab(
false);
1625 const bool isMouse = QQuickDeliveryAgentPrivate::isEventFromMouseOrTouchpad(event)
1626#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
1627 || d->selectByTouchDrag
1631#if QT_CONFIG(clipboard)
1632 if (isMouse && QGuiApplication::clipboard()->supportsSelection()) {
1633 if (event->button() == Qt::LeftButton) {
1634 d->copy(QClipboard::Selection);
1635 }
else if (!d->m_readOnly && event->button() == Qt::MiddleButton) {
1637 d->insert(QGuiApplication::clipboard()->text(QClipboard::Selection));
1646 if (!isMouse && (!d->hasSelectedText() || d->hadSelectionOnMousePress))
1647 d->moveCursor(d->positionAt(event->position()),
false);
1651 d->hadSelectionOnMousePress =
false;
1653 if (d->focusOnPress && qGuiApp->styleHints()->setFocusOnTouchRelease())
1654 ensureActiveFocus(Qt::MouseFocusReason);
1656 if (!event->isAccepted())
1657 QQuickImplicitSizeItem::mouseReleaseEvent(event);
1660#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
1661bool QQuickTextInputPrivate::handleContextMenuEvent(QContextMenuEvent *event)
1663bool QQuickTextInput::contextMenuEvent(QContextMenuEvent *event)
1666 Q_Q(QQuickTextInput);
1667 const QPoint pos = event->reason() == QContextMenuEvent::Keyboard
1668 ? q->cursorRectangle().center().toPoint() : event->pos();
1669 QContextMenuEvent mapped(event->reason(), pos, event->globalPos(),
1670 event->modifiers());
1671 const bool eventProcessed = QQuickItemPrivate::handleContextMenuEvent(&mapped);
1672 event->setAccepted(mapped.isAccepted());
1673 return eventProcessed;
1676bool QQuickTextInputPrivate::sendMouseEventToInputContext(QMouseEvent *event)
1679 if (composeMode()) {
1680 int tmp_cursor = positionAt(event->position());
1681 int mousePos = tmp_cursor - m_cursor;
1682 if (mousePos >= 0 && mousePos <= m_textLayout.preeditAreaText().size()) {
1683 if (event->type() == QEvent::MouseButtonRelease) {
1684 QGuiApplication::inputMethod()->invokeAction(QInputMethod::Click, mousePos);
1696void QQuickTextInput::mouseUngrabEvent()
1698 Q_D(QQuickTextInput);
1699 d->selectPressed =
false;
1700 setKeepMouseGrab(
false);
1703bool QQuickTextInput::event(QEvent* ev)
1705#if QT_CONFIG(shortcut)
1706 Q_D(QQuickTextInput);
1707 if (ev->type() == QEvent::ShortcutOverride) {
1708 if (d->m_readOnly) {
1712 QKeyEvent* ke =
static_cast<QKeyEvent*>(ev);
1713 if (ke == QKeySequence::Copy
1714 || ke == QKeySequence::Paste
1715 || ke == QKeySequence::Cut
1716 || ke == QKeySequence::Redo
1717 || ke == QKeySequence::Undo
1718 || ke == QKeySequence::MoveToNextWord
1719 || ke == QKeySequence::MoveToPreviousWord
1720 || ke == QKeySequence::MoveToStartOfDocument
1721 || ke == QKeySequence::MoveToEndOfDocument
1722 || ke == QKeySequence::SelectNextWord
1723 || ke == QKeySequence::SelectPreviousWord
1724 || ke == QKeySequence::SelectStartOfLine
1725 || ke == QKeySequence::SelectEndOfLine
1726 || ke == QKeySequence::SelectStartOfBlock
1727 || ke == QKeySequence::SelectEndOfBlock
1728 || ke == QKeySequence::SelectStartOfDocument
1729 || ke == QKeySequence::SelectAll
1730 || ke == QKeySequence::SelectEndOfDocument
1731 || ke == QKeySequence::DeleteCompleteLine) {
1734 }
else if (ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::ShiftModifier
1735 || ke->modifiers() == Qt::KeypadModifier) {
1736 if (ke->key() < Qt::Key_Escape) {
1740 switch (ke->key()) {
1741 case Qt::Key_Delete:
1744 case Qt::Key_Backspace:
1758 return QQuickImplicitSizeItem::event(ev);
1761void QQuickTextInput::geometryChange(
const QRectF &newGeometry,
1762 const QRectF &oldGeometry)
1764 Q_D(QQuickTextInput);
1766 if (newGeometry.width() != oldGeometry.width())
1768 else if (newGeometry.height() != oldGeometry.height() && d->vAlign != QQuickTextInput::AlignTop)
1769 d->updateBaselineOffset();
1770 updateCursorRectangle();
1772 QQuickImplicitSizeItem::geometryChange(newGeometry, oldGeometry);
1775void QQuickTextInput::itemChange(ItemChange change,
const ItemChangeData &value)
1777 Q_D(QQuickTextInput);
1780 case ItemDevicePixelRatioHasChanged:
1781 if (d->containsUnscalableGlyphs) {
1792 QQuickImplicitSizeItem::itemChange(change, value);
1795void QQuickTextInputPrivate::ensureVisible(
int position,
int preeditCursor,
int preeditLength)
1797 Q_Q(QQuickTextInput);
1798 QTextLine textLine = m_textLayout.lineForTextPosition(position + preeditCursor);
1799 const qreal width = qMax<qreal>(0, q->width() - q->leftPadding() - q->rightPadding());
1801 qreal widthUsed = 0;
1802 if (textLine.isValid()) {
1803 cix = textLine.cursorToX(position + preeditLength);
1804 const qreal cursorWidth = cix >= 0 ? cix : width - cix;
1805 widthUsed = qMax(textLine.naturalTextWidth(), cursorWidth);
1807 int previousScroll = hscroll;
1809 if (widthUsed <= width) {
1812 Q_ASSERT(textLine.isValid());
1813 if (cix - hscroll >= width) {
1815 hscroll = cix - width;
1816 }
else if (cix - hscroll < 0 && hscroll < widthUsed) {
1819 }
else if (widthUsed - hscroll < width) {
1822 hscroll = widthUsed - width;
1823 }
else if (width - hscroll > widthUsed) {
1826 hscroll = width - widthUsed;
1829 if (preeditLength > 0) {
1832 cix = textLine.cursorToX(position + qMax(0, preeditCursor - 1));
1838 if (previousScroll != hscroll)
1839 textLayoutDirty =
true;
1842void QQuickTextInputPrivate::updateHorizontalScroll()
1844 if (autoScroll && m_echoMode != QQuickTextInput::NoEcho) {
1846 const int preeditLength = m_textLayout.preeditAreaText().size();
1847 ensureVisible(m_cursor, m_preeditCursor, preeditLength);
1849 ensureVisible(m_cursor);
1856void QQuickTextInputPrivate::updateVerticalScroll()
1858 Q_Q(QQuickTextInput);
1860 const int preeditLength = m_textLayout.preeditAreaText().size();
1862 const qreal height = qMax<qreal>(0, q->height() - q->topPadding() - q->bottomPadding());
1863 qreal heightUsed = contentSize.height();
1864 qreal previousScroll = vscroll;
1866 if (!autoScroll || heightUsed <= height) {
1868 vscroll = -QQuickTextUtil::alignedY(
1869 heightUsed, height, vAlign & ~(Qt::AlignAbsolute|Qt::AlignHorizontal_Mask));
1872 QTextLine currentLine = m_textLayout.lineForTextPosition(m_cursor + preeditLength);
1874 QTextLine currentLine = m_textLayout.lineForTextPosition(m_cursor);
1876 QRectF r = currentLine.isValid() ? currentLine.rect() : QRectF();
1877 qreal top = r.top();
1878 int bottom = r.bottom();
1880 if (bottom - vscroll >= height) {
1882 vscroll = bottom - height;
1883 }
else if (top - vscroll < 0 && vscroll < heightUsed) {
1886 }
else if (heightUsed - vscroll < height) {
1889 vscroll = heightUsed - height;
1892 if (preeditLength > 0) {
1895 currentLine = m_textLayout.lineForTextPosition(m_cursor + qMax(0, m_preeditCursor - 1));
1896 top = currentLine.isValid() ? currentLine.rect().top() : 0;
1902 if (previousScroll != vscroll)
1903 textLayoutDirty =
true;
1906void QQuickTextInput::triggerPreprocess()
1908 Q_D(QQuickTextInput);
1909 if (d->updateType == QQuickTextInputPrivate::UpdateNone)
1910 d->updateType = QQuickTextInputPrivate::UpdateOnlyPreprocess;
1915void QQuickTextInput::updatePolish()
1917 invalidateFontCaches();
1920void QQuickTextInput::invalidateFontCaches()
1922 Q_D(QQuickTextInput);
1924 if (d->m_textLayout.engine() !=
nullptr)
1925 d->m_textLayout.engine()->resetFontEngineCache();
1928void QQuickTextInput::ensureActiveFocus(Qt::FocusReason reason)
1930 bool hadActiveFocus = hasActiveFocus();
1931 forceActiveFocus(reason);
1933 Q_D(QQuickTextInput);
1935 if (hasActiveFocus() && hadActiveFocus && !d->m_readOnly)
1936 qGuiApp->inputMethod()->show();
1938 Q_UNUSED(hadActiveFocus);
1942QSGNode *QQuickTextInput::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data)
1945 Q_D(QQuickTextInput);
1947 if (d->updateType != QQuickTextInputPrivate::UpdatePaintNode && oldNode !=
nullptr) {
1949 d->updateType = QQuickTextInputPrivate::UpdateNone;
1953 d->updateType = QQuickTextInputPrivate::UpdateNone;
1955 QSGInternalTextNode *node =
static_cast<QSGInternalTextNode *>(oldNode);
1956 if (node ==
nullptr)
1957 node = d->sceneGraphContext()->createInternalTextNode(d->sceneGraphRenderContext());
1960 const bool showCursor = !isReadOnly() && d->cursorItem ==
nullptr && d->cursorVisible && d->m_blinkStatus;
1962 if (!d->textLayoutDirty && oldNode !=
nullptr) {
1964 node->setCursor(cursorRectangle(), d->color,
nullptr);
1966 node->clearCursor();
1968 node->setRenderType(QSGTextNode::RenderType(d->renderType));
1970 QSGInternalTextNode::RecycleBin recycleBin;
1971 node->recycle(&recycleBin);
1972 node->setMatrix(QMatrix4x4());
1973 node->setTextStyle(QSGInternalTextNode::Normal);
1974 node->setColor(d->color);
1975 node->setSelectionTextColor(d->selectedTextColor);
1976 node->setSelectionColor(d->selectionColor);
1977 node->setFiltering(smooth() ? QSGTexture::Linear : QSGTexture::Nearest);
1979 if (flags().testFlag(ItemObservesViewport))
1980 node->setViewport(clipRect());
1982 node->setViewport(QRectF{});
1984 QPointF offset(leftPadding(), topPadding());
1985 offset -= QPointF(d->hscroll, d->vscroll);
1987 if (!d->m_textLayout.text().isEmpty()
1989 || !d->m_textLayout.preeditAreaText().isEmpty()
1992 node->addTextLayout(offset,
1995 d->selectionStart(),
1996 d->selectionEnd() - 1);
2001 node->setCursor(cursorRectangle(), d->color, &recycleBin);
2002 node->discardUnusedNodes(&recycleBin);
2004 d->textLayoutDirty =
false;
2007 d->containsUnscalableGlyphs = node->containsUnscalableGlyphs();
2009 invalidateFontCaches();
2015QVariant QQuickTextInput::inputMethodQuery(Qt::InputMethodQuery property)
const
2019 if (property == Qt::ImEnterKeyType) {
2020 Q_D(
const QQuickItem);
2022 if (!d->extra.isAllocated()
2023 || d->extra->enterKeyAttached ==
nullptr
2024 || d->extra->enterKeyAttached->type() == Qt::EnterKeyDefault) {
2026 QQuickItem *next =
const_cast<QQuickTextInput*>(
this)->nextItemInFocusChain();
2027 QQuickItem *originalNext = next;
2028 while (next && next !=
this && !next->activeFocusOnTab()) {
2029 next = next->nextItemInFocusChain();
2030 if (next == originalNext) {
2036 const auto nextYPos = next->mapToGlobal(QPoint(0, 0)).y();
2037 const auto currentYPos =
this->mapToGlobal(QPoint(0, 0)).y();
2038 if (currentYPos < nextYPos)
2041 return Qt::EnterKeyNext;
2046 return inputMethodQuery(property, QVariant());
2049QVariant QQuickTextInput::inputMethodQuery(Qt::InputMethodQuery property,
const QVariant &argument)
const
2051 Q_D(
const QQuickTextInput);
2054 return QVariant((
bool)(flags() & ItemAcceptsInputMethod));
2056 return QVariant((
int) d->effectiveInputMethodHints());
2057 case Qt::ImCursorRectangle:
2058 return cursorRectangle();
2059 case Qt::ImAnchorRectangle:
2060 return d->anchorRectangle();
2063 case Qt::ImCursorPosition: {
2064 const QPointF pt = argument.toPointF();
2066 return QVariant(d->positionAt(pt));
2067 return QVariant(d->m_cursor);
2069 case Qt::ImSurroundingText:
2070 if (d->m_echoMode == PasswordEchoOnEdit && !d->m_passwordEchoEditing) {
2071 return QVariant(displayText());
2073 return QVariant(d->realText());
2075 case Qt::ImCurrentSelection:
2076 return QVariant(selectedText());
2077 case Qt::ImMaximumTextLength:
2078 return QVariant(maxLength());
2079 case Qt::ImAnchorPosition:
2080 if (d->selectionStart() == d->selectionEnd())
2081 return QVariant(d->m_cursor);
2082 else if (d->selectionStart() == d->m_cursor)
2083 return QVariant(d->selectionEnd());
2085 return QVariant(d->selectionStart());
2086 case Qt::ImAbsolutePosition:
2087 return QVariant(d->m_cursor);
2088 case Qt::ImTextAfterCursor:
2089 if (argument.isValid())
2090 return QVariant(d->m_text.mid(d->m_cursor, argument.toInt()));
2091 return QVariant(d->m_text.mid(d->m_cursor));
2092 case Qt::ImTextBeforeCursor:
2093 if (argument.isValid())
2094 return QVariant(QStringView{d->m_text}.left(d->m_cursor).right(argument.toInt()).toString());
2095 return QVariant(d->m_text.left(d->m_cursor));
2096 case Qt::ImReadOnly:
2097 return QVariant(d->m_readOnly);
2099 return QQuickItem::inputMethodQuery(property);
2105
2106
2107
2108
2109void QQuickTextInput::deselect()
2111 Q_D(QQuickTextInput);
2116
2117
2118
2119
2120void QQuickTextInput::selectAll()
2122 Q_D(QQuickTextInput);
2123 d->setSelection(0, text().size());
2127
2128
2129
2130
2131
2132bool QQuickTextInput::isRightToLeft(
int start,
int end)
2135 qmlWarning(
this) <<
"isRightToLeft(start, end) called with the end property being smaller than the start.";
2138 return QStringView{text()}.mid(start, end - start).isRightToLeft();
2142#if QT_CONFIG(clipboard)
2144
2145
2146
2147
2148
2149
2150
2151
2152void QQuickTextInput::cut()
2154 Q_D(QQuickTextInput);
2155 if (!d->m_readOnly && d->m_echoMode == QQuickTextInput::Normal) {
2162
2163
2164
2165
2166
2167
2168
2169
2170void QQuickTextInput::copy()
2172 Q_D(QQuickTextInput);
2177
2178
2179
2180
2181void QQuickTextInput::paste()
2183 Q_D(QQuickTextInput);
2190
2191
2192
2193
2194
2195
2197void QQuickTextInput::undo()
2199 Q_D(QQuickTextInput);
2200 if (!d->m_readOnly) {
2203 d->finishChange(-1,
true);
2208
2209
2210
2211
2213void QQuickTextInput::redo()
2215 Q_D(QQuickTextInput);
2216 if (!d->m_readOnly) {
2224
2225
2226
2227
2229void QQuickTextInput::insert(
int position,
const QString &text)
2231 Q_D(QQuickTextInput);
2232 if (d->m_echoMode == QQuickTextInput::Password) {
2233 if (d->m_passwordMaskDelay > 0)
2234 d->m_passwordEchoTimer.start(d->m_passwordMaskDelay,
this);
2236 if (position < 0 || position > d->m_text.size())
2239 const int priorState = d->m_undoState;
2241 QString insertText = text;
2243 if (d->hasSelectedText()) {
2244 d->addCommand(QQuickTextInputPrivate::Command(
2245 QQuickTextInputPrivate::SetSelection, d->m_cursor, u'\0', d->m_selstart, d->m_selend));
2247 if (d->m_maskData) {
2248 insertText = d->maskString(position, insertText);
2249 for (
int i = 0; i < insertText.size(); ++i) {
2250 d->addCommand(QQuickTextInputPrivate::Command(
2251 QQuickTextInputPrivate::DeleteSelection, position + i, d->m_text.at(position + i), -1, -1));
2252 d->addCommand(QQuickTextInputPrivate::Command(
2253 QQuickTextInputPrivate::Insert, position + i, insertText.at(i), -1, -1));
2255 d->m_text.replace(position, insertText.size(), insertText);
2256 if (!insertText.isEmpty())
2257 d->m_textDirty =
true;
2258 if (position < d->m_selend && position + insertText.size() > d->m_selstart)
2259 d->m_selDirty =
true;
2261 int remaining = d->m_maxLength - d->m_text.size();
2262 if (remaining != 0) {
2263 insertText = insertText.left(remaining);
2264 d->m_text.insert(position, insertText);
2265 for (
int i = 0; i < insertText.size(); ++i)
2266 d->addCommand(QQuickTextInputPrivate::Command(
2267 QQuickTextInputPrivate::Insert, position + i, insertText.at(i), -1, -1));
2268 if (d->m_cursor >= position)
2269 d->m_cursor += insertText.size();
2270 if (d->m_selstart >= position)
2271 d->m_selstart += insertText.size();
2272 if (d->m_selend >= position)
2273 d->m_selend += insertText.size();
2274 d->m_textDirty =
true;
2275 if (position >= d->m_selstart && position <= d->m_selend)
2276 d->m_selDirty =
true;
2280 d->addCommand(QQuickTextInputPrivate::Command(
2281 QQuickTextInputPrivate::SetSelection, d->m_cursor, u'\0', d->m_selstart, d->m_selend));
2282 d->finishChange(priorState);
2284 if (d->lastSelectionStart != d->lastSelectionEnd) {
2285 if (d->m_selstart != d->lastSelectionStart) {
2286 d->lastSelectionStart = d->m_selstart;
2287 emit selectionStartChanged();
2289 if (d->m_selend != d->lastSelectionEnd) {
2290 d->lastSelectionEnd = d->m_selend;
2291 emit selectionEndChanged();
2297
2298
2299
2300
2302void QQuickTextInput::remove(
int start,
int end)
2304 Q_D(QQuickTextInput);
2306 start = qBound(0, start, d->m_text.size());
2307 end = qBound(0, end, d->m_text.size());
2311 else if (start == end)
2314 if (start < d->m_selend && end > d->m_selstart)
2315 d->m_selDirty =
true;
2317 const int priorState = d->m_undoState;
2319 d->addCommand(QQuickTextInputPrivate::Command(
2320 QQuickTextInputPrivate::SetSelection, d->m_cursor, u'\0', d->m_selstart, d->m_selend));
2322 if (start <= d->m_cursor && d->m_cursor < end) {
2325 for (
int i = d->m_cursor; i >= start; --i) {
2326 d->addCommand(QQuickTextInputPrivate::Command(
2327 QQuickTextInputPrivate::DeleteSelection, i, d->m_text.at(i), -1, 1));
2329 for (
int i = end - 1; i > d->m_cursor; --i) {
2330 d->addCommand(QQuickTextInputPrivate::Command(
2331 QQuickTextInputPrivate::DeleteSelection, i - d->m_cursor + start - 1, d->m_text.at(i), -1, -1));
2334 for (
int i = end - 1; i >= start; --i) {
2335 d->addCommand(QQuickTextInputPrivate::Command(
2336 QQuickTextInputPrivate::RemoveSelection, i, d->m_text.at(i), -1, -1));
2339 if (d->m_maskData) {
2340 d->m_text.replace(start, end - start, d->clearString(start, end - start));
2341 for (
int i = 0; i < end - start; ++i) {
2342 d->addCommand(QQuickTextInputPrivate::Command(
2343 QQuickTextInputPrivate::Insert, start + i, d->m_text.at(start + i), -1, -1));
2346 d->m_text.remove(start, end - start);
2348 if (d->m_cursor > start)
2349 d->m_cursor -= qMin(d->m_cursor, end) - start;
2350 if (d->m_selstart > start)
2351 d->m_selstart -= qMin(d->m_selstart, end) - start;
2352 if (d->m_selend >= end)
2353 d->m_selend -= end - start;
2355 d->addCommand(QQuickTextInputPrivate::Command(
2356 QQuickTextInputPrivate::SetSelection, d->m_cursor, u'\0', d->m_selstart, d->m_selend));
2358 d->m_textDirty =
true;
2359 d->finishChange(priorState);
2361 if (d->lastSelectionStart != d->lastSelectionEnd) {
2362 if (d->m_selstart != d->lastSelectionStart) {
2363 d->lastSelectionStart = d->m_selstart;
2364 emit selectionStartChanged();
2366 if (d->m_selend != d->lastSelectionEnd) {
2367 d->lastSelectionEnd = d->m_selend;
2368 emit selectionEndChanged();
2375
2376
2377
2378
2379void QQuickTextInput::selectWord()
2381 Q_D(QQuickTextInput);
2382 d->selectWordAtPos(d->m_cursor);
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396QString QQuickTextInput::passwordCharacter()
const
2398 Q_D(
const QQuickTextInput);
2399 return QString(d->m_passwordCharacter);
2402void QQuickTextInput::setPasswordCharacter(
const QString &str)
2404 Q_D(QQuickTextInput);
2407 d->m_passwordCharacter = str.constData()[0];
2408 if (d->m_echoMode == Password || d->m_echoMode == PasswordEchoOnEdit)
2409 d->updateDisplayText();
2410 emit passwordCharacterChanged();
2414
2415
2416
2417
2418
2419
2420
2421int QQuickTextInput::passwordMaskDelay()
const
2423 Q_D(
const QQuickTextInput);
2424 return d->m_passwordMaskDelay;
2427void QQuickTextInput::setPasswordMaskDelay(
int delay)
2429 Q_D(QQuickTextInput);
2430 if (d->m_passwordMaskDelay != delay) {
2431 d->m_passwordMaskDelay = delay;
2432 emit passwordMaskDelayChanged(delay);
2436void QQuickTextInput::resetPasswordMaskDelay()
2438 setPasswordMaskDelay(qGuiApp->styleHints()->passwordMaskDelay());
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457QString QQuickTextInput::displayText()
const
2459 Q_D(
const QQuickTextInput);
2460 return d->m_textLayout.text().insert(d->m_textLayout.preeditAreaPosition(), d->m_textLayout.preeditAreaText());
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475QString QQuickTextInput::preeditText()
const
2477 Q_D(
const QQuickTextInput);
2478 return d->m_textLayout.preeditAreaText();
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499bool QQuickTextInput::selectByMouse()
const
2501 Q_D(
const QQuickTextInput);
2502 return d->selectByMouse;
2505void QQuickTextInput::setSelectByMouse(
bool on)
2507 Q_D(QQuickTextInput);
2508 if (d->selectByMouse != on) {
2509 d->selectByMouse = on;
2510 emit selectByMouseChanged(on);
2515
2516
2517
2518
2519
2520
2521
2522
2523
2525QQuickTextInput::SelectionMode QQuickTextInput::mouseSelectionMode()
const
2527 Q_D(
const QQuickTextInput);
2528 return d->mouseSelectionMode;
2531void QQuickTextInput::setMouseSelectionMode(SelectionMode mode)
2533 Q_D(QQuickTextInput);
2534 if (d->mouseSelectionMode != mode) {
2535 d->mouseSelectionMode = mode;
2536 emit mouseSelectionModeChanged(mode);
2541
2542
2543
2544
2545
2547bool QQuickTextInput::persistentSelection()
const
2549 Q_D(
const QQuickTextInput);
2550 return d->persistentSelection;
2553void QQuickTextInput::setPersistentSelection(
bool on)
2555 Q_D(QQuickTextInput);
2556 if (d->persistentSelection == on)
2558 d->persistentSelection = on;
2559 emit persistentSelectionChanged();
2563
2564
2565
2566
2567
2568
2569bool QQuickTextInput::canPaste()
const
2571#if QT_CONFIG(clipboard)
2572 Q_D(
const QQuickTextInput);
2573 if (!d->canPasteValid) {
2574 bool canPaste =
false;
2575 if (!d->m_readOnly) {
2576 if (
const QMimeData *mimeData = QGuiApplication::clipboard()->mimeData())
2577 canPaste = mimeData->hasText();
2579 const_cast<QQuickTextInputPrivate *>(d)->canPaste = canPaste;
2580 const_cast<QQuickTextInputPrivate *>(d)->canPasteValid =
true;
2589
2590
2591
2592
2593
2594
2596bool QQuickTextInput::canUndo()
const
2598 Q_D(
const QQuickTextInput);
2603
2604
2605
2606
2607
2608
2610bool QQuickTextInput::canRedo()
const
2612 Q_D(
const QQuickTextInput);
2617
2618
2619
2620
2621
2622
2624qreal QQuickTextInput::contentWidth()
const
2626 Q_D(
const QQuickTextInput);
2627 return d->contentSize.width();
2631
2632
2633
2634
2635
2636
2638qreal QQuickTextInput::contentHeight()
const
2640 Q_D(
const QQuickTextInput);
2641 return d->contentSize.height();
2644void QQuickTextInput::moveCursorSelection(
int position)
2646 Q_D(QQuickTextInput);
2647 d->moveCursor(position,
true);
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686void QQuickTextInput::moveCursorSelection(
int pos, SelectionMode mode)
2688 Q_D(QQuickTextInput);
2690 if (mode == SelectCharacters) {
2691 d->moveCursor(pos,
true);
2692 }
else if (pos != d->m_cursor) {
2693 const int cursor = d->m_cursor;
2695 if (!d->hasSelectedText())
2696 anchor = d->m_cursor;
2697 else if (d->selectionStart() == d->m_cursor)
2698 anchor = d->selectionEnd();
2700 anchor = d->selectionStart();
2702 if (anchor < pos || (anchor == pos && cursor < pos)) {
2703 const QString text =
this->text();
2704 QTextBoundaryFinder finder(QTextBoundaryFinder::Word, text);
2705 finder.setPosition(anchor);
2707 const QTextBoundaryFinder::BoundaryReasons reasons = finder.boundaryReasons();
2708 if (anchor < text.size() && (reasons == QTextBoundaryFinder::NotAtBoundary
2709 || (reasons & QTextBoundaryFinder::EndOfItem))) {
2710 finder.toPreviousBoundary();
2712 anchor = finder.position() != -1 ? finder.position() : 0;
2714 finder.setPosition(pos);
2715 if (pos > 0 && !finder.boundaryReasons())
2716 finder.toNextBoundary();
2717 const int cursor = finder.position() != -1 ? finder.position() : text.size();
2719 d->setSelection(anchor, cursor - anchor);
2720 }
else if (anchor > pos || (anchor == pos && cursor > pos)) {
2721 const QString text =
this->text();
2722 QTextBoundaryFinder finder(QTextBoundaryFinder::Word, text);
2723 finder.setPosition(anchor);
2725 const QTextBoundaryFinder::BoundaryReasons reasons = finder.boundaryReasons();
2726 if (anchor > 0 && (reasons == QTextBoundaryFinder::NotAtBoundary
2727 || (reasons & QTextBoundaryFinder::StartOfItem))) {
2728 finder.toNextBoundary();
2730 anchor = finder.position() != -1 ? finder.position() : text.size();
2732 finder.setPosition(pos);
2733 if (pos < text.size() && !finder.boundaryReasons())
2734 finder.toPreviousBoundary();
2735 const int cursor = finder.position() != -1 ? finder.position() : 0;
2737 d->setSelection(anchor, cursor - anchor);
2742void QQuickTextInput::focusInEvent(QFocusEvent *event)
2744 Q_D(QQuickTextInput);
2745 d->handleFocusEvent(event);
2746 QQuickImplicitSizeItem::focusInEvent(event);
2749void QQuickTextInputPrivate::handleFocusEvent(QFocusEvent *event)
2751 Q_Q(QQuickTextInput);
2752 bool focus = event->gotFocus();
2754 q->setCursorVisible(focus);
2755 setBlinkingCursorEnabled(focus);
2758 q->q_updateAlignment();
2760 if (focusOnPress && !m_readOnly)
2761 qGuiApp->inputMethod()->show();
2762 q->connect(QGuiApplication::inputMethod(), SIGNAL(inputDirectionChanged(Qt::LayoutDirection)),
2763 q, SLOT(q_updateAlignment()));
2766 if ((m_passwordEchoEditing || m_passwordEchoTimer.isActive())) {
2767 updatePasswordEchoEditing(
false);
2770 if (event->reason() != Qt::ActiveWindowFocusReason
2771 && event->reason() != Qt::PopupFocusReason
2772 && hasSelectedText()
2773 && !persistentSelection)
2776 if (hasAcceptableInput(m_text) == AcceptableInput || fixup())
2777 emit q->editingFinished();
2780 q->disconnect(QGuiApplication::inputMethod(), SIGNAL(inputDirectionChanged(Qt::LayoutDirection)),
2781 q, SLOT(q_updateAlignment()));
2786void QQuickTextInput::focusOutEvent(QFocusEvent *event)
2788 Q_D(QQuickTextInput);
2789 d->handleFocusEvent(event);
2790 QQuickImplicitSizeItem::focusOutEvent(event);
2793void QQuickTextInputPrivate::readOnlyChanged(
bool isReadOnly)
2795 Q_UNUSED(isReadOnly);
2796#if QT_CONFIG(accessibility)
2797 if (QQuickAccessibleAttached *accessibleAttached =
2798 QQuickAccessibleAttached::attachedProperties(q_func()))
2799 accessibleAttached->set_readOnly(isReadOnly);
2803void QQuickTextInputPrivate::echoModeChanged(QQuickTextInput::EchoMode echoMode)
2805#if QT_CONFIG(accessibility)
2806 if (!QAccessible::isActive())
2809 if (QQuickAccessibleAttached *accessibleAttached =
2810 QQuickAccessibleAttached::attachedProperties(q_func()))
2811 accessibleAttached->set_passwordEdit((echoMode == QQuickTextInput::Password
2812 || echoMode == QQuickTextInput::PasswordEchoOnEdit)
2820#if QT_CONFIG(accessibility)
2821void QQuickTextInputPrivate::accessibilityActiveChanged(
bool active)
2826 Q_Q(QQuickTextInput);
2827 QQuickAccessibleAttached *accessibleAttached = qobject_cast<QQuickAccessibleAttached *>(
2828 qmlAttachedPropertiesObject<QQuickAccessibleAttached>(q,
true));
2829 Q_ASSERT(accessibleAttached);
2830 accessibleAttached->setRole(effectiveAccessibleRole());
2831 accessibleAttached->set_readOnly(m_readOnly);
2832 accessibleAttached->set_passwordEdit((m_echoMode == QQuickTextInput::Password
2833 || m_echoMode == QQuickTextInput::PasswordEchoOnEdit)
2838QAccessible::Role QQuickTextInputPrivate::accessibleRole()
const
2840 return QAccessible::EditableText;
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856bool QQuickTextInput::isInputMethodComposing()
const
2861 Q_D(
const QQuickTextInput);
2862 return d->hasImState;
2866QQuickTextInputPrivate::ExtraData::ExtraData()
2872 , explicitTopPadding(
false)
2873 , explicitLeftPadding(
false)
2874 , explicitRightPadding(
false)
2875 , explicitBottomPadding(
false)
2876 , implicitResize(
true)
2880void QQuickTextInputPrivate::init()
2882 Q_Q(QQuickTextInput);
2883#if QT_CONFIG(clipboard)
2884 if (QGuiApplication::clipboard()->supportsSelection())
2885 q->setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton);
2888 q->setAcceptedMouseButtons(Qt::LeftButton);
2891 q->setFlag(QQuickItem::ItemAcceptsInputMethod);
2893 q->setFlag(QQuickItem::ItemHasContents);
2894#if QT_CONFIG(clipboard)
2895 qmlobject_connect(QGuiApplication::clipboard(), QClipboard, SIGNAL(dataChanged()),
2896 q, QQuickTextInput, SLOT(q_canPasteChanged()));
2899 lastSelectionStart = 0;
2900 lastSelectionEnd = 0;
2901 determineHorizontalAlignment();
2903 if (!qmlDisableDistanceField()) {
2904 QTextOption option = m_textLayout.textOption();
2905 option.setUseDesignMetrics(renderType != QQuickTextInput::NativeRendering);
2906 m_textLayout.setTextOption(option);
2909 m_inputControl =
new QInputControl(QInputControl::LineEdit, q);
2910 setSizePolicy(QLayoutPolicy::Expanding, QLayoutPolicy::Fixed);
2912 QObjectPrivate::connect(q, &QQuickTextInput::readOnlyChanged,
this,
2913 &QQuickTextInputPrivate::readOnlyChanged);
2914 QObjectPrivate::connect(q, &QQuickTextInput::echoModeChanged,
this,
2915 &QQuickTextInputPrivate::echoModeChanged);
2918void QQuickTextInputPrivate::cancelInput()
2921 Q_Q(QQuickTextInput);
2922 if (!m_readOnly && q->hasActiveFocus() && qGuiApp)
2927void QQuickTextInput::updateCursorRectangle(
bool scroll)
2929 Q_D(QQuickTextInput);
2930 if (!isComponentComplete())
2934 d->updateHorizontalScroll();
2935 d->updateVerticalScroll();
2937 d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
2940 emit cursorRectangleChanged();
2941 if (d->cursorItem) {
2942 QRectF r = cursorRectangle();
2943 d->cursorItem->setPosition(r.topLeft());
2944 d->cursorItem->setHeight(r.height());
2947 updateInputMethod(Qt::ImCursorRectangle | Qt::ImAnchorRectangle);
2951void QQuickTextInput::selectionChanged()
2953 Q_D(QQuickTextInput);
2954 d->textLayoutDirty =
true;
2955 d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
2958 emit selectedTextChanged();
2960 if (d->lastSelectionStart != d->selectionStart()) {
2961 d->lastSelectionStart = d->selectionStart();
2962 if (d->lastSelectionStart == -1)
2963 d->lastSelectionStart = d->m_cursor;
2964 emit selectionStartChanged();
2966 if (d->lastSelectionEnd != d->selectionEnd()) {
2967 d->lastSelectionEnd = d->selectionEnd();
2968 if (d->lastSelectionEnd == -1)
2969 d->lastSelectionEnd = d->m_cursor;
2970 emit selectionEndChanged();
2974QRectF QQuickTextInput::boundingRect()
const
2976 Q_D(
const QQuickTextInput);
2978 int cursorWidth = d->cursorItem ? 0 : 1;
2980 qreal hscroll = d->hscroll;
2981 if (!d->autoScroll || d->contentSize.width() < width())
2982 hscroll -= QQuickTextUtil::alignedX(d->contentSize.width(), width(), effectiveHAlign());
2985 QRectF r(-hscroll, -d->vscroll, d->contentSize.width(), d->contentSize.height());
2986 r.setRight(r.right() + cursorWidth);
2990QRectF QQuickTextInput::clipRect()
const
2992 Q_D(
const QQuickTextInput);
2994 int cursorWidth = d->cursorItem ? d->cursorItem->width() : 1;
2997 QRectF r = QQuickImplicitSizeItem::clipRect();
2998 r.setRight(r.right() + cursorWidth);
3002void QQuickTextInput::q_canPasteChanged()
3004 Q_D(QQuickTextInput);
3005 bool old = d->canPaste;
3006#if QT_CONFIG(clipboard)
3007 bool canPaste =
false;
3008 if (!d->m_readOnly) {
3009 if (
const QMimeData *mimeData = QGuiApplication::clipboard()->mimeData())
3010 canPaste = mimeData->hasText();
3012 d->canPaste = canPaste;
3015 bool changed = d->canPaste != old || !d->canPasteValid;
3016 d->canPasteValid =
true;
3018 emit canPasteChanged();
3022void QQuickTextInput::q_updateAlignment()
3024 Q_D(QQuickTextInput);
3025 if (d->determineHorizontalAlignment()) {
3027 updateCursorRectangle();
3032
3033
3034
3035
3036
3037void QQuickTextInputPrivate::updateDisplayText(
bool forceUpdate)
3039 QString orig = m_textLayout.text();
3041 if (m_echoMode == QQuickTextInput::NoEcho)
3042 str = QString::fromLatin1(
"");
3046 if (m_echoMode == QQuickTextInput::Password) {
3047 str.fill(m_passwordCharacter);
3048 if (m_passwordEchoTimer.isActive() && m_cursor > 0 && m_cursor <= m_text.size()) {
3049 int cursor = m_cursor - 1;
3050 QChar uc = m_text.at(cursor);
3052 if (cursor > 0 && uc.unicode() >= 0xdc00 && uc.unicode() < 0xe000) {
3055 uc = m_text.at(cursor - 1);
3056 if (uc.unicode() >= 0xd800 && uc.unicode() < 0xdc00)
3057 str[cursor - 1] = uc;
3060 }
else if (m_echoMode == QQuickTextInput::PasswordEchoOnEdit && !m_passwordEchoEditing) {
3061 str.fill(m_passwordCharacter);
3067 QChar* uc = str.data();
3068 for (
int i = 0; i < str.size(); ++i) {
3069 if (uc[i] == QChar::LineSeparator
3070 || uc[i] == QChar::ParagraphSeparator
3071 || uc[i] == QChar::ObjectReplacementCharacter)
3072 uc[i] = QChar(0x0020);
3075 if (str != orig || forceUpdate) {
3076 m_textLayout.setText(str);
3078 emit q_func()->displayTextChanged();
3082qreal QQuickTextInputPrivate::calculateImplicitWidthForText(
const QString &text)
const
3084 Q_Q(
const QQuickTextInput);
3085 QTextLayout layout(text);
3087 QTextOption option = m_textLayout.textOption();
3088 option.setTextDirection(m_layoutDirection);
3089 option.setFlags(QTextOption::IncludeTrailingSpaces);
3090 option.setWrapMode(QTextOption::WrapMode(wrapMode));
3091 option.setAlignment(Qt::Alignment(q->effectiveHAlign()));
3092 layout.setTextOption(option);
3093 layout.setFont(font);
3095 layout.setPreeditArea(m_textLayout.preeditAreaPosition(), m_textLayout.preeditAreaText());
3097 layout.beginLayout();
3099 QTextLine line = layout.createLine();
3100 line.setLineWidth(qreal(INT_MAX));
3101 const qreal theImplicitWidth = qCeil(line.naturalTextWidth()) + q->leftPadding() + q->rightPadding();
3104 return theImplicitWidth;
3107qreal QQuickTextInputPrivate::getImplicitWidth()
const
3109 Q_Q(
const QQuickTextInput);
3110 if (!requireImplicitWidth) {
3111 QQuickTextInputPrivate *d =
const_cast<QQuickTextInputPrivate *>(
this);
3112 d->requireImplicitWidth =
true;
3114 if (q->isComponentComplete())
3115 d->implicitWidth = calculateImplicitWidthForText(m_text);
3117 return implicitWidth;
3120void QQuickTextInputPrivate::setTopPadding(qreal value,
bool reset)
3122 Q_Q(QQuickTextInput);
3123 qreal oldPadding = q->topPadding();
3124 if (!reset || extra.isAllocated()) {
3125 extra.value().topPadding = value;
3126 extra.value().explicitTopPadding = !reset;
3128 if ((!reset && !qFuzzyCompare(oldPadding, value)) || (reset && !qFuzzyCompare(oldPadding, padding()))) {
3130 q->updateCursorRectangle();
3131 emit q->topPaddingChanged();
3135void QQuickTextInputPrivate::setLeftPadding(qreal value,
bool reset)
3137 Q_Q(QQuickTextInput);
3138 qreal oldPadding = q->leftPadding();
3139 if (!reset || extra.isAllocated()) {
3140 extra.value().leftPadding = value;
3141 extra.value().explicitLeftPadding = !reset;
3143 if ((!reset && !qFuzzyCompare(oldPadding, value)) || (reset && !qFuzzyCompare(oldPadding, padding()))) {
3145 q->updateCursorRectangle();
3146 emit q->leftPaddingChanged();
3150void QQuickTextInputPrivate::setRightPadding(qreal value,
bool reset)
3152 Q_Q(QQuickTextInput);
3153 qreal oldPadding = q->rightPadding();
3154 if (!reset || extra.isAllocated()) {
3155 extra.value().rightPadding = value;
3156 extra.value().explicitRightPadding = !reset;
3158 if ((!reset && !qFuzzyCompare(oldPadding, value)) || (reset && !qFuzzyCompare(oldPadding, padding()))) {
3160 q->updateCursorRectangle();
3161 emit q->rightPaddingChanged();
3165void QQuickTextInputPrivate::setBottomPadding(qreal value,
bool reset)
3167 Q_Q(QQuickTextInput);
3168 qreal oldPadding = q->bottomPadding();
3169 if (!reset || extra.isAllocated()) {
3170 extra.value().bottomPadding = value;
3171 extra.value().explicitBottomPadding = !reset;
3173 if ((!reset && !qFuzzyCompare(oldPadding, value)) || (reset && !qFuzzyCompare(oldPadding, padding()))) {
3175 q->updateCursorRectangle();
3176 emit q->bottomPaddingChanged();
3180bool QQuickTextInputPrivate::isImplicitResizeEnabled()
const
3182 return !extra.isAllocated() || extra->implicitResize;
3185void QQuickTextInputPrivate::setImplicitResizeEnabled(
bool enabled)
3188 extra.value().implicitResize =
false;
3189 else if (extra.isAllocated())
3190 extra->implicitResize =
true;
3193void QQuickTextInputPrivate::updateLayout()
3195 Q_Q(QQuickTextInput);
3197 if (!q->isComponentComplete())
3201 QTextOption option = m_textLayout.textOption();
3202 option.setTextDirection(layoutDirection());
3203 option.setWrapMode(QTextOption::WrapMode(wrapMode));
3204 option.setAlignment(Qt::Alignment(q->effectiveHAlign()));
3205 if (!qmlDisableDistanceField())
3206 option.setUseDesignMetrics(renderType != QQuickTextInput::NativeRendering);
3208 m_textLayout.setTextOption(option);
3209 m_textLayout.setFont(font);
3211 m_textLayout.beginLayout();
3213 QTextLine line = m_textLayout.createLine();
3214 if (requireImplicitWidth) {
3215 line.setLineWidth(qreal(INT_MAX));
3216 const bool wasInLayout = inLayout;
3218 if (isImplicitResizeEnabled())
3219 q->setImplicitWidth(qCeil(line.naturalTextWidth()) + q->leftPadding() + q->rightPadding());
3220 inLayout = wasInLayout;
3224 qreal lineWidth = q->widthValid() || !isImplicitResizeEnabled() ? q->width() - q->leftPadding() - q->rightPadding() : qreal(INT_MAX);
3228 line.setLineWidth(lineWidth);
3229 line.setPosition(QPointF(0, height));
3231 height += line.height();
3232 width = qMax(width, line.naturalTextWidth());
3234 line = m_textLayout.createLine();
3235 }
while (line.isValid());
3236 m_textLayout.endLayout();
3238 option.setWrapMode(QTextOption::NoWrap);
3239 m_textLayout.setTextOption(option);
3241 textLayoutDirty =
true;
3243 const QSizeF previousSize = contentSize;
3244 contentSize = QSizeF(width, height);
3246 updateType = UpdatePaintNode;
3250 if (isImplicitResizeEnabled()) {
3251 if (!requireImplicitWidth && !q->widthValid())
3252 q->setImplicitSize(width + q->leftPadding() + q->rightPadding(), height + q->topPadding() + q->bottomPadding());
3254 q->setImplicitHeight(height + q->topPadding() + q->bottomPadding());
3257 updateBaselineOffset();
3259 if (previousSize != contentSize)
3260 emit q->contentSizeChanged();
3264
3265
3266
3267
3268
3269void QQuickTextInputPrivate::updateBaselineOffset()
3271 Q_Q(QQuickTextInput);
3272 if (!q->isComponentComplete())
3274 QFontMetricsF fm(font);
3276 if (q->heightValid()) {
3277 const qreal surplusHeight = q->height() - contentSize.height() - q->topPadding() - q->bottomPadding();
3278 if (vAlign == QQuickTextInput::AlignBottom)
3279 yoff = surplusHeight;
3280 else if (vAlign == QQuickTextInput::AlignVCenter)
3281 yoff = surplusHeight/2;
3284 if (m_textLayout.lineCount() > 0) {
3285 QTextLine line = m_textLayout.lineAt(0);
3286 ascent = line.y() + line.ascent();
3288 ascent = fm.ascent();
3290 q->setBaselineOffset(ascent + yoff + q->topPadding());
3293#if QT_CONFIG(clipboard)
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304void QQuickTextInputPrivate::copy(QClipboard::Mode mode)
const
3306 QString t = selectedText();
3307 if (!t.isEmpty() && m_echoMode == QQuickTextInput::Normal) {
3308 QGuiApplication::clipboard()->setText(t, mode);
3313
3314
3315
3316
3317
3318
3319
3320void QQuickTextInputPrivate::paste(QClipboard::Mode clipboardMode)
3322 QString clip = QGuiApplication::clipboard()->text(clipboardMode);
3323 if (!clip.isEmpty() || hasSelectedText()) {
3334
3335
3336void QQuickTextInputPrivate::commitPreedit()
3338 Q_Q(QQuickTextInput);
3343 QGuiApplication::inputMethod()->commit();
3348 QInputMethodEvent ev;
3349 QCoreApplication::sendEvent(q, &ev);
3352void QQuickTextInputPrivate::cancelPreedit()
3354 Q_Q(QQuickTextInput);
3359 QGuiApplication::inputMethod()->reset();
3361 QInputMethodEvent ev;
3362 QCoreApplication::sendEvent(q, &ev);
3367
3368
3369
3370
3371
3372
3373
3374
3375void QQuickTextInputPrivate::backspace()
3377 int priorState = m_undoState;
3378 if (separateSelection()) {
3379 removeSelectedText();
3380 }
else if (m_cursor) {
3383 m_cursor = prevMaskBlank(m_cursor);
3384 QChar uc = m_text.at(m_cursor);
3385 if (m_cursor > 0 && uc.unicode() >= 0xdc00 && uc.unicode() < 0xe000) {
3388 uc = m_text.at(m_cursor - 1);
3389 if (uc.unicode() >= 0xd800 && uc.unicode() < 0xdc00) {
3390 internalDelete(
true);
3394 internalDelete(
true);
3396 finishChange(priorState);
3400
3401
3402
3403
3404
3405
3406
3407
3408void QQuickTextInputPrivate::del()
3410 int priorState = m_undoState;
3411 if (separateSelection()) {
3412 removeSelectedText();
3414 int n = m_textLayout.nextCursorPosition(m_cursor) - m_cursor;
3418 finishChange(priorState);
3422
3423
3424
3425
3426
3427
3428void QQuickTextInputPrivate::insert(
const QString &newText)
3430 int priorState = m_undoState;
3431 if (separateSelection())
3432 removeSelectedText();
3433 internalInsert(newText);
3434 finishChange(priorState);
3438
3439
3440
3441
3442void QQuickTextInputPrivate::clear()
3444 int priorState = m_undoState;
3445 separateSelection();
3447 m_selend = m_text.size();
3448 removeSelectedText();
3450 finishChange(priorState,
false,
false);
3454
3455
3456
3457
3458
3459
3460
3461void QQuickTextInputPrivate::setSelection(
int start,
int length)
3463 Q_Q(QQuickTextInput);
3468 if (start < 0 || start > m_text.size()) {
3469 qWarning(
"QQuickTextInputPrivate::setSelection: Invalid start position");
3474 if (start == m_selstart && start + length == m_selend && m_cursor == m_selend)
3477 m_selend = qMin(start + length, m_text.size());
3478 m_cursor = m_selend;
3479 }
else if (length < 0) {
3480 if (start == m_selend && start + length == m_selstart && m_cursor == m_selstart)
3482 m_selstart = qMax(start + length, 0);
3484 m_cursor = m_selstart;
3485 }
else if (m_selstart != m_selend) {
3491 emitCursorPositionChanged();
3494 emit q->selectionChanged();
3495 emitCursorPositionChanged();
3497 q->updateInputMethod(Qt::ImCursorRectangle | Qt::ImAnchorRectangle | Qt::ImCursorPosition | Qt::ImAnchorPosition
3498 | Qt::ImCurrentSelection);
3503
3504
3505
3506
3507
3508
3509
3510void QQuickTextInputPrivate::updatePasswordEchoEditing(
bool editing)
3512 cancelPasswordEchoTimer();
3513 m_passwordEchoEditing = editing;
3514 updateDisplayText();
3518
3519
3520
3521
3522
3523
3524bool QQuickTextInputPrivate::fixup()
3526#if QT_CONFIG(validator)
3528 QString textCopy = m_text;
3529 int cursorCopy = m_cursor;
3530 m_validator->fixup(textCopy);
3531 if (m_validator->validate(textCopy, cursorCopy) == QValidator::Acceptable) {
3532 if (textCopy != m_text || cursorCopy != m_cursor)
3533 internalSetText(textCopy, cursorCopy);
3542
3543
3544
3545
3546
3547void QQuickTextInputPrivate::moveCursor(
int pos,
bool mark)
3549 Q_Q(QQuickTextInput);
3554 if (pos != m_cursor) {
3557 pos = pos > m_cursor ? nextMaskBlank(pos) : prevMaskBlank(pos);
3561 if (m_selend > m_selstart && m_cursor == m_selstart)
3563 else if (m_selend > m_selstart && m_cursor == m_selend)
3564 anchor = m_selstart;
3567 m_selstart = qMin(anchor, pos);
3568 m_selend = qMax(anchor, pos);
3573 if (mark || m_selDirty) {
3575 emit q->selectionChanged();
3577 emitCursorPositionChanged();
3579 q->updateInputMethod();
3585
3586
3587
3588
3589
3590void QQuickTextInputPrivate::processInputMethodEvent(QInputMethodEvent *event)
3592 Q_Q(QQuickTextInput);
3594 int priorState = -1;
3595 bool isGettingInput = !event->commitString().isEmpty()
3596 || event->preeditString() != preeditAreaText()
3597 || event->replacementLength() > 0;
3598 bool cursorPositionChanged =
false;
3599 bool selectionChange =
false;
3600 m_preeditDirty = event->preeditString() != preeditAreaText();
3602 if (isGettingInput) {
3604 priorState = m_undoState;
3605 separateSelection();
3606 if (m_echoMode == QQuickTextInput::PasswordEchoOnEdit && !m_passwordEchoEditing) {
3607 updatePasswordEchoEditing(
true);
3609 m_selend = m_text.size();
3611 removeSelectedText();
3615 if (event->replacementStart() <= 0)
3616 c += event->commitString().size() - qMin(-event->replacementStart(), event->replacementLength());
3618 int cursorInsertPos = m_cursor + event->replacementStart();
3619 if (cursorInsertPos < 0)
3620 cursorInsertPos = 0;
3623 if (event->replacementLength()) {
3624 m_selstart = cursorInsertPos;
3625 m_selend = m_selstart + event->replacementLength();
3626 m_selend = qMin(m_selend, m_text.size());
3627 removeSelectedText();
3629 m_cursor = cursorInsertPos;
3631 if (!event->commitString().isEmpty()) {
3632 internalInsert(event->commitString());
3633 cursorPositionChanged =
true;
3635 m_cursor = qBound(0, c, m_text.size());
3638 for (
int i = 0; i < event->attributes().size(); ++i) {
3639 const QInputMethodEvent::Attribute &a = event->attributes().at(i);
3640 if (a.type == QInputMethodEvent::Selection) {
3647 if (!cursorPositionChanged || !m_maskData)
3648 m_cursor = qBound(0, a.start + a.length, m_text.size());
3650 m_selstart = qMax(0, qMin(a.start, m_text.size()));
3651 m_selend = m_cursor;
3652 if (m_selend < m_selstart) {
3653 qSwap(m_selstart, m_selend);
3655 selectionChange =
true;
3657 selectionChange = m_selstart != m_selend;
3658 m_selstart = m_selend = 0;
3660 cursorPositionChanged =
true;
3663 QString oldPreeditString = m_textLayout.preeditAreaText();
3664 m_textLayout.setPreeditArea(m_cursor, event->preeditString());
3665 if (oldPreeditString != m_textLayout.preeditAreaText()) {
3666 emit q->preeditTextChanged();
3667 if (!event->preeditString().isEmpty() && m_undoPreeditState == -1)
3669 m_undoPreeditState = priorState;
3671 const int oldPreeditCursor = m_preeditCursor;
3672 m_preeditCursor = event->preeditString().size();
3673 hasImState = !event->preeditString().isEmpty();
3674 bool cursorVisible =
true;
3675 QList<QTextLayout::FormatRange> formats;
3676 for (
int i = 0; i < event->attributes().size(); ++i) {
3677 const QInputMethodEvent::Attribute &a = event->attributes().at(i);
3678 if (a.type == QInputMethodEvent::Cursor) {
3680 m_preeditCursor = a.start;
3681 cursorVisible = a.length != 0;
3682 }
else if (a.type == QInputMethodEvent::TextFormat) {
3684 QTextCharFormat f = qvariant_cast<QTextFormat>(a.value).toCharFormat();
3686 QTextLayout::FormatRange o;
3687 o.start = a.start + m_cursor;
3688 o.length = a.length;
3694 m_textLayout.setFormats(formats);
3700 q->setCursorVisible(cursorVisible);
3702 updateDisplayText(
true);
3703 if (cursorPositionChanged && emitCursorPositionChanged())
3704 q->updateInputMethod(Qt::ImCursorPosition | Qt::ImAnchorPosition);
3705 else if (m_preeditCursor != oldPreeditCursor || isGettingInput)
3706 q->updateCursorRectangle();
3709 finishChange(priorState);
3711 if (selectionChange) {
3712 emit q->selectionChanged();
3713 q->updateInputMethod(Qt::ImCursorRectangle | Qt::ImAnchorRectangle
3714 | Qt::ImCurrentSelection);
3718 if (event->preeditString().isEmpty())
3719 m_undoPreeditState = -1;
3725
3726
3727
3728
3729
3730
3731void QQuickTextInputPrivate::selectWordAtPos(
int cursor)
3733 int next = cursor + 1;
3736 int c = m_textLayout.previousCursorPosition(next, QTextLayout::SkipWords);
3737 moveCursor(c,
false);
3739 int end = m_textLayout.nextCursorPosition(c, QTextLayout::SkipWords);
3740 while (end > cursor && m_text.at(end - 1).isSpace())
3742 moveCursor(end,
true);
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757bool QQuickTextInputPrivate::finishChange(
int validateFromState,
bool update,
bool edited)
3759 Q_Q(QQuickTextInput);
3763 bool inputMethodAttributesChanged = m_textDirty || m_selDirty;
3765 bool alignmentChanged =
false;
3766 bool textChanged =
false;
3770 bool wasValidInput = m_validInput;
3771 bool wasAcceptable = m_acceptableInput;
3772 m_validInput =
true;
3773 m_acceptableInput =
true;
3774#if QT_CONFIG(validator)
3776 QString textCopy = m_text;
3778 textCopy = maskString(0, m_text,
true);
3779 int cursorCopy = m_cursor;
3780 QValidator::State state = m_validator->validate(textCopy, cursorCopy);
3783 m_validInput = state != QValidator::Invalid;
3784 m_acceptableInput = state == QValidator::Acceptable;
3785 if (m_validInput && !m_maskData) {
3786 if (m_text != textCopy) {
3787 internalSetText(textCopy, cursorCopy);
3790 m_cursor = cursorCopy;
3800 if (m_undoPreeditState >= 0 && (m_undoPreeditState < validateFromState || validateFromState < 0))
3801 validateFromState = m_undoPreeditState;
3803 if (validateFromState >= 0 && wasValidInput && !m_validInput) {
3804 if (m_transactions.size())
3806 internalUndo(validateFromState);
3807 m_history.resize(m_undoState);
3808 m_validInput =
true;
3809 m_acceptableInput = wasAcceptable;
3810 m_textDirty =
false;
3815 m_textDirty =
false;
3817 m_preeditDirty =
false;
3819 alignmentChanged = determineHorizontalAlignment();
3821 emit q->textEdited();
3822 emit q->textChanged();
3825 updateDisplayText(alignmentChanged);
3827 if (m_acceptableInput != wasAcceptable)
3828 emit q->acceptableInputChanged();
3831 if (m_preeditDirty) {
3832 m_preeditDirty =
false;
3833 if (determineHorizontalAlignment()) {
3834 alignmentChanged =
true;
3842 emit q->selectionChanged();
3846 inputMethodAttributesChanged |= (m_cursor != m_lastCursorPos);
3847 if (inputMethodAttributesChanged)
3848 q->updateInputMethod();
3850 emitUndoRedoChanged();
3852 if (!emitCursorPositionChanged() && (alignmentChanged || textChanged))
3853 q->updateCursorRectangle();
3859
3860
3861
3862
3863void QQuickTextInputPrivate::internalSetText(
const QString &txt,
int pos,
bool edited)
3866 QString oldText = m_text;
3868 m_text = maskString(0, txt,
true);
3869 m_text += clearString(m_text.size(), m_maxLength - m_text.size());
3871 m_text = txt.isEmpty() ? txt : txt.left(m_maxLength);
3876 m_undoPreeditState = -1;
3878 m_cursor = (pos < 0 || pos > m_text.size()) ? m_text.size() : pos;
3879 m_textDirty = (oldText != m_text);
3881 bool changed = finishChange(-1,
true, edited);
3882#if !QT_CONFIG(accessibility)
3885 Q_Q(QQuickTextInput);
3886 if (changed && QAccessible::isActive()) {
3887 if (QObject *acc = QQuickAccessibleAttached::findAccessible(q, QAccessible::EditableText)) {
3888 QAccessibleTextUpdateEvent ev(acc, 0, oldText, m_text);
3889 QAccessible::updateAccessibility(&ev);
3897
3898
3899
3900
3901
3902void QQuickTextInputPrivate::addCommand(
const Command &cmd)
3904 if (m_separator && m_undoState && m_history[m_undoState - 1].type != Separator) {
3905 m_history.resize(m_undoState + 2);
3906 m_history[m_undoState++] = Command(Separator, m_cursor, u'\0', m_selstart, m_selend);
3908 m_history.resize(m_undoState + 1);
3910 m_separator =
false;
3911 m_history[m_undoState++] = cmd;
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924void QQuickTextInputPrivate::internalInsert(
const QString &s)
3926 Q_Q(QQuickTextInput);
3927 if (m_echoMode == QQuickTextInput::Password) {
3928 if (m_passwordMaskDelay > 0)
3929 m_passwordEchoTimer.start(m_passwordMaskDelay, q);
3931 Q_ASSERT(!hasSelectedText());
3933 QString ms = maskString(m_cursor, s);
3934 for (
int i = 0; i < ms.size(); ++i) {
3935 addCommand (Command(DeleteSelection, m_cursor + i, m_text.at(m_cursor + i), -1, -1));
3936 addCommand(Command(Insert, m_cursor + i, ms.at(i), -1, -1));
3938 m_text.replace(m_cursor, ms.size(), ms);
3939 m_cursor += ms.size();
3940 m_cursor = nextMaskBlank(m_cursor);
3943 int remaining = m_maxLength - m_text.size();
3944 if (remaining != 0) {
3945 const QStringView remainingStr = QStringView{s}.left(remaining);
3946 m_text.insert(m_cursor, remainingStr);
3947 for (
auto e : remainingStr)
3948 addCommand(Command(Insert, m_cursor++, e, -1, -1));
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965void QQuickTextInputPrivate::internalDelete(
bool wasBackspace)
3967 if (m_cursor < m_text.size()) {
3968 cancelPasswordEchoTimer();
3969 Q_ASSERT(!hasSelectedText());
3970 addCommand(Command((CommandType)((m_maskData ? 2 : 0) + (wasBackspace ? Remove : Delete)),
3971 m_cursor, m_text.at(m_cursor), -1, -1));
3973 m_text.replace(m_cursor, 1, clearString(m_cursor, 1));
3974 addCommand(Command(Insert, m_cursor, m_text.at(m_cursor), -1, -1));
3976 m_text.remove(m_cursor, 1);
3983
3984
3985
3986
3987
3988
3989
3990
3991void QQuickTextInputPrivate::removeSelectedText()
3993 if (m_selstart < m_selend && m_selend <= m_text.size()) {
3994 cancelPasswordEchoTimer();
3996 if (m_selstart <= m_cursor && m_cursor < m_selend) {
3999 for (i = m_cursor; i >= m_selstart; --i)
4000 addCommand (Command(DeleteSelection, i, m_text.at(i), -1, 1));
4001 for (i = m_selend - 1; i > m_cursor; --i)
4002 addCommand (Command(DeleteSelection, i - m_cursor + m_selstart - 1, m_text.at(i), -1, -1));
4004 for (i = m_selend-1; i >= m_selstart; --i)
4005 addCommand (Command(RemoveSelection, i, m_text.at(i), -1, -1));
4008 m_text.replace(m_selstart, m_selend - m_selstart, clearString(m_selstart, m_selend - m_selstart));
4009 for (
int i = 0; i < m_selend - m_selstart; ++i)
4010 addCommand(Command(Insert, m_selstart + i, m_text.at(m_selstart + i), -1, -1));
4012 m_text.remove(m_selstart, m_selend - m_selstart);
4014 if (m_cursor > m_selstart)
4015 m_cursor -= qMin(m_cursor, m_selend) - m_selstart;
4022
4023
4024
4025
4026
4027
4029bool QQuickTextInputPrivate::separateSelection()
4031 if (hasSelectedText()) {
4033 addCommand(Command(SetSelection, m_cursor, u'\0', m_selstart, m_selend));
4041
4042
4043
4044
4045
4046void QQuickTextInputPrivate::parseInputMask(
const QString &maskFields)
4048 int delimiter = maskFields.indexOf(QLatin1Char(
';'));
4049 if (maskFields.isEmpty() || delimiter == 0) {
4051 m_maskData.reset(
nullptr);
4052 m_maxLength = 32767;
4053 internalSetText(QString());
4058 if (delimiter == -1) {
4059 m_blank = QLatin1Char(
' ');
4060 m_inputMask = maskFields;
4062 m_inputMask = maskFields.left(delimiter);
4063 m_blank = (delimiter + 1 < maskFields.size()) ? maskFields[delimiter + 1] : QLatin1Char(
' ');
4069 for (
int i=0; i<m_inputMask.size(); i++) {
4070 c = m_inputMask.at(i);
4071 if (i > 0 && m_inputMask.at(i-1) == QLatin1Char(
'\\')) {
4075 if (c != QLatin1Char(
'\\') && c != QLatin1Char(
'!') &&
4076 c != QLatin1Char(
'<') && c != QLatin1Char(
'>') &&
4077 c != QLatin1Char(
'{') && c != QLatin1Char(
'}') &&
4078 c != QLatin1Char(
'[') && c != QLatin1Char(
']'))
4082 m_maskData.reset(
new MaskInputData[m_maxLength]);
4084 MaskInputData::Casemode m = MaskInputData::NoCaseMode;
4087 bool escape =
false;
4089 for (
int i = 0; i < m_inputMask.size(); i++) {
4090 c = m_inputMask.at(i);
4093 m_maskData[index].maskChar = c;
4094 m_maskData[index].separator = s;
4095 m_maskData[index].caseMode = m;
4098 }
else if (c == QLatin1Char(
'<')) {
4099 m = MaskInputData::Lower;
4100 }
else if (c == QLatin1Char(
'>')) {
4101 m = MaskInputData::Upper;
4102 }
else if (c == QLatin1Char(
'!')) {
4103 m = MaskInputData::NoCaseMode;
4104 }
else if (c != QLatin1Char(
'{') && c != QLatin1Char(
'}') && c != QLatin1Char(
'[') && c != QLatin1Char(
']')) {
4105 switch (c.unicode()) {
4132 m_maskData[index].maskChar = c;
4133 m_maskData[index].separator = s;
4134 m_maskData[index].caseMode = m;
4139 internalSetText(m_text);
4144
4145
4146
4147
4148bool QQuickTextInputPrivate::isValidInput(QChar key, QChar mask)
const
4150 switch (mask.unicode()) {
4156 if (key.isLetter() || key == m_blank)
4160 if (key.isLetterOrNumber())
4164 if (key.isLetterOrNumber() || key == m_blank)
4168 if (key.isPrint() && key != m_blank)
4172 if (key.isPrint() || key == m_blank)
4180 if (key.isNumber() || key == m_blank)
4184 if (key.isNumber() && key.digitValue() > 0)
4188 if ((key.isNumber() && key.digitValue() > 0) || key == m_blank)
4192 if (key.isNumber() || key == QLatin1Char(
'+') || key == QLatin1Char(
'-') || key == m_blank)
4196 if (key == QLatin1Char(
'0') || key == QLatin1Char(
'1'))
4200 if (key == QLatin1Char(
'0') || key == QLatin1Char(
'1') || key == m_blank)
4204 if (key.isNumber() || (key >= QLatin1Char(
'a') && key <= QLatin1Char(
'f')) || (key >= QLatin1Char(
'A') && key <= QLatin1Char(
'F')))
4208 if (key.isNumber() || (key >= QLatin1Char(
'a') && key <= QLatin1Char(
'f')) || (key >= QLatin1Char(
'A') && key <= QLatin1Char(
'F')) || key == m_blank)
4218
4219
4220
4221
4222
4223
4224
4225QQuickTextInputPrivate::ValidatorState QQuickTextInputPrivate::hasAcceptableInput(
const QString &str)
const
4227#if QT_CONFIG(validator)
4228 QString textCopy = str;
4229 int cursorCopy = m_cursor;
4231 QValidator::State state = m_validator->validate(textCopy, cursorCopy);
4232 if (state != QValidator::Acceptable)
4233 return ValidatorState(state);
4238 return AcceptableInput;
4240 if (str.size() != m_maxLength)
4241 return InvalidInput;
4243 for (
int i=0; i < m_maxLength; ++i) {
4244 if (m_maskData[i].separator) {
4245 if (str.at(i) != m_maskData[i].maskChar)
4246 return InvalidInput;
4248 if (!isValidInput(str.at(i), m_maskData[i].maskChar))
4249 return InvalidInput;
4252 return AcceptableInput;
4256
4257
4258
4259
4260
4261
4262
4263QString QQuickTextInputPrivate::maskString(uint pos,
const QString &str,
bool clear)
const
4265 if (pos >= (uint)m_maxLength)
4266 return QString::fromLatin1(
"");
4269 fill = clear ? clearString(0, m_maxLength) : m_text;
4272 QString s = QString::fromLatin1(
"");
4274 while (i < m_maxLength) {
4275 if (strIndex < str.size()) {
4276 if (m_maskData[i].separator) {
4277 s += m_maskData[i].maskChar;
4278 if (str[strIndex] == m_maskData[i].maskChar)
4282 if (isValidInput(str[strIndex], m_maskData[i].maskChar)) {
4283 switch (m_maskData[i].caseMode) {
4284 case MaskInputData::Upper:
4285 s += str[strIndex].toUpper();
4287 case MaskInputData::Lower:
4288 s += str[strIndex].toLower();
4296 int n = findInMask(i,
true,
true, str[strIndex]);
4298 if (str.size() != 1 || i == 0 || (i > 0 && (!m_maskData[i-1].separator || m_maskData[i-1].maskChar != str[strIndex]))) {
4299 s += QStringView{fill}.mid(i, n-i+1);
4304 n = findInMask(i,
true,
false, str[strIndex]);
4306 s += QStringView{fill}.mid(i, n-i);
4307 switch (m_maskData[n].caseMode) {
4308 case MaskInputData::Upper:
4309 s += str[strIndex].toUpper();
4311 case MaskInputData::Lower:
4312 s += str[strIndex].toLower();
4333
4334
4335
4336
4337
4338QString QQuickTextInputPrivate::clearString(uint pos, uint len)
const
4340 if (pos >= (uint)m_maxLength)
4344 int end = qMin((uint)m_maxLength, pos + len);
4345 for (
int i = pos; i < end; ++i)
4346 if (m_maskData[i].separator)
4347 s += m_maskData[i].maskChar;
4355
4356
4357
4358
4359
4360QString QQuickTextInputPrivate::stripString(
const QString &str)
const
4366 int end = qMin(m_maxLength, str.size());
4367 for (
int i = 0; i < end; ++i) {
4368 if (m_maskData[i].separator)
4369 s += m_maskData[i].maskChar;
4370 else if (str[i] != m_blank)
4378
4379
4380
4381int QQuickTextInputPrivate::findInMask(
int pos,
bool forward,
bool findSeparator, QChar searchChar)
const
4383 if (pos >= m_maxLength || pos < 0)
4386 int end = forward ? m_maxLength : -1;
4387 int step = forward ? 1 : -1;
4391 if (findSeparator) {
4392 if (m_maskData[i].separator && m_maskData[i].maskChar == searchChar)
4395 if (!m_maskData[i].separator) {
4396 if (searchChar.isNull())
4398 else if (isValidInput(searchChar, m_maskData[i].maskChar))
4407void QQuickTextInputPrivate::internalUndo(
int until)
4409 if (!isUndoAvailable())
4411 cancelPasswordEchoTimer();
4413 while (m_undoState && m_undoState > until) {
4414 Command& cmd = m_history[--m_undoState];
4417 m_text.remove(cmd.pos, 1);
4421 m_selstart = cmd.selStart;
4422 m_selend = cmd.selEnd;
4426 case RemoveSelection:
4427 m_text.insert(cmd.pos, cmd.uc);
4428 m_cursor = cmd.pos + 1;
4431 case DeleteSelection:
4432 m_text.insert(cmd.pos, cmd.uc);
4438 if (until < 0 && m_undoState) {
4439 Command& next = m_history[m_undoState-1];
4440 if (next.type != cmd.type
4441 && next.type < RemoveSelection
4442 && (cmd.type < RemoveSelection || next.type == Separator)) {
4451void QQuickTextInputPrivate::internalRedo()
4453 if (!isRedoAvailable())
4456 while (m_undoState < m_history.size()) {
4457 Command& cmd = m_history[m_undoState++];
4460 m_text.insert(cmd.pos, cmd.uc);
4461 m_cursor = cmd.pos + 1;
4464 m_selstart = cmd.selStart;
4465 m_selend = cmd.selEnd;
4470 case RemoveSelection:
4471 case DeleteSelection:
4472 m_text.remove(cmd.pos, 1);
4473 m_selstart = cmd.selStart;
4474 m_selend = cmd.selEnd;
4478 m_selstart = cmd.selStart;
4479 m_selend = cmd.selEnd;
4483 if (m_undoState < m_history.size()) {
4484 Command& next = m_history[m_undoState];
4485 if (next.type != cmd.type
4486 && cmd.type < RemoveSelection
4487 && next.type != Separator
4488 && (next.type < RemoveSelection || cmd.type == Separator)) {
4496void QQuickTextInputPrivate::emitUndoRedoChanged()
4498 Q_Q(QQuickTextInput);
4499 const bool previousUndo = canUndo;
4500 const bool previousRedo = canRedo;
4502 canUndo = isUndoAvailable();
4503 canRedo = isRedoAvailable();
4505 if (previousUndo != canUndo)
4506 emit q->canUndoChanged();
4507 if (previousRedo != canRedo)
4508 emit q->canRedoChanged();
4512
4513
4514
4515
4516
4517bool QQuickTextInputPrivate::emitCursorPositionChanged()
4519 Q_Q(QQuickTextInput);
4520 if (m_cursor != m_lastCursorPos) {
4521 m_lastCursorPos = m_cursor;
4523 q->updateCursorRectangle();
4524 emit q->cursorPositionChanged();
4526 if (!hasSelectedText()) {
4527 if (lastSelectionStart != m_cursor) {
4528 lastSelectionStart = m_cursor;
4529 emit q->selectionStartChanged();
4531 if (lastSelectionEnd != m_cursor) {
4532 lastSelectionEnd = m_cursor;
4533 emit q->selectionEndChanged();
4537#if QT_CONFIG(accessibility)
4538 if (QAccessible::isActive()) {
4539 if (QObject *acc = QQuickAccessibleAttached::findAccessible(q, QAccessible::EditableText)) {
4540 QAccessibleTextCursorEvent ev(acc, m_cursor);
4541 QAccessible::updateAccessibility(&ev);
4552void QQuickTextInputPrivate::setBlinkingCursorEnabled(
bool enable)
4554 if (enable == m_blinkEnabled)
4557 m_blinkEnabled = enable;
4558 updateCursorBlinking();
4561 connect(qApp->styleHints(), &QStyleHints::cursorFlashTimeChanged,
this, &QQuickTextInputPrivate::updateCursorBlinking);
4563 disconnect(qApp->styleHints(), &QStyleHints::cursorFlashTimeChanged,
this, &QQuickTextInputPrivate::updateCursorBlinking);
4566void QQuickTextInputPrivate::updateCursorBlinking()
4568 Q_Q(QQuickTextInput);
4571 q->killTimer(m_blinkTimer);
4575 if (m_blinkEnabled && cursorVisible && !cursorItem && !m_readOnly) {
4576 int flashTime = QGuiApplication::styleHints()->cursorFlashTime();
4578 m_blinkTimer = q->startTimer(flashTime / 2);
4582 updateType = UpdatePaintNode;
4587void QQuickTextInput::timerEvent(QTimerEvent *event)
4589 Q_D(QQuickTextInput);
4590 if (event->timerId() == d->m_blinkTimer) {
4591 d->m_blinkStatus = !d->m_blinkStatus;
4592 d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
4595 }
else if (event->timerId() == d->m_passwordEchoTimer.timerId()) {
4596 d->m_passwordEchoTimer.stop();
4597 d->updateDisplayText();
4598 updateCursorRectangle();
4602void QQuickTextInputPrivate::processKeyEvent(QKeyEvent* event)
4604 Q_Q(QQuickTextInput);
4606 if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
4607 if (hasAcceptableInput(m_text) == AcceptableInput || fixup()) {
4609 QInputMethod *inputMethod = QGuiApplication::inputMethod();
4610 inputMethod->commit();
4615 emit q->editingFinished();
4625 updateCursorBlinking();
4627 if (m_echoMode == QQuickTextInput::PasswordEchoOnEdit
4628 && !m_passwordEchoEditing
4630 && !event->text().isEmpty()
4631 && !(event->modifiers() & Qt::ControlModifier)) {
4637 updatePasswordEchoEditing(
true);
4641 bool unknown =
false;
4642#if QT_CONFIG(shortcut)
4643 bool visual = cursorMoveStyle() == Qt::VisualMoveStyle;
4648#if QT_CONFIG(shortcut)
4649 else if (event == QKeySequence::Undo) {
4652 else if (event == QKeySequence::Redo) {
4655 else if (event == QKeySequence::SelectAll) {
4658#if QT_CONFIG(clipboard)
4659 else if (event == QKeySequence::Copy) {
4662 else if (event == QKeySequence::Paste) {
4664 QClipboard::Mode mode = QClipboard::Clipboard;
4668 else if (event == QKeySequence::Cut) {
4671 else if (event == QKeySequence::DeleteEndOfLine) {
4676 else if (event == QKeySequence::MoveToStartOfLine || event == QKeySequence::MoveToStartOfBlock) {
4679 else if (event == QKeySequence::MoveToEndOfLine || event == QKeySequence::MoveToEndOfBlock) {
4682 else if (event == QKeySequence::SelectStartOfLine || event == QKeySequence::SelectStartOfBlock) {
4685 else if (event == QKeySequence::SelectEndOfLine || event == QKeySequence::SelectEndOfBlock) {
4688 else if (event == QKeySequence::MoveToNextChar) {
4689 if (hasSelectedText()) {
4690 moveCursor(selectionEnd(),
false);
4692 cursorForward(0, visual ? 1 : (layoutDirection() == Qt::LeftToRight ? 1 : -1));
4695 else if (event == QKeySequence::SelectNextChar) {
4696 cursorForward(1, visual ? 1 : (layoutDirection() == Qt::LeftToRight ? 1 : -1));
4698 else if (event == QKeySequence::MoveToPreviousChar) {
4699 if (hasSelectedText()) {
4700 moveCursor(selectionStart(),
false);
4702 cursorForward(0, visual ? -1 : (layoutDirection() == Qt::LeftToRight ? -1 : 1));
4705 else if (event == QKeySequence::SelectPreviousChar) {
4706 cursorForward(1, visual ? -1 : (layoutDirection() == Qt::LeftToRight ? -1 : 1));
4708 else if (event == QKeySequence::MoveToNextWord) {
4709 if (m_echoMode == QQuickTextInput::Normal)
4710 layoutDirection() == Qt::LeftToRight ? cursorWordForward(0) : cursorWordBackward(0);
4712 layoutDirection() == Qt::LeftToRight ? end(0) : home(0);
4714 else if (event == QKeySequence::MoveToPreviousWord) {
4715 if (m_echoMode == QQuickTextInput::Normal)
4716 layoutDirection() == Qt::LeftToRight ? cursorWordBackward(0) : cursorWordForward(0);
4717 else if (!m_readOnly) {
4718 layoutDirection() == Qt::LeftToRight ? home(0) : end(0);
4721 else if (event == QKeySequence::SelectNextWord) {
4722 if (m_echoMode == QQuickTextInput::Normal)
4723 layoutDirection() == Qt::LeftToRight ? cursorWordForward(1) : cursorWordBackward(1);
4725 layoutDirection() == Qt::LeftToRight ? end(1) : home(1);
4727 else if (event == QKeySequence::SelectPreviousWord) {
4728 if (m_echoMode == QQuickTextInput::Normal)
4729 layoutDirection() == Qt::LeftToRight ? cursorWordBackward(1) : cursorWordForward(1);
4731 layoutDirection() == Qt::LeftToRight ? home(1) : end(1);
4733 else if (event == QKeySequence::Delete) {
4737 else if (event == QKeySequence::DeleteEndOfWord) {
4741 else if (event == QKeySequence::DeleteStartOfWord) {
4743 deleteStartOfWord();
4744 }
else if (event == QKeySequence::DeleteCompleteLine) {
4747#if QT_CONFIG(clipboard)
4755 bool handled =
false;
4756 if (event->modifiers() & Qt::ControlModifier) {
4757 switch (event->key()) {
4758 case Qt::Key_Backspace:
4760 deleteStartOfWord();
4767 switch (event->key()) {
4768 case Qt::Key_Backspace:
4780 if (event->key() == Qt::Key_Direction_L || event->key() == Qt::Key_Direction_R) {
4781 setLayoutDirection((event->key() == Qt::Key_Direction_L) ? Qt::LeftToRight : Qt::RightToLeft);
4785 if (unknown && !m_readOnly) {
4786 if (m_inputControl->isAcceptableInput(event)) {
4790 && !hasSelectedText()
4791 && !(m_cursor == q_func()->text().size())) {
4795 insert(event->text());
4808
4809
4810
4811
4813void QQuickTextInputPrivate::deleteStartOfWord()
4815 int priorState = m_undoState;
4817 if (!separateSelection()) {
4818 Command cmd(SetSelection, m_cursor, u'\0', m_selstart, m_selend);
4820 cursorWordBackward(
true);
4824 removeSelectedText();
4825 finishChange(priorState);
4829
4830
4831
4832
4834void QQuickTextInputPrivate::deleteEndOfWord()
4836 int priorState = m_undoState;
4837 Command cmd(SetSelection, m_cursor, u'\0', m_selstart, m_selend);
4839 cursorWordForward(
true);
4843 removeSelectedText();
4844 finishChange(priorState);
4848
4849
4850
4851
4853void QQuickTextInputPrivate::deleteEndOfLine()
4855 int priorState = m_undoState;
4856 Command cmd(SetSelection, m_cursor, u'\0', m_selstart, m_selend);
4858 setSelection(m_cursor, end());
4860 removeSelectedText();
4861 finishChange(priorState);
4865
4866
4867
4868
4869
4870
4871
4872
4873void QQuickTextInput::ensureVisible(
int position)
4875 Q_D(QQuickTextInput);
4876 d->ensureVisible(position);
4877 updateCursorRectangle(
false);
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891void QQuickTextInput::clear()
4893 Q_D(QQuickTextInput);
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921qreal QQuickTextInput::padding()
const
4923 Q_D(
const QQuickTextInput);
4924 return d->padding();
4927void QQuickTextInput::setPadding(qreal padding)
4929 Q_D(QQuickTextInput);
4930 if (qFuzzyCompare(d->padding(), padding))
4933 d->extra.value().padding = padding;
4935 updateCursorRectangle();
4936 emit paddingChanged();
4937 if (!d->extra.isAllocated() || !d->extra->explicitTopPadding)
4938 emit topPaddingChanged();
4939 if (!d->extra.isAllocated() || !d->extra->explicitLeftPadding)
4940 emit leftPaddingChanged();
4941 if (!d->extra.isAllocated() || !d->extra->explicitRightPadding)
4942 emit rightPaddingChanged();
4943 if (!d->extra.isAllocated() || !d->extra->explicitBottomPadding)
4944 emit bottomPaddingChanged();
4947void QQuickTextInput::resetPadding()
4952qreal QQuickTextInput::topPadding()
const
4954 Q_D(
const QQuickTextInput);
4955 if (d->extra.isAllocated() && d->extra->explicitTopPadding)
4956 return d->extra->topPadding;
4957 return d->padding();
4960void QQuickTextInput::setTopPadding(qreal padding)
4962 Q_D(QQuickTextInput);
4963 d->setTopPadding(padding);
4966void QQuickTextInput::resetTopPadding()
4968 Q_D(QQuickTextInput);
4969 d->setTopPadding(0,
true);
4972qreal QQuickTextInput::leftPadding()
const
4974 Q_D(
const QQuickTextInput);
4975 if (d->extra.isAllocated() && d->extra->explicitLeftPadding)
4976 return d->extra->leftPadding;
4977 return d->padding();
4980void QQuickTextInput::setLeftPadding(qreal padding)
4982 Q_D(QQuickTextInput);
4983 d->setLeftPadding(padding);
4986void QQuickTextInput::resetLeftPadding()
4988 Q_D(QQuickTextInput);
4989 d->setLeftPadding(0,
true);
4992qreal QQuickTextInput::rightPadding()
const
4994 Q_D(
const QQuickTextInput);
4995 if (d->extra.isAllocated() && d->extra->explicitRightPadding)
4996 return d->extra->rightPadding;
4997 return d->padding();
5000void QQuickTextInput::setRightPadding(qreal padding)
5002 Q_D(QQuickTextInput);
5003 d->setRightPadding(padding);
5006void QQuickTextInput::resetRightPadding()
5008 Q_D(QQuickTextInput);
5009 d->setRightPadding(0,
true);
5012qreal QQuickTextInput::bottomPadding()
const
5014 Q_D(
const QQuickTextInput);
5015 if (d->extra.isAllocated() && d->extra->explicitBottomPadding)
5016 return d->extra->bottomPadding;
5017 return d->padding();
5020void QQuickTextInput::setBottomPadding(qreal padding)
5022 Q_D(QQuickTextInput);
5023 d->setBottomPadding(padding);
5026void QQuickTextInput::resetBottomPadding()
5028 Q_D(QQuickTextInput);
5029 d->setBottomPadding(0,
true);
5032#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
5033void QQuickTextInput::setOldSelectionDefault()
5035 Q_D(QQuickTextInput);
5036 d->selectByMouse =
false;
5037 d->selectByTouchDrag =
true;
5038 qCDebug(lcQuickTextInput,
"pre-6.4 behavior chosen: selectByMouse defaults false; if enabled, touchscreen acts like a mouse");
5042QQuickPre64TextInput::QQuickPre64TextInput(QQuickItem *parent)
5043 : QQuickTextInput(parent)
5045 setOldSelectionDefault();
5051#include "moc_qquicktextinput_p.cpp"
Combined button and popup list for selecting options.