Button.qml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * Base class for button
  3. */
  4. import QtQuick 1.1
  5. Rectangle {
  6. property Style style : undefined // must be setted by owner
  7. id: button_pane
  8. width: style.but_width
  9. height: style.but_height
  10. smooth: true
  11. border.width: 1
  12. border.color: style.col_but_border
  13. radius: style.but_outer_radius
  14. state: "plain"
  15. property string lang: "en"
  16. property bool fixed: false //!< is animation available in transition from state to state
  17. property string label_en: ""
  18. property string shift_label_en: ""
  19. property string label_ru: ""
  20. property string shift_label_ru: ""
  21. property string en_label: ""
  22. property string ru_label: ""
  23. property string special: ""
  24. property Gradient grad: style.grad_but_def
  25. signal sigButtonPressed(string key)
  26. /**
  27. * Obtain current Key
  28. */
  29. function currentKey()
  30. {
  31. if (button_pane.special != "")
  32. {
  33. return button_pane.special;
  34. }
  35. return button_pane.lang == "en" ? text_label_en.text : text_label_ru.text;
  36. }
  37. /**
  38. * Change language handler
  39. */
  40. function changeLanguage()
  41. {
  42. text_label_en.state = lang == "en" ? "active" : "inactive"
  43. text_label_ru.state = lang == "ru" ? "active" : "inactive"
  44. }
  45. /**
  46. * Change shift state handler, invoked by owner
  47. */
  48. function changeShiftState (pressed)
  49. {
  50. text_label_en.text = pressed ? shift_label_en : label_en ;
  51. text_label_ru.text = pressed ? shift_label_ru : label_ru ;
  52. }
  53. onLangChanged: {
  54. if (!fixed)
  55. {
  56. changeLanguage()
  57. }
  58. }
  59. Component.onCompleted: {
  60. // initializing
  61. changeLanguage();
  62. changeShiftState (false);
  63. }
  64. states: [
  65. State {
  66. name: "plain"
  67. PropertyChanges {
  68. target: text_label_en
  69. state: "active"
  70. }
  71. PropertyChanges {
  72. target: text_label_ru
  73. state: "inactive"
  74. }
  75. },
  76. State {
  77. name: "shifted"
  78. PropertyChanges {
  79. target: text_label_en
  80. state: "inactive"
  81. }
  82. PropertyChanges {
  83. target: text_label_ru
  84. state: "active"
  85. }
  86. }
  87. ]
  88. transform: Scale {
  89. id: scaling_factor
  90. origin.x: button_pane.width / 2
  91. origin.y: button_pane.height
  92. xScale: 1.0
  93. yScale: 1.0
  94. }
  95. Rectangle {
  96. id: contento
  97. anchors.fill: parent
  98. anchors.leftMargin: 2
  99. anchors.rightMargin: 1
  100. anchors.topMargin: 2
  101. anchors.bottomMargin: 1
  102. radius: root.style.but_outer_radius - 2
  103. smooth: true
  104. gradient: button_pane.grad
  105. KeyLabel {
  106. id: text_label_en
  107. text: button_pane.label_en
  108. state: "active"
  109. cstyle: button_pane.style
  110. }
  111. KeyLabel {
  112. id: text_label_ru
  113. text: button_pane.label_ru
  114. state: "inactive"
  115. cstyle: button_pane.style
  116. }
  117. MouseArea {
  118. hoverEnabled: true
  119. anchors.fill: parent
  120. onEntered: {
  121. button_pane.border.color = root.style.col_but_border_hovered
  122. }
  123. onExited: {
  124. button_pane.border.color = root.style.col_but_border
  125. }
  126. onPressed:
  127. {
  128. button_pane.sigButtonPressed (button_pane.currentKey());
  129. click_animation.start();
  130. }
  131. }
  132. }
  133. SequentialAnimation {
  134. id: click_animation
  135. /**
  136. * For future purposes, for example for handling of the end of animation
  137. signal sigAnimationStop
  138. onRunningChanged: {
  139. if (!running)
  140. {
  141. sigAnimationStop()
  142. }
  143. }
  144. */
  145. PropertyAnimation {
  146. target: button_pane
  147. property: "z"
  148. to: 100
  149. duration: 0
  150. }
  151. ParallelAnimation {
  152. PropertyAnimation {
  153. target: scaling_factor
  154. property: "xScale"
  155. from: 1.0
  156. to: 1.2
  157. duration: 100
  158. }
  159. PropertyAnimation {
  160. target: scaling_factor
  161. property: "yScale"
  162. from: 1.0
  163. to: 2.0
  164. duration: 100
  165. }
  166. PropertyAnimation {
  167. target: button_pane
  168. property: "opacity"
  169. from: 1.0
  170. to: 0.75
  171. duration: 100
  172. }
  173. }
  174. ParallelAnimation {
  175. PropertyAnimation {
  176. target: scaling_factor
  177. property: "xScale"
  178. from: 1.2
  179. to: 1.0
  180. duration: 100
  181. }
  182. PropertyAnimation {
  183. target: scaling_factor
  184. property: "yScale"
  185. from: 2
  186. to: 1.0
  187. duration: 100
  188. }
  189. PropertyAnimation {
  190. target: button_pane
  191. property: "opacity"
  192. from: 0.75
  193. to: 1.0
  194. duration: 100
  195. }
  196. }
  197. PropertyAnimation {
  198. target: button_pane
  199. property: "z"
  200. to: 0
  201. duration: 0
  202. }
  203. }
  204. }