vk.qml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * QML Virtual Keyboard main file
  3. * Copyright © 2012 Pavel Ustinov aka rotoZOOM
  4. *
  5. * All rights reserved.
  6. *
  7. */
  8. import QtQuick 1.1
  9. Rectangle {
  10. id: root
  11. property Style style : Style { } // inspired by colibri keyboard
  12. anchors.fill: parent
  13. color: style.col_background
  14. signal sigButtonPressed (string key) // signal what key was pressed for outside use, just connect it
  15. signal sigStickedButtonLatched (string special_key) // special(sticked) key latched event (ctrl,shift,alt,capslock etc)
  16. signal sigStickedButtonUnlatched (string special_key) // special(sticked) key unlatched event (ctrl,shift,alt,capslock etc)
  17. onWidthChanged: checkOutSizes()
  18. onHeightChanged: checkOutSizes()
  19. Component.onCompleted: {
  20. shift_button.sigState.connect(shiftPressed)
  21. lang_switcher.sigLanguageSwitch.connect (rusLatPressed)
  22. root.sigButtonPressed.connect (buttonPressed) // only for test purposes
  23. walkThroughKeyboard (function(key)
  24. {
  25. if (key.sigState !== undefined)
  26. {
  27. /// sticked key
  28. key.sigState.connect(stickedButtonClicked)
  29. }
  30. else
  31. {
  32. key.sigButtonPressed.connect(sigButtonPressed)
  33. }
  34. }
  35. )
  36. }
  37. Column {
  38. id: keyboard
  39. spacing: style.row_spacing
  40. anchors.centerIn: parent
  41. Row {
  42. id: row1
  43. spacing: style.col_spacing
  44. Button { style: root.style; label_en: "Esc"; id: escape_button; fixed: true; grad: style.grad_but_esc; special: "esc" }
  45. Button { style: root.style; label_en: "`"; shift_label_en: "~"; label_ru: "ё"; shift_label_ru: "Ё" }
  46. Button { style: root.style; label_en: "1"; shift_label_en: "!"; label_ru: "1"; shift_label_ru: "!" }
  47. Button { style: root.style; label_en: "2"; shift_label_en: "@"; label_ru: "2"; shift_label_ru: "\"" }
  48. Button { style: root.style; label_en: "3"; shift_label_en: "#"; label_ru: "3"; shift_label_ru: "№" }
  49. Button { style: root.style; label_en: "4"; shift_label_en: "$"; label_ru: "4"; shift_label_ru: ";" }
  50. Button { style: root.style; label_en: "5"; shift_label_en: "%"; label_ru: "5"; shift_label_ru: "%" }
  51. Button { style: root.style; label_en: "6"; shift_label_en: "^"; label_ru: "6"; shift_label_ru: ":" }
  52. Button { style: root.style; label_en: "7"; shift_label_en: "&"; label_ru: "7"; shift_label_ru: "?" }
  53. Button { style: root.style; label_en: "8"; shift_label_en: "*"; label_ru: "8"; shift_label_ru: "*" }
  54. Button { style: root.style; label_en: "9"; shift_label_en: "("; label_ru: "9"; shift_label_ru: "(" }
  55. Button { style: root.style; label_en: "0"; shift_label_en: ")"; label_ru: "0"; shift_label_ru: ")" }
  56. Button { style: root.style; label_en: "-"; shift_label_en: "_"; label_ru: "-"; shift_label_ru: "_" }
  57. Button { style: root.style; label_en: "="; shift_label_en: "+"; label_ru: "="; shift_label_ru: "+" }
  58. Button { style: root.style; label_en: "<html>&larr;</html>"; width: style.but_width_and_half; fixed: true; special: "backspace" }
  59. }
  60. Row {
  61. id: row2
  62. spacing: root.style.col_spacing
  63. anchors.left: parent.left
  64. anchors.leftMargin: root.style.but_width
  65. Button { style: root.style; label_en: "q"; shift_label_en: "Q"; label_ru: "й"; shift_label_ru: "Й" }
  66. Button { style: root.style; label_en: "w"; shift_label_en: "W"; label_ru: "ц"; shift_label_ru: "Ц" }
  67. Button { style: root.style; label_en: "e"; shift_label_en: "E"; label_ru: "у"; shift_label_ru: "У" }
  68. Button { style: root.style; label_en: "r"; shift_label_en: "R"; label_ru: "к"; shift_label_ru: "К" }
  69. Button { style: root.style; label_en: "t"; shift_label_en: "T"; label_ru: "е"; shift_label_ru: "Е" }
  70. Button { style: root.style; label_en: "y"; shift_label_en: "Y"; label_ru: "н"; shift_label_ru: "Н" }
  71. Button { style: root.style; label_en: "u"; shift_label_en: "U"; label_ru: "г"; shift_label_ru: "Г" }
  72. Button { style: root.style; label_en: "i"; shift_label_en: "I"; label_ru: "ш"; shift_label_ru: "Ш" }
  73. Button { style: root.style; label_en: "o"; shift_label_en: "O"; label_ru: "щ"; shift_label_ru: "Щ" }
  74. Button { style: root.style; label_en: "p"; shift_label_en: "P"; label_ru: "з"; shift_label_ru: "З" }
  75. Button { style: root.style; label_en: "["; shift_label_en: "{"; label_ru: "х"; shift_label_ru: "Х" }
  76. Button { style: root.style; label_en: "]"; shift_label_en: "}"; label_ru: "ъ"; shift_label_ru: "Ъ" }
  77. }
  78. Row {
  79. id: row3
  80. spacing: style.col_spacing
  81. anchors.left: parent.left
  82. anchors.leftMargin: root.style.but_width_and_half
  83. Button { style: root.style; label_en: "a"; shift_label_en: "A"; label_ru: "ф"; shift_label_ru: "Ф" }
  84. Button { style: root.style; label_en: "s"; shift_label_en: "S"; label_ru: "ы"; shift_label_ru: "Ы" }
  85. Button { style: root.style; label_en: "d"; shift_label_en: "D"; label_ru: "в"; shift_label_ru: "В" }
  86. Button { style: root.style; label_en: "f"; shift_label_en: "F"; label_ru: "а"; shift_label_ru: "А" }
  87. Button { style: root.style; label_en: "g"; shift_label_en: "G"; label_ru: "п"; shift_label_ru: "П" }
  88. Button { style: root.style; label_en: "h"; shift_label_en: "H"; label_ru: "р"; shift_label_ru: "Р" }
  89. Button { style: root.style; label_en: "j"; shift_label_en: "J"; label_ru: "о"; shift_label_ru: "О" }
  90. Button { style: root.style; label_en: "k"; shift_label_en: "K"; label_ru: "л"; shift_label_ru: "Л" }
  91. Button { style: root.style; label_en: "l"; shift_label_en: "L"; label_ru: "д"; shift_label_ru: "Д" }
  92. Button { style: root.style; label_en: ";"; shift_label_en: ":"; label_ru: "ж"; shift_label_ru: "Ж" }
  93. Button { style: root.style; label_en: "'"; shift_label_en: "\""; label_ru: "э"; shift_label_ru: "Э" }
  94. Button { style: root.style; label_en: "\\"; shift_label_en: "|"; label_ru: "\\"; shift_label_ru: "/" }
  95. Button { style: root.style; label_en: "Enter"; width: style.but_width_and_half; fixed: true; special: "enter" }
  96. }
  97. Row {
  98. id: row4
  99. spacing: style.col_spacing
  100. StickedButton { style: root.style; id: shift_button; label_en: "<html>&uarr;</html>"; width: style.but_doubled_width; fixed: true; special: "shift" }
  101. Button { style: root.style; label_en: "z"; shift_label_en: "Z"; label_ru: "я"; shift_label_ru: "Я" }
  102. Button { style: root.style; label_en: "x"; shift_label_en: "X"; label_ru: "ч"; shift_label_ru: "Ч" }
  103. Button { style: root.style; label_en: "c"; shift_label_en: "C"; label_ru: "с"; shift_label_ru: "С" }
  104. Button { style: root.style; label_en: "v"; shift_label_en: "V"; label_ru: "м"; shift_label_ru: "М" }
  105. Button { style: root.style; label_en: "b"; shift_label_en: "B"; label_ru: "и"; shift_label_ru: "И" }
  106. Button { style: root.style; label_en: "n"; shift_label_en: "N"; label_ru: "т"; shift_label_ru: "Т" }
  107. Button { style: root.style; label_en: "m"; shift_label_en: "M"; label_ru: "ь"; shift_label_ru: "Ь" }
  108. Button { style: root.style; label_en: ","; shift_label_en: "<"; label_ru: "б"; shift_label_ru: "Б" }
  109. Button { style: root.style; label_en: "."; shift_label_en: ">"; label_ru: "ю"; shift_label_ru: "Ю" }
  110. Button { style: root.style; label_en: "/"; shift_label_en: "?"; label_ru: "."; shift_label_ru: "," }
  111. }
  112. Row {
  113. id: row5
  114. spacing: style.col_spacing
  115. StickedButton { style: root.style; id: ctrl_button; label_en: "Ctrl"; width: style.but_doubled_width; fixed: true; special: "ctrl" }
  116. StickedButton { style: root.style; id: alt_button; label_en: "Alt"; fixed: true; special: "alt" }
  117. RusLatButton { style: root.style; id: lang_switcher}
  118. Button { style: root.style; id: space_button; width: style.but_doubled_width*4; fixed: true; special: " " }
  119. }
  120. }
  121. Column {
  122. id: arrow_keys
  123. anchors.right: keyboard.right
  124. anchors.bottom: keyboard.bottom
  125. spacing: root.style.row_spacing
  126. Row {
  127. anchors.horizontalCenter: parent.horizontalCenter;
  128. Button { style: root.style; label_en: "<html>&uarr;</html>"; special: "up"; fixed: true }
  129. }
  130. Row {
  131. spacing: root.style.col_spacing
  132. Button { style: root.style; label_en: "<html>&larr;</html>"; special: "left"; fixed: true }
  133. Button { style: root.style; label_en: "<html>&darr;</html>"; special: "down"; fixed: true }
  134. Button { style: root.style; label_en: "<html>&rarr;</html>"; special: "right"; fixed: true }
  135. }
  136. }
  137. /**
  138. * Test function TODO: remove from release
  139. */
  140. function buttonPressed (key)
  141. {
  142. console.log (key)
  143. }
  144. function stickedButtonClicked (key, is_checked)
  145. {
  146. console.log ([key,is_checked]) /// for debugging purpose TODO: remove from release
  147. if (is_checked)
  148. {
  149. sigStickedButtonLatched (key)
  150. }
  151. else
  152. {
  153. sigStickedButtonUnlatched (key)
  154. }
  155. }
  156. /**
  157. * Shift press/release handler
  158. */
  159. function shiftPressed (key, is_checked)
  160. {
  161. walkThroughKeyboard (function(key){
  162. if (!key.fixed)
  163. {
  164. key.changeShiftState (is_checked)
  165. }
  166. })
  167. }
  168. /**
  169. * Language switch key pressed
  170. */
  171. function rusLatPressed (lng)
  172. {
  173. walkThroughKeyboard (function(key){
  174. if (!key.fixed)
  175. {
  176. key.lang = lng == "ru" ? "ru" : "en"
  177. }
  178. })
  179. }
  180. /**
  181. * Walk through all keys and invoke visitor function on each key
  182. */
  183. function walkThroughKeyboard (visitor)
  184. {
  185. var i, j, row, key; // it's my paranoya from C++ :)
  186. for (i = 0 ; i < keyboard.children.length ; i++)
  187. {
  188. row = keyboard.children[i];
  189. for (j = 0 ; j < row.children.length ; j++)
  190. {
  191. key = row.children[j];
  192. visitor (key) /// call user function
  193. }
  194. }
  195. for (i = 0 ; i < arrow_keys.children.length ; i++)
  196. {
  197. row = arrow_keys.children[i];
  198. for (j = 0 ; j < row.children.length ; j++)
  199. {
  200. key = row.children[j];
  201. visitor (key) /// call user function
  202. }
  203. }
  204. }
  205. /**
  206. * Correct button's size depends on pane width and height
  207. * P.S. the worst place in code (ughhh so ugly), must to do it more nicely!!!
  208. */
  209. function checkOutSizes ()
  210. {
  211. /// calculate base button width
  212. /// it bases on the most lengthy row (number 1 at the moment),
  213. /// 14 normal and one long keys
  214. var x = Math.round((root.width - 14 * style.col_spacing) / 15.5) - 1;
  215. style.but_width = Math.max (x,style.min_but_width);
  216. /// calculate base button height
  217. x = Math.round((root.height - 4 * style.row_spacing) / 5) - 1;
  218. style.but_height = Math.max(x,style.min_but_height);
  219. }
  220. }