added android components
[mardrone] / mardrone / imports / com / nokia / android.1.1 / TextField.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Qt Components project.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
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
20 **     distribution.
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
24 **     permission.
25 **
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."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 1.1
42 import "." 1.1
43
44 FocusScope {
45     id: root
46
47     // Common Public API
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
63
64     function copy() {
65         textInput.copy()
66     }
67
68     function paste() {
69         textInput.paste()
70     }
71
72     function cut() {
73         textInput.cut()
74     }
75
76     function select(start, end) {
77         textInput.select(start, end)
78     }
79
80     function selectAll() {
81         textInput.selectAll()
82     }
83
84     function selectWord() {
85         textInput.selectWord()
86     }
87
88     function positionAt(x) {
89         return textInput.positionAt(mapToItem(textInput, x, 0).x)
90     }
91
92     function positionToRectangle(pos) {
93         var rect = textInput.positionToRectangle(pos)
94         rect.x = mapFromItem(textInput, rect.x, 0).x
95         return rect;
96     }
97
98     function openSoftwareInputPanel() {
99         textInput.openSoftwareInputPanel()
100     }
101
102     function closeSoftwareInputPanel() {
103         textInput.closeSoftwareInputPanel()
104     }
105
106     // API extensions
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))
111
112     property bool enabled: true // overriding due to QTBUG-15797 and related bugs
113
114     property real platformLeftMargin: platformStyle.paddingSmall
115     property real platformRightMargin: platformStyle.paddingSmall
116     property bool platformInverted: false
117
118     // Private data
119     QtObject {
120         id: priv
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)
126
127         function bg_postfix() {
128             if (root.errorHighlight)
129                 return "error"
130             else if (root.readOnly || !root.enabled)
131                 return "uneditable"
132             else if (textInput.activeFocus)
133                 return "highlighted"
134             else
135                 return "editable"
136         }
137     }
138
139     BorderImage {
140         id: frame
141         anchors.fill: parent
142         source: privateStyle.imagePath("qtg_fr_textfield_" + priv.bg_postfix(), root.platformInverted)
143         border {
144             left: platformStyle.borderSizeMedium
145             top: platformStyle.borderSizeMedium
146             right: platformStyle.borderSizeMedium
147             bottom: platformStyle.borderSizeMedium
148         }
149         smooth: true
150     }
151
152     Item {
153         id: container
154
155         anchors {
156             left: root.left; leftMargin: root.platformLeftMargin
157             right: root.right; rightMargin: root.platformRightMargin
158             verticalCenter : root.verticalCenter
159         }
160         clip: true
161         height: root.height
162
163         Flickable {
164             id: flick
165
166             property real tiny: Math.round(platformStyle.borderSizeMedium / 2)
167
168             function ensureVisible(rect) {
169                 if (rect.x >= 0 && Math.round(contentX) > Math.round(rect.x))
170                     contentX = rect.x
171                 else if (Math.round(contentX + width) < Math.round(rect.x + rect.width))
172                   contentX = rect.x + rect.width - width
173             }
174
175             anchors {
176                 left: parent.left; leftMargin: tiny
177                 right: parent.right; rightMargin: tiny
178                 verticalCenter : parent.verticalCenter
179             }
180
181             boundsBehavior: Flickable.StopAtBounds
182             contentWidth: textInput.paintedWidth
183             height: textInput.paintedHeight
184
185             onWidthChanged: ensureVisible(textInput.cursorRectangle)
186
187             TextInput {
188                 id: textInput; objectName: "textInput"
189
190                 property real paintedWidth: textInput.model.paintedWidth
191                 property real paintedHeight: textInput.model.paintedHeight
192
193                 // TODO: See TODO: Refactor implicit size...
194                 property variant model: Text {
195                     font: textInput.font
196                     text: textInput.text
197                     horizontalAlignment: textInput.horizontalAlignment
198                     visible: false
199                     opacity: 0
200                 }
201
202                 activeFocusOnPress: false
203                 cursorVisible: activeFocus && !selectedText
204                 enabled: root.enabled
205                 color: root.platformInverted ? platformStyle.colorNormalLightInverted
206                                              : platformStyle.colorNormalDark
207                 focus: true
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)
214
215                 onEnabledChanged: {
216                     if (!enabled) {
217                         deselect()
218                         // De-focusing requires setting focus elsewhere, in this case editor's parent
219                         if (root.parent)
220                             root.parent.forceActiveFocus()
221                     }
222                 }
223
224                 onCursorPositionChanged: flick.ensureVisible(textInput.cursorRectangle)
225
226                 Keys.forwardTo: touchController
227
228                 TextTouchController {
229                     id: touchController
230
231                     //  selection handles require touch area geometry to differ from TextInput's geometry
232                     anchors {
233                         left: parent.left;
234                         leftMargin: -flick.tiny
235                         verticalCenter : parent.verticalCenter
236                     }
237                     height: root.height
238                     width: Math.max(root.width, flick.contentWidth + flick.tiny * 2)
239                     editorScrolledX: flick.contentX - container.anchors.leftMargin - flick.tiny
240                     editorScrolledY: 0
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() }
246                     Connections {
247                         target: textInput
248                         onHeightChanged: touchController.updateGeometry()
249                         onWidthChanged: touchController.updateGeometry()
250                         onHorizontalAlignmentChanged: touchController.updateGeometry()
251                         onFontChanged: touchController.updateGeometry()
252                     }
253                 }
254             }
255         }
256
257         Text {
258             id: placeholder; objectName: "placeholder"
259             anchors {
260                 left: parent.left; leftMargin: flick.tiny
261                 right: parent.right; rightMargin: flick.tiny
262                 verticalCenter : parent.verticalCenter
263             }
264             color: root.platformInverted ? platformStyle.colorNormalMidInverted
265                                          : platformStyle.colorNormalMid
266             font: textInput.font
267             visible: (!textInput.activeFocus || readOnly) && !textInput.text && text
268         }
269
270     }
271 }