File: TextArea.qml

package info (click to toggle)
kde-runtime 4%3A4.14.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 24,712 kB
  • ctags: 11,598
  • sloc: cpp: 108,653; ansic: 2,447; perl: 1,580; xml: 793; sh: 378; python: 28; makefile: 13
file content (429 lines) | stat: -rw-r--r-- 12,998 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
*   Copyright (C) 2011 by Daker Fernandes Pinheiro <dakerfp@gmail.com>
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU Library General Public License as
*   published by the Free Software Foundation; either version 2, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details
*
*   You should have received a copy of the GNU Library General Public
*   License along with this program; if not, write to the
*   Free Software Foundation, Inc.,
*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

import QtQuick 1.1
import org.kde.plasma.core 0.1 as PlasmaCore
import "private" as Private

/**
 * A plasma theme based text area.
 */
Item {
    id: textArea

    // Common API
    /**
     * type:font
     * Font used in the text field.
     *
     * The default font value is the font from plasma desktop theme.
     */
    property alias font: textEdit.font

    /**
     * The currently supported input method hints for the text field.
     *
     * The default values is Qt.ImhNone.
     */
    property int inputMethodHints

    /**
     * Whether the text field is highlighted or not.
     *
     * If it is true then the problematic lines will be highlighted.
     *
     * @warning unimplemented in plasma components.
     */
    property bool errorHighlight

    /**
     * type:int
     * Current cursor position.
     */
    property alias cursorPosition: textEdit.cursorPosition

    /**
     * type:enum
     * Sets the horizontal alignment of the text within the TextArea item's
     * width and height.
     *
     * By default, the text alignment follows the natural alignment of the
     * text, for example text that is read from left to right will be aligned
     * to the left.
     *
     * Valid values:
     *
     * - TextEdit.AlignLeft (default)
     * - TextEdit.AlignRight
     * - TextEdit.AlignHCenter
     * - TextEdit.AlignJustify
     */
    property alias horizontalAlignment: textEdit.horizontalAlignment

    /**
     * type:enum
     * Sets the vertical alignment of the text within the TextArea item's width
     * and height.
     *
     * Valid values:
     *
     * - TextEdit.AlignTop (default)
     * - TextEdit.AlignBottom
     * - TextEdit.AlignVCenter
     */
    property alias verticalAlignment: textEdit.verticalAlignment

    /**
     * type:bool
     * Whether the TextArea is in read-only mode, and cannot be modified by the
     * user.
     *
     * The default value is false.
     */
    property alias readOnly: textEdit.readOnly

    /**
     * type:string
     * The text selected by the user.  If no text is selected it holds an empty
     * string.
     *
     * This property is read-only.
     */
    property alias selectedText: textEdit.selectedText

    /**
     * type:int
     * The cursor position after the last character in the current selection.
     *
     * This property is read-only.
     */
    property alias selectionEnd: textEdit.selectionEnd

    /**
     * type:int
     * The cursor position before the first character in the current selection.
     *
     * This property is read-only.
     */
    property alias selectionStart: textEdit.selectionStart


    /**
     * type:string
     * The text in the TextArea.
     */
    property alias text: textEdit.text

    /**
     * type:enum
     * The way the text property should be displayed.
     *
     * Valid values:
     *
     * - TextEdit.AutoText
     * - TextEdit.PlainText
     * - TextEdit.RichText
     * - TextEdit.StyledText
     *
     * The default is TextEdit.AutoText. If the text format is
     * TextEdit.AutoText the text edit will automatically determine whether the
     * text should be treated as rich text. This determination is made using
     * Qt::mightBeRichText().
     */
    property alias textFormat: textEdit.textFormat

    /**
     * type:enum
     * Set this property to wrap the text to the TextArea item's width. The
     * text will only wrap if an explicit width has been set.
     *
     * Valid values:
     *
     * - TextEdit.NoWrap: no wrapping will be performed.  If the text contains
     *   insufficient newlines, then implicitWidth will exceed a set width.
     * - TextEdit.WordWrap: wrapping is done on word boundaries only.  If a
     *   word is too long, implicitWidth will exceed a set width.
     * - TextEdit.WrapAnywhere: wrapping is done at any point on a line, even
     *   if it occurs in the middle of a word.
     * - TextEdit.Wrap: if possible, wrapping occurs at a word boundary;
     *   otherwise it will occur at the appropriate point on the line, even in
     *   the middle of a word.
     *
     * The default is TextEdit.NoWrap. If you set a width, consider using
     * TextEdit.Wrap.
     */
    property alias wrapMode: textEdit.wrapMode

    /**
     * The text displayed when the text property is empty.
     *
     * The default value is an empty string, meaning no placeholderText shown.
     */
    property string placeholderText

    // functions
    /**
     * Copies the currently selected text to the system clipboard.
     */
    function copy() {
        textEdit.copy();
    }
    /**
     * Replaces the currently selected text by the contents of the system
     * clipboard.
     */
    function paste() {
        textEdit.paste();
    }

    /**
     * Moves the currently selected text to the system clipboard.
     */
    function cut() {
        textEdit.cut();
    }

    /**
     * Causes the text from start to end to be selected.
     *
     * If either start or end is out of range, the selection is not changed.
     * After calling this, selectionStart will become the lesser and
     * selectionEnd will become the greater (regardless of the order passed to
     * this method).
     *
     * @param type:int start Start of selection
     * @param type:int end End of selection
     */
    function select(start, end) {
        textEdit.select(start, end);
    }

    /**
     * Causes all text to be selected.
     */
    function selectAll() {
        textEdit.selectAll();
    }

    /**
     * Causes the word closest to the current cursor position to be selected.
     */
    function selectWord() {
        textEdit.selectWord();
    }

    /**
     * This function returns the character position at x pixels from the left
     * of the TextArea.
     *
     * Position 0 is before the first character, position 1 is after the first
     * character but before the second, and so on until position text.length,
     * which is after all characters.  This means that for all x values before
     * the first character this function returns 0, and for all x values after
     * the last character this function returns text.length.
     *
     * @param type:int pos x-coordinate we are interested in.
     * @return type:int the character position
     */
    // Does this work at all? doc for TextEdit says positionAt() accepts two
    // ints: x and y
    function positionAt(pos) {
        return textEdit.positionAt(pos);
    }

    /**
     * Returns the rectangle at the given position in the text.
     *
     * The x, y, and height properties correspond to the cursor that would
     * describe that position.
     *
     * @param type:int pos the text position
     * @return type:rectangle the cursor rectangle
     */
    function positionToRectangle(pos) {
        return textEdit.positionToRectangle(pos);
    }

    // Plasma API
    /**
     * type:bool
     * This property describes whether the user can interact with the TextArea
     * flicking content. A user cannot drag or flick a TextArea that is not
     * interactive. This property is useful for temporarily disabling
     * flicking.
     */
    property alias interactive: flickArea.interactive

    /**
     * type:int
     * Maximum width that the text content can have.
     */
    property alias contentMaxWidth: textEdit.width

    /**
     * type:int
     * Maximum height that the text content can have.
     */
    property alias contentMaxHeight: textEdit.height

    // Set active focus to it's internal textInput.
    // Overriding QtQuick.Item forceActiveFocus function.
    function forceActiveFocus() {
        textEdit.forceActiveFocus();
    }

    // Overriding QtQuick.Item activeFocus property.
    property alias activeFocus: textEdit.activeFocus

    opacity: enabled ? 1.0 : 0.5

    Private.TextFieldFocus {
        id: hover
        state: textArea.activeFocus ? "focus" : (mouseWatcher.containsMouse ? "hover" : "hidden")
        anchors.fill: base
    }

    MouseArea {
        id: mouseWatcher
        anchors.fill: hover
        hoverEnabled: true
    }

    PlasmaCore.FrameSvgItem {
        id: base

        // TODO: see what is the best policy for margins
        anchors {
            fill: parent
        }
        imagePath: "widgets/lineedit"
        prefix: "base"
    }

    Flickable {
        id: flickArea
        anchors {
            fill: parent
            leftMargin: 2 * base.margins.left
            rightMargin: 2 * base.margins.right + (verticalScroll.visible ? verticalScroll.width : 0)
            topMargin: 2 * base.margins.top
            bottomMargin: 2 * base.margins.bottom + (horizontalScroll.visible ? verticalScroll.width : 0)
        }
        interactive: !verticalScroll.interactive //textArea.activeFocus
        contentWidth: {
            if (textEdit.wrapMode == TextEdit.NoWrap)
                return textEdit.paintedWidth;

            return Math.min(textEdit.paintedWidth, textEdit.width);
        }
        contentHeight: Math.min(textEdit.paintedHeight, textEdit.height)
        clip: true

        TextEdit {
            id: textEdit

            width: flickArea.width
            height: flickArea.height
            clip: true
            wrapMode: TextEdit.Wrap
            enabled: textArea.enabled
            font.capitalization: theme.defaultFont.capitalization
            font.family: theme.defaultFont.family
            font.italic: theme.defaultFont.italic
            font.letterSpacing: theme.defaultFont.letterSpacing
            font.pointSize: theme.defaultFont.pointSize
            font.strikeout: theme.defaultFont.strikeout
            font.underline: theme.defaultFont.underline
            font.weight: theme.defaultFont.weight
            font.wordSpacing: theme.defaultFont.wordSpacing
            color: theme.buttonTextColor
            selectByMouse: verticalScroll.interactive

            onCursorPositionChanged: {
                if (cursorRectangle.x < flickArea.contentX) {
                    flickArea.contentX = cursorRectangle.x;
                    return;
                }

                if (cursorRectangle.x > flickArea.contentX +
                    flickArea.width - cursorRectangle.width) {
                    flickArea.contentX = cursorRectangle.x -
                        cursorRectangle.width;
                    return;
                }

                if (cursorRectangle.y < flickArea.contentY) {
                    flickArea.contentY = cursorRectangle.y;
                    return;
                }

                if (cursorRectangle.y > flickArea.contentY +
                    flickArea.height - cursorRectangle.height) {
                    flickArea.contentY = cursorRectangle.y -
                        cursorRectangle.height;
                    return;
                }
            }

            // Proxying keys events  is not required by the
            //     common API but is desired in the plasma API.
            Keys.onPressed: textArea.Keys.pressed(event);
            Keys.onReleased: textArea.Keys.released(event);

            Text {
                anchors.fill: parent
                text: textArea.placeholderText
                visible: textEdit.text == "" && !textArea.activeFocus
                color: theme.buttonTextColor
                opacity: 0.5
            }
            onActiveFocusChanged: {
                if (!textEdit.activeFocus) {
                    textEdit.closeSoftwareInputPanel()
                }
            }
        }
    }

    ScrollBar {
        id: horizontalScroll
        anchors {
            bottom: parent.bottom
            left: parent.left
            right: flickArea.right
        }
        enabled: parent.enabled
        flickableItem: flickArea
        orientation: Qt.Horizontal
        stepSize: textEdit.font.pixelSize
    }

    ScrollBar {
        id: verticalScroll
        anchors {
            right: parent.right
            top: parent.top
            bottom: flickArea.bottom
        }
        enabled: parent.enabled
        flickableItem: flickArea
        orientation: Qt.Vertical
        stepSize: textEdit.font.pixelSize
    }
}