KeyPopup.qml 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import QtQuick 2.0
  2. import QtGraphicalEffects 1.0
  3. /**
  4. * This is the key popup that shows a character preview above the
  5. * pressed key button like it is known from keyboards on phones.
  6. */
  7. Item {
  8. id: root
  9. property alias text: txt.text
  10. property alias font: txt.font
  11. width: 40
  12. height: 40
  13. visible: false
  14. Rectangle {
  15. id: popup
  16. anchors.fill: parent
  17. radius: Math.round(height / 30)
  18. z: shadow.z + 1
  19. gradient: Gradient {
  20. GradientStop { position: 0.0; color: Qt.lighter(Qt.lighter("#35322f"))}
  21. GradientStop { position: 1.0; color: Qt.lighter("#35322f")}
  22. }
  23. Text {
  24. id: txt
  25. color: "white"
  26. anchors.fill: parent
  27. fontSizeMode: Text.Fit
  28. font.pixelSize: height
  29. horizontalAlignment: Text.AlignHCenter
  30. verticalAlignment: Text.AlignVCenter
  31. }
  32. }
  33. Rectangle {
  34. id: shadow
  35. width: root.width
  36. height: root.height
  37. color: "#3F000000"
  38. x: 4
  39. y: 4
  40. }
  41. function popup(keybutton, inputPanel) {
  42. // console.log("popup: " + inputPanel.objectName);
  43. // console.log("keybutton.text: " + keybutton.text);
  44. width = keybutton.width * 1.4;
  45. height = keybutton.height * 1.4;
  46. var KeyButtonGlobalLeft = keybutton.mapToItem(inputPanel, 0, 0).x;
  47. var KeyButtonGlobalTop = keybutton.mapToItem(inputPanel, 0, 0).y;
  48. var PopupGlobalLeft = KeyButtonGlobalLeft - (width - keybutton.width) / 2;
  49. var PopupGlobalTop = KeyButtonGlobalTop - height - pimpl.verticalSpacing * 1.5;
  50. // console.log("Popup position left: " + KeyButtonGlobalLeft);
  51. var PopupLeft = root.parent.mapFromItem(inputPanel, PopupGlobalLeft, PopupGlobalTop).x;
  52. y = root.parent.mapFromItem(inputPanel, PopupGlobalLeft, PopupGlobalTop).y;
  53. if (PopupGlobalLeft < 0)
  54. {
  55. x = 0;
  56. }
  57. else if ((PopupGlobalLeft + width) > inputPanel.width)
  58. {
  59. x = PopupLeft - (PopupGlobalLeft + width - inputPanel.width);
  60. }
  61. else
  62. {
  63. x = PopupLeft;
  64. }
  65. text = keybutton.displayText;
  66. font.family = keybutton.font.family
  67. visible = Qt.binding(function() {return keybutton.isHighlighted});
  68. }
  69. }