Debian Codegraph @

src/imports/Components/1.2/TextField.qml

raw
1 /*
2 * Copyright 2012 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 1.2 as Lomiri
19 import Lomiri.Components.Popups 1.0
20
21 /*!
22 \qmltype TextField
23 \inqmlmodule Lomiri.Components
24 \ingroup lomiri
25 \brief The TextField element displays a single line of editable plain text.
26 Input constraints can be set through validator or inputMask. Setting echoMode
27 to an appropriate value enables TextField to be used as password input field.
28
29 \l {https://design.lomiri.com/apps/building-blocks/text-input#text-field}{See also the Design Guidelines on the Text Field}.
30
31 Example:
32 \qml
33 Item {
34 TextField {
35 placeholderText: "hint text"
36 }
37
38 TextField {
39 placeholderText: "without clear sign"
40 hasClearButton: false
41 }
42
43 TextField {
44 placeholderText: "password"
45 echoMode: TextInput.Password
46 }
47
48 TextField {
49 placeholderText: "overlaid in front"
50 primaryItem: Image {
51 height: parent.height
52 width: height
53 source: "magnifier.png"
54 }
55 secondaryItem: Row {
56 Button {
57 height: parent.height
58 width: height
59 iconName: "compose"
60 onClicked: doSomething()
61 }
62 Button {
63 height: parent.height
64 width: height
65 iconName: "undo"
66 onClicked: doSomething()
67 }
68 }
69 }
70 }
71 \endqml
72
73 \section2 Scrolling and text selection
74 The input is activated when the tap or mouse is released after being pressed
75 over the component.
76
77 The text can be scrolled horizontally by swiping over the content both when
78 the component is active or inactive.
79
80 The content can be selected in the following ways:
81 \list
82 \li - double tapping/left mouse clicking over the content, when the word that
83 had been tapped over will be selected
84 \li - by pressing and dragging the selection cursor over the text input. Note
85 that there has to be a delay of approx. 200 ms between the press and drag
86 gesture, time when the input switches from scroll mode to selection mode
87 \endlist
88
89 The input is focused (activated) upon tap/left mouse button release. The cursor
90 will be placed at the position the mouse/tap point at release time. If the click
91 is happening on a selected area, the selection will be cleared. Long press above
92 a selected area brings up the clipboard context menu. When the long press happens
93 over a non-selected area, the cursor will be moved to the position and the component
94 enters in selection mode. The selection mode can also be activated by tapping and
95 keeping the tap/mouse over for approx 300 ms. If there is a move during this time,
96 the component enters into scrolling mode. The mode is exited once the scrolling
97 finishes. During the scrolling mode the selected text is preserved.
98
99 \note During text selection all interactive parent Flickables are turned off.
100 */
101
102 Lomiri.ActionItem {
103 id: control
104
105 implicitWidth: units.gu(25)
106 implicitHeight: units.gu(4)
107 /*!
108 The property presents whether the TextField is highlighted or not. By
109 default the TextField gets highlighted when gets the focus, so can accept
110 text input. This property allows to control the highlight separately from
111 the focused behavior.
112 */
113 property bool highlighted: focus
114
115 /*!
116 Text that appears when there is no content in the component.
117
118 \qmlproperty string placeholderText
119 */
120 property alias placeholderText: hint.text
121
122 /*!
123 Specifies whether the control has a clear button or not.
124 */
125 property bool hasClearButton: true
126
127 /*!
128 \deprecated
129 Component to be shown and used instead of the default On Screen Keyboard.
130 */
131 property Component customSoftwareInputPanel
132 onCustomSoftwareInputPanelChanged: console.error("customSoftwareInputPanel property deprecated.")
133
134 /*!
135 The property overrides the default popover of the TextField. When set, the TextField
136 will open the given popover instead of the default one defined. The popover can either
137 be a component or a URL to be loaded.
138 */
139 property var popover
140
141 /*!
142 Overlaid component that can be set for the fore side of the TextField,
143 e.g.showing a magnifier to implement search functionality.
144
145 \qmlproperty list<Object> primaryItem
146 */
147 property alias primaryItem: leftPane.data
148
149 /*!
150 Overlaid component that can be set for the rear side of the TextField,
151 e.g.showing a CAPS LOCK or NUM LOCK indication. The overlaid components
152 will be placed right after the clear button.
153
154 \qmlproperty list<Object> secondaryItem
155 */
156 property alias secondaryItem: rightPane.data
157
158 /*!
159 Allows highlighting errors in the TextField.
160 */
161 property bool errorHighlight: !acceptableInput
162
163 // aliased properties from TextInput
164 /*!
165 This property is always true unless a validator or input mask has been set.
166 If a validator or input mask has been set, this property will only be true
167 if the current text is acceptable to the validator or input mask as a final
168 string (not as an intermediate string).
169
170 \qmlproperty bool acceptableInput
171 */
172 property alias acceptableInput: editor.acceptableInput
173
174 /*!
175 Whether the TextField should gain active focus on a mouse press. By default
176 this is set to true.
177 \qmlproperty bool activeFocusOnPress
178 */
179 property alias activeFocusOnPress: editor.activeFocusOnPress
180
181 /*!
182 Whether the TextField should scroll when the text is longer than the width.
183 By default this is set to true.
184
185 \qmlproperty bool autoScroll
186 */
187 property alias autoScroll: editor.autoScroll
188
189 /*!
190 Returns true if the TextField is writable and the content of the clipboard
191 is suitable for pasting into the TextField.
192
193 \qmlproperty bool canPaste
194 */
195 property alias canPaste: editor.canPaste
196
197 /*!
198 Returns true if the TextField is writable and there are undone operations
199 that can be redone.
200
201 \qmlproperty bool canRedo
202 */
203 property alias canRedo: editor.canRedo
204
205 /*!
206 Returns true if the TextField is writable and there are previous operations
207 that can be undone.
208
209 \qmlproperty bool canUndo
210 */
211 property alias canUndo: editor.canUndo
212
213 /*!
214 The text color.
215 \qmlproperty color color
216 */
217 property alias color: editor.color
218
219 /*!
220 Returns the height of the text, including the height past the height that
221 is covered if the text does not fit within the set height.
222
223 \qmlproperty real contentHeight
224 */
225 property alias contentHeight: editor.contentHeight
226
227 /*!
228 Returns the width of the text, including the width past the width which
229 is covered due to insufficient wrapping if wrapMode is set.
230
231 \qmlproperty real contentWidth
232 */
233 property alias contentWidth: editor.contentWidth
234
235 /*!
236 The delegate for the cursor in the TextField.
237
238 If you set a cursorDelegate for a TextField, this delegate will be used for
239 drawing the cursor instead of the standard cursor. An instance of the delegate
240 will be created and managed by the TextField when a cursor is needed, and
241 the x property of delegate instance will be set so as to be one pixel before
242 the top left of the current character.
243
244 Note that the root item of the delegate component must be a QQuickItem or
245 QQuickItem derived item.
246 */
247 property Component cursorDelegate: null
248
249 /*!
250 The position of the cursor in the TextField.
251
252 \qmlproperty int cursorPosition
253 */
254 property alias cursorPosition: editor.cursorPosition
255
256 /*!
257 The rectangle where the standard text cursor is rendered within the text
258 input. Read only.
259
260 The position and height of a custom cursorDelegate are updated to follow
261 the cursorRectangle automatically when it changes. The width of the delegate
262 is unaffected by changes in the cursor rectangle.
263
264 \qmlproperty rectangle cursorRectangle
265 */
266 property alias cursorRectangle: editor.cursorRectangle
267
268 /*!
269 Set to true when the TextField shows a cursor.
270
271 This property is set and unset when the TextField gets active focus, so that
272 other properties can be bound to whether the cursor is currently showing.
273 As it gets set and unset automatically, when you set the value yourself you
274 must keep in mind that your value may be overwritten.
275
276 It can be set directly in script, for example if a KeyProxy might forward keys
277 to it and you desire it to look active when this happens (but without actually
278 giving it active focus).
279
280 It should not be set directly on the item, like in the below QML, as the specified
281 value will be overridden an lost on focus changes.
282 \qml
283 TextField {
284 text: "Text"
285 cursorVisible: false
286 }
287 \endqml
288 In the above snippet the cursor will still become visible when the TextField
289 gains active focus.
290
291 \qmlproperty bool cursorVisible
292 */
293 property alias cursorVisible: editor.cursorVisible
294
295 /*!
296 This is the text displayed in the TextField.
297
298 If echoMode is set to TextInput::Normal, this holds the same value as the
299 \l text property. Otherwise, this property holds the text visible to the
300 user, while the text property holds the actual entered text.
301
302 \qmlproperty string displayText
303 */
304 property alias displayText: editor.displayText
305
306 /*!
307 Specifies how the text should be displayed in the TextField.
308 \list
309 \li - TextInput.Normal - Displays the text as it is. (Default)
310 \li - TextInput.Password - Displays asterixes instead of characters.
311 \li - TextInput.NoEcho - Displays nothing.
312 \li - TextInput.PasswordEchoOnEdit - Displays characters as they are entered while
313 editing, otherwise displays asterisks.
314 \endlist
315
316 \qmlproperty enumeration echoMode
317 */
318 property alias echoMode: editor.echoMode
319
320 /*!
321 Font used in the TextField.
322
323 \qmlproperty font font
324 */
325 property alias font: editor.font
326
327 /*!
328 Allows you to set an input mask on the TextField, restricting the text
329 inputs. See QLineEdit::inputMask for further details, as the exact same mask strings
330 are used by TextField.
331
332 \qmlproperty string inputMask
333 */
334 property alias inputMask: editor.inputMask
335
336 /*!
337 This property holds whether the TextInput has partial text input from an input method.
338
339 While it is composing an input method may rely on mouse or key events from the
340 TextField to edit or commit the partial text. This property can be used to determine
341 when to disable events handlers that may interfere with the correct operation
342 of an input method.
343
344 \qmlproperty bool inputMethodComposing
345 */
346 property alias inputMethodComposing: editor.inputMethodComposing
347
348 /*!
349 Provides hints to the input method about the expected content of the text input and how it
350 should operate.
351
352 The value is a bit-wise combination of flags, or Qt.ImhNone if no hints are set.
353
354 Flags that alter behaviour are:
355
356 \list
357 \li - Qt.ImhHiddenText - Characters should be hidden, as is typically used when entering passwords.
358 This is automatically set when setting echoMode to \c TextInput.Password.
359 \li - Qt.ImhSensitiveData - Typed text should not be stored by the active input method
360 in any persistent storage like predictive user dictionary.
361 \li - Qt.ImhNoAutoUppercase - The input method should not try to automatically switch to upper case
362 when a sentence ends.
363 \li - Qt.ImhPreferNumbers - Numbers are preferred (but not required).
364 \li - Qt.ImhPreferUppercase - Upper case letters are preferred (but not required).
365 \li - Qt.ImhPreferLowercase - Lower case letters are preferred (but not required).
366 \li - Qt.ImhNoPredictiveText - Do not use predictive text (i.e. dictionary lookup) while typing.
367
368 \li - Qt.ImhDate - The text editor functions as a date field.
369 \li - Qt.ImhTime - The text editor functions as a time field.
370 \endlist
371
372 Flags that restrict input (exclusive flags) are:
373
374 \list
375 \li - Qt.ImhDigitsOnly - Only digits are allowed.
376 \li - Qt.ImhFormattedNumbersOnly - Only number input is allowed. This includes decimal point and minus sign.
377 \li - Qt.ImhUppercaseOnly - Only upper case letter input is allowed.
378 \li - Qt.ImhLowercaseOnly - Only lower case letter input is allowed.
379 \li - Qt.ImhDialableCharactersOnly - Only characters suitable for phone dialing are allowed.
380 \li - Qt.ImhEmailCharactersOnly - Only characters suitable for email addresses are allowed.
381 \li - Qt.ImhUrlCharactersOnly - Only characters suitable for URLs are allowed.
382 \endlist
383
384 Masks:
385
386 \list
387 \li - Qt.ImhExclusiveInputMask - This mask yields nonzero if any of the exclusive flags are used.
388 \endlist
389
390 \qmlproperty enumeration inputMethodHints
391 */
392 property alias inputMethodHints: editor.inputMethodHints
393
394 /*!
395 Returns the total number of characters in the TextField item.
396
397 If the TextField has an inputMask the length will include mask characters
398 and may differ from the length of the string returned by the text property.
399
400 This property can be faster than querying the length the text property as
401 it doesn't require any copying or conversion of the TextField's internal
402 string data.
403
404 \qmlproperty int length
405 */
406 property alias length: editor.length
407
408 /*!
409 The maximum permitted length of the text in the TextField.
410
411 If the text is too long, it is truncated at the limit.
412
413 By default, this property contains a value of 32767.
414
415 \qmlproperty int maximumLength
416 */
417 property alias maximumLength: editor.maximumLength
418
419 /*!
420 Specifies how text should be selected using a mouse.
421 \list
422 \li -TextInput.SelectCharacters - The selection is updated with individual
423 characters. (Default)
424 \li -TextInput.SelectWords - The selection is updated with whole words.
425 \endlist
426 This property only applies when selectByMouse is true.
427
428 \qmlproperty enumeration mouseSelectionMode
429 */
430 property alias mouseSelectionMode: editor.mouseSelectionMode
431
432 /*!
433 Whether the TextField should keep its selection when it loses active focus
434 to another item in the scene. By default this is set to false.
435
436 \qmlproperty bool persistentSelection
437 */
438 property alias persistentSelection: editor.persistentSelection
439
440 /*!
441 Sets whether user input can modify the contents of the TextField.
442
443 If readOnly is set to true, then user input will not affect the
444 text property. Any bindings or attempts to set the text property
445 will still work.
446
447 \qmlproperty bool readOnly
448 */
449 property alias readOnly: editor.readOnly
450
451 /*!
452 Override the default rendering type for this component.
453
454 Supported render types are:
455 \list
456 \li - Text.QtRendering - (default)
457 \li - Text.NativeRendering
458 \endlist
459
460 Select Text.NativeRendering if you prefer text to look native on the target
461 platform and do not require advanced features such as transformation of the
462 text. Using such features in combination with the NativeRendering render type
463 will lend poor and sometimes pixelated results.
464
465 \qmlproperty enumeration renderType
466 */
467 property alias renderType: editor.renderType
468
469 /*!
470 Defaults to true.
471
472 If false, the user cannot use the mouse to select text, only can use it to
473 focus the input.
474 \qmlproperty bool selectByMouse
475 */
476 property alias selectByMouse: editor.selectByMouse
477
478 /*!
479 This read-only property provides the text currently selected in the text input.
480
481 It is equivalent to the following snippet, but is faster and easier to use.
482 \code
483 myTextInput.text.toString().substring(myTextInput.selectionStart,
484 myTextInput.selectionEnd);
485 \endcode
486
487 \qmlproperty string selectedText
488 */
489 readonly property alias selectedText: editor.selectedText
490
491 /*!
492 The cursor position before the first character in the current selection.
493
494 This property is read-only. To change the selection, use select(start,end),
495 selectAll(), or selectWord().
496
497 \qmlproperty int selectionStart
498 */
499 property alias selectionStart: editor.selectionStart
500
501 /*!
502 The cursor position after the last character in the current selection.
503
504 This property is read-only. To change the selection, use select(start,end),
505 selectAll(), or selectWord().
506
507 \qmlproperty int selectionEnd
508 */
509 property alias selectionEnd: editor.selectionEnd
510
511 /*!
512 The text in the TextField.
513
514 \qmlproperty string text
515 */
516 property alias text: editor.text
517
518 /*!
519 Allows you to set a validator on the TextInput. When a validator is set the
520 TextField will only accept input which leaves the text property in an acceptable
521 or intermediate state. The accepted signal will only be sent if the text is in
522 an acceptable state when enter is pressed.
523
524 Currently supported validators are IntValidator, DoubleValidator and RegExpValidator.
525 An example of using validators is shown below, which allows input of integers
526 between 11 and 31 into the text input:
527
528 \qml
529 import QtQuick 2.4
530 import Lomiri.Components 1.2
531 TextField{
532 validator: IntValidator{bottom: 11; top: 31;}
533 focus: true
534 }
535 \endqml
536
537 \sa acceptableInput, inputMask
538
539 \qmlproperty Validator validator
540 */
541 property alias validator: editor.validator
542
543 /*!
544 Sets the horizontal alignment of the text within the item's width and height.
545 By default, the text alignment follows the natural alignment of the text, for
546 example text that is read from left to right will be aligned to the left.
547
548 TextField does not have vertical alignment, as the natural height is exactly
549 the height of the single line of text. If you set the height manually to something
550 larger, TextInput will always be top aligned vertically. You can use anchors to
551 align it however you want within another item.
552
553 The valid values for horizontalAlignment are TextInput.AlignLeft,
554 TextInput.AlignRight and TextInput.AlignHCenter.
555
556 Valid values for verticalAlignment are TextInput.AlignTop (default), TextInput.AlignBottom
557 TextInput.AlignVCenter.
558
559 When using the attached property LayoutMirroring::enabled to mirror application
560 layouts, the horizontal alignment of text will also be mirrored. However,
561 the property horizontalAlignment will remain unchanged. To query the effective
562 horizontal alignment of TextField, use the read-only property effectiveHorizontalAlignment.
563
564 \qmlproperty enumeration horizontalAlignment
565 */
566 property alias horizontalAlignment: editor.horizontalAlignment
567 /*!
568 \qmlproperty enumeration effectiveHorizontalAlignment
569
570 See \l horizontalAlignment for details.
571 */
572 property alias effectiveHorizontalAlignment: editor.effectiveHorizontalAlignment
573 /*!
574 \qmlproperty enumeration verticalAlignment
575
576 See \l horizontalAlignment for details.
577 */
578 property alias verticalAlignment: editor.verticalAlignment
579
580 /*!
581 This is the character displayed when echoMode is set to Password or
582 PasswordEchoOnEdit. By default it is the unicode character 2022.
583
584 If this property is set to a string with more than one character, the first
585 character is used. If the string is empty, the value is ignored and the property
586 is not set.
587
588 \qmlproperty string passwordCharacter
589 */
590 property alias passwordCharacter: editor.passwordCharacter
591
592 /*!
593 The text highlight color, used behind selections.
594
595 \qmlproperty color selectionColor
596 */
597 property alias selectionColor: editor.selectionColor
598
599 /*!
600 The highlighted text color, used in selections.
601
602 \qmlproperty color selectedTextColor
603 */
604 property alias selectedTextColor: editor.selectedTextColor
605
606 /*!
607 This handler is called when the Return or Enter key is pressed. Note that if
608 there is a validator or inputMask set on the text input, the handler will only
609 be emitted if the input is in an acceptable state.
610 */
611 signal accepted()
612
613 /*!
614 \internal
615 If used with an action or Option accepting propagates the new text.
616 */
617 onAccepted: {
618 control.triggered(control.text)
619 }
620
621
622 /*!
623 Copies the currently selected text to the system clipboard.
624 */
625 function copy()
626 {
627 editor.copy();
628 }
629
630 /*!
631 Moves the currently selected text to the system clipboard.
632 */
633 function cut()
634 {
635 editor.cut();
636 }
637
638 /*!
639 Places the clipboard or the data given as parameter into the text input.
640 The selected text will be replaces with the data.
641 */
642 function paste(data)
643 {
644 if ((data !== undefined) && (typeof data === "string") && !editor.readOnly) {
645 var selTxt = editor.selectedText;
646 var txt = editor.text;
647 var pos = (selTxt !== "") ? txt.indexOf(selTxt) : editor.cursorPosition
648 if (selTxt !== "") {
649 editor.text = txt.substring(0, pos) + data + txt.substr(pos + selTxt.length);
650 } else {
651 editor.text = txt.substring(0, pos) + data + txt.substr(pos);
652 }
653 editor.cursorPosition = pos + data.length;
654 } else
655 editor.paste();
656 }
657
658 /*!
659 Removes active text selection.
660 */
661 function deselect()
662 {
663 editor.deselect();
664 }
665
666 /*!
667 Inserts \a text into the TextField at \a position.
668 */
669 function insert(position, text) {
670 editor.insert(position, text);
671 }
672
673 /*!
674 This function returns the character position at x pixels from the left of
675 the TextField. Position 0 is before the first character, position 1 is after
676 the first character but before the second, and so on until position text.length,
677 which is after all characters.
678
679 This means that for all x values before the first character this function returns 0,
680 and for all x values after the last character this function returns text.length.
681
682 The cursor position type specifies how the cursor position should be resolved.
683 \list
684 \li - TextInput.CursorBetweenCharacters - Returns the position between characters
685 that is nearest x.
686 \li - TextInput.CursorOnCharacter - Returns the position before the character that
687 is nearest x.
688 \endlist
689 */
690 function positionAt(x, position)
691 {
692 if (position == undefined)
693 return editor.positionAt(x);
694 else
695 return editor.positionAt(x, position);
696 }
697
698 /*!
699 This function takes a character position and returns the rectangle that the
700 cursor would occupy, if it was placed at that character position.
701
702 This is similar to setting the cursorPosition, and then querying the cursor
703 rectangle, but the cursorPosition is not changed.
704 */
705 function positionToRectangle(pos)
706 {
707 return editor.positionToRectangle(pos);
708 }
709
710 /*!
711 Causes the text from start to end to be selected.
712
713 If either start or end is out of range, the selection is not changed.
714
715 After calling this, selectionStart will become the lesser and selectionEnd
716 will become the greater (regardless of the order passed to this method).
717 */
718 function select(start, end)
719 {
720 editor.select(start, end);
721 }
722
723 /*!
724 Causes all text to be selected.
725 */
726 function selectAll()
727 {
728 editor.selectAll();
729 }
730
731 /*!
732 Causes the word closest to the current cursor position to be selected.
733 */
734 function selectWord()
735 {
736 editor.selectWord();
737 }
738
739 /*!
740 Returns true if the natural reading direction of the editor text found between
741 positions start and end is right to left.
742 */
743 function isRightToLeft(start, end) {
744 return editor.isRightToLeft(start, end);
745 }
746
747 /*!
748 Moves the cursor to position and updates the selection according to the
749 optional mode parameter. (To only move the cursor, set the cursorPosition property.)
750
751 When this method is called it additionally sets either the selectionStart
752 or the selectionEnd (whichever was at the previous cursor position) to the
753 specified position. This allows you to easily extend and contract the selected
754 text range.
755
756 The selection mode specifies whether the selection is updated on a per character
757 or a per word basis. If not specified the selection mode will default to
758 TextInput.SelectCharacters.
759 \list
760 \li - TextInput.SelectCharacters - Sets either the selectionStart or selectionEnd
761 (whichever was at the previous cursor position) to the specified position.
762 \li - TextInput.SelectWords - Sets the selectionStart and selectionEnd to
763 include all words between the specified position and the previous cursor
764 position. Words partially in the range are included.
765 \endlist
766
767 For example, take this sequence of calls:
768 \code
769 cursorPosition = 5
770 moveCursorSelection(9, TextInput.SelectCharacters)
771 moveCursorSelection(7, TextInput.SelectCharacters)
772 \endcode
773 This moves the cursor to position 5, extend the selection end from 5 to 9
774 and then retract the selection end from 9 to 7, leaving the text from position
775 5 to 7 selected (the 6th and 7th characters).
776
777 The same sequence with TextInput.SelectWords will extend the selection
778 start to a word boundary before or on position 5 and extend the selection
779 end to a word boundary on or past position 9.
780 */
781 function moveCursorSelection(position, mode) {
782 editor.moveCursorSelection(position, mode)
783 }
784
785 /*!
786 Redoes the last operation if redo is \l {canRedo}{available}.
787 */
788 function redo() {
789 editor.redo();
790 }
791
792 /*!
793 Undoes the last operation if undo is \l {canUndo}{available}. Deselects any current
794 selection, and updates the selection start to the current cursor position.
795 */
796 function undo() {
797 editor.undo();
798 }
799
800 /*!
801 Removes the section of text that is between the start and end positions from the TextField.
802 */
803 function remove(start, end) {
804 editor.remove(start, end);
805 }
806
807 /*!
808 Returns the section of text that is between the start and end positions.
809
810 If the TextField has an inputMask the length will include mask characters.
811 */
812 function getText(start, end) {
813 return editor.getText(start, end);
814 }
815
816 // internals
817
818 opacity: enabled ? 1.0 : 0.3
819 activeFocusOnPress: true
820
821 /*! \internal */
822 onVisibleChanged: {
823 if (!visible)
824 control.focus = false;
825 }
826
827 LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft
828 LayoutMirroring.childrenInherit: true
829
830 // grab clicks from the area between the frame and the input
831 MouseArea {
832 anchors.fill: parent
833 acceptedButtons: Qt.LeftButton | Qt.RightButton
834 // use it only when there is space between the frame and input
835 enabled: internal.spacing > 0
836 preventStealing: false
837 // forward mouse events to input so we can handle those uniformly
838 Lomiri.Mouse.forwardTo: [inputHandler]
839 }
840
841 Text { id: fontHolder }
842
843 //internals
844 QtObject {
845 id: internal
846 // array of borders in left, top, right, bottom order
847 property real spacing: control.__styleInstance.frameSpacing
848
849 property int type: action ? action.parameterType : Lomiri.Action.None
850 onTypeChanged: {
851 // Don't undo explicitly specified hints
852 if (inputMethodHints != Qt.ImhNone)
853 return
854
855 if (type == Lomiri.Action.Integer
856 || type == Lomiri.Action.Real)
857 inputMethodHints = Qt.ImhDigitsOnly
858 }
859 }
860
861 //left pane
862 Item {
863 id: leftPane
864 anchors {
865 left: parent.left
866 leftMargin: internal.spacing
867 verticalCenter: parent.verticalCenter
868 }
869 // the left pane width depends on its children width
870 height: parent.height
871 width: childrenRect.width
872 // Overlay needs to have priority
873 z: 1
874 onChildrenChanged: {
875 // reparenting
876 for (var i = 0; i < children.length; i++) {
877 children[i].parent = leftPane;
878 children[i].anchors.verticalCenter = verticalCenter;
879 }
880 }
881 }
882
883 // right pane
884 Item {
885 id: rightPane
886 anchors {
887 right: parent.right
888 rightMargin: internal.spacing
889 verticalCenter: parent.verticalCenter
890 }
891 // the right pane width depends on its children width
892 height: parent.height
893 width: childrenRect.width
894 // Overlay needs to have priority
895 z: 1
896 onChildrenChanged: {
897 // reparenting
898 for (var i = 0; i < children.length; i++) {
899 children[i].parent = rightPane;
900 children[i].anchors.verticalCenter = verticalCenter;
901 }
902 }
903 }
904
905 AbstractButton {
906 id: clearButton
907 objectName: "clear_button"
908 activeFocusOnPress: false
909
910 anchors {
911 top: parent.top
912 right: rightPane.left
913 bottom: parent.bottom
914 margins: internal.spacing
915 }
916 /* draggedItemMouseArea and dragger in TextCursor are reparented to the
917 TextField and end up being on top of the clear button.
918 Ensure that the clear button receives touch/mouse events first.
919 */
920 z: 100
921 width: visible ? icon.width : 0
922 visible: control.hasClearButton &&
923 !control.readOnly &&
924 (control.activeFocus && ((editor.text != "") || editor.inputMethodComposing))
925
926 Lomiri.Icon {
927 id: icon
928 anchors.verticalCenter: parent.verticalCenter
929 width: units.gu(2.5)
930 height: width
931 // use icon from icon-theme
932 name: control.hasClearButton && !control.readOnly ? "edit-clear" : ""
933 }
934
935 onClicked: editor.text = ""
936 }
937
938 // hint text
939 Label {
940 id: hint
941 verticalAlignment: Text.AlignVCenter
942 horizontalAlignment: editor.horizontalAlignment
943 anchors {
944 left: leftPane.right
945 right: clearButton.left
946 top: parent.top
947 bottom: parent.bottom
948 margins: internal.spacing
949 }
950 // hint is shown till user types something in the field
951 visible: (editor.text == "") && !editor.inputMethodComposing
952 color: Theme.palette.normal.backgroundText
953 font: editor.font
954 elide: Text.ElideRight
955 }
956
957
958 // text input
959 Flickable {
960 id: flicker
961 objectName: "input_scroller"
962 anchors {
963 left: leftPane.right
964 right: clearButton.left
965 top: parent.top
966 bottom: parent.bottom
967 margins: internal.spacing
968 }
969 topMargin: internal.spacing
970 // do not allow rebounding
971 boundsBehavior: Flickable.StopAtBounds
972 // need to forward events as events occurred on topMargin area are not grabbed by the MouseArea.
973 Lomiri.Mouse.forwardTo: [inputHandler]
974
975 clip: true
976 contentWidth: editor.contentWidth
977 contentHeight: editor.contentHeight
978
979 TextInput {
980 id: editor
981 objectName: "text_input"
982 // FocusScope will forward focus to this component
983 focus: true
984 anchors.verticalCenter: parent.verticalCenter
985 verticalAlignment: TextInput.AlignVCenter
986 width: flicker.width
987 height: flicker.height
988 cursorDelegate: TextCursor {
989 handler: inputHandler
990 }
991 color: control.__styleInstance.color
992 selectedTextColor: control.__styleInstance.selectedTextColor
993 selectionColor: control.__styleInstance.selectionColor
994 font.pixelSize: FontUtils.sizeToPixels("medium")
995 passwordCharacter: "\u2022"
996 // forward keys to the root element so it can be captured outside of it
997 // as well as to InputHandler to handle PageUp/PageDown keys
998 Keys.forwardTo: [control, inputHandler]
999
1000 // overrides
1001 selectByMouse: true
1002 activeFocusOnPress: true
1003 onActiveFocusChanged: if (!activeFocus && inputHandler.popover) PopupUtils.close(inputHandler.popover)
1004
1005 // input selection and navigation handling
1006 Lomiri.Mouse.forwardTo: [inputHandler]
1007 InputHandler {
1008 id: inputHandler
1009 anchors.fill: parent
1010 main: control
1011 input: editor
1012 flickable: flicker
1013 }
1014 }
1015 }
1016
1017 Component.onCompleted: {
1018 editor.accepted.connect(control.accepted);
1019 cursorPosition = 0;
1020 }
1021
1022 style: Theme.createStyleComponent("TextFieldStyle.qml", control)
1023 }