CLLineEdit.qml 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * Copyright © 2010 Digia Plc
  3. * Copyright © 2010 Nokia Corporation
  4. *
  5. * All rights reserved.
  6. *
  7. * Nokia and Nokia Connecting People are registered trademarks of
  8. * Nokia Corporation.
  9. * Java and all Java-based marks are trademarks or registered
  10. * trademarks of
  11. * Sun Microsystems, Inc. Other product and company names
  12. * mentioned herein may be
  13. * trademarks or trade names of their respective owners.
  14. *
  15. *
  16. * Subject to the conditions below, you may, without charge:
  17. *
  18. * · Use, copy, modify and/or merge copies of this software and
  19. * associated documentation files (the "Software")
  20. *
  21. * · Publish, distribute, sub-licence and/or sell new software
  22. * derived from or incorporating the Software.
  23. *
  24. *
  25. * This file, unmodified, shall be included with all copies or
  26. * substantial portions
  27. * of the Software that are distributed in source code form.
  28. *
  29. * The Software cannot constitute the primary value of any new
  30. * software derived
  31. * from or incorporating the Software.
  32. *
  33. * Any person dealing with the Software shall not misrepresent
  34. * the source of the Software.
  35. *
  36. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  37. * KIND, EXPRESS OR IMPLIED,
  38. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  39. * MERCHANTABILITY, FITNESS FOR A
  40. * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41. * AUTHORS OR COPYRIGHT
  42. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  43. * WHETHER IN AN ACTION
  44. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  45. * CONNECTION WITH THE
  46. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  47. */
  48. import Qt 4.7
  49. import "javascripts/functions.js" as Functions
  50. /* TODO:
  51. * Next releases of Qt will change the usage of selectionStart and selectionEnd properties.
  52. * Properties are to be change to read only properties.
  53. * For selection functionalities, there will be select (start, end) -function.
  54. * When these changes are made, this component's selectionStart & selectionEnd- functionalities
  55. * has to be changed corresponding selection funtionality.
  56. */
  57. Rectangle {
  58. id: rectangle
  59. property CLStyle style: CLStyle {}
  60. property bool activeStateOn: style.activeStateOn
  61. property color borderColor: style.borderColor
  62. property color borderColorWhenActive: style.borderColorWhenActive
  63. property color borderColorWhenHovered: style.borderColorWhenHovered
  64. property int borderWidth: style.borderWidth
  65. property color colorWhenDefault: style.colorWhenDefault
  66. property color colorWhenActive: style.colorWhenDefault
  67. property color colorWhenHovered: style.colorWhenHovered
  68. property string fontFamily: style.fontFamily
  69. property real fontSize: style.fontSize
  70. property string fontWeight: style.fontWeight
  71. property bool gradientActiveOn: style.gradientActiveOn
  72. property bool gradientDefaultOn: style.gradientDefaultOn
  73. property bool gradientHoveredOn: style.gradientHoveredOn
  74. property Gradient gradientWhenActive: style.gradientWhenDefault
  75. property Gradient gradientWhenDefault: style.gradientWhenDefault
  76. property Gradient gradientWhenHovered: style.gradientWhenHovered
  77. property bool hoveredStateOn: style.hoveredStateOn
  78. property real roundness: style.itemRoundness
  79. property color textColor: style.textColor
  80. property color textColorWhenSelected: style.selectedTextColor
  81. property color textAreaColorWhenSelected: style.selectionColor
  82. property int maxLength: -1
  83. property bool cursorVisible: false
  84. property alias selectionStart: lineEdit.selectionStart
  85. property alias selectionEnd: lineEdit.selectionEnd
  86. property alias cursorPosition: lineEdit.cursorPosition
  87. property bool setFocus: false
  88. property alias text: lineEdit.text
  89. property Gradient nullGradient: Gradient{}
  90. signal clicked()
  91. signal textChange()
  92. width: 150
  93. height: 30
  94. color: rectangle.colorWhenDefault
  95. smooth: false
  96. radius: Functions.countRadius(roundness,width,height,0,1)
  97. border.color: rectangle.borderColor
  98. border.width: rectangle.borderWidth
  99. Component.onCompleted: {
  100. if (!rectangle.hoveredStateOn) stateHovered.destroy();
  101. if (!rectangle.activeStateOn) stateActive.destroy();
  102. if (maxLength != -1 && lineEdit.text.length >= maxLength) {
  103. lineEdit.text = lineEdit.text.slice(0,maxLength);
  104. lineEdit.cursorPosition = maxLength;
  105. }
  106. }
  107. /**
  108. * Function which can be used to insert text in lineEdit.
  109. *
  110. * @param insert Text to insert.
  111. * @return nothing
  112. */
  113. function insertText(insert) {
  114. var selection = lineEdit.selectedText;
  115. var selectionStart = lineEdit.selectionStart;
  116. var selectionEnd = lineEdit.selectionEnd;
  117. if (selection == "") {
  118. var cursor = lineEdit.cursorPosition;
  119. var start = lineEdit.text.substring(0,cursor);
  120. var end = lineEdit.text.substring(cursor,lineEdit.text.length);
  121. var resultText = ""+start+insert+end;
  122. lineEdit.text = resultText;
  123. lineEdit.cursorPosition = cursor+1;
  124. }
  125. else {
  126. var start = lineEdit.text.substring(0,selectionStart);
  127. var end = lineEdit.text.substring(selectionEnd,lineEdit.text.length);
  128. var resultText = ""+start+insert+end;
  129. lineEdit.text = resultText;
  130. lineEdit.cursorPosition = selectionEnd;
  131. }
  132. }
  133. /**
  134. * Function which is used to remove text in text lineEdit.
  135. */
  136. function removeText() {
  137. var selection = lineEdit.selectedText;
  138. var selectionStart = lineEdit.selectionStart;
  139. var selectionEnd = lineEdit.selectionEnd;
  140. if (selection == "") {
  141. var cursor = lineEdit.cursorPosition;
  142. if (cursor > 0) {
  143. var cursor = lineEdit.cursorPosition;
  144. var start = lineEdit.text.substring(0,cursor-1);
  145. var end = lineEdit.text.substring(cursor,lineEdit.text.length);
  146. var resultText = ""+start+end;
  147. lineEdit.text = resultText;
  148. lineEdit.cursorPosition = cursor-1;
  149. }
  150. else {
  151. lineEdit.cursorPosition = 0;
  152. }
  153. }
  154. else {
  155. var start = lineEdit.text.substring(0,selectionStart);
  156. var end = lineEdit.text.substring(selectionEnd,lineEdit.text.length);
  157. var resultText = ""+start+end;
  158. lineEdit.text = resultText;
  159. lineEdit.cursorPosition = selectionStart;
  160. }
  161. }
  162. /**
  163. * Makes text selection between given parameters
  164. *
  165. * @param start where selection starts.
  166. * @param end where selection ends.
  167. *
  168. */
  169. function selectText (start,end){
  170. lineEdit.cursorPosition = start;
  171. lineEdit.moveCursorSelection(end);
  172. return;
  173. }
  174. Rectangle {
  175. id: gradientContainer
  176. anchors.fill: rectangle
  177. color: rectangle.colorWhenDefault
  178. gradient: (rectangle.gradientDefaultOn)?rectangle.gradientWhenDefault:rectangle.nullGradient
  179. radius: rectangle.radius
  180. }
  181. MouseArea {
  182. id: mouseArea
  183. anchors.fill: rectangle
  184. onClicked: rectangle.clicked()
  185. hoverEnabled: true;
  186. }
  187. TextInput {
  188. id: lineEdit
  189. onTextChanged: {
  190. rectangle.textChange()
  191. if (maxLength != -1 && lineEdit.text.length >= maxLength) {
  192. lineEdit.text = lineEdit.text.slice(0,maxLength);
  193. lineEdit.cursorPosition = maxLength;
  194. }
  195. }
  196. text: ""
  197. x: roundness*5+2
  198. focus: rectangle.setFocus
  199. cursorVisible: rectangle.cursorVisible
  200. width: rectangle.width - x *2
  201. height: rectangle.height * 0.5
  202. font.family: fontFamily
  203. font.pointSize: lineEdit.height * 0.6
  204. font.weight: fontWeight
  205. color: textColor
  206. anchors.verticalCenter: rectangle.verticalCenter
  207. selectedTextColor: textColorWhenSelected
  208. selectionColor: textAreaColorWhenSelected
  209. selectByMouse:true
  210. }
  211. states: [
  212. State {
  213. id: stateActive
  214. name: "active"; when: lineEdit.focus
  215. PropertyChanges { target: gradientContainer; gradient: (rectangle.gradientActiveOn)?rectangle.gradientWhenActive:rectangle.nullGradient;}
  216. PropertyChanges { target: rectangle; border.color: rectangle.borderColorWhenActive }
  217. PropertyChanges { target: rectangle; color: rectangle.colorWhenActive}
  218. },
  219. State {
  220. id: stateHovered
  221. name: "entered"; when: mouseArea.containsMouse
  222. PropertyChanges { target: gradientContainer; gradient: (rectangle.gradientHoveredOn)?rectangle.gradientWhenHovered:rectangle.nullGradient;}
  223. PropertyChanges { target: rectangle; border.color: rectangle.borderColorWhenHovered }
  224. PropertyChanges { target: rectangle; color: rectangle.colorWhenHovered}
  225. }
  226. ]
  227. }