/
qquicktextinput.cpp
4177 lines (3591 loc) · 127 KB
/
qquicktextinput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qquicktextinput_p.h"
#include "qquicktextinput_p_p.h"
#include "qquickcanvas.h"
#include <private/qqmlglobal_p.h>
#include <QtQml/qqmlinfo.h>
#include <QtGui/qevent.h>
#include <QTextBoundaryFinder>
#include "qquicktextnode_p.h"
#include <QtQuick/qsgsimplerectnode.h>
#include <QtGui/qstylehints.h>
#include <QtGui/qinputmethod.h>
#ifndef QT_NO_ACCESSIBILITY
#include "qaccessible.h"
#endif
QT_BEGIN_NAMESPACE
DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
#ifdef QT_GUI_PASSWORD_ECHO_DELAY
static const int qt_passwordEchoDelay = QT_GUI_PASSWORD_ECHO_DELAY;
#endif
/*!
\qmlclass TextInput QQuickTextInput
\inqmlmodule QtQuick 2
\ingroup qml-basic-visual-elements
\brief The TextInput item displays an editable line of text.
\inherits Item
The TextInput element displays a single line of editable plain text.
TextInput is used to accept a line of text input. Input constraints
can be placed on a TextInput item (for example, through a \l validator or \l inputMask),
and setting \l echoMode to an appropriate value enables TextInput to be used for
a password input field.
On Mac OS X, the Up/Down key bindings for Home/End are explicitly disabled.
If you want such bindings (on any platform), you will need to construct them in QML.
\sa TextEdit, Text, {declarative/text/textselection}{Text Selection example}
*/
QQuickTextInput::QQuickTextInput(QQuickItem* parent)
: QQuickImplicitSizeItem(*(new QQuickTextInputPrivate), parent)
{
Q_D(QQuickTextInput);
d->init();
}
QQuickTextInput::~QQuickTextInput()
{
}
void QQuickTextInput::componentComplete()
{
Q_D(QQuickTextInput);
QQuickImplicitSizeItem::componentComplete();
d->checkIsValid();
d->updateLayout();
updateCursorRectangle();
if (d->cursorComponent && d->cursorComponent->isReady())
createCursor();
}
/*!
\qmlproperty string QtQuick2::TextInput::text
The text in the TextInput.
*/
QString QQuickTextInput::text() const
{
Q_D(const QQuickTextInput);
QString content = d->m_text;
if (!d->m_tentativeCommit.isEmpty())
content.insert(d->m_cursor, d->m_tentativeCommit);
QString res = d->m_maskData ? d->stripString(content) : content;
return (res.isNull() ? QString::fromLatin1("") : res);
}
void QQuickTextInput::setText(const QString &s)
{
Q_D(QQuickTextInput);
if (s == text())
return;
if (d->composeMode())
qApp->inputMethod()->reset();
d->m_tentativeCommit.clear();
d->internalSetText(s, -1, false);
}
/*!
\qmlproperty int QtQuick2::TextInput::length
Returns the total number of characters in the TextInput item.
If the TextInput has an inputMask the length will include mask characters and may differ
from the length of the string returned by the \l text property.
This property can be faster than querying the length the \l text property as it doesn't
require any copying or conversion of the TextInput's internal string data.
*/
int QQuickTextInput::length() const
{
Q_D(const QQuickTextInput);
return d->m_text.length();
}
/*!
\qmlmethod string QtQuick2::TextInput::getText(int start, int end)
Returns the section of text that is between the \a start and \a end positions.
If the TextInput has an inputMask the length will include mask characters.
*/
QString QQuickTextInput::getText(int start, int end) const
{
Q_D(const QQuickTextInput);
if (start > end)
qSwap(start, end);
return d->m_text.mid(start, end - start);
}
QString QQuickTextInputPrivate::realText() const
{
QString res = m_maskData ? stripString(m_text) : m_text;
return (res.isNull() ? QString::fromLatin1("") : res);
}
/*!
\qmlproperty string QtQuick2::TextInput::font.family
Sets the family name of the font.
The family name is case insensitive and may optionally include a foundry name, e.g. "Helvetica [Cronyx]".
If the family is available from more than one foundry and the foundry isn't specified, an arbitrary foundry is chosen.
If the family isn't available a family will be set using the font matching algorithm.
*/
/*!
\qmlproperty bool QtQuick2::TextInput::font.bold
Sets whether the font weight is bold.
*/
/*!
\qmlproperty enumeration QtQuick2::TextInput::font.weight
Sets the font's weight.
The weight can be one of:
\list
\li Font.Light
\li Font.Normal - the default
\li Font.DemiBold
\li Font.Bold
\li Font.Black
\endlist
\qml
TextInput { text: "Hello"; font.weight: Font.DemiBold }
\endqml
*/
/*!
\qmlproperty bool QtQuick2::TextInput::font.italic
Sets whether the font has an italic style.
*/
/*!
\qmlproperty bool QtQuick2::TextInput::font.underline
Sets whether the text is underlined.
*/
/*!
\qmlproperty bool QtQuick2::TextInput::font.strikeout
Sets whether the font has a strikeout style.
*/
/*!
\qmlproperty real QtQuick2::TextInput::font.pointSize
Sets the font size in points. The point size must be greater than zero.
*/
/*!
\qmlproperty int QtQuick2::TextInput::font.pixelSize
Sets the font size in pixels.
Using this function makes the font device dependent.
Use \c pointSize to set the size of the font in a device independent manner.
*/
/*!
\qmlproperty real QtQuick2::TextInput::font.letterSpacing
Sets the letter spacing for the font.
Letter spacing changes the default spacing between individual letters in the font.
A positive value increases the letter spacing by the corresponding pixels; a negative value decreases the spacing.
*/
/*!
\qmlproperty real QtQuick2::TextInput::font.wordSpacing
Sets the word spacing for the font.
Word spacing changes the default spacing between individual words.
A positive value increases the word spacing by a corresponding amount of pixels,
while a negative value decreases the inter-word spacing accordingly.
*/
/*!
\qmlproperty enumeration QtQuick2::TextInput::font.capitalization
Sets the capitalization for the text.
\list
\li Font.MixedCase - This is the normal text rendering option where no capitalization change is applied.
\li Font.AllUppercase - This alters the text to be rendered in all uppercase type.
\li Font.AllLowercase - This alters the text to be rendered in all lowercase type.
\li Font.SmallCaps - This alters the text to be rendered in small-caps type.
\li Font.Capitalize - This alters the text to be rendered with the first character of each word as an uppercase character.
\endlist
\qml
TextInput { text: "Hello"; font.capitalization: Font.AllLowercase }
\endqml
*/
QFont QQuickTextInput::font() const
{
Q_D(const QQuickTextInput);
return d->sourceFont;
}
void QQuickTextInput::setFont(const QFont &font)
{
Q_D(QQuickTextInput);
if (d->sourceFont == font)
return;
d->sourceFont = font;
QFont oldFont = d->font;
d->font = font;
if (d->font.pointSizeF() != -1) {
// 0.5pt resolution
qreal size = qRound(d->font.pointSizeF()*2.0);
d->font.setPointSizeF(size/2.0);
}
if (oldFont != d->font) {
d->updateLayout();
updateCursorRectangle();
updateInputMethod(Qt::ImCursorRectangle | Qt::ImFont);
}
emit fontChanged(d->sourceFont);
}
/*!
\qmlproperty color QtQuick2::TextInput::color
The text color.
*/
QColor QQuickTextInput::color() const
{
Q_D(const QQuickTextInput);
return d->color;
}
void QQuickTextInput::setColor(const QColor &c)
{
Q_D(QQuickTextInput);
if (c != d->color) {
d->color = c;
d->textLayoutDirty = true;
d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
update();
emit colorChanged();
}
}
/*!
\qmlproperty color QtQuick2::TextInput::selectionColor
The text highlight color, used behind selections.
*/
QColor QQuickTextInput::selectionColor() const
{
Q_D(const QQuickTextInput);
return d->selectionColor;
}
void QQuickTextInput::setSelectionColor(const QColor &color)
{
Q_D(QQuickTextInput);
if (d->selectionColor == color)
return;
d->selectionColor = color;
if (d->hasSelectedText()) {
d->textLayoutDirty = true;
d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
update();
}
emit selectionColorChanged();
}
/*!
\qmlproperty color QtQuick2::TextInput::selectedTextColor
The highlighted text color, used in selections.
*/
QColor QQuickTextInput::selectedTextColor() const
{
Q_D(const QQuickTextInput);
return d->selectedTextColor;
}
void QQuickTextInput::setSelectedTextColor(const QColor &color)
{
Q_D(QQuickTextInput);
if (d->selectedTextColor == color)
return;
d->selectedTextColor = color;
if (d->hasSelectedText()) {
d->textLayoutDirty = true;
d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
update();
}
emit selectedTextColorChanged();
}
/*!
\qmlproperty enumeration QtQuick2::TextInput::horizontalAlignment
\qmlproperty enumeration QtQuick2::TextInput::effectiveHorizontalAlignment
\qmlproperty enumeration QtQuick2::TextInput::verticalAlignment
Sets the horizontal alignment of the text within the TextInput item's
width and height. By default, the text alignment follows the natural alignment
of the text, for example text that is read from left to right will be aligned to
the left.
TextInput does not have vertical alignment, as the natural height is
exactly the height of the single line of text. If you set the height
manually to something larger, TextInput will always be top aligned
vertically. You can use anchors to align it however you want within
another item.
The valid values for \c horizontalAlignment are \c TextInput.AlignLeft, \c TextInput.AlignRight and
\c TextInput.AlignHCenter.
Valid values for \c verticalAlignment are \c TextInput.AlignTop (default),
\c TextInput.AlignBottom \c TextInput.AlignVCenter.
When using the attached property LayoutMirroring::enabled to mirror application
layouts, the horizontal alignment of text will also be mirrored. However, the property
\c horizontalAlignment will remain unchanged. To query the effective horizontal alignment
of TextInput, use the read-only property \c effectiveHorizontalAlignment.
*/
QQuickTextInput::HAlignment QQuickTextInput::hAlign() const
{
Q_D(const QQuickTextInput);
return d->hAlign;
}
void QQuickTextInput::setHAlign(HAlignment align)
{
Q_D(QQuickTextInput);
bool forceAlign = d->hAlignImplicit && d->effectiveLayoutMirror;
d->hAlignImplicit = false;
if (d->setHAlign(align, forceAlign) && isComponentComplete()) {
d->updateLayout();
updateCursorRectangle();
}
}
void QQuickTextInput::resetHAlign()
{
Q_D(QQuickTextInput);
d->hAlignImplicit = true;
if (d->determineHorizontalAlignment() && isComponentComplete()) {
d->updateLayout();
updateCursorRectangle();
}
}
QQuickTextInput::HAlignment QQuickTextInput::effectiveHAlign() const
{
Q_D(const QQuickTextInput);
QQuickTextInput::HAlignment effectiveAlignment = d->hAlign;
if (!d->hAlignImplicit && d->effectiveLayoutMirror) {
switch (d->hAlign) {
case QQuickTextInput::AlignLeft:
effectiveAlignment = QQuickTextInput::AlignRight;
break;
case QQuickTextInput::AlignRight:
effectiveAlignment = QQuickTextInput::AlignLeft;
break;
default:
break;
}
}
return effectiveAlignment;
}
bool QQuickTextInputPrivate::setHAlign(QQuickTextInput::HAlignment alignment, bool forceAlign)
{
Q_Q(QQuickTextInput);
if ((hAlign != alignment || forceAlign) && alignment <= QQuickTextInput::AlignHCenter) { // justify not supported
QQuickTextInput::HAlignment oldEffectiveHAlign = q->effectiveHAlign();
hAlign = alignment;
emit q->horizontalAlignmentChanged(alignment);
if (oldEffectiveHAlign != q->effectiveHAlign())
emit q->effectiveHorizontalAlignmentChanged();
return true;
}
return false;
}
bool QQuickTextInputPrivate::determineHorizontalAlignment()
{
if (hAlignImplicit) {
// if no explicit alignment has been set, follow the natural layout direction of the text
QString text = q_func()->text();
if (text.isEmpty())
text = m_textLayout.preeditAreaText();
bool isRightToLeft = text.isEmpty() ? qApp->inputMethod()->inputDirection() == Qt::RightToLeft
: text.isRightToLeft();
return setHAlign(isRightToLeft ? QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft);
}
return false;
}
QQuickTextInput::VAlignment QQuickTextInput::vAlign() const
{
Q_D(const QQuickTextInput);
return d->vAlign;
}
void QQuickTextInput::setVAlign(QQuickTextInput::VAlignment alignment)
{
Q_D(QQuickTextInput);
if (alignment == d->vAlign)
return;
d->vAlign = alignment;
emit verticalAlignmentChanged(d->vAlign);
if (isComponentComplete()) {
updateCursorRectangle();
}
}
/*!
\qmlproperty enumeration QtQuick2::TextInput::wrapMode
Set this property to wrap the text to the TextInput item's width.
The text will only wrap if an explicit width has been set.
\list
\li TextInput.NoWrap - no wrapping will be performed. If the text contains insufficient newlines, then implicitWidth will exceed a set width.
\li TextInput.WordWrap - wrapping is done on word boundaries only. If a word is too long, implicitWidth will exceed a set width.
\li TextInput.WrapAnywhere - wrapping is done at any point on a line, even if it occurs in the middle of a word.
\li TextInput.Wrap - if possible, wrapping occurs at a word boundary; otherwise it will occur at the appropriate point on the line, even in the middle of a word.
\endlist
The default is TextInput.NoWrap. If you set a width, consider using TextInput.Wrap.
*/
QQuickTextInput::WrapMode QQuickTextInput::wrapMode() const
{
Q_D(const QQuickTextInput);
return d->wrapMode;
}
void QQuickTextInput::setWrapMode(WrapMode mode)
{
Q_D(QQuickTextInput);
if (mode == d->wrapMode)
return;
d->wrapMode = mode;
d->updateLayout();
updateCursorRectangle();
emit wrapModeChanged();
}
void QQuickTextInputPrivate::mirrorChange()
{
Q_Q(QQuickTextInput);
if (q->isComponentComplete()) {
if (!hAlignImplicit && (hAlign == QQuickTextInput::AlignRight || hAlign == QQuickTextInput::AlignLeft)) {
q->updateCursorRectangle();
emit q->effectiveHorizontalAlignmentChanged();
}
}
}
/*!
\qmlproperty bool QtQuick2::TextInput::readOnly
Sets whether user input can modify the contents of the TextInput.
If readOnly is set to true, then user input will not affect the text
property. Any bindings or attempts to set the text property will still
work.
*/
bool QQuickTextInput::isReadOnly() const
{
Q_D(const QQuickTextInput);
return d->m_readOnly;
}
void QQuickTextInput::setReadOnly(bool ro)
{
Q_D(QQuickTextInput);
if (d->m_readOnly == ro)
return;
setFlag(QQuickItem::ItemAcceptsInputMethod, !ro);
d->m_readOnly = ro;
if (!ro)
d->setCursorPosition(d->end());
updateInputMethod(Qt::ImEnabled);
q_canPasteChanged();
d->emitUndoRedoChanged();
emit readOnlyChanged(ro);
}
/*!
\qmlproperty int QtQuick2::TextInput::maximumLength
The maximum permitted length of the text in the TextInput.
If the text is too long, it is truncated at the limit.
By default, this property contains a value of 32767.
*/
int QQuickTextInput::maxLength() const
{
Q_D(const QQuickTextInput);
return d->m_maxLength;
}
void QQuickTextInput::setMaxLength(int ml)
{
Q_D(QQuickTextInput);
if (d->m_maxLength == ml || d->m_maskData)
return;
d->m_maxLength = ml;
d->internalSetText(d->m_text, -1, false);
emit maximumLengthChanged(ml);
}
/*!
\qmlproperty bool QtQuick2::TextInput::cursorVisible
Set to true when the TextInput shows a cursor.
This property is set and unset when the TextInput gets active focus, so that other
properties can be bound to whether the cursor is currently showing. As it
gets set and unset automatically, when you set the value yourself you must
keep in mind that your value may be overwritten.
It can be set directly in script, for example if a KeyProxy might
forward keys to it and you desire it to look active when this happens
(but without actually giving it active focus).
It should not be set directly on the element, like in the below QML,
as the specified value will be overridden an lost on focus changes.
\code
TextInput {
text: "Text"
cursorVisible: false
}
\endcode
In the above snippet the cursor will still become visible when the
TextInput gains active focus.
*/
bool QQuickTextInput::isCursorVisible() const
{
Q_D(const QQuickTextInput);
return d->cursorVisible;
}
void QQuickTextInput::setCursorVisible(bool on)
{
Q_D(QQuickTextInput);
if (d->cursorVisible == on)
return;
d->cursorVisible = on;
d->setCursorBlinkPeriod(on ? qApp->styleHints()->cursorFlashTime() : 0);
d->updateType = QQuickTextInputPrivate::UpdatePaintNode;
update();
emit cursorVisibleChanged(d->cursorVisible);
}
/*!
\qmlproperty int QtQuick2::TextInput::cursorPosition
The position of the cursor in the TextInput.
*/
int QQuickTextInput::cursorPosition() const
{
Q_D(const QQuickTextInput);
return d->m_cursor;
}
void QQuickTextInput::setCursorPosition(int cp)
{
Q_D(QQuickTextInput);
if (cp < 0 || cp > text().length())
return;
d->moveCursor(cp);
}
/*!
\qmlproperty rectangle QtQuick2::TextInput::cursorRectangle
The rectangle where the standard text cursor is rendered within the text input. Read only.
The position and height of a custom cursorDelegate are updated to follow the cursorRectangle
automatically when it changes. The width of the delegate is unaffected by changes in the
cursor rectangle.
*/
QRectF QQuickTextInput::cursorRectangle() const
{
Q_D(const QQuickTextInput);
int c = d->m_cursor;
if (d->m_preeditCursor != -1)
c += d->m_preeditCursor;
if (d->m_echoMode == NoEcho)
c = 0;
QTextLine l = d->m_textLayout.lineForTextPosition(c);
if (!l.isValid())
return QRectF();
return QRectF(l.cursorToX(c) - d->hscroll, l.y() - d->vscroll, 1, l.height());
}
/*!
\qmlproperty int QtQuick2::TextInput::selectionStart
The cursor position before the first character in the current selection.
This property is read-only. To change the selection, use select(start,end),
selectAll(), or selectWord().
\sa selectionEnd, cursorPosition, selectedText
*/
int QQuickTextInput::selectionStart() const
{
Q_D(const QQuickTextInput);
return d->lastSelectionStart;
}
/*!
\qmlproperty int QtQuick2::TextInput::selectionEnd
The cursor position after the last character in the current selection.
This property is read-only. To change the selection, use select(start,end),
selectAll(), or selectWord().
\sa selectionStart, cursorPosition, selectedText
*/
int QQuickTextInput::selectionEnd() const
{
Q_D(const QQuickTextInput);
return d->lastSelectionEnd;
}
/*!
\qmlmethod void QtQuick2::TextInput::select(int start, int end)
Causes the text from \a start to \a end to be selected.
If either start or end is out of range, the selection is not changed.
After calling this, selectionStart will become the lesser
and selectionEnd will become the greater (regardless of the order passed
to this method).
\sa selectionStart, selectionEnd
*/
void QQuickTextInput::select(int start, int end)
{
Q_D(QQuickTextInput);
if (start < 0 || end < 0 || start > d->m_text.length() || end > d->m_text.length())
return;
d->setSelection(start, end-start);
}
/*!
\qmlproperty string QtQuick2::TextInput::selectedText
This read-only property provides the text currently selected in the
text input.
It is equivalent to the following snippet, but is faster and easier
to use.
\js
myTextInput.text.toString().substring(myTextInput.selectionStart,
myTextInput.selectionEnd);
\endjs
*/
QString QQuickTextInput::selectedText() const
{
Q_D(const QQuickTextInput);
return d->selectedText();
}
/*!
\qmlproperty bool QtQuick2::TextInput::activeFocusOnPress
Whether the TextInput should gain active focus on a mouse press. By default this is
set to true.
*/
bool QQuickTextInput::focusOnPress() const
{
Q_D(const QQuickTextInput);
return d->focusOnPress;
}
void QQuickTextInput::setFocusOnPress(bool b)
{
Q_D(QQuickTextInput);
if (d->focusOnPress == b)
return;
d->focusOnPress = b;
emit activeFocusOnPressChanged(d->focusOnPress);
}
/*!
\qmlproperty bool QtQuick2::TextInput::autoScroll
Whether the TextInput should scroll when the text is longer than the width. By default this is
set to true.
*/
bool QQuickTextInput::autoScroll() const
{
Q_D(const QQuickTextInput);
return d->autoScroll;
}
void QQuickTextInput::setAutoScroll(bool b)
{
Q_D(QQuickTextInput);
if (d->autoScroll == b)
return;
d->autoScroll = b;
//We need to repaint so that the scrolling is taking into account.
updateCursorRectangle();
emit autoScrollChanged(d->autoScroll);
}
#ifndef QT_NO_VALIDATOR
/*!
\qmlclass IntValidator QIntValidator
\inqmlmodule QtQuick 2
\ingroup qml-basic-visual-elements
This element provides a validator for integer values.
If no \l locale is set IntValidator uses the \l {QLocale::setDefault()}{default locale} to
interpret the number and will accept locale specific digits, group separators, and positive
and negative signs. In addition, IntValidator is always guaranteed to accept a number
formatted according to the "C" locale.
*/
QQuickIntValidator::QQuickIntValidator(QObject *parent)
: QIntValidator(parent)
{
}
/*!
\qmlproperty string QtQuick2::IntValidator::locale
This property holds the name of the locale used to interpret the number.
\sa QML:Qt::locale()
*/
QString QQuickIntValidator::localeName() const
{
return locale().name();
}
void QQuickIntValidator::setLocaleName(const QString &name)
{
if (locale().name() != name) {
setLocale(QLocale(name));
emit localeNameChanged();
}
}
void QQuickIntValidator::resetLocaleName()
{
QLocale defaultLocale;
if (locale() != defaultLocale) {
setLocale(defaultLocale);
emit localeNameChanged();
}
}
/*!
\qmlproperty int QtQuick2::IntValidator::top
This property holds the validator's highest acceptable value.
By default, this property's value is derived from the highest signed integer available (typically 2147483647).
*/
/*!
\qmlproperty int QtQuick2::IntValidator::bottom
This property holds the validator's lowest acceptable value.
By default, this property's value is derived from the lowest signed integer available (typically -2147483647).
*/
/*!
\qmlclass DoubleValidator QDoubleValidator
\inqmlmodule QtQuick 2
\ingroup qml-basic-visual-elements
This element provides a validator for non-integer numbers.
Input is accepted if it contains a double that is within the valid range
and is in the correct format.
Input is accepected but invalid if it contains a double that is outside
the range or is in the wrong format; e.g. with too many digits after the
decimal point or is empty.
Input is rejected if it is not a double.
Note: If the valid range consists of just positive doubles (e.g. 0.0 to
100.0) and input is a negative double then it is rejected. If \l notation
is set to DoubleValidator.StandardNotation, and the input contains more
digits before the decimal point than a double in the valid range may have,
it is also rejected. If \l notation is DoubleValidator.ScientificNotation,
and the input is not in the valid range, it is accecpted but invalid. The
value may yet become valid by changing the exponent.
*/
QQuickDoubleValidator::QQuickDoubleValidator(QObject *parent)
: QDoubleValidator(parent)
{
}
/*!
\qmlproperty string QtQuick2::DoubleValidator::locale
This property holds the name of the locale used to interpret the number.
\sa QML:Qt::locale()
*/
QString QQuickDoubleValidator::localeName() const
{
return locale().name();
}
void QQuickDoubleValidator::setLocaleName(const QString &name)
{
if (locale().name() != name) {
setLocale(QLocale(name));
emit localeNameChanged();
}
}
void QQuickDoubleValidator::resetLocaleName()
{
QLocale defaultLocale;
if (locale() != defaultLocale) {
setLocale(defaultLocale);
emit localeNameChanged();
}
}
/*!
\qmlproperty real QtQuick2::DoubleValidator::top
This property holds the validator's maximum acceptable value.
By default, this property contains a value of infinity.
*/
/*!
\qmlproperty real QtQuick2::DoubleValidator::bottom
This property holds the validator's minimum acceptable value.
By default, this property contains a value of -infinity.
*/
/*!
\qmlproperty int QtQuick2::DoubleValidator::decimals
This property holds the validator's maximum number of digits after the decimal point.
By default, this property contains a value of 1000.
*/
/*!
\qmlproperty enumeration QtQuick2::DoubleValidator::notation
This property holds the notation of how a string can describe a number.
The possible values for this property are:
\list
\li DoubleValidator.StandardNotation
\li DoubleValidator.ScientificNotation (default)
\endlist
If this property is set to DoubleValidator.ScientificNotation, the written number may have an exponent part (e.g. 1.5E-2).
*/
/*!
\qmlclass RegExpValidator QRegExpValidator
\inqmlmodule QtQuick 2
\ingroup qml-basic-visual-elements
This element provides a validator, which counts as valid any string which
matches a specified regular expression.
*/
/*!
\qmlproperty regExp QtQuick2::RegExpValidator::regExp
This property holds the regular expression used for validation.
Note that this property should be a regular expression in JS syntax, e.g /a/ for the regular expression
matching "a".
By default, this property contains a regular expression with the pattern .* that matches any string.
*/
/*!
\qmlproperty Validator QtQuick2::TextInput::validator
Allows you to set a validator on the TextInput. When a validator is set
the TextInput will only accept input which leaves the text property in
an acceptable or intermediate state. The accepted signal will only be sent
if the text is in an acceptable state when enter is pressed.
Currently supported validators are IntValidator, DoubleValidator and
RegExpValidator. An example of using validators is shown below, which allows
input of integers between 11 and 31 into the text input:
\code
import QtQuick 2.0