IpAddressInput.qml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import QtQuick 2.0
  2. Item {
  3. id: ipAddresInput
  4. property bool ipvisible: true
  5. property double space: 4
  6. property double ipwidth: 200
  7. property double posX: 0
  8. property double posY: 0
  9. property double partwidth: (ipwidth / 4) - (3 * space)
  10. property color focuscolor: "green"
  11. property color ipcolor: "black"
  12. property color invalidcolor: "red"
  13. property color ipBackColor: "silver"
  14. property string ipAddress: ""
  15. property double ipFontSize: 14
  16. property bool isValid: false
  17. property int ipRadius: 0
  18. property int ipFocus: 0
  19. signal keyReturnPressed
  20. signal keyUpPressed
  21. signal keyDownPressed
  22. function makeIpAddress() {
  23. ipAddress = parseInt(textInput1.text, 10) + "." + parseInt(textInput2.text, 10) + "." +
  24. parseInt(textInput3.text, 10) + "." + parseInt(textInput4.text, 10);
  25. if((textInput1.text >= 0) && (textInput1.text <= 255))
  26. textInput1.color = (!textInput1.focus)?ipcolor:focuscolor;
  27. else textInput1.color = invalidcolor;
  28. if((textInput2.text >= 0) && (textInput2.text <= 255))
  29. textInput2.color = (!textInput2.focus)?ipcolor:focuscolor;
  30. else textInput2.color = invalidcolor;
  31. if((textInput3.text >= 0) && (textInput3.text <= 255))
  32. textInput3.color = (!textInput3.focus)?ipcolor:focuscolor;
  33. else textInput3.color = invalidcolor;
  34. if((textInput4.text >= 0) && (textInput4.text <= 255))
  35. textInput4.color = (!textInput4.focus)?ipcolor:focuscolor;
  36. else textInput4.color = invalidcolor;
  37. }
  38. function fillIpFields() {
  39. function padDigits(number, digits) {
  40. return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
  41. };
  42. var iplist = ipAddress.split(".");
  43. var i;
  44. for ( i = 0; i < iplist.length; i++) iplist[i] = padDigits(iplist[i], 3);
  45. if(iplist.length >= 1) textInput1.text = iplist[0]; else textInput1.text ="000";
  46. if(iplist.length >= 2) textInput2.text = iplist[1]; else textInput2.text ="000";
  47. if(iplist.length >= 3) textInput3.text = iplist[2]; else textInput3.text ="000";
  48. if(iplist.length >= 4) textInput4.text = iplist[3]; else textInput4.text ="000";
  49. }
  50. onIpAddressChanged: {
  51. fillIpFields();
  52. }
  53. Component.onCompleted: {
  54. fillIpFields();
  55. }
  56. onIpFocusChanged: {
  57. textInput1.focus = (ipFocus == 1)?true:false;
  58. textInput2.focus = (ipFocus == 2)?true:false;
  59. textInput3.focus = (ipFocus == 3)?true:false;
  60. textInput4.focus = (ipFocus == 4)?true:false;
  61. }
  62. Rectangle {
  63. x: posX
  64. y: posY
  65. visible: ipAddresInput.ipvisible
  66. width: textInput1.width
  67. height: textInput1.height
  68. radius: ipRadius
  69. color: ipBackColor
  70. }
  71. TextInput {
  72. id: textInput1
  73. x: posX
  74. y: posY
  75. width: partwidth
  76. font.pixelSize: ipFontSize
  77. visible: ipAddresInput.ipvisible
  78. KeyNavigation.tab: textInput2
  79. focus: (ipAddresInput.ipFocus == 1)?true:false
  80. color: ipcolor
  81. validator: IntValidator {bottom:0; top:999}
  82. inputMask: "000;"
  83. inputMethodHints: Qt.ImhDigitsOnly
  84. maximumLength: 3
  85. Keys.onReleased: {
  86. if(cursorPosition == maximumLength){
  87. textInput2.focus = true;
  88. }
  89. }
  90. Keys.onPressed: {
  91. if((event.key == Qt.Key_Left) && (cursorPosition == 0)){
  92. textInput4.focus = true; textInput4.cursorPosition = 2;
  93. }
  94. if((event.key == Qt.Key_Right) && (cursorPosition == maximumLength)){
  95. textInput2.focus = true;
  96. }
  97. if((event.key == Qt.Key_Enter) || (event.key == Qt.Key_Return))
  98. {
  99. makeIpAddress();
  100. keyReturnPressed();
  101. }
  102. if(event.key == Qt.Key_Up)
  103. {
  104. makeIpAddress();
  105. keyUpPressed();
  106. }
  107. if(event.key == Qt.Key_Down)
  108. {
  109. makeIpAddress();
  110. keyDownPressed();
  111. }
  112. }
  113. onFocusChanged: {
  114. color = (focus) ? focuscolor : ipcolor;
  115. makeIpAddress();
  116. cursorPosition = 0;
  117. if(focus) ipFocus = 1;
  118. }
  119. }
  120. Text{
  121. id: ipDot1
  122. font.pixelSize: ipFontSize
  123. x: posX + textInput1.width
  124. y: textInput1.y
  125. visible: ipAddresInput.ipvisible
  126. color: ipcolor
  127. text:"."
  128. }
  129. Rectangle {
  130. x: posX + space + partwidth + ipDot1.width
  131. y: posY
  132. visible: ipAddresInput.ipvisible
  133. width: textInput2.width
  134. height: textInput2.height
  135. radius: ipRadius
  136. color: ipBackColor
  137. }
  138. TextInput {
  139. id: textInput2
  140. x: posX + space + partwidth + ipDot1.width
  141. y: posY
  142. visible: ipAddresInput.ipvisible
  143. width: partwidth
  144. font.pixelSize: ipFontSize
  145. KeyNavigation.tab: textInput3
  146. focus: (ipAddresInput.ipFocus == 2)?true:false
  147. color: ipcolor
  148. validator: IntValidator {bottom:0; top:999}
  149. inputMask: "000;"
  150. inputMethodHints: Qt.ImhDigitsOnly
  151. maximumLength: 3
  152. Keys.onReleased: {
  153. if(cursorPosition == maximumLength){
  154. textInput3.focus = true;
  155. }
  156. }
  157. Keys.onPressed: {
  158. if((event.key == Qt.Key_Left) && (cursorPosition == 0)){
  159. textInput1.focus = true; textInput1.cursorPosition = 2;
  160. }
  161. if((event.key == Qt.Key_Right) && (cursorPosition == maximumLength)){
  162. textInput3.focus = true;
  163. }
  164. if((event.key == Qt.Key_Enter) || (event.key == Qt.Key_Return))
  165. {
  166. makeIpAddress();
  167. keyReturnPressed();
  168. }
  169. if(event.key == Qt.Key_Up)
  170. {
  171. makeIpAddress();
  172. keyUpPressed();
  173. }
  174. if(event.key == Qt.Key_Down)
  175. {
  176. makeIpAddress();
  177. keyDownPressed();
  178. }
  179. }
  180. onFocusChanged: {
  181. color = (focus) ? focuscolor : ipcolor;
  182. makeIpAddress();
  183. cursorPosition = 0;
  184. if(focus) ipFocus = 2;
  185. }
  186. }
  187. Text{
  188. id: ipDot2
  189. font.pixelSize: ipFontSize
  190. x: textInput2.x + textInput2.width
  191. y: textInput1.y
  192. visible: ipAddresInput.ipvisible
  193. color: ipcolor
  194. text:"."
  195. }
  196. Rectangle {
  197. x: posX + 2*space + 2*partwidth +ipDot2.width
  198. y: posY
  199. visible: ipAddresInput.ipvisible
  200. width: textInput3.width
  201. height: textInput3.height
  202. radius: ipRadius
  203. color: ipBackColor
  204. }
  205. TextInput {
  206. id: textInput3
  207. x: posX + 2*space + 2*partwidth +ipDot2.width
  208. y: posY
  209. visible: ipAddresInput.ipvisible
  210. width: partwidth
  211. font.pixelSize: ipFontSize
  212. KeyNavigation.tab: textInput4
  213. focus: (ipAddresInput.ipFocus == 3)?true:false
  214. color: ipcolor
  215. validator: IntValidator {bottom:0; top:999}
  216. inputMask: "000;"
  217. inputMethodHints: Qt.ImhDigitsOnly
  218. maximumLength: 3
  219. Keys.onReleased: {
  220. if(cursorPosition == maximumLength){
  221. textInput4.focus = true;
  222. }
  223. }
  224. Keys.onPressed: {
  225. if((event.key == Qt.Key_Left) && (cursorPosition == 0)){
  226. textInput2.focus = true; textInput2.cursorPosition = 2;
  227. }
  228. if((event.key == Qt.Key_Right) && (cursorPosition == maximumLength)){
  229. textInput4.focus = true;
  230. }
  231. if((event.key == Qt.Key_Enter) || (event.key == Qt.Key_Return))
  232. {
  233. makeIpAddress();
  234. keyReturnPressed();
  235. }
  236. if(event.key == Qt.Key_Up)
  237. {
  238. makeIpAddress();
  239. keyUpPressed();
  240. }
  241. if(event.key == Qt.Key_Down)
  242. {
  243. makeIpAddress();
  244. keyDownPressed();
  245. }
  246. }
  247. onFocusChanged: {
  248. color = (focus) ? focuscolor : ipcolor;
  249. makeIpAddress();
  250. cursorPosition = 0;
  251. if(focus) ipFocus = 3;
  252. }
  253. }
  254. Text{
  255. id: ipDot3
  256. font.pixelSize: ipFontSize
  257. x: textInput3.x + textInput3.width
  258. y: textInput1.y
  259. visible: ipAddresInput.ipvisible
  260. color: ipcolor
  261. text:"."
  262. }
  263. Rectangle {
  264. x: posX + 3*space + 3*partwidth + ipDot3.width
  265. y: posY
  266. visible: ipAddresInput.ipvisible
  267. width: textInput4.width
  268. height: textInput4.height
  269. radius: ipRadius
  270. color: ipBackColor
  271. }
  272. TextInput {
  273. id: textInput4
  274. x: posX + 3*space + 3*partwidth + ipDot3.width
  275. y: posY
  276. visible: ipAddresInput.ipvisible
  277. width: partwidth
  278. font.pixelSize: ipFontSize
  279. KeyNavigation.tab: textInput1
  280. focus: (ipAddresInput.ipFocus == 4)?true:false
  281. color: ipcolor
  282. validator: IntValidator {bottom:0; top:999}
  283. inputMask: "000;"
  284. inputMethodHints: Qt.ImhDigitsOnly
  285. maximumLength: 3
  286. Keys.onReleased: {
  287. if(cursorPosition == maximumLength){
  288. textInput1.focus = true;
  289. }
  290. }
  291. Keys.onPressed: {
  292. if((event.key == Qt.Key_Left) && (cursorPosition == 0)){
  293. textInput3.focus = true; textInput3.cursorPosition = 2;
  294. }
  295. if((event.key == Qt.Key_Right) && (cursorPosition == maximumLength)){
  296. textInput1.focus = true;
  297. }
  298. if((event.key == Qt.Key_Enter) || (event.key == Qt.Key_Return))
  299. {
  300. makeIpAddress();
  301. keyReturnPressed();
  302. }
  303. if(event.key == Qt.Key_Up)
  304. {
  305. makeIpAddress();
  306. keyUpPressed();
  307. }
  308. if(event.key == Qt.Key_Down)
  309. {
  310. makeIpAddress();
  311. keyDownPressed();
  312. }
  313. }
  314. onFocusChanged: {
  315. color = (focus) ? focuscolor : ipcolor;
  316. makeIpAddress();
  317. cursorPosition = 0;
  318. if(focus) ipFocus = 4;
  319. }
  320. }
  321. }