AlternativeKeys.qml 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. property bool active: listView.currentIndex != -1
  7. property int highlightIndex: -1
  8. property alias listView: listView
  9. property int keyCode
  10. property point origin
  11. signal clicked
  12. LayoutMirroring.enabled: false
  13. LayoutMirroring.childrenInherit: true
  14. z: 1
  15. visible: active
  16. anchors.fill: parent
  17. ListModel {
  18. id: listModel
  19. }
  20. ListView {
  21. id: listView
  22. spacing: 0
  23. model: listModel
  24. delegate: keyboard.style.alternateKeysListDelegate
  25. highlight: keyboard.style.alternateKeysListHighlight ? keyboard.style.alternateKeysListHighlight : defaultHighlight
  26. highlightMoveDuration: 0
  27. highlightResizeDuration: 0
  28. keyNavigationWraps: true
  29. orientation: ListView.Horizontal
  30. height: keyboard.style ? keyboard.style.alternateKeysListItemHeight : 0
  31. x: origin.x
  32. y: keyboard.style ? origin.y - height - keyboard.style.alternateKeysListBottomMargin : 0
  33. Component {
  34. id: defaultHighlight
  35. Item {}
  36. }
  37. }
  38. Loader {
  39. id: backgroundLoader
  40. sourceComponent: keyboard.style.alternateKeysListBackground
  41. anchors.fill: listView
  42. z: -1
  43. states: State {
  44. name: "highlighted"
  45. when: highlightIndex !== -1 && highlightIndex === listView.currentIndex &&
  46. backgroundLoader.item !== null && backgroundLoader.item.hasOwnProperty("currentItemHighlight")
  47. PropertyChanges {
  48. target: backgroundLoader.item
  49. currentItemHighlight: true
  50. }
  51. }
  52. }
  53. onClicked: {
  54. if (active && listView.currentIndex >= 0 && listView.currentIndex < listView.model.count) {
  55. var activeKey = listView.model.get(listView.currentIndex)
  56. InputContext.inputEngine.virtualKeyClick(keyCode, activeKey.data,
  57. InputContext.uppercase ? Qt.ShiftModifier : 0)
  58. }
  59. }
  60. function open(key, originX, originY) {
  61. keyCode = key.key
  62. var alternativeKeys = key.effectiveAlternativeKeys
  63. var displayAlternativeKeys = key.displayAlternativeKeys
  64. if (alternativeKeys.length > 0 && displayAlternativeKeys.length === alternativeKeys.length) {
  65. for (var i = 0; i < alternativeKeys.length; i++) {
  66. listModel.append({
  67. "text": InputContext.uppercase ? displayAlternativeKeys[i].toUpperCase() : displayAlternativeKeys[i],
  68. "data": InputContext.uppercase ? alternativeKeys[i].toUpperCase() : alternativeKeys[i]
  69. })
  70. }
  71. listView.width = keyboard.style.alternateKeysListItemWidth * listModel.count
  72. listView.forceLayout()
  73. highlightIndex = key.effectiveAlternativeKeysHighlightIndex
  74. if (highlightIndex === -1) {
  75. console.log("AlternativeKeys: active key \"" + key.text + "\" not found in alternativeKeys \"" + alternativeKeys + ".\"")
  76. highlightIndex = 0
  77. }
  78. listView.currentIndex = highlightIndex
  79. var currentItemOffset = (listView.currentIndex + 0.5) * keyboard.style.alternateKeysListItemWidth
  80. origin = Qt.point(Math.min(Math.max(keyboard.style.alternateKeysListLeftMargin, originX - currentItemOffset), width - listView.width - keyboard.style.alternateKeysListRightMargin), originY)
  81. if (backgroundLoader.item && backgroundLoader.item.hasOwnProperty("currentItemOffset")) {
  82. backgroundLoader.item.currentItemOffset = currentItemOffset
  83. }
  84. }
  85. return active
  86. }
  87. function move(mouseX) {
  88. var newIndex = listView.indexAt(Math.max(1, Math.min(listView.width - 1, mapToItem(listView, mouseX, 0).x)), 1)
  89. if (newIndex !== listView.currentIndex) {
  90. listView.currentIndex = newIndex
  91. }
  92. }
  93. function close() {
  94. listView.currentIndex = -1
  95. listModel.clear()
  96. }
  97. }