2011-07-20 22:56:36 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-02-06 13:37:56 +01:00
|
|
|
import QtQuick 2.0
|
2011-07-20 22:56:36 +02:00
|
|
|
import org.kde.plasma.core 0.1 as PlasmaCore
|
2011-12-23 14:23:58 +01:00
|
|
|
import "private" as Private
|
2011-07-20 22:56:36 +02:00
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* A plasma theme based text field widget.
|
|
|
|
*/
|
2012-04-25 12:03:03 +02:00
|
|
|
FocusScope {
|
2011-07-20 22:56:36 +02:00
|
|
|
id: textField
|
|
|
|
|
|
|
|
// Common API
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* Whether the text field is highlighted or not
|
|
|
|
*
|
|
|
|
* If it is true then the problematic lines will be highlighted.
|
|
|
|
*
|
|
|
|
* @warning Not implemented in Plasma components.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
property bool errorHighlight: false // TODO
|
2012-12-17 23:34:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* type:string
|
|
|
|
*
|
|
|
|
* The text displayed in when the text is empty.
|
|
|
|
*
|
|
|
|
* The default value is an empty string, meaning no placeholderText shown.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
property string placeholderText
|
2012-12-17 23:34:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* type:int
|
|
|
|
* The currently supported input method hints for the text field.
|
|
|
|
*
|
|
|
|
* The default values is Qt.ImhNone.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
property alias inputMethodHints: textInput.inputMethodHints
|
2012-12-17 23:34:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* type:font
|
|
|
|
* The font used in the text field.
|
|
|
|
*
|
|
|
|
* The default font value is the font from plasma desktop theme.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
property alias font: textInput.font
|
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* type:int
|
|
|
|
* This property holds the current cursor position.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
property alias cursorPosition: textInput.cursorPosition
|
2012-12-17 23:34:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* type:bool
|
|
|
|
* This property holds if the text field can be modified by the user interaction.
|
|
|
|
*
|
|
|
|
* The default value is false.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
property alias readOnly: textInput.readOnly
|
2012-12-17 23:34:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* type:enum
|
|
|
|
*
|
|
|
|
* Specifies how the text should be displayed in the TextField.
|
|
|
|
*
|
|
|
|
* The acceptable values are:
|
|
|
|
* - TextInput.Normal: Displays the text as it is. (Default)
|
|
|
|
* - TextInput.Password: Displays asterixes instead of characters.
|
|
|
|
* - TextInput.NoEcho: Displays nothing.
|
|
|
|
* - TextInput.PasswordEchoOnEdit: Displays all but the current character
|
|
|
|
* as asterixes.
|
|
|
|
*
|
|
|
|
* The default value is TextInput.Normal
|
|
|
|
*/
|
|
|
|
property alias echoMode: textInput.echoMode
|
|
|
|
|
|
|
|
|
2012-04-25 12:04:29 +02:00
|
|
|
property alias passwordCharacter: textInput.passwordCharacter
|
2011-07-20 22:56:36 +02:00
|
|
|
property alias acceptableInput: textInput.acceptableInput // read-only
|
2012-12-17 23:34:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* type:string
|
|
|
|
*
|
|
|
|
* Allows you to set an input mask on the TextField, restricting the
|
|
|
|
* allowable text inputs.
|
|
|
|
*
|
|
|
|
* See QLineEdit::inputMask for further details, as the exact same mask
|
|
|
|
* strings are used by TextInput.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
property alias inputMask: textInput.inputMask
|
2012-12-17 23:34:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* type:QtObject
|
|
|
|
*
|
|
|
|
* Allows you to set a validator on the TextField. When a validator is set
|
|
|
|
* the TextField will only accept input which leaves the text property in
|
|
|
|
* an acceptable or intermediate state. The accepted signal will only be
|
|
|
|
* sent if the text is in an acceptable state when enter is pressed.
|
|
|
|
*
|
|
|
|
* Currently supported validators are IntValidator, DoubleValidator and
|
|
|
|
* RegExpValidator.
|
|
|
|
*
|
|
|
|
* An example of using validators is shown below, which allows input of
|
|
|
|
* integers between 11 and 31 into the text input:
|
|
|
|
*
|
|
|
|
* @code
|
2013-02-06 13:37:56 +01:00
|
|
|
* import QtQuick 2.0
|
2012-12-17 23:34:31 +01:00
|
|
|
* TextInput {
|
|
|
|
* validator: IntValidator { bottom: 11; top: 31 }
|
|
|
|
* focus: true
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
property alias validator: textInput.validator
|
2012-12-17 23:34:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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: textInput.selectedText
|
|
|
|
|
|
|
|
/**
|
|
|
|
* type:int
|
|
|
|
* The cursor position after the last character in the current selection.
|
|
|
|
*
|
|
|
|
* This property is read-only.
|
|
|
|
*/
|
|
|
|
property alias selectionEnd: textInput.selectionEnd
|
|
|
|
|
|
|
|
/**
|
|
|
|
* type:int
|
|
|
|
* The cursor position before the first character in the current selection.
|
|
|
|
*
|
|
|
|
* This property is read-only.
|
|
|
|
*/
|
|
|
|
property alias selectionStart: textInput.selectionStart
|
|
|
|
|
|
|
|
/**
|
|
|
|
* type:string
|
|
|
|
* The text in the text field.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
property alias text: textInput.text
|
2012-12-17 23:34:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* type:int
|
|
|
|
*
|
|
|
|
* The maximum permitted length of the text in the TextField. If the text
|
|
|
|
* is too long, it is truncated at the limit.
|
|
|
|
*
|
|
|
|
* Default value is 32767.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
property alias maximumLength: textInput.maximumLength
|
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* Emitted when the text input is accepted.
|
|
|
|
*/
|
2012-03-22 12:57:36 +01:00
|
|
|
signal accepted()
|
2012-01-27 16:09:40 +01:00
|
|
|
|
2011-11-14 18:55:29 +01:00
|
|
|
//Plasma api
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* Whether the button to clear the text from TextField is visible.
|
|
|
|
*/
|
2011-11-14 18:55:29 +01:00
|
|
|
property bool clearButtonShown: false
|
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* Copies the currently selected text to the system clipboard.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
function copy() {
|
|
|
|
textInput.copy();
|
|
|
|
}
|
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* Replaces the currently selected text by the contents of the system
|
|
|
|
* clipboard.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
function paste() {
|
|
|
|
textInput.paste();
|
|
|
|
}
|
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* Moves the currently selected text to the system clipboard.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
function cut() {
|
|
|
|
textInput.cut();
|
|
|
|
}
|
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* 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 int start Start of selection
|
|
|
|
* @param int end End of selection
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
function select(start, end) {
|
|
|
|
textInput.select(start, end);
|
|
|
|
}
|
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* Causes all text to be selected.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
function selectAll() {
|
|
|
|
textInput.selectAll();
|
|
|
|
}
|
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* Causes the word closest to the current cursor position to be selected.
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
function selectWord() {
|
|
|
|
textInput.selectWord();
|
|
|
|
}
|
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* This function returns the character position at x pixels from the left
|
|
|
|
* of the TextField.
|
|
|
|
*
|
|
|
|
* 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 int pos x-coordinate we are interested in.
|
|
|
|
* @return int the character position
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
function positionAt(pos) {
|
2011-12-22 21:43:39 +01:00
|
|
|
return textInput.positionAt(pos);
|
2011-07-20 22:56:36 +02:00
|
|
|
}
|
|
|
|
|
2012-12-17 23:34:31 +01:00
|
|
|
/**
|
|
|
|
* 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 int pos the text position
|
|
|
|
* @param type:rectangle the cursor rectangle
|
|
|
|
*/
|
2011-07-20 22:56:36 +02:00
|
|
|
function positionToRectangle(pos) {
|
2011-12-22 21:43:39 +01:00
|
|
|
return textInput.positionToRectangle(pos);
|
2011-07-20 22:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set active focus to it's internal textInput.
|
|
|
|
// Overriding QtQuick.Item forceActiveFocus function.
|
|
|
|
function forceActiveFocus() {
|
|
|
|
textInput.forceActiveFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overriding QtQuick.Item activeFocus property.
|
|
|
|
property alias activeFocus: textInput.activeFocus
|
|
|
|
|
|
|
|
// TODO: fix default size
|
2012-10-11 16:57:54 +02:00
|
|
|
implicitWidth: theme.defaultFont.mSize.width*12 + base.internalPadding*2
|
|
|
|
implicitHeight: theme.defaultFont.mSize.height + base.internalPadding*2
|
2011-07-20 22:56:36 +02:00
|
|
|
// TODO: needs to define if there will be specific graphics for
|
|
|
|
// disabled text fields
|
|
|
|
opacity: enabled ? 1.0 : 0.5
|
|
|
|
|
2011-12-22 14:58:11 +01:00
|
|
|
Private.TextFieldFocus {
|
2011-11-08 14:18:12 +01:00
|
|
|
id: hover
|
|
|
|
state: textInput.activeFocus ? "focus" : (mouseWatcher.containsMouse ? "hover" : "hidden")
|
|
|
|
anchors.fill: base
|
|
|
|
}
|
|
|
|
|
2011-07-20 22:56:36 +02:00
|
|
|
PlasmaCore.FrameSvgItem {
|
|
|
|
id: base
|
|
|
|
|
|
|
|
// TODO: see what is the correct policy for margins
|
|
|
|
anchors.fill: parent
|
|
|
|
imagePath: "widgets/lineedit"
|
|
|
|
prefix: "base"
|
2012-10-11 16:57:54 +02:00
|
|
|
property real internalPadding: theme.defaultFont.mSize.height*0.3
|
2011-07-20 22:56:36 +02:00
|
|
|
}
|
|
|
|
|
2011-10-10 19:40:46 +02:00
|
|
|
MouseArea {
|
|
|
|
id: mouseWatcher
|
|
|
|
anchors.fill: hover
|
|
|
|
hoverEnabled: true
|
2012-04-25 12:03:03 +02:00
|
|
|
onClicked: {
|
|
|
|
// If we don't set focus on click here then clicking between the
|
|
|
|
// line of text and the bottom or top of the widget will not focus
|
|
|
|
// it.
|
|
|
|
textInput.forceActiveFocus();
|
|
|
|
}
|
2011-07-20 22:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Text {
|
2011-11-09 22:06:25 +01:00
|
|
|
anchors {
|
|
|
|
left: parent.left
|
|
|
|
right: parent.right
|
|
|
|
verticalCenter: parent.verticalCenter
|
2012-10-11 16:57:54 +02:00
|
|
|
leftMargin: base.margins.left + base.internalPadding
|
|
|
|
rightMargin: base.margins.right + base.internalPadding
|
2011-11-09 22:06:25 +01:00
|
|
|
}
|
2011-07-20 22:56:36 +02:00
|
|
|
text: placeholderText
|
|
|
|
visible: textInput.text == "" && !textField.activeFocus
|
|
|
|
// XXX: using textColor and low opacity for theming placeholderText
|
2012-06-06 12:16:44 +02:00
|
|
|
color: theme.buttonTextColor
|
2011-07-20 22:56:36 +02:00
|
|
|
opacity: 0.5
|
|
|
|
elide: Text.ElideRight
|
|
|
|
clip: true
|
2011-10-13 18:23:40 +02:00
|
|
|
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
|
2011-07-20 22:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TextInput {
|
|
|
|
id: textInput
|
|
|
|
|
|
|
|
anchors {
|
|
|
|
left: parent.left
|
|
|
|
right: parent.right
|
2011-11-08 20:54:13 +01:00
|
|
|
verticalCenter: parent.verticalCenter
|
2011-07-20 22:56:36 +02:00
|
|
|
// TODO: see what is the correct policy for margins
|
2012-10-11 16:57:54 +02:00
|
|
|
leftMargin: base.margins.left + base.internalPadding
|
|
|
|
rightMargin: base.margins.right + (clearButton.opacity > 0 ? clearButton.width : 0) + base.internalPadding
|
2011-07-20 22:56:36 +02:00
|
|
|
}
|
2012-04-25 12:04:29 +02:00
|
|
|
passwordCharacter: "•"
|
2011-11-08 20:54:13 +01:00
|
|
|
selectByMouse: true
|
2012-06-06 12:16:44 +02:00
|
|
|
color: theme.buttonTextColor
|
2011-07-20 22:56:36 +02:00
|
|
|
enabled: textField.enabled
|
|
|
|
clip: true
|
2012-04-25 12:03:03 +02:00
|
|
|
focus: true
|
2011-11-30 19:48:54 +01:00
|
|
|
onActiveFocusChanged: {
|
|
|
|
if (!textField.activeFocus) {
|
|
|
|
textInput.closeSoftwareInputPanel()
|
|
|
|
}
|
|
|
|
}
|
2012-01-27 16:09:40 +01:00
|
|
|
onAccepted: textField.accepted()
|
2012-11-10 17:47:07 +01:00
|
|
|
Keys.forwardTo: textField
|
2011-07-20 22:56:36 +02:00
|
|
|
}
|
2011-11-14 18:55:29 +01:00
|
|
|
|
2012-11-26 20:49:16 +01:00
|
|
|
PlasmaCore.IconItem {
|
2011-11-30 17:49:29 +01:00
|
|
|
id: clearButton
|
2012-01-13 20:10:34 +01:00
|
|
|
source: "edit-clear-locationbar-rtl"
|
|
|
|
height: Math.max(textInput.height, theme.smallIconSize)
|
|
|
|
width: height
|
2011-11-14 18:55:29 +01:00
|
|
|
opacity: (textInput.text != "" && clearButtonShown) ? 1 : 0
|
|
|
|
Behavior on opacity {
|
|
|
|
NumberAnimation {
|
|
|
|
duration: 250
|
|
|
|
easing.type: Easing.InOutQuad
|
|
|
|
}
|
|
|
|
}
|
|
|
|
anchors {
|
|
|
|
right: parent.right
|
|
|
|
rightMargin: y
|
|
|
|
verticalCenter: textInput.verticalCenter
|
|
|
|
}
|
|
|
|
MouseArea {
|
|
|
|
anchors.fill: parent
|
|
|
|
onClicked: {
|
|
|
|
textInput.text = ""
|
|
|
|
textInput.forceActiveFocus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 16:40:10 +01:00
|
|
|
}
|