MyTextEdit.qml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import QtQuick 1.0
  2. import InvMA 1.0
  3. import "Magnifier.js" as MagnifierPopup
  4. FocusScope {
  5. id: root
  6. property alias font: textEdit.font
  7. property alias cursorPosition: textEdit.cursorPosition
  8. property alias text: textEdit.text
  9. property alias readOnly: textEdit.readOnly
  10. property alias textFormat: textEdit.textFormat
  11. property alias wrapMode: textEdit.wrapMode
  12. signal textChanged
  13. signal cursorRectangleChanged
  14. property alias cursorRectangle: textEdit.cursorRectangle
  15. property alias tx: textEdit.x
  16. property alias ty: textEdit.y
  17. function copy() {
  18. textEdit.copy()
  19. }
  20. function paste() {
  21. textEdit.paste()
  22. }
  23. function cut() {
  24. textEdit.cut()
  25. }
  26. function positionAt(x, y) {
  27. var p = mapToItem(textEdit, x, y);
  28. return textEdit.positionAt(p.x, p.y)
  29. }
  30. onActiveFocusChanged: {
  31. if (activeFocus && !readOnly) {
  32. textEdit.openSoftwareInputPanel();
  33. //repositionTimer.running = true;
  34. } else if (!activeFocus) {
  35. if (!readOnly) {
  36. textEdit.closeSoftwareInputPanel();
  37. }
  38. }
  39. }
  40. /*Rectangle {
  41. id: wrapperTE
  42. //anchors.fill: parent
  43. anchors.top: parent.top
  44. anchors.left: parent.left
  45. width: parent.width
  46. height: parent.height
  47. color: "white"
  48. border.color: "blue"
  49. radius: 5
  50. }*/
  51. TextEdit {
  52. id: textEdit
  53. clip: true
  54. anchors.top: parent.top;
  55. anchors.left: parent.left; anchors.leftMargin: 5
  56. width: parent.width - 2*anchors.leftMargin
  57. height: parent.height
  58. font.pixelSize: 28
  59. //FIXME:
  60. function updateMagnifierPosition(posX, posY) {
  61. var magnifier = MagnifierPopup.popup;
  62. var cursorHeight = textEdit.positionToRectangle(0,0).height;
  63. var mappedPos = mapToItem(magnifier.parent, posX - magnifier.width / 2,
  64. posY - magnifier.height / 2 - cursorHeight - 70);
  65. magnifier.xCenter = mapToItem(magnifier.sourceItem, posX, 0).x / magnifier.sourceItem.width;
  66. magnifier.x = mappedPos.x;
  67. if (-root.mapFromItem(magnifier.__rootElement(), 0,0).y - (posY - cursorHeight) < (magnifier.height / 1.5)) {
  68. magnifier.yAdjustment = Math.max(0,(magnifier.height / 1.5) + root.mapFromItem(magnifier.__rootElement(), 0,0).y - (posY - cursorHeight));
  69. } else {
  70. magnifier.yAdjustment = 0;
  71. }
  72. magnifier.yCenter = 1.0 - ((50 + posY - cursorHeight) / root.height);
  73. magnifier.y = mappedPos.y + magnifier.yAdjustment;
  74. }
  75. onTextChanged: {
  76. root.textChanged()
  77. }
  78. onCursorRectangleChanged: {
  79. root.cursorRectangleChanged()
  80. }
  81. InverseMouseArea {
  82. anchors.fill: parent
  83. enabled: root.activeFocus
  84. onClickedOutside: {
  85. root.parent.focus = true;
  86. }
  87. }
  88. //FIXME:
  89. MouseArea {
  90. anchors.fill: parent
  91. enabled: root.activeFocus
  92. onPressAndHold:{
  93. // possible pre-edit word have to be commited before showing the magnifier
  94. console.log("MouseArea: onPressAndHold")
  95. if ((root.text != "") && root.activeFocus) {
  96. //inputContext.reset()
  97. //attemptToActivate = false
  98. //parent.selectByMouse = false
  99. MagnifierPopup.open(root);
  100. var magnifier = MagnifierPopup.popup;
  101. parent.cursorPosition = parent.positionAt(mouse.x,mouse.y)
  102. parent.updateMagnifierPosition(mouse.x,mouse.y)
  103. root.z = Number.MAX_VALUE
  104. }
  105. }
  106. }
  107. }
  108. }