summaryrefslogtreecommitdiff
path: root/src/controls/TextField.qml
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@digia.com>2014-07-18 14:31:17 +0200
committerJ-P Nurmi <jpnurmi@digia.com>2014-07-18 16:43:16 +0200
commit1f8cccd791b0fe401692d5a6eaa865606e479cc5 (patch)
tree744cedd7052a5bbb849f26b4d44968cb8f16b367 /src/controls/TextField.qml
parent62babdaa5810c575215565dcc9086e869d272517 (diff)
downloadqtquickcontrols-1f8cccd791b0fe401692d5a6eaa865606e479cc5.tar.gz
TextField: refactor selection handles
Move the selection handles to a private type that can be re-used in SpinBox and editable ComboBox. Task-number: QTBUG-38934 Change-Id: Ib1eec3259d19ffea7aa8c2b76fed4e914f2ce3ec Reviewed-by: Liang Qi <liang.qi@digia.com>
Diffstat (limited to 'src/controls/TextField.qml')
-rw-r--r--src/controls/TextField.qml125
1 files changed, 6 insertions, 119 deletions
diff --git a/src/controls/TextField.qml b/src/controls/TextField.qml
index f74c6ed0..e318d796 100644
--- a/src/controls/TextField.qml
+++ b/src/controls/TextField.qml
@@ -584,7 +584,7 @@ Control {
This property holds whether the control is being hovered.
*/
- readonly property alias hovered: mouseArea.containsMouse
+ readonly property alias hovered: textInput.containsMouse
/*! \internal */
property alias __contentHeight: textInput.contentHeight
@@ -617,13 +617,16 @@ Control {
Behavior on opacity { NumberAnimation { duration: 90 } }
}
- TextInput {
+ TextInputWithHandles {
id: textInput
focus: true
- selectByMouse: textfield.selectByMouse && (!cursorHandle.delegate || !selectionHandle.delegate)
selectionColor: __panel ? __panel.selectionColor : "darkred"
selectedTextColor: __panel ? __panel.selectedTextColor : "white"
+ control: textfield
+ cursorHandle: __style ? __style.cursorHandle : undefined
+ selectionHandle: __style ? __style.selectionHandle : undefined
+
font: __panel ? __panel.font : TextSingleton.font
anchors.leftMargin: __panel ? __panel.leftMargin : 0
anchors.topMargin: __panel ? __panel.topMargin : 0
@@ -647,121 +650,5 @@ Control {
}
onEditingFinished: textfield.editingFinished()
-
- property bool blockRecursion: false
- property bool hasSelection: selectionStart !== selectionEnd
- readonly property int selectionPosition: selectionStart !== cursorPosition ? selectionStart : selectionEnd
-
- // force re-evaluation when selection moves:
- // - cyrsorRectangle changes => content scrolled
- // - contentWidth changes => text layout changed
- property rect selectionRectangle: cursorRectangle.x && contentWidth ? positionToRectangle(selectionPosition)
- : positionToRectangle(selectionPosition)
-
- onSelectionStartChanged: {
- if (!blockRecursion && selectionHandle.delegate) {
- blockRecursion = true
- selectionHandle.position = selectionPosition
- blockRecursion = false
- }
- }
-
- onCursorPositionChanged: {
- if (!blockRecursion && cursorHandle.delegate) {
- blockRecursion = true
- cursorHandle.position = cursorPosition
- blockRecursion = false
- }
- }
-
- function activate() {
- if (activeFocusOnPress) {
- forceActiveFocus()
- if (!readOnly)
- Qt.inputMethod.show()
- }
- cursorHandle.activate()
- selectionHandle.activate()
- }
-
- function moveHandles(cursor, selection) {
- blockRecursion = true
- Qt.inputMethod.commit()
- cursorPosition = cursor
- if (selection === -1) {
- selectWord()
- selection = selectionStart
- }
- selectionHandle.position = selection
- cursorHandle.position = cursorPosition
- blockRecursion = false
- }
- }
-
- MouseArea {
- id: mouseArea
- anchors.fill: parent
- hoverEnabled: true
- cursorShape: Qt.IBeamCursor
- acceptedButtons: textInput.selectByMouse ? Qt.NoButton : Qt.LeftButton
- onClicked: {
- var pos = textInput.positionAt(mouse.x, mouse.y)
- textInput.moveHandles(pos, pos)
- textInput.activate()
- }
- onPressAndHold: {
- var pos = textInput.positionAt(mouse.x, mouse.y)
- textInput.moveHandles(pos, textfield.selectByMouse ? -1 : pos)
- textInput.activate()
- }
- }
-
- TextHandle {
- id: selectionHandle
-
- editor: textInput
- control: textfield
- active: textfield.selectByMouse
- delegate: __style.selectionHandle
- maximum: cursorHandle.position - 1
- readonly property real selectionX: textInput.selectionRectangle.x
- x: textInput.x + (pressed ? Math.max(0, selectionX) : selectionX)
- y: textInput.selectionRectangle.y + textInput.y
- visible: pressed || (textInput.hasSelection && handleX + handleWidth >= -1 && handleX <= textfield.width + 1)
-
- onPositionChanged: {
- if (!textInput.blockRecursion) {
- textInput.blockRecursion = true
- textInput.select(selectionHandle.position, cursorHandle.position)
- if (pressed)
- textInput.ensureVisible(position)
- textInput.blockRecursion = false
- }
- }
- }
-
- TextHandle {
- id: cursorHandle
-
- editor: textInput
- control: textfield
- active: textfield.selectByMouse
- delegate: __style.cursorHandle
- minimum: textInput.hasSelection ? selectionHandle.position + 1 : -1
- x: textInput.cursorRectangle.x + textInput.x
- y: textInput.cursorRectangle.y + textInput.y
- visible: pressed || ((textInput.cursorVisible || textInput.hasSelection)
- && handleX + handleWidth >= -1 && handleX <= textfield.width + 1)
-
- onPositionChanged: {
- if (!textInput.blockRecursion) {
- textInput.blockRecursion = true
- if (!textInput.hasSelection)
- selectionHandle.position = cursorHandle.position
- Qt.inputMethod.commit()
- textInput.select(selectionHandle.position, cursorHandle.position)
- textInput.blockRecursion = false
- }
- }
}
}