FlickKey.qml 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2021 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
  3. import QtQuick
  4. import QtQuick.VirtualKeyboard
  5. /*!
  6. \qmltype FlickKey
  7. \inqmlmodule QtQuick.VirtualKeyboard.Components
  8. \ingroup qmlclass
  9. \ingroup qtvirtualkeyboard-components-qml
  10. \ingroup qtvirtualkeyboard-key-types
  11. \inherits Key
  12. \since QtQuick.VirtualKeyboard 6.1
  13. \brief Flick key for keyboard layouts.
  14. Allows to enter an alternative character in a four-way gesture.
  15. Characters are taken from the alternate keys starting with the
  16. key at index \c 0 (excluding the main key text) and the positions
  17. are filled in the following order: left, top, bottom, right.
  18. */
  19. Key {
  20. property int __key
  21. property string __text
  22. property point pt1
  23. readonly property real __centerRadius: width * 0.4
  24. readonly property var flickKeys: {
  25. var keys = InputContext.uppercase ? alternativeKeys.toUpperCase() : alternativeKeys.toLowerCase()
  26. var textIndex = __text ? keys.indexOf(InputContext.uppercase ? __text.toUpperCase() : __text.toLowerCase()) : -1
  27. if (textIndex === -1)
  28. return keys
  29. return keys.slice(0, textIndex).concat(keys.slice(textIndex + 1))
  30. }
  31. property string flickLeft: flickKeys.length > 0 ? flickKeys[0] : ""
  32. property string flickTop: flickKeys.length > 2 ? flickKeys[1] : ""
  33. property string flickBottom: flickKeys.length > 3 ? flickKeys[3] : (flickKeys.length > 2 ? flickKeys[2] : "")
  34. property string flickRight: flickKeys.length > 3 ? flickKeys[2] : (flickKeys.length === 2 ? flickKeys[1] : "")
  35. keyType: QtVirtualKeyboard.KeyType.FlickKey
  36. Component.onCompleted: {
  37. __key = key
  38. __text = text
  39. }
  40. onActiveChanged: {
  41. key = __key
  42. text = __text
  43. }
  44. function __angle(pt2) {
  45. var dx = pt2.x - pt1.x
  46. var dy = pt2.y - pt1.y
  47. var theta = Math.atan2(-dy, dx) * 360 / (2 * Math.PI)
  48. var theta_normalized = theta < 0 ? theta + 360 : theta
  49. return theta_normalized >= 360 ? 0 : theta_normalized
  50. }
  51. function __distance(pt2) {
  52. var dx = pt2.x - pt1.x
  53. dx = dx * dx
  54. var dy = pt2.y - pt1.y
  55. dy = dy * dy
  56. return Math.sqrt(dx + dy)
  57. }
  58. function press(x, y) {
  59. pt1 = Qt.point(x, y)
  60. }
  61. function update(x, y) {
  62. var pt = Qt.point(x, y)
  63. var distance = __distance(pt)
  64. if (distance < __centerRadius) {
  65. return
  66. }
  67. var currentText
  68. var angle = __angle(pt)
  69. if (angle < 45 || angle > 315) {
  70. currentText = flickRight
  71. } else if (angle < 135) {
  72. currentText = flickTop
  73. } else if (angle < 225) {
  74. currentText = flickLeft
  75. } else {
  76. currentText = flickBottom
  77. }
  78. if (currentText.length === 1 && text !== currentText) {
  79. key = currentText.toUpperCase().charCodeAt(0)
  80. text = currentText
  81. }
  82. }
  83. }