Advertisement
Guest User

Currency Converter

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