TextInputWithHandles.qml 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Controls module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQuick 2.2
  40. import QtQuick.Window 2.2
  41. import QtQuick.Controls.Private 1.0
  42. TextInput {
  43. id: input
  44. property Item control
  45. property alias cursorHandle: cursorHandle.delegate
  46. property alias selectionHandle: selectionHandle.delegate
  47. property bool blockRecursion: false
  48. property bool hasSelection: selectionStart !== selectionEnd
  49. readonly property int selectionPosition: selectionStart !== cursorPosition ? selectionStart : selectionEnd
  50. readonly property alias containsMouse: mouseArea.containsMouse
  51. property alias editMenu: editMenu
  52. cursorDelegate: __style && __style.__cursorDelegate ? __style.__cursorDelegate : null
  53. selectByMouse: control.selectByMouse && (!Settings.isMobile || !cursorHandle.delegate || !selectionHandle.delegate)
  54. // force re-evaluation when selection moves:
  55. // - cursorRectangle changes => content scrolled
  56. // - contentWidth changes => text layout changed
  57. property rect selectionRectangle: cursorRectangle.x && contentWidth ? positionToRectangle(selectionPosition)
  58. : positionToRectangle(selectionPosition)
  59. onSelectionStartChanged: syncHandlesWithSelection()
  60. onCursorPositionChanged: syncHandlesWithSelection()
  61. function syncHandlesWithSelection()
  62. {
  63. if (!blockRecursion && selectionHandle.delegate) {
  64. blockRecursion = true
  65. // We cannot use property selectionPosition since it gets updated after onSelectionStartChanged
  66. cursorHandle.position = cursorPosition
  67. selectionHandle.position = (selectionStart !== cursorPosition) ? selectionStart : selectionEnd
  68. blockRecursion = false
  69. }
  70. }
  71. function activate() {
  72. if (activeFocusOnPress) {
  73. forceActiveFocus()
  74. if (!readOnly)
  75. Qt.inputMethod.show()
  76. }
  77. cursorHandle.activate()
  78. selectionHandle.activate()
  79. }
  80. function moveHandles(cursor, selection) {
  81. blockRecursion = true
  82. cursorPosition = cursor
  83. if (selection === -1) {
  84. selectWord()
  85. selection = selectionStart
  86. }
  87. selectionHandle.position = selection
  88. cursorHandle.position = cursorPosition
  89. blockRecursion = false
  90. }
  91. MouseArea {
  92. id: mouseArea
  93. anchors.fill: parent
  94. hoverEnabled: Settings.hoverEnabled
  95. cursorShape: Qt.IBeamCursor
  96. acceptedButtons: (input.selectByMouse ? Qt.NoButton : Qt.LeftButton) | (control.menu ? Qt.RightButton : Qt.NoButton)
  97. onClicked: {
  98. if (editMenu.item)
  99. return;
  100. var pos = input.positionAt(mouse.x, mouse.y)
  101. input.moveHandles(pos, pos)
  102. input.activate()
  103. }
  104. onPressAndHold: {
  105. if (editMenu.item)
  106. return;
  107. var pos = input.positionAt(mouse.x, mouse.y)
  108. input.moveHandles(pos, control.selectByMouse ? -1 : pos)
  109. input.activate()
  110. }
  111. }
  112. EditMenu {
  113. id: editMenu
  114. input: parent
  115. mouseArea: mouseArea
  116. control: parent.control
  117. cursorHandle: cursorHandle
  118. selectionHandle: selectionHandle
  119. anchors.fill: parent
  120. }
  121. ScenePosListener {
  122. id: listener
  123. item: input
  124. enabled: input.activeFocus && Qt.platform.os !== "ios" && Settings.isMobile
  125. }
  126. TextHandle {
  127. id: selectionHandle
  128. editor: input
  129. z: 1000001 // DefaultWindowDecoration+1
  130. parent: !input.activeFocus || Qt.platform.os === "ios" ? control : Window.contentItem // float (QTBUG-42538)
  131. control: input.control
  132. active: control.selectByMouse && Settings.isMobile
  133. maximum: cursorHandle.position - 1
  134. readonly property var mappedOrigin: editor.mapToItem(parent, 0,0)
  135. // Mention scenePos in the mappedPos binding to force re-evaluation if it changes
  136. readonly property var mappedPos: listener.scenePos.x !== listener.scenePos.y !== Number.MAX_VALUE ?
  137. editor.mapToItem(parent, editor.selectionRectangle.x, editor.selectionRectangle.y) : -1
  138. x: mappedPos.x
  139. y: mappedPos.y
  140. visible: pressed || (input.hasSelection && handleX + handleWidth >= -1 && handleX - mappedOrigin.x <= control.width + 1)
  141. onPositionChanged: {
  142. if (!input.blockRecursion) {
  143. input.blockRecursion = true
  144. input.select(selectionHandle.position, cursorHandle.position)
  145. if (pressed)
  146. input.ensureVisible(position)
  147. input.blockRecursion = false
  148. }
  149. }
  150. }
  151. TextHandle {
  152. id: cursorHandle
  153. editor: input
  154. z: 1000001 // DefaultWindowDecoration+1
  155. parent: !input.activeFocus || Qt.platform.os === "ios" ? control : Window.contentItem // float (QTBUG-42538)
  156. control: input.control
  157. active: control.selectByMouse && Settings.isMobile
  158. minimum: input.hasSelection ? selectionHandle.position + 1 : -1
  159. readonly property var mappedOrigin: editor.mapToItem(parent, 0,0)
  160. // Mention scenePos in the mappedPos binding to force re-evaluation if it changes
  161. readonly property var mappedPos: listener.scenePos.x !== listener.scenePos.y !== Number.MAX_VALUE ?
  162. editor.mapToItem(parent, editor.cursorRectangle.x, editor.cursorRectangle.y) : -1
  163. x: mappedPos.x
  164. y: mappedPos.y
  165. visible: pressed || ((input.cursorVisible || input.hasSelection) && handleX + handleWidth >= -1 && handleX - mappedOrigin.x <= control.width + 1)
  166. onPositionChanged: {
  167. if (!input.blockRecursion) {
  168. input.blockRecursion = true
  169. if (!input.hasSelection)
  170. selectionHandle.position = cursorHandle.position
  171. input.select(selectionHandle.position, cursorHandle.position)
  172. input.blockRecursion = false
  173. }
  174. }
  175. }
  176. }