CLButton.qml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. Rectangle {
  51. id: rectangle
  52. property CLStyle style: CLStyle {}
  53. property alias text: text.text
  54. property color colorWhenDefault: style.colorWhenDefault
  55. property color colorWhenPressed: style.colorWhenPressed
  56. property color colorWhenHovered: style.colorWhenHovered
  57. property color colorWhenSelected: style.colorWhenSelected
  58. property color textColor: style.textColor
  59. property real roundness: style.roundness
  60. property color borderColor: style.borderColor
  61. property int borderWidth: style.borderWidth
  62. property real fontSize: style.fontSize
  63. property string fontFamily: style.fontFamily
  64. property string fontWeight: style.fontWeight
  65. property color borderColorWhenHovered: style.borderColorWhenHovered
  66. property color borderColorWhenPressed: style.borderColorWhenPressed
  67. property color borderColorWhenSelected: style.borderColorWhenSelected
  68. /* Properties for background images
  69. * --------------------------------
  70. * This solution is temporary. Remember performance.
  71. */
  72. //Private properties start
  73. property Image nullImage: Image { //this property is "private" don't write it to documentation
  74. id: "null"
  75. source: ""
  76. width: rectangle.height
  77. height: rectangle.height
  78. fillMode: Image.PreserveAspectCrop
  79. smooth: false
  80. scale: 1
  81. }
  82. property Gradient nullGradient: Gradient{}
  83. property Image currentImage: backgroundImageWhenDefault //this property is "private" don't write it to documentation
  84. //Private properties end
  85. property Image backgroundImage: nullImage
  86. property Image backgroundImageWhenDefault: backgroundImage
  87. property Image backgroundImageWhenHovered: backgroundImage
  88. property Image backgroundImageWhenPressed: backgroundImage
  89. property Image backgroundImageWhenSelected: backgroundImage
  90. property bool gradientDefaultOn: style.gradientDefaultOn
  91. property bool gradientHoveredOn: style.gradientHoveredOn
  92. property bool gradientPressedOn: style.gradientPressedOn
  93. property bool gradientSelectedOn: style.gradientSelectedOn
  94. property bool hoveredStateOn: style.hoveredStateOn
  95. property bool pressedStateOn: style.pressedStateOn
  96. property Gradient gradientWhenDefault: style.gradientWhenDefault
  97. property Gradient gradientWhenHovered: style.gradientWhenHovered
  98. property Gradient gradientWhenPressed: style.gradientWhenPressed
  99. property Gradient gradientWhenSelected: style.gradientWhenSelected
  100. property bool disabled: false
  101. property bool selected: false
  102. property string textAlign: "center"
  103. property string imageAlign: "center"
  104. property real leftMargin: 0
  105. property real rightMargin: 0
  106. signal clicked()
  107. Component.onCompleted: {
  108. if(!hoveredStateOn) stateHovered.destroy();
  109. if(!pressedStateOn) statePressed.destroy();
  110. }
  111. width: text.width + 15
  112. height: text.height + 15
  113. color: colorWhenDefault
  114. smooth: true
  115. radius: Functions.countRadius(roundness,width,height,0,1)
  116. border.color: borderColor
  117. border.width: borderWidth
  118. gradient: (gradientDefaultOn)?gradientWhenDefault:nullGradient
  119. Image {
  120. id: image
  121. source: currentImage.source
  122. width: currentImage.width
  123. height: currentImage.height
  124. fillMode: currentImage.fillMode
  125. smooth: currentImage.smooth
  126. scale: currentImage.scale
  127. anchors.left: if(imageAlign == "left") parent.left
  128. anchors.right: if(imageAlign == "right") parent.right
  129. anchors.horizontalCenter: if(imageAlign == "center") parent.horizontalCenter
  130. anchors.verticalCenter: parent.verticalCenter
  131. x: currentImage.x
  132. y: currentImage.y
  133. //TODO: add all image properties and get them from current image
  134. }
  135. Text {
  136. id: text
  137. text: "CLButton"
  138. anchors.horizontalCenter: if(textAlign == "center") rectangle.horizontalCenter
  139. anchors.left: if(textAlign == "left") rectangle.left
  140. anchors.right: if(textAlign == "right" ) rectangle.right
  141. anchors.rightMargin: rightMargin
  142. anchors.leftMargin: leftMargin
  143. anchors.verticalCenter: rectangle.verticalCenter
  144. font.family: fontFamily
  145. font.pointSize: 0.001 + fontSize
  146. color: textColor
  147. font.weight: fontWeight
  148. }
  149. MouseArea {
  150. id: mouseArea
  151. anchors.fill: parent
  152. onClicked: rectangle.clicked()
  153. hoverEnabled: true
  154. }
  155. state: ""
  156. states: [
  157. State {
  158. id: statePressed
  159. name: "pressed"; when: mouseArea.pressed && !disabled
  160. PropertyChanges { target: rectangle; gradient: (gradientPressedOn)?gradientWhenPressed:nullGradient; color: colorWhenPressed; }
  161. PropertyChanges { target: rectangle; border.color: borderColorWhenPressed }
  162. PropertyChanges { target: rectangle; color: colorWhenPressed}
  163. PropertyChanges { target: rectangle; currentImage: backgroundImageWhenPressed}
  164. },
  165. State {
  166. id: stateSelected
  167. name: "selected"; when: selected
  168. PropertyChanges { target: rectangle; gradient: (gradientSelectedOn)?gradientWhenSelected:nullGradient; color: colorWhenSelected; }
  169. PropertyChanges { target: rectangle; border.color: borderColorWhenSelected }
  170. PropertyChanges { target: rectangle; color: colorWhenSelected}
  171. PropertyChanges { target: rectangle; currentImage: backgroundImageWhenSelected }
  172. },
  173. State {
  174. id: stateHovered
  175. name: "entered"; when: mouseArea.containsMouse && !disabled
  176. PropertyChanges { target: rectangle; gradient: (gradientHoveredOn)?gradientWhenHovered:nullGradient; color: colorWhenHovered; }
  177. PropertyChanges { target: rectangle; border.color: borderColorWhenHovered }
  178. PropertyChanges { target: rectangle; color: colorWhenHovered}
  179. PropertyChanges { target: rectangle; currentImage: backgroundImageWhenHovered }
  180. },
  181. State {
  182. id: stateDisabled
  183. name: "disabled"; when: disabled
  184. PropertyChanges {target: rectangle; color: if(gradientDefaultOn) "white"; else colorWhenDefault}
  185. PropertyChanges {target: rectangle; opacity: 0.6}
  186. PropertyChanges {target: mouseArea; enabled: false}
  187. }
  188. ]
  189. }