texteditor.qml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. import Qt 4.7
  5. Item {
  6. id: textEditor
  7. property alias text: sourceText.text
  8. property alias codeTemplateModel: codeButtonList.model
  9. signal tryDemoClicked()
  10. signal backClicked()
  11. function flickToLine(line) {
  12. // Calculate the line position in pixels, we take 5 lines too up so that the selection
  13. // would be a little lower than the top of the window, it seems that the space between
  14. // rows is 3 pixels...
  15. var ypos = (line - 5) * (sourceText.font.pixelSize + 3)
  16. if(ypos < 0) { ypos = 0 }
  17. view.contentY = ypos
  18. }
  19. function getPosByLFCount(beginPos, lines) {
  20. var lineCount = 0
  21. if(lines <= 0) { return beginPos }
  22. for (var i = beginPos; i<sourceText.text.length; i++) {
  23. if(sourceText.text.charAt(i) == '\n') {
  24. lineCount++;
  25. if(lineCount == lines) { return i + 1}
  26. }
  27. }
  28. return i
  29. }
  30. function selectLine(lineNumber) {
  31. var lineBeginPos = -1
  32. var lineEndPos = -1
  33. lineBeginPos = getPosByLFCount(0, lineNumber - 1)
  34. lineEndPos = getPosByLFCount(lineBeginPos, 1) - 1
  35. flickToLine(lineNumber)
  36. editButton.pressed = true
  37. sourceText.select(lineBeginPos, lineEndPos)
  38. sourceText.cursorVisible = true
  39. sourceText.focus = true
  40. }
  41. function findCode(string) {
  42. // Finds given code in editor, flicks it to visible and selects then code right after the selection ending to \n
  43. var index = sourceText.text.indexOf(string)
  44. if(index == -1) {
  45. return // string was not found
  46. }
  47. var beginPos = index + string.length
  48. var endPos = sourceText.text.indexOf('\n', beginPos)
  49. var lineCount = 0
  50. // Calculates the row count, this does not take account the word wrapping!!!
  51. for (var i=0; i<index; i++) {
  52. if(sourceText.text.charAt(i) == '\n') {
  53. lineCount++;
  54. }
  55. }
  56. flickToLine(lineCount)
  57. editButton.pressed = true
  58. sourceText.select(beginPos, endPos)
  59. sourceText.cursorVisible = true
  60. sourceText.focus = true
  61. }
  62. anchors.fill: parent
  63. clip: true
  64. Image {
  65. anchors.centerIn: parent
  66. source: "qtqp_codeview.png"
  67. }
  68. Flickable {
  69. id: view
  70. anchors.fill: parent
  71. anchors.rightMargin: parent.width / 4
  72. clip: true
  73. contentWidth: sourceText.width
  74. contentHeight: sourceText.height
  75. TextEdit {
  76. id: sourceText
  77. x: 7; y: 5; z:1
  78. width: view.width
  79. color: "white"
  80. wrapMode: "WrapAtWordBoundaryOrAnywhere"
  81. readOnly: true
  82. activeFocusOnPress: true
  83. selectedTextColor: color
  84. selectionColor: "#00000000"
  85. font.pixelSize: 13
  86. }
  87. }
  88. Rectangle {
  89. id: scrollBar
  90. anchors.right: view.right
  91. y: view.visibleArea.yPosition * view.height
  92. width: 6; height: view.visibleArea.heightRatio * view.height
  93. opacity: 0.5
  94. }
  95. Button {
  96. id: editButton
  97. property bool pressed: false
  98. anchors { top: parent.top; topMargin: 16; left: view.right; leftMargin: 16 }
  99. width: 100; height: 60
  100. opacity: 0.4
  101. onPressedChanged: {
  102. if (pressed) {
  103. sourceText.openSoftwareInputPanel();
  104. }
  105. else {
  106. sourceText.closeSoftwareInputPanel();
  107. }
  108. }
  109. Image { id: btn_pen; source: "btn_editbrowse_pen.png"; opacity: 0.4 }
  110. Image { id: btn_edit; source: "btn_editbrowse_edit.png"; anchors.left: btn_pen.right; opacity: 0.4 }
  111. Image { id: btn_slash; source: "btn_editbrowse_slash.png"; anchors.left: btn_edit.right; opacity: 0.4 }
  112. Image { id: btn_browse; source: "btn_editbrowse_browse.png"; anchors.left: btn_pen.right; anchors.top: btn_edit.bottom; opacity: 1.0 }
  113. MouseArea { anchors.fill: parent; onClicked: { editButton.pressed = !editButton.pressed } }
  114. states: State {
  115. name: "EditMode"
  116. when: editButton.pressed
  117. PropertyChanges { target: btn_pen; opacity: 1.0 }
  118. PropertyChanges { target: btn_edit; opacity: 1.0 }
  119. PropertyChanges { target: btn_slash; opacity: 1.0 }
  120. PropertyChanges { target: btn_browse; opacity: 0.4 }
  121. PropertyChanges { target: view; interactive: false }
  122. PropertyChanges { target: sourceText; readOnly: false }
  123. PropertyChanges { target: sourceText; selectionColor: "blue" }
  124. }
  125. }
  126. Button {
  127. id: backButton
  128. anchors { top: parent.top; topMargin: 5; right: parent.right; rightMargin: 10 }
  129. source: "btn_mainmenu.png"
  130. onClicked: {
  131. sourceText.closeSoftwareInputPanel()
  132. backClicked(index)
  133. }
  134. }
  135. Button {
  136. id: startDemoButton
  137. anchors { top: backButton.bottom; topMargin: 15; right: parent.right; rightMargin: 10 }
  138. source: "btn_trydemo.png"
  139. onClicked: {
  140. sourceText.closeSoftwareInputPanel()
  141. tryDemoClicked()
  142. }
  143. }
  144. Component {
  145. id: drawer
  146. Item {
  147. width: codeButtonList.width
  148. height: 60
  149. CodeButton {
  150. width: parent.width - 5; height: parent.height - 10
  151. title: titleText
  152. search: searchText
  153. }
  154. }
  155. }
  156. ListView {
  157. id: codeButtonList
  158. anchors.top: startDemoButton.bottom; anchors.topMargin: 20; anchors.bottom: parent.bottom; anchors.bottomMargin: 10
  159. anchors.left: view.right; anchors.leftMargin: 10; anchors.right: parent.right; anchors.rightMargin: 10
  160. delegate: drawer
  161. orientation: "Vertical"
  162. interactive: false
  163. }
  164. }