| 1 |
/* |
| 2 |
* Copyright 2014 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
import Lomiri.Components 1.3 |
| 19 |
import Lomiri.Components.Popups 1.3 |
| 20 |
|
| 21 |
/* |
| 22 |
This component is a unified text selection and scrolling handler for both |
| 23 |
TextField and TextArea components. |
| 24 |
*/ |
| 25 |
|
| 26 |
MultiPointTouchArea { |
| 27 |
id: inputHandler |
| 28 |
objectName: "input_handler" |
| 29 |
|
| 30 |
// the root control |
| 31 |
property Item main |
| 32 |
// the input instance |
| 33 |
property Item input |
| 34 |
// the Flickable holding the input instance |
| 35 |
property Flickable flickable |
| 36 |
|
| 37 |
// item filling the visible text input area used to check handler visibility |
| 38 |
property Item visibleArea: Item { |
| 39 |
parent: flickable |
| 40 |
anchors.fill: parent |
| 41 |
} |
| 42 |
|
| 43 |
// line size and spacing |
| 44 |
property real lineSpacing: units.dp(3) |
| 45 |
property real lineSize: input.font.pixelSize + lineSpacing |
| 46 |
// input x/y distance from the frame |
| 47 |
property point frameDistance: Qt.point(flickable.x, flickable.y) |
| 48 |
|
| 49 |
// signal triggered when popup should be opened |
| 50 |
signal pressAndHold(int pos, bool fromTouch) |
| 51 |
signal tap(int pos) |
| 52 |
property string oldText: "" |
| 53 |
signal textModified() |
| 54 |
|
| 55 |
function activateInput() { |
| 56 |
if (!input.activeFocus) { |
| 57 |
input.forceActiveFocus(); |
| 58 |
} |
| 59 |
showInputPanel(); |
| 60 |
} |
| 61 |
|
| 62 |
function showInputPanel() { |
| 63 |
if (!LomiriApplication.inputMethod.visible) { |
| 64 |
LomiriApplication.inputMethod.show(); |
| 65 |
} |
| 66 |
textChanged = false; |
| 67 |
} |
| 68 |
|
| 69 |
// internal properties/functions |
| 70 |
readonly property bool singleLine: input.hasOwnProperty("validator") |
| 71 |
property var flickableList: new Array() |
| 72 |
property bool textChanged: false |
| 73 |
property var popover |
| 74 |
property bool suppressReleaseEvent: false |
| 75 |
property int pressedPosition: -1 |
| 76 |
// move properties |
| 77 |
property int moveStarts: -1 |
| 78 |
property int moveEnds: -1 |
| 79 |
// set scroller to the first Flickable that scrolls the input |
| 80 |
// this can be the internal Flickable if the full autosize is disabled |
| 81 |
// or one of the input's parent Flickable |
| 82 |
readonly property bool scrollingDisabled: main && main.hasOwnProperty("autoSize") ? |
| 83 |
(main.autoSize && (main.maximumLineCount <= 0)) : false |
| 84 |
onScrollingDisabledChanged: if (state == "") flickable.interactive = !scrollingDisabled |
| 85 |
readonly property Flickable grandScroller: firstFlickableParent(main) |
| 86 |
readonly property Flickable scroller: (scrollingDisabled && grandScroller) ? grandScroller : flickable |
| 87 |
|
| 88 |
// ensures the text cursorRectangle is always in the internal Flickable's visible area |
| 89 |
function ensureVisible(rect) |
| 90 |
{ |
| 91 |
if (flickable.moving || flickable.flicking) |
| 92 |
return; |
| 93 |
if (flickable.contentX >= rect.x) |
| 94 |
flickable.contentX = rect.x; |
| 95 |
else if (flickable.contentX + flickable.width <= rect.x + rect.width) |
| 96 |
flickable.contentX = rect.x + rect.width - flickable.width; |
| 97 |
if (flickable.contentY >= rect.y) |
| 98 |
flickable.contentY = rect.y; |
| 99 |
else if (flickable.contentY + flickable.height <= rect.y + rect.height) |
| 100 |
flickable.contentY = rect.y + rect.height - flickable.height; |
| 101 |
} |
| 102 |
// returns the cursor position from x,y pair |
| 103 |
function unadulteratedCursorPosition(x, y) { |
| 104 |
return singleLine ? input.positionAt(x, TextInput.CursorOnCharacter) : input.positionAt(x, y, TextInput.CursorOnCharacter); |
| 105 |
} |
| 106 |
// returns the cursor position taking frame into account |
| 107 |
function cursorPosition(x, y) { |
| 108 |
var frameSpacing = main.__styleInstance.frameSpacing; |
| 109 |
var cursorPosition = unadulteratedCursorPosition(x, y); |
| 110 |
if (cursorPosition == 0) |
| 111 |
cursorPosition = unadulteratedCursorPosition(x + frameSpacing, y + frameSpacing); |
| 112 |
if (cursorPosition == text.length) |
| 113 |
cursorPosition = unadulteratedCursorPosition(x - frameSpacing, y - frameSpacing); |
| 114 |
return cursorPosition |
| 115 |
} |
| 116 |
|
| 117 |
// returns the mouse position |
| 118 |
function mousePosition(mouse) { |
| 119 |
return cursorPosition(mouse.x, mouse.y); |
| 120 |
} |
| 121 |
// checks whether the position is in the selected text |
| 122 |
function positionInSelection(pos) { |
| 123 |
return (input.selectionStart !== input.selectionEnd) |
| 124 |
&& (pos >= Math.min(input.selectionStart, input.selectionEnd)) |
| 125 |
&& (pos <= Math.max(input.selectionStart, input.selectionEnd)); |
| 126 |
} |
| 127 |
|
| 128 |
// check whether the mouse is inside a selected text area |
| 129 |
function mouseInSelection(mouse) { |
| 130 |
var pos = mousePosition(mouse); |
| 131 |
return positionInSelection(pos); |
| 132 |
} |
| 133 |
// selects text |
| 134 |
function selectText(mouse) { |
| 135 |
state = "select"; |
| 136 |
moveEnds = mousePosition(mouse); |
| 137 |
if (moveStarts < 0) { |
| 138 |
moveStarts = moveEnds; |
| 139 |
} |
| 140 |
input.select(moveStarts, moveEnds); |
| 141 |
} |
| 142 |
// returns the first Flickable parent of a given item |
| 143 |
function firstFlickableParent(item) { |
| 144 |
var p = item ? item.parent : null; |
| 145 |
while (p && !p.hasOwnProperty("flicking")) { |
| 146 |
p = p.parent; |
| 147 |
} |
| 148 |
return p; |
| 149 |
} |
| 150 |
// focuses the input if not yet focused, and shows the context menu |
| 151 |
function openContextMenu(mouse, noAutoselect) { |
| 152 |
var pos = mousePosition(mouse); |
| 153 |
if (!main.focus || !mouseInSelection(mouse)) { |
| 154 |
activateInput(); |
| 155 |
input.cursorPosition = pressedPosition = mousePosition(mouse); |
| 156 |
if (!noAutoselect) { |
| 157 |
input.selectWord(); |
| 158 |
} |
| 159 |
} |
| 160 |
// open context menu at the cursor position |
| 161 |
inputHandler.pressAndHold(input.cursorPosition, mouse.touch); |
| 162 |
// if opened with left press (touch falls into this criteria as well), we need to set state to inactive |
| 163 |
// so the mouse moves won't result in selected text loss/change |
| 164 |
if (mouse.button === Qt.LeftButton) { |
| 165 |
state = "inactive"; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// disables interactive Flickable parents, stops at the first non-interactive flickable. |
| 170 |
function toggleFlickablesInteractive(turnOn) { |
| 171 |
var p; |
| 172 |
if (!turnOn) { |
| 173 |
// handle the scroller separately |
| 174 |
p = firstFlickableParent(scroller) |
| 175 |
while (p) { |
| 176 |
if (p.interactive) { |
| 177 |
flickableList.push(p); |
| 178 |
p.interactive = false; |
| 179 |
} else { |
| 180 |
break; |
| 181 |
} |
| 182 |
p = firstFlickableParent(p); |
| 183 |
} |
| 184 |
} else { |
| 185 |
while (flickableList.length > 0) { |
| 186 |
p = flickableList.pop(); |
| 187 |
p.interactive = true; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// moves the specified position, called by the cursor handler |
| 193 |
// positioner = "currentPosition/selectionStart/selectionEnd" |
| 194 |
function positionCaret(positioner, x, y) { |
| 195 |
if (positioner === "cursorPosition") { |
| 196 |
input[positioner] = cursorPosition(x, y); |
| 197 |
} else { |
| 198 |
var pos = cursorPosition(x, y); |
| 199 |
if (positioner === "selectionStart" && (pos < input.selectionEnd)) { |
| 200 |
input.select(pos, input.selectionEnd); |
| 201 |
} else if (positioner === "selectionEnd" && (pos > input.selectionStart)) { |
| 202 |
input.select(input.selectionStart, pos); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
// moves the cursor one page forward with or without positioning the cursor |
| 208 |
function movePage(forward) { |
| 209 |
var cx = input.cursorRectangle.x; |
| 210 |
var cy = input.cursorRectangle.y; |
| 211 |
if (forward) { |
| 212 |
if (singleLine) { |
| 213 |
cx += visibleArea.width; |
| 214 |
} else { |
| 215 |
cy += visibleArea.height; |
| 216 |
} |
| 217 |
} else { |
| 218 |
if (singleLine) { |
| 219 |
cx -= visibleArea.width; |
| 220 |
} else { |
| 221 |
cy -= visibleArea.height; |
| 222 |
} |
| 223 |
} |
| 224 |
input.cursorPosition = cursorPosition(cx, cy); |
| 225 |
} |
| 226 |
|
| 227 |
Component.onCompleted: { |
| 228 |
state = (main.focus) ? "" : "inactive"; |
| 229 |
// FIXME: Qt5.3 related! mouseEnabled is a 5.3 related property which has a positive |
| 230 |
// default value. That value messes up the current understanding (5.2) of the |
| 231 |
// MultiPointTouchArea functioning. We need to set it to false until 5.3 will be the |
| 232 |
// default supported Qt version, when we can have a clean property value assignment |
| 233 |
if (inputHandler.hasOwnProperty("mouseEnabled")) { |
| 234 |
inputHandler.mouseEnabled = false; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// states |
| 239 |
states: [ |
| 240 |
// override default state to turn on the saved Flickable interactive mode |
| 241 |
State { |
| 242 |
name: "" |
| 243 |
StateChangeScript { |
| 244 |
// restore interactive for all Flickable parents |
| 245 |
script: toggleFlickablesInteractive(true); |
| 246 |
} |
| 247 |
}, |
| 248 |
State { |
| 249 |
name: "inactive" |
| 250 |
// we do not disable scroller here as in case the internal scrolling |
| 251 |
// is disabled (scrollingDisabled = true) the outer scroller (grandScroller) |
| 252 |
// would be blocked as well, which we don't want to |
| 253 |
PropertyChanges { |
| 254 |
target: flickable |
| 255 |
interactive: false |
| 256 |
} |
| 257 |
}, |
| 258 |
State { |
| 259 |
name: "scrolling" |
| 260 |
StateChangeScript { |
| 261 |
script: { |
| 262 |
// stop scrolling all the parents |
| 263 |
toggleFlickablesInteractive(false); |
| 264 |
// stop touch timers |
| 265 |
touchPoint.reset(); |
| 266 |
} |
| 267 |
} |
| 268 |
}, |
| 269 |
State { |
| 270 |
name: "select" |
| 271 |
// during select state all the flickables are blocked (interactive = false) |
| 272 |
// we can use scroller here as we need to disable the outer scroller too! |
| 273 |
PropertyChanges { |
| 274 |
target: scroller |
| 275 |
interactive: false |
| 276 |
} |
| 277 |
StateChangeScript { |
| 278 |
script: { |
| 279 |
// turn off interactive for all parent flickables |
| 280 |
toggleFlickablesInteractive(false); |
| 281 |
if (!positionInSelection(pressedPosition)) { |
| 282 |
input.cursorPosition = pressedPosition; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
] |
| 288 |
|
| 289 |
// brings the state back to default when the component looses focus |
| 290 |
// and ensures input has active focus when component regains focus |
| 291 |
Connections { |
| 292 |
target: main |
| 293 |
ignoreUnknownSignals: true |
| 294 |
function onActiveFocusChanged() { |
| 295 |
input.focus = main.activeFocus; |
| 296 |
} |
| 297 |
function onKeyNavigationFocusChanged() { |
| 298 |
if (main.keyNavigationFocus) { |
| 299 |
input.forceActiveFocus(); |
| 300 |
} |
| 301 |
} |
| 302 |
function onFocusChanged() { |
| 303 |
LomiriApplication.inputMethod.commit() |
| 304 |
state = (main.focus) ? "" : "inactive"; |
| 305 |
if (main.focus) { |
| 306 |
input.forceActiveFocus() |
| 307 |
} |
| 308 |
} |
| 309 |
function onVisibleChanged() { |
| 310 |
if (!main.visible) |
| 311 |
main.focus = false; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// input specific signals |
| 316 |
Connections { |
| 317 |
target: input |
| 318 |
function onCursorRectangleChanged() { ensureVisible(input.cursorRectangle) } |
| 319 |
function onTextChanged() { |
| 320 |
inputHandler.textChanged = true; |
| 321 |
if (inputHandler.oldText != input.text) { |
| 322 |
inputHandler.textModified() |
| 323 |
inputHandler.oldText = input.text |
| 324 |
} |
| 325 |
} |
| 326 |
// make sure we show the OSK |
| 327 |
function onActiveFocusChanged() { |
| 328 |
if (!input.activeFocus && popover) |
| 329 |
PopupUtils.close(popover); |
| 330 |
showInputPanel(); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
// inner or outer Flickable controlling |
| 335 |
Connections { |
| 336 |
target: scroller |
| 337 |
// turn scrolling state on |
| 338 |
function onFlickStarted() { toggleScrollingState(true) } |
| 339 |
function onMovementStarted() { toggleScrollingState(true) } |
| 340 |
// reset to default state |
| 341 |
function onMovementEnded() { toggleScrollingState(false) } |
| 342 |
|
| 343 |
function toggleScrollingState(turnOn) { |
| 344 |
if (!main.focus) { |
| 345 |
return; |
| 346 |
} |
| 347 |
inputHandler.state = (turnOn) ? "scrolling" : "" |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
// PageUp and PageDown handling |
| 352 |
Keys.onPressed: { |
| 353 |
if (event.key === Qt.Key_PageUp && event.modifiers === Qt.NoModifier) { |
| 354 |
movePage(false); |
| 355 |
} else if (event.key === Qt.Key_PageDown && event.modifiers === Qt.NoModifier) { |
| 356 |
movePage(true); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// touch and mous handling |
| 361 |
function handlePressed(event) { |
| 362 |
if (event.touch) { |
| 363 |
// we do not have longTap or double tap, therefore we need to generate those |
| 364 |
event.touch(); |
| 365 |
} else { |
| 366 |
// consume event so it does not get forwarded to the input |
| 367 |
event.accepted = true; |
| 368 |
} |
| 369 |
// remember pressed position as we need it when entering into selection state |
| 370 |
pressedPosition = mousePosition(event); |
| 371 |
} |
| 372 |
function handleReleased(event) { |
| 373 |
if (event.touch) { |
| 374 |
event.untouch(); |
| 375 |
} |
| 376 |
if ((!main.focus && !main.activeFocusOnPress) || suppressReleaseEvent === true) { |
| 377 |
suppressReleaseEvent = false; |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
activateInput(); |
| 382 |
if (state === "" || event.touch) { |
| 383 |
input.cursorPosition = mousePosition(event); |
| 384 |
} |
| 385 |
moveStarts = moveEnds = -1; |
| 386 |
state = ""; |
| 387 |
// check if we get right-click from the frame or the area that has no text |
| 388 |
if (event.button === Qt.RightButton) { |
| 389 |
// open the popover |
| 390 |
inputHandler.pressAndHold(input.cursorPosition, event.touch); |
| 391 |
} else { |
| 392 |
inputHandler.tap(input.cursorPosition); |
| 393 |
} |
| 394 |
} |
| 395 |
function handleMove(event) { |
| 396 |
// leave if not focus, not the left button or not in select state |
| 397 |
if (!input.activeFocus || (!event.touch && event.button !== Qt.LeftButton) || !main.selectByMouse) { |
| 398 |
return; |
| 399 |
} |
| 400 |
selectText(event); |
| 401 |
} |
| 402 |
function handleDblClick(event) { |
| 403 |
if (main.selectByMouse) { |
| 404 |
openContextMenu(event, false); |
| 405 |
// turn selection state temporarily so the selection is not cleared on release |
| 406 |
state = "selection"; |
| 407 |
suppressReleaseEvent = true; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
// Mouse handling |
| 412 |
Mouse.forwardTo: [main] |
| 413 |
Mouse.onPressed: handlePressed(mouse) |
| 414 |
Mouse.onReleased: handleReleased(mouse) |
| 415 |
Mouse.onPositionChanged: handleMove(mouse) |
| 416 |
Mouse.onDoubleClicked: handleDblClick(mouse) |
| 417 |
|
| 418 |
// right button handling |
| 419 |
MouseArea { |
| 420 |
anchors.fill: parent |
| 421 |
acceptedButtons: Qt.RightButton |
| 422 |
// trigger pressAndHold |
| 423 |
onReleased: openContextMenu(mouse, true) |
| 424 |
cursorShape: Qt.IBeamCursor |
| 425 |
} |
| 426 |
Keys.onMenuPressed: inputHandler.pressAndHold(input.cursorPosition, false); |
| 427 |
|
| 428 |
// touch handling |
| 429 |
touchPoints: TouchPoint { |
| 430 |
id: touchPoint |
| 431 |
function touch() { |
| 432 |
longTap.restart(); |
| 433 |
if (!doubleTap.running) { |
| 434 |
doubleTap.restart(); |
| 435 |
} else if (doubleTap.tapCount > 0) { |
| 436 |
doubleTap.running = false; |
| 437 |
handleDblClick(touchPoint, true); |
| 438 |
} |
| 439 |
} |
| 440 |
function untouch() { |
| 441 |
longTap.running = false; |
| 442 |
} |
| 443 |
function reset() { |
| 444 |
longTap.running = false; |
| 445 |
doubleTap.running = false; |
| 446 |
} |
| 447 |
} |
| 448 |
Timer { |
| 449 |
id: longTap |
| 450 |
// sync with QQuickMouseArea constant |
| 451 |
interval: 800 |
| 452 |
onTriggered: { |
| 453 |
// do not open context menu if the input is not focus |
| 454 |
if (!main.focus) { |
| 455 |
return; |
| 456 |
} |
| 457 |
|
| 458 |
// do not open context menu if this is scrolling |
| 459 |
if (touchPoint.startY - touchPoint.y < -units.gu(2)) |
| 460 |
return; |
| 461 |
|
| 462 |
openContextMenu(touchPoint, false); |
| 463 |
suppressReleaseEvent = true; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
property bool doubleTapInProgress: doubleTap.running |
| 468 |
Timer { |
| 469 |
id: doubleTap |
| 470 |
property int tapCount: 0 |
| 471 |
interval: 400 |
| 472 |
onRunningChanged: { |
| 473 |
tapCount = running; |
| 474 |
} |
| 475 |
} |
| 476 |
onPressed: handlePressed(touchPoints[0]) |
| 477 |
onReleased: handleReleased(touchPoints[0]) |
| 478 |
|
| 479 |
property Item cursorPositionCursor: null |
| 480 |
property Item selectionStartCursor: null |
| 481 |
property Item selectionEndCursor: null |
| 482 |
|
| 483 |
// cursors to use when text is selected |
| 484 |
Connections { |
| 485 |
target: input |
| 486 |
function onSelectedTextChanged() { |
| 487 |
if (selectedText !== "") { |
| 488 |
if (!selectionStartCursor) { |
| 489 |
selectionStartCursor = input.cursorDelegate.createObject( |
| 490 |
input, { |
| 491 |
"positionProperty": "selectionStart", |
| 492 |
"handler": inputHandler, |
| 493 |
} |
| 494 |
); |
| 495 |
moveSelectionCursor(selectionStartCursor); |
| 496 |
selectionEndCursor = input.cursorDelegate.createObject( |
| 497 |
input, { |
| 498 |
"positionProperty": "selectionEnd", |
| 499 |
"handler": inputHandler, |
| 500 |
} |
| 501 |
); |
| 502 |
moveSelectionCursor(selectionEndCursor); |
| 503 |
} |
| 504 |
} else { |
| 505 |
if (selectionStartCursor) { |
| 506 |
selectionStartCursor.destroy(); |
| 507 |
selectionStartCursor = null; |
| 508 |
selectionEndCursor.destroy(); |
| 509 |
selectionEndCursor = null; |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
function onSelectionStartChanged() { moveSelectionCursor(selectionStartCursor, true); } |
| 514 |
function onSelectionEndChanged() { moveSelectionCursor(selectionEndCursor, true); } |
| 515 |
|
| 516 |
function moveSelectionCursor(cursor, updateProperty) { |
| 517 |
if (!cursor) { |
| 518 |
return; |
| 519 |
} |
| 520 |
// workaround for https://bugreports.qt-project.org/browse/QTBUG-38704 |
| 521 |
// selectedTextChanged signal is not emitted for TextEdit when selectByMouse is false |
| 522 |
if (updateProperty && QuickUtils.className(input) === "QQuickTextEdit") { |
| 523 |
input.selectedTextChanged(); |
| 524 |
} |
| 525 |
|
| 526 |
var pos = input.positionToRectangle(input[cursor.positionProperty]); |
| 527 |
cursor.x = pos.x; |
| 528 |
cursor.y = pos.y; |
| 529 |
cursor.height = pos.height; |
| 530 |
ensureVisible(pos); |
| 531 |
} |
| 532 |
} |
| 533 |
} |