1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the Qt Components project.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
15 ** * Redistributions of source code must retain the above copyright
16 ** notice, this list of conditions and the following disclaimer.
17 ** * Redistributions in binary form must reproduce the above copyright
18 ** notice, this list of conditions and the following disclaimer in
19 ** the documentation and/or other materials provided with the
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 ** the names of its contributors may be used to endorse or promote
23 ** products derived from this software without specific prior written
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
48 property alias placeholderText: placeholder.text
49 property alias inputMethodHints: textInput.inputMethodHints
50 property alias font: textInput.font
51 property alias cursorPosition: textInput.cursorPosition
52 property alias readOnly: textInput.readOnly
53 property alias echoMode: textInput.echoMode
54 property alias acceptableInput: textInput.acceptableInput
55 property alias inputMask: textInput.inputMask
56 property alias validator: textInput.validator
57 property alias selectedText: textInput.selectedText
58 property alias selectionEnd: textInput.selectionEnd
59 property alias selectionStart: textInput.selectionStart
60 property alias text: textInput.text
61 property bool errorHighlight: !acceptableInput
62 property alias maximumLength: textInput.maximumLength
76 function select(start, end) {
77 textInput.select(start, end)
80 function selectAll() {
84 function selectWord() {
85 textInput.selectWord()
88 function positionAt(x) {
89 return textInput.positionAt(mapToItem(textInput, x, 0).x)
92 function positionToRectangle(pos) {
93 var rect = textInput.positionToRectangle(pos)
94 rect.x = mapFromItem(textInput, rect.x, 0).x
98 function openSoftwareInputPanel() {
99 textInput.openSoftwareInputPanel()
102 function closeSoftwareInputPanel() {
103 textInput.closeSoftwareInputPanel()
107 implicitWidth: platformLeftMargin + platformRightMargin + flick.tiny * 2 +
108 Math.max(privateStyle.textWidth(text, textInput.font), priv.minWidth)
109 implicitHeight: Math.max(privateStyle.textFieldHeight,
110 2 * platformStyle.paddingMedium + privateStyle.fontHeight(textInput.font))
112 property bool enabled: true // overriding due to QTBUG-15797 and related bugs
114 property real platformLeftMargin: platformStyle.paddingSmall
115 property real platformRightMargin: platformStyle.paddingSmall
116 property bool platformInverted: false
121 // Minimum width is either placeholder text lenght or 5 spaces on current font.
122 // Using placeholder text lenght as minimum width prevents implicit sized item from
123 // shrinking on focus gain.
124 property real minWidth: placeholder.text ? privateStyle.textWidth(placeholder.text, textInput.font)
125 : privateStyle.textWidth(" ", textInput.font)
127 function bg_postfix() {
128 if (root.errorHighlight)
130 else if (root.readOnly || !root.enabled)
132 else if (textInput.activeFocus)
142 source: privateStyle.imagePath("qtg_fr_textfield_" + priv.bg_postfix(), root.platformInverted)
144 left: platformStyle.borderSizeMedium
145 top: platformStyle.borderSizeMedium
146 right: platformStyle.borderSizeMedium
147 bottom: platformStyle.borderSizeMedium
156 left: root.left; leftMargin: root.platformLeftMargin
157 right: root.right; rightMargin: root.platformRightMargin
158 verticalCenter : root.verticalCenter
166 property real tiny: Math.round(platformStyle.borderSizeMedium / 2)
168 function ensureVisible(rect) {
169 if (rect.x >= 0 && Math.round(contentX) > Math.round(rect.x))
171 else if (Math.round(contentX + width) < Math.round(rect.x + rect.width))
172 contentX = rect.x + rect.width - width
176 left: parent.left; leftMargin: tiny
177 right: parent.right; rightMargin: tiny
178 verticalCenter : parent.verticalCenter
181 boundsBehavior: Flickable.StopAtBounds
182 contentWidth: textInput.paintedWidth
183 height: textInput.paintedHeight
185 onWidthChanged: ensureVisible(textInput.cursorRectangle)
188 id: textInput; objectName: "textInput"
190 property real paintedWidth: textInput.model.paintedWidth
191 property real paintedHeight: textInput.model.paintedHeight
193 // TODO: See TODO: Refactor implicit size...
194 property variant model: Text {
197 horizontalAlignment: textInput.horizontalAlignment
202 activeFocusOnPress: false
203 cursorVisible: activeFocus && !selectedText
204 enabled: root.enabled
205 color: root.platformInverted ? platformStyle.colorNormalLightInverted
206 : platformStyle.colorNormalDark
208 font { family: platformStyle.fontFamilyRegular; pixelSize: platformStyle.fontSizeMedium }
209 selectedTextColor: root.platformInverted ? platformStyle.colorNormalDarkInverted
210 : platformStyle.colorNormalLight
211 selectionColor: root.platformInverted ? platformStyle.colorTextSelectionInverted
212 : platformStyle.colorTextSelection
213 width: Math.max(flick.width, paintedWidth)
218 // De-focusing requires setting focus elsewhere, in this case editor's parent
220 root.parent.forceActiveFocus()
224 onCursorPositionChanged: flick.ensureVisible(textInput.cursorRectangle)
226 Keys.forwardTo: touchController
228 TextTouchController {
231 // selection handles require touch area geometry to differ from TextInput's geometry
234 leftMargin: -flick.tiny
235 verticalCenter : parent.verticalCenter
238 width: Math.max(root.width, flick.contentWidth + flick.tiny * 2)
239 editorScrolledX: flick.contentX - container.anchors.leftMargin - flick.tiny
241 copyEnabled: textInput.selectedText
242 cutEnabled: !textInput.readOnly && textInput.selectedText
243 platformInverted: root.platformInverted
244 Component.onCompleted: flick.movementEnded.connect(touchController.flickEnded)
245 Connections { target: screen; onCurrentOrientationChanged: touchController.updateGeometry() }
248 onHeightChanged: touchController.updateGeometry()
249 onWidthChanged: touchController.updateGeometry()
250 onHorizontalAlignmentChanged: touchController.updateGeometry()
251 onFontChanged: touchController.updateGeometry()
258 id: placeholder; objectName: "placeholder"
260 left: parent.left; leftMargin: flick.tiny
261 right: parent.right; rightMargin: flick.tiny
262 verticalCenter : parent.verticalCenter
264 color: root.platformInverted ? platformStyle.colorNormalMidInverted
265 : platformStyle.colorNormalMid
267 visible: (!textInput.activeFocus || readOnly) && !textInput.text && text