~ubuntu-branches/ubuntu/vivid/lightdm-kde/vivid

« back to all changes in this revision

Viewing changes to themes/classic/TextField.qml

  • Committer: Package Import Robot
  • Author(s): Michal Zajac
  • Date: 2012-07-22 19:44:44 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120722194444-aaueaylu3ddnqcth
Tags: 0.2.1-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
*   Copyright (C) 2011 by Daker Fernandes Pinheiro <dakerfp@gmail.com>
 
3
*
 
4
*   This program is free software; you can redistribute it and/or modify
 
5
*   it under the terms of the GNU Library General Public License as
 
6
*   published by the Free Software Foundation; either version 2, or
 
7
*   (at your option) any later version.
 
8
*
 
9
*   This program is distributed in the hope that it will be useful,
 
10
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
*   GNU General Public License for more details
 
13
*
 
14
*   You should have received a copy of the GNU Library General Public
 
15
*   License along with this program; if not, write to the
 
16
*   Free Software Foundation, Inc.,
 
17
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
*/
 
19
 
 
20
/**Documented API
 
21
Inherits:
 
22
        Item
 
23
 
 
24
Imports:
 
25
        QtQuick 1.1
 
26
 
 
27
Description:
 
28
        Creates a simple plasma theme based text field widget.
 
29
 
 
30
Properties:
 
31
      * font font:
 
32
        This property holds the font used in the text field.
 
33
    The default font value is the font from plasma desktop theme.
 
34
 
 
35
      * Qt::InputMethodHints inputMethodHints:
 
36
        This property holds the the currently supported input method hints
 
37
     for the text field.
 
38
     The default values is Qt.ImhNone.
 
39
 
 
40
      * bool errorHighlight:
 
41
        This property holds if the text field is highlighted or not
 
42
    If it is true then the problematic lines will be highlighted.
 
43
    This feature is defined in the Common API but is unimplemented in plasma components.
 
44
 
 
45
      * int cursorPosition:
 
46
        This property holds the current cursor position.
 
47
 
 
48
      * bool readOnly:
 
49
        This property holds if the text field can be modified by the user interaction.
 
50
    The default value is false.
 
51
 
 
52
      * string selectedText:
 
53
        This property holds the text selected by the user.
 
54
    If no text is selected it holds an empty string.
 
55
    This property is read-only.
 
56
 
 
57
      * int selectionEnd:
 
58
        This property holds the cursor position after the last character in the current selection.
 
59
    This property is read-only.
 
60
 
 
61
      * int selectionStart:
 
62
        This property holds the cursor position before the first character in the current selection.
 
63
    This property is read-only.
 
64
 
 
65
      * string text:
 
66
        This property holds the entire text in the text field.
 
67
 
 
68
      * string placeholderText:
 
69
        This property holds the text displayed in when the text is empty.
 
70
    The default value is empty string, meaning no placeholderText shown.
 
71
 
 
72
      * enumeration echoMode:
 
73
        This property specifies how the text should be displayed in the TextField.
 
74
    The acceptable values are:
 
75
        - TextInput.Normal - Displays the text as it is. (Default)
 
76
        - TextInput.Password - Displays asterixes instead of characters.
 
77
        - TextInput.NoEcho - Displays nothing.
 
78
        - TextInput.PasswordEchoOnEdit - Displays all but the current character as asterixes.
 
79
    The default value is TextInput.Normal
 
80
 
 
81
      * string inputMask:
 
82
        Allows you to set an input mask on the TextField, restricting the allowable text inputs.
 
83
    See QLineEdit::inputMask for further details, as the exact same mask strings are used by TextInput.
 
84
 
 
85
      * Validator validator:
 
86
        Allows you to set a validator on the TextField. When a validator is set the TextField
 
87
    will only accept input which leaves the text property in an acceptable or intermediate state.
 
88
    The accepted signal will only be sent if the text is in an acceptable state when enter is pressed.
 
89
    Currently supported validators are IntValidator, DoubleValidator and RegExpValidator.
 
90
    An example of using validators is shown below, which allows input of integers
 
91
    between 11 and 31 into the text input:
 
92
    <code>
 
93
    import QtQuick 1.0
 
94
    TextInput {
 
95
        validator: IntValidator{bottom: 11; top: 31;}
 
96
        focus: true
 
97
    }
 
98
    </code>
 
99
 
 
100
      * int maximumLength:
 
101
        The maximum permitted length of the text in the TextField.
 
102
    If the text is too long, it is truncated at the limit.
 
103
    By default, this property contains a value of 32767.
 
104
 
 
105
       * bool acceptableInput:
 
106
         This property is always true unless a validator or input mask has been set.
 
107
    If a validator or input mask has been set, this property will only be true if the current
 
108
    text is acceptable to the validator or input mask as a final string (not as an intermediate string).
 
109
    This property is always true unless a validator has been set.
 
110
    If a validator has been set, this property will only be true if the current text is acceptable to the
 
111
    validator as a final string (not as an intermediate string).
 
112
    This property is read-only.
 
113
 
 
114
       * bool clearButtonShown:
 
115
         Holds if the button to clear the text from TextField is visible.
 
116
Signals:
 
117
       * accepted():
 
118
        This signal is emitted when the text input is accepted.
 
119
 
 
120
Methods:
 
121
       * void copy():
 
122
         Copies the currently selected text to the system clipboard.
 
123
 
 
124
       * void cut():
 
125
         Moves the currently selected text to the system clipboard.
 
126
 
 
127
       * void deselect():
 
128
         Removes active text selection.
 
129
 
 
130
       * void paste():
 
131
         Replaces the currently selected text by the contents of the system clipboard.
 
132
 
 
133
       * void select(int start, int end):
 
134
         Causes the text from start to end to be selected.
 
135
     If either start or end is out of range, the selection is not changed.
 
136
     After calling this, selectionStart will become the lesser and selectionEnd will become the greater
 
137
     (regardless of the order passed to this method).
 
138
 
 
139
       * void selectAll():
 
140
         Causes all text to be selected.
 
141
 
 
142
       * void selectWord():
 
143
         Causes the word closest to the current cursor position to be selected.
 
144
 
 
145
       * void positionAt(int position):
 
146
         This function returns the character position at x pixels from the left of the TextField.
 
147
     Position 0 is before the first character, position 1 is after the first character but before the second,
 
148
     and so on until position text.length, which is after all characters.
 
149
     This means that for all x values before the first character this function returns 0,
 
150
     and for all x values after the last character this function returns text.length.
 
151
 
 
152
       * rectangle positionToRectangle(position):
 
153
         Returns the rectangle at the given position in the text.
 
154
     The x, y, and height properties correspond to the cursor that would describe that position.
 
155
**/
 
