fx_dialog.py (4298B)
1 from kivy.app import App 2 from kivy.factory import Factory 3 from kivy.properties import ObjectProperty, BooleanProperty 4 from kivy.lang import Builder 5 6 Builder.load_string(''' 7 <FxDialog@Popup> 8 id: popup 9 title: 'Fiat Currency' 10 size_hint: 0.8, 0.8 11 pos_hint: {'top':0.9} 12 BoxLayout: 13 orientation: 'vertical' 14 15 Widget: 16 size_hint: 1, 0.1 17 18 BoxLayout: 19 orientation: 'horizontal' 20 size_hint: 1, 0.1 21 Label: 22 text: _('Currency') 23 height: '48dp' 24 Spinner: 25 height: '48dp' 26 id: ccy 27 on_text: popup.on_currency(self.text) 28 29 Widget: 30 size_hint: 1, 0.05 31 32 BoxLayout: 33 orientation: 'horizontal' 34 size_hint: 1, 0.2 35 Label: 36 text: _('History rates') 37 CheckBox: 38 id:hist 39 active: popup.has_history_rates 40 on_active: popup.on_checkbox_history(self.active) 41 42 Widget: 43 size_hint: 1, 0.05 44 45 BoxLayout: 46 orientation: 'horizontal' 47 size_hint: 1, 0.1 48 Label: 49 text: _('Source') 50 height: '48dp' 51 Spinner: 52 height: '48dp' 53 id: exchanges 54 on_text: popup.on_exchange(self.text) 55 56 Widget: 57 size_hint: 1, 0.1 58 59 BoxLayout: 60 orientation: 'horizontal' 61 size_hint: 1, 0.2 62 Button: 63 text: 'Cancel' 64 size_hint: 0.5, None 65 height: '48dp' 66 on_release: popup.dismiss() 67 Button: 68 text: 'OK' 69 size_hint: 0.5, None 70 height: '48dp' 71 on_release: 72 root.callback() 73 popup.dismiss() 74 ''') 75 76 77 from kivy.uix.label import Label 78 from kivy.uix.checkbox import CheckBox 79 from kivy.uix.widget import Widget 80 from kivy.clock import Clock 81 82 from electrum.gui.kivy.i18n import _ 83 from functools import partial 84 85 class FxDialog(Factory.Popup): 86 87 def __init__(self, app, plugins, config, callback): 88 self.app = app 89 self.config = config 90 self.callback = callback 91 self.fx = self.app.fx 92 if self.fx.get_history_config(allow_none=True) is None: 93 # If nothing is set, force-enable it. (Note that as fiat rates itself 94 # are disabled by default, it is enough to set this here. If they 95 # were enabled by default, this would be too late.) 96 self.fx.set_history_config(True) 97 self.has_history_rates = self.fx.get_history_config() 98 99 Factory.Popup.__init__(self) 100 self.add_currencies() 101 102 def add_exchanges(self): 103 ex = self.ids.exchanges 104 if self.fx.is_enabled(): 105 exchanges = sorted(self.fx.get_exchanges_by_ccy(self.fx.get_currency(), self.has_history_rates)) 106 mx = self.fx.exchange.name() 107 if mx in exchanges: 108 ex.text = mx 109 elif exchanges: 110 ex.text = exchanges[0] 111 else: 112 ex.text = '' 113 else: 114 exchanges = [] 115 ex.text = '' 116 ex.values = exchanges 117 118 def on_exchange(self, text): 119 if not text: 120 return 121 if self.fx.is_enabled() and text != self.fx.exchange.name(): 122 self.fx.set_exchange(text) 123 124 def add_currencies(self): 125 currencies = [_('None')] + self.fx.get_currencies(self.has_history_rates) 126 my_ccy = self.fx.get_currency() if self.fx.is_enabled() else _('None') 127 self.ids.ccy.values = currencies 128 self.ids.ccy.text = my_ccy 129 130 def on_checkbox_history(self, checked): 131 self.fx.set_history_config(checked) 132 self.has_history_rates = checked 133 self.add_currencies() 134 self.on_currency(self.ids.ccy.text) 135 136 def on_currency(self, ccy): 137 b = (ccy != _('None')) 138 self.fx.set_enabled(b) 139 if b: 140 if ccy != self.fx.get_currency(): 141 self.fx.set_currency(ccy) 142 self.app.fiat_unit = ccy 143 else: 144 self.app.is_fiat = False 145 Clock.schedule_once(lambda dt: self.add_exchanges())