Debian Codegraph @

src/imports/Components/1.3/TextField.qml

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