156
 
 
157
import QtQuick 1.1
 
158
import org.kde.plasma.core 0.1 as PlasmaCore
 
159
import "private" as Private
 
160
 
 
161
FocusScope {
 
162
    id: textField
 
163
 
 
164
    // Common API
 
165
    property bool errorHighlight: false // TODO
 
166
    property string placeholderText
 
167
    property alias inputMethodHints: textInput.inputMethodHints
 
168
    property alias font: textInput.font
 
169
 
 
170
    property alias cursorPosition: textInput.cursorPosition
 
171
    property alias readOnly: textInput.readOnly
 
172
    property alias echoMode: textInput.echoMode // Supports TextInput.Normal,TextInput.Password, TextInput.NoEcho, TextInput.PasswordEchoOnEdit
 
173
    property alias passwordCharacter: textInput.passwordCharacter
 
174
    property alias acceptableInput: textInput.acceptableInput // read-only
 
175
    property alias inputMask: textInput.inputMask
 
176
    property alias validator: textInput.validator
 
177
    property alias selectedText: textInput.selectedText // read-only
 
178
    property alias selectionEnd: textInput.selectionEnd // read-only
 
179
    property alias selectionStart: textInput.selectionStart // read-only
 
180
    property alias text: textInput.text
 
181
    property alias maximumLength: textInput.maximumLength
 
182
 
 
183
    signal accepted
 
184
 
 
185
    //Plasma api
 
186
    property bool clearButtonShown: false
 
187
 
 
188
    function copy() {
 
189
        textInput.copy();
 
190
    }
 
191
 
 
192
    function paste() {
 
193
        textInput.paste();
 
194
    }
 
195
 
 
196
    function cut() {
 
197
        textInput.cut();
 
198
    }
 
199
 
 
200
    function select(start, end) {
 
201
        textInput.select(start, end);
 
202
    }
 
203
 
 
204
    function selectAll() {
 
205
        textInput.selectAll();
 
206
    }
 
207
 
 
208
    function selectWord() {
 
209
        textInput.selectWord();
 
210
    }
 
211
 
 
212
    function positionAt(pos) {
 
213
        return textInput.positionAt(pos);
 
214
    }
 
215
 
 
216
    function positionToRectangle(pos) {
 
217
        return textInput.positionToRectangle(pos);
 
218
    }
 
219
 
 
220
 
 
221
    // Set active focus to it's internal textInput.
 
222
    // Overriding QtQuick.Item forceActiveFocus function.
 
223
    function forceActiveFocus() {
 
224
        textInput.forceActiveFocus();
 
225
    }
 
226
 
 
227
    // Overriding QtQuick.Item activeFocus property.
 
228
    property alias activeFocus: textInput.activeFocus
 
229
 
 
230
    // TODO: fix default size
 
231
    implicitWidth: theme.defaultFont.mSize.width*12
 
232
    implicitHeight: theme.defaultFont.mSize.height*1.6
 
233
    // TODO: needs to define if there will be specific graphics for
 
234
    //     disabled text fields
 
235
    opacity: enabled ? 1.0 : 0.5
 
236
 
 
237
    Private.TextFieldFocus {
 
238
        id: hover
 
239
        state: textInput.activeFocus ? "focus" : (mouseWatcher.containsMouse ? "hover" : "hidden")
 
240
        anchors.fill: base
 
241
    }
 
242
 
 
243
    PlasmaCore.FrameSvgItem {
 
244
        id: base
 
245
 
 
246
        // TODO: see what is the correct policy for margins
 
247
        anchors.fill: parent
 
248
        imagePath: "widgets/lineedit"
 
249
        prefix: "base"
 
250
    }
 
251
 
 
252
    MouseArea {
 
253
        id: mouseWatcher
 
254
        anchors.fill: hover
 
255
        hoverEnabled: true
 
256
        onClicked: {
 
257
            textInput.forceActiveFocus();
 
258
        }
 
259
    }
 
260
 
 
261
    Text {
 
262
        anchors {
 
263
            left: parent.left
 
264
            right: parent.right
 
265
            verticalCenter: parent.verticalCenter
 
266
            leftMargin: 2 * base.margins.left
 
267
            rightMargin: 2 * base.margins.right
 
268
        }
 
269
        text: placeholderText
 
270
        visible: textInput.text == "" && !textField.activeFocus
 
271
        // XXX: using textColor and low opacity for theming placeholderText
 
272
        color: theme.viewTextColor
 
273
        opacity: 0.5
 
274
        elide: Text.ElideRight
 
275
        clip: true
 
276
        font.capitalization: theme.defaultFont.capitalization
 
277
        font.family: theme.defaultFont.family
 
278
        font.italic: theme.defaultFont.italic
 
279
        font.letterSpacing: theme.defaultFont.letterSpacing
 
280
        font.pointSize: theme.defaultFont.pointSize
 
281
        font.strikeout: theme.defaultFont.strikeout
 
282
        font.underline: theme.defaultFont.underline
 
283
        font.weight: theme.defaultFont.weight
 
284
        font.wordSpacing: theme.defaultFont.wordSpacing
 
285
    }
 
286
 
 
287
    TextInput {
 
288
        id: textInput
 
289
 
 
290
        anchors {
 
291
            left: parent.left
 
292
            right: parent.right
 
293
            verticalCenter: parent.verticalCenter
 
294
            // TODO: see what is the correct policy for margins
 
295
            leftMargin: 2 * base.margins.left
 
296
            rightMargin: 2 * base.margins.right + (clearButton.opacity > 0 ? clearButton.width : 0)
 
297
        }
 
298
        passwordCharacter: "•"
 
299
        selectByMouse: true
 
300
        color: theme.textColor
 
301
        enabled: textField.enabled
 
302
        clip: true
 
303
        focus: true
 
304
        onActiveFocusChanged: {
 
305
            if (!textField.activeFocus) {
 
306
                textInput.closeSoftwareInputPanel()
 
307
            }
 
308
        }
 
309
        onAccepted: textField.accepted()
 
310
    }
 
311
 
 
312
    Private.IconLoader {
 
313
        id: clearButton
 
314
        source: "edit-clear-locationbar-rtl"
 
315
        height: Math.max(textInput.height, theme.smallIconSize)
 
316
        width: height
 
317
        opacity: (textInput.text != "" && clearButtonShown) ? 1 : 0
 
318
        Behavior on opacity {
 
319
            NumberAnimation {
 
320
                duration: 250
 
321
                easing.type: Easing.InOutQuad
 
322
            }
 
323
        }
 
324
        anchors {
 
325
            right: parent.right
 
326
            rightMargin: y
 
327
            verticalCenter: textInput.verticalCenter
 
328
        }
 
329
        MouseArea {
 
330
            anchors.fill: parent
 
331
            onClicked: {
 
332
                textInput.text = ""
 
333
                textInput.forceActiveFocus()
 
334
            }
 
335
        }
 
336
    }
 
337
}