summaryrefslogtreecommitdiff
path: root/src/controls/TextField.qml
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@digia.com>2014-05-15 18:08:34 +0200
committerJ-P Nurmi <jpnurmi@digia.com>2014-06-16 18:35:40 +0200
commit55e3a5d301e9382a66daf97718d2907d80c7c427 (patch)
tree84826422123f5831f44919d1e7fbc8e876f19b04 /src/controls/TextField.qml
parente7af888e86ddfd0f9b18b03f5ecbf8425fc5b0c0 (diff)
downloadqtquickcontrols-55e3a5d301e9382a66daf97718d2907d80c7c427.tar.gz
TextField & TextArea: add support for selection handles
Task-number: QTBUG-38934 Change-Id: Id581a6e56d3461b0df476c2b35c3e642fd505fc9 Reviewed-by: Caroline Chao <caroline.chao@digia.com> Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
Diffstat (limited to 'src/controls/TextField.qml')
-rw-r--r--src/controls/TextField.qml122
1 files changed, 113 insertions, 9 deletions
diff --git a/src/controls/TextField.qml b/src/controls/TextField.qml
index a20a5c40..24f5e5e7 100644
--- a/src/controls/TextField.qml
+++ b/src/controls/TextField.qml
@@ -572,14 +572,6 @@ Control {
Accessible.role: Accessible.EditableText
Accessible.description: placeholderText
- MouseArea {
- id: mouseArea
- anchors.fill: parent
- hoverEnabled: true
- cursorShape: Qt.IBeamCursor
- onClicked: textfield.forceActiveFocus()
- }
-
Text {
id: placeholderTextComponent
anchors.fill: textInput
@@ -597,7 +589,7 @@ Control {
TextInput {
id: textInput
focus: true
- selectByMouse: Qt.platform.os !== "android" // Workaround for QTBUG-36515
+ selectByMouse: !cursorHandle.delegate || !selectionHandle.delegate
selectionColor: __panel ? __panel.selectionColor : "darkred"
selectedTextColor: __panel ? __panel.selectedTextColor : "white"
@@ -624,5 +616,117 @@ 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()
+ }
+ }
+
+ 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, -1)
+ textInput.activate()
+ }
+ }
+
+ TextHandle {
+ id: selectionHandle
+
+ editor: textInput
+ control: textfield
+ 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
+ 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
+ }
+ }
}
}