SelectionControl.qml 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2016 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
  3. import QtQuick
  4. import QtQuick.VirtualKeyboard
  5. Item {
  6. id: root
  7. property bool handleIsMoving: false
  8. property var inputContext: InputContext
  9. visible: enabled && (inputContext.selectionControlVisible || handleIsMoving) && !InputContext.animating
  10. Loader {
  11. id: anchorHandle
  12. sourceComponent: keyboard.style.selectionHandle
  13. Behavior on opacity {
  14. NumberAnimation { duration: 200 }
  15. }
  16. opacity: inputContext !== null && inputContext.anchorRectIntersectsClipRect ? 1.0 : 0.0
  17. MouseArea {
  18. width: parent.width * 2
  19. height: width * 1.12
  20. anchors.centerIn: parent
  21. onPositionChanged: function(mouse) {
  22. // we don't move the handles, the handles will move as the selection changes.
  23. // The middle of a handle is mapped to the middle of the line above it
  24. root.handleIsMoving = true
  25. var xx = x + anchorHandle.x + mouse.x
  26. var yy = y + anchorHandle.y + mouse.y - (anchorHandle.height + inputContext.anchorRectangle.height)/2
  27. var x2 = cursorHandle.x + cursorHandle.width/2
  28. var y2 = cursorHandle.y - inputContext.cursorRectangle.height/2
  29. inputContext.setSelectionOnFocusObject(Qt.point(xx,yy), Qt.point(x2,y2))
  30. }
  31. onReleased: {
  32. root.handleIsMoving = false
  33. }
  34. }
  35. }
  36. // selection cursor handle
  37. Loader {
  38. id: cursorHandle
  39. sourceComponent: keyboard.style.selectionHandle
  40. Behavior on opacity {
  41. NumberAnimation { duration: 200 }
  42. }
  43. opacity: inputContext !== null && inputContext.cursorRectIntersectsClipRect ? 1.0 : 0.0
  44. MouseArea {
  45. width: parent.width * 2
  46. height: width * 1.12
  47. anchors.centerIn: parent
  48. onPositionChanged: function(mouse) {
  49. // we don't move the handles, the handles will move as the selection changes.
  50. root.handleIsMoving = true
  51. var xx = anchorHandle.x + anchorHandle.width/2
  52. var yy = anchorHandle.y - inputContext.anchorRectangle.height/2
  53. var x2 = x + cursorHandle.x + mouse.x
  54. var y2 = y + cursorHandle.y + mouse.y - (cursorHandle.height + inputContext.cursorRectangle.height)/2
  55. inputContext.setSelectionOnFocusObject(Qt.point(xx, yy), Qt.point(x2, y2))
  56. }
  57. onReleased: {
  58. root.handleIsMoving = false
  59. }
  60. }
  61. }
  62. Connections {
  63. target: inputContext
  64. function onCursorRectangleChanged() {
  65. var cursorItemPos = root.mapFromItem(null, inputContext.cursorRectangle.x, inputContext.cursorRectangle.y)
  66. cursorHandle.x = cursorItemPos.x - cursorHandle.width/2
  67. cursorHandle.y = cursorItemPos.y + inputContext.cursorRectangle.height
  68. }
  69. function onAnchorRectangleChanged() {
  70. var anchorItemPos = root.mapFromItem(null, inputContext.anchorRectangle.x, inputContext.anchorRectangle.y)
  71. anchorHandle.x = anchorItemPos.x - anchorHandle.width/2
  72. anchorHandle.y = anchorItemPos.y + inputContext.anchorRectangle.height
  73. }
  74. }
  75. }