Advertisement
Guest User

Untitled

a guest
Jan 26th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. import QtQuick 2.0
  2. import QtQuick.XmlListModel 2.0
  3. import Ubuntu.Components 0.1
  4. import Ubuntu.Components.ListItems 0.1
  5. import Ubuntu.Components.Popups 0.1
  6.  
  7.  
  8. Rectangle {
  9. id: root
  10. width: units.gu(60)
  11. height: units.gu(80)
  12. color: "lightgray"
  13.  
  14. property real margins: units.gu(2)
  15. property real buttonWidth: units.gu(9)
  16.  
  17. Label {
  18. id: title
  19. ItemStyle.class: "title"
  20. text: i18n.tr("Currency Converter")
  21. height: contentHeight + root.margins
  22. anchors {
  23. left: parent.left
  24. right: parent.right
  25. top: parent.top
  26. }
  27. }
  28. ListModel {
  29. id: currencies
  30. ListElement {
  31. currency: "EUR"
  32. rate: 1.0
  33. }
  34.  
  35. function getCurrency(idx) {
  36. return (idx >= 0 && idx < count) ? get(idx).currency: ""
  37. }
  38.  
  39. function getRate(idx) {
  40. return (idx >= 0 && idx < count) ? get(idx).rate: 0.0
  41. }
  42. }
  43. XmlListModel {
  44. id: ratesFetcher
  45. source: "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"
  46. namespaceDeclarations: "declare namespace gesmes='http://www.gesmes.org/xml/2002-08-01';"
  47. +"declare default element namespace 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref';"
  48. query: "/gesmes:Envelope/Cube/Cube/Cube"
  49.  
  50. onStatusChanged: {
  51. if (status === XmlListModel.Ready) {
  52. for (var i = 0; i < count; i++)
  53. currencies.append({"currency": get(i).currency, "rate": parseFloat(get(i).rate)})
  54. }
  55. }
  56.  
  57. XmlRole { name: "currency"; query: "@currency/string()" }
  58. XmlRole { name: "rate"; query: "@rate/string()" }
  59. }
  60. ActivityIndicator {
  61. anchors.right: parent.right
  62. running: ratesFetcher.status === XmlListModel.Loading
  63. }
  64. function convert(from, fromRateIndex, toRateIndex) {
  65. var fromRate = currencies.getRate(fromRateIndex);
  66. if (from.length <= 0 || fromRate <= 0.0)
  67. return "";
  68. return currencies.getRate(toRateIndex) * (parseFloat(from) / fromRate);
  69. }
  70. Component {
  71. id: currencySelector
  72. Popover {
  73. Column {
  74. anchors {
  75. top: parent.top
  76. left: parent.left
  77. right: parent.right
  78. }
  79. height: pageLayout.height
  80. Header {
  81. id: header
  82. text: i18n.tr("Select currency")
  83. }
  84. ListView {
  85. clip: true
  86. width: parent.width
  87. height: parent.height - header.height
  88. model: currencies
  89. delegate: Standard {
  90. text: currency
  91. onClicked: {
  92. caller.currencyIndex = index
  93. caller.input.update()
  94. hide()
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. Column {
  102. id: pageLayout
  103.  
  104. anchors {
  105. fill: parent
  106. margins: root.margins
  107. topMargin: title.height
  108. }
  109. spacing: units.gu(1)
  110.  
  111. Row {
  112. spacing: units.gu(1)
  113.  
  114. Button {
  115. id: selectorFrom
  116. property int currencyIndex: 0
  117. property TextField input: inputFrom
  118. text: currencies.getCurrency(currencyIndex)
  119. onClicked: PopupUtils.open(currencySelector, selectorFrom)
  120. }
  121.  
  122. TextField {
  123. id: inputFrom
  124. errorHighlight: false
  125. validator: DoubleValidator {notation: DoubleValidator.StandardNotation}
  126. width: pageLayout.width - 2 * root.margins - root.buttonWidth
  127. height: units.gu(4)
  128. font.pixelSize: FontUtils.sizeToPixels("medium")
  129. text: '0.0'
  130. onTextChanged: {
  131. if (activeFocus) {
  132. inputTo.text = convert(inputFrom.text, selectorFrom.currencyIndex, selectorTo.currencyIndex)
  133. }
  134. }
  135. function update() {
  136. text = convert(inputTo.text, selectorTo.currencyIndex, selectorFrom.currencyIndex)
  137. }
  138. }
  139. }
  140.  
  141. Row {
  142. spacing: units.gu(1)
  143. Button {
  144. id: selectorTo
  145. property int currencyIndex: 1
  146. property TextField input: inputTo
  147. text: currencies.getCurrency(currencyIndex)
  148. onClicked: PopupUtils.open(currencySelector, selectorTo)
  149. }
  150.  
  151. TextField {
  152. id: inputTo
  153. errorHighlight: false
  154. validator: DoubleValidator {notation: DoubleValidator.StandardNotation}
  155. width: pageLayout.width - 2 * root.margins - root.buttonWidth
  156. height: units.gu(4)
  157. font.pixelSize: FontUtils.sizeToPixels("medium")
  158. text: '0.0'
  159. onTextChanged: {
  160. if (activeFocus) {
  161. inputFrom.text = convert(inputTo.text, selectorTo.currencyIndex, selectorFrom.currencyIndex)
  162. }
  163. }
  164. function update() {
  165. text = convert(inputFrom.text, selectorFrom.currencyIndex, selectorTo.currencyIndex)
  166. }
  167. }
  168. }
  169.  
  170. Button {
  171. text: i18n.tr("Clear")
  172. width: units.gu(12)
  173. onClicked: {
  174. inputTo.text = '0.0';
  175. inputFrom.text = '0.0';
  176. }
  177. }
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement