IpAddressInput.qml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import QtQuick 2.0
  2. Item {
  3. id: ipAddresInput
  4. property double space: 4
  5. property double ipwidth: 200
  6. property double posX: 0
  7. property double posY: 0
  8. property double partwidth: (ipwidth / 4) - (3 * space)
  9. property color focuscolor: "green"
  10. property color ipcolor: "black"
  11. property color invalidcolor: "red"
  12. property string ipAddress: ""
  13. property double ipFontSize: 14
  14. property bool isValid: false
  15. function makeIpAddress() {
  16. ipAddress = parseInt(textInput1.text, 10) + "." + parseInt(textInput2.text, 10) + "." +
  17. parseInt(textInput3.text, 10) + "." + parseInt(textInput4.text, 10);
  18. if((textInput1.text >= 0) && (textInput1.text <= 255))
  19. textInput1.color = (!textInput1.focus)?ipcolor:focuscolor;
  20. else textInput1.color = invalidcolor;
  21. if((textInput2.text >= 0) && (textInput2.text <= 255))
  22. textInput2.color = (!textInput2.focus)?ipcolor:focuscolor;
  23. else textInput2.color = invalidcolor;
  24. if((textInput3.text >= 0) && (textInput3.text <= 255))
  25. textInput3.color = (!textInput3.focus)?ipcolor:focuscolor;
  26. else textInput3.color = invalidcolor;
  27. if((textInput4.text >= 0) && (textInput4.text <= 255))
  28. textInput4.color = (!textInput4.focus)?ipcolor:focuscolor;
  29. else textInput4.color = invalidcolor;
  30. }
  31. function fillIpFields() {
  32. function padDigits(number, digits) {
  33. return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
  34. };
  35. var iplist = ipAddress.split(".");
  36. var i;
  37. for ( i = 0; i < iplist.length; i++) iplist[i] = padDigits(iplist[i], 3);
  38. if(iplist.length >= 1) textInput1.text = iplist[0];
  39. if(iplist.length >= 2) textInput2.text = iplist[1];
  40. if(iplist.length >= 3) textInput3.text = iplist[2];
  41. if(iplist.length >= 4) textInput4.text = iplist[3];
  42. }
  43. onIpAddressChanged: {
  44. fillIpFields();
  45. }
  46. Component.onCompleted: {
  47. fillIpFields();
  48. }
  49. TextInput {
  50. id: textInput1
  51. x: posX
  52. y: posY
  53. width: partwidth
  54. font.pixelSize: ipFontSize
  55. KeyNavigation.tab: textInput2
  56. Keys.onReturnPressed:
  57. {
  58. textInput2.focus = true;
  59. }
  60. color: ipcolor
  61. validator: IntValidator {bottom:0; top:999}
  62. inputMask: "000;"
  63. inputMethodHints: Qt.ImhDigitsOnly
  64. maximumLength: 3
  65. Keys.onReleased: {
  66. if(cursorPosition == maximumLength){
  67. textInput2.focus = true;
  68. }
  69. }
  70. Keys.onPressed: {
  71. if((event.key == Qt.Key_Left) && (cursorPosition == 0)){
  72. textInput4.focus = true; textInput4.cursorPosition = 2;
  73. }
  74. }
  75. onFocusChanged: {
  76. color = (focus) ? focuscolor : ipcolor;
  77. makeIpAddress();
  78. cursorPosition = 0;
  79. }
  80. }
  81. Text{
  82. id: ipDot1
  83. font.pixelSize: ipFontSize
  84. x: posX + textInput1.width
  85. y: textInput1.y
  86. color: ipcolor
  87. text:"."
  88. }
  89. TextInput {
  90. id: textInput2
  91. x: posX + space + partwidth + ipDot1.width
  92. y: posY
  93. width: partwidth
  94. font.pixelSize: ipFontSize
  95. KeyNavigation.tab: textInput3
  96. Keys.onReturnPressed:
  97. {
  98. textInput3.focus = true;
  99. }
  100. color: ipcolor
  101. validator: IntValidator {bottom:0; top:999}
  102. inputMask: "000;"
  103. inputMethodHints: Qt.ImhDigitsOnly
  104. maximumLength: 3
  105. Keys.onReleased: {
  106. if(cursorPosition == maximumLength){
  107. textInput3.focus = true;
  108. }
  109. }
  110. Keys.onPressed: {
  111. if((event.key == Qt.Key_Left) && (cursorPosition == 0)){
  112. textInput1.focus = true; textInput1.cursorPosition = 2;
  113. }
  114. }
  115. onFocusChanged: {
  116. color = (focus) ? focuscolor : ipcolor;
  117. makeIpAddress();
  118. cursorPosition = 0;
  119. }
  120. }
  121. Text{
  122. id: ipDot2
  123. font.pixelSize: ipFontSize
  124. x: textInput2.x + textInput2.width
  125. y: textInput1.y
  126. color: ipcolor
  127. text:"."
  128. }
  129. TextInput {
  130. id: textInput3
  131. x: posX + 2*space + 2*partwidth +ipDot2.width
  132. y: posY
  133. width: partwidth
  134. font.pixelSize: ipFontSize
  135. KeyNavigation.tab: textInput4
  136. Keys.onReturnPressed:
  137. {
  138. textInput4.focus = true;
  139. }
  140. color: ipcolor
  141. validator: IntValidator {bottom:0; top:999}
  142. inputMask: "000;"
  143. inputMethodHints: Qt.ImhDigitsOnly
  144. maximumLength: 3
  145. Keys.onReleased: {
  146. if(cursorPosition == maximumLength){
  147. textInput4.focus = true;
  148. }
  149. }
  150. Keys.onPressed: {
  151. if((event.key == Qt.Key_Left) && (cursorPosition == 0)){
  152. textInput2.focus = true; textInput2.cursorPosition = 2;
  153. }
  154. }
  155. onFocusChanged: {
  156. color = (focus) ? focuscolor : ipcolor;
  157. makeIpAddress();
  158. cursorPosition = 0;
  159. }
  160. }
  161. Text{
  162. id: ipDot3
  163. font.pixelSize: ipFontSize
  164. x: textInput3.x + textInput3.width
  165. y: textInput1.y
  166. color: ipcolor
  167. text:"."
  168. }
  169. TextInput {
  170. id: textInput4
  171. x: posX + 3*space + 3*partwidth + ipDot3.width
  172. y: posY
  173. width: partwidth
  174. font.pixelSize: ipFontSize
  175. KeyNavigation.tab: textInput1
  176. Keys.onReturnPressed:
  177. {
  178. textInput1.focus = true;
  179. }
  180. color: ipcolor
  181. validator: IntValidator {bottom:0; top:999}
  182. inputMask: "000;"
  183. inputMethodHints: Qt.ImhDigitsOnly
  184. maximumLength: 3
  185. Keys.onReleased: {
  186. if(cursorPosition == maximumLength){
  187. textInput1.focus = true;
  188. }
  189. }
  190. Keys.onPressed: {
  191. if((event.key == Qt.Key_Left) && (cursorPosition == 0)){
  192. textInput3.focus = true; textInput3.cursorPosition = 2;
  193. }
  194. }
  195. onFocusChanged: {
  196. color = (focus) ? focuscolor : ipcolor;
  197. makeIpAddress();
  198. cursorPosition = 0;
  199. }
  200. }
  201. }