URI: 
       tkivy: use exchange rates in amount dialog - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit b700340ff90fe74f0664e65da2254c3d5d3320bd
   DIR parent 74a9e2296c43320c18893d0cefd78e3cbdf0b9fb
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Thu,  3 Dec 2015 22:43:43 +0100
       
       kivy: use exchange rates in amount dialog
       
       Diffstat:
         M gui/kivy/main.kv                    |       2 +-
         M gui/kivy/main_window.py             |      93 ++++++++++++++++---------------
         M gui/kivy/uix/screens.py             |       3 ---
         M gui/kivy/uix/ui_screens/amount.kv   |      12 +++++++++---
         M plugins/exchange_rate/kivy.py       |      10 +++++++++-
       
       5 files changed, 68 insertions(+), 52 deletions(-)
       ---
   DIR diff --git a/gui/kivy/main.kv b/gui/kivy/main.kv
       t@@ -221,7 +221,7 @@
        <KButton@Button>:
            size_hint: 1, None
            height: '48dp'
       -    on_release: self.label.amount = app.update_amount(self.label.amount, self.text)
       +    on_release: app.update_amount(self.label, self.text)
        
        
        <TabbedPanelStrip>:
   DIR diff --git a/gui/kivy/main_window.py b/gui/kivy/main_window.py
       t@@ -11,6 +11,7 @@ from electrum.i18n import _, set_language
        from electrum.contacts import Contacts
        from electrum.util import profiler
        from electrum.plugins import run_hook
       +from electrum.util import format_satoshis, format_satoshis_plain
        
        from kivy.app import App
        from kivy.core.window import Window
       t@@ -35,7 +36,7 @@ Factory.register('ELTextInput', module='electrum_gui.kivy.uix.screens')
        
        
        # delayed imports: for startup speed on android
       -notification = app = ref = format_satoshis = None
       +notification = app = ref = None
        util = False
        
        
       t@@ -76,35 +77,35 @@ class ElectrumWindow(App):
        
            status = StringProperty(_('Not Connected'))
        
       +    fiat_unit = StringProperty('')
       +
            def decimal_point(self):
                return base_units[self.base_unit]
        
       -    def _get_num_zeros(self):
       -        try:
       -            return self.electrum_config.get('num_zeros', 0)
       -        except AttributeError:
       -            return 0
       -
       -    def _set_num_zeros(self):
       -        try:
       -            self.electrum_config.set_key('num_zeros', value, True)
       -        except AttributeError:
       -            Logger.error('Electrum: Config not available '
       -                         'While trying to save value to config')
       -
       -    num_zeros = AliasProperty(_get_num_zeros , _set_num_zeros)
       -    '''Number of zeros used while representing the value in base_unit.
       -    '''
       -
       -    def get_amount_text(self, amount_str, is_fiat):
       -        text = amount_str + ' ' + self.base_unit if amount_str else ''
       -        if text:
       -            amount = self.get_amount(text)
       -            x = run_hook('format_amount_and_units', amount)
       -            if x:
       -                text += ' / ' + x
       -        return text
       -
       +    def toggle_fiat(self, a):
       +        if not a.is_fiat:
       +            if a.fiat_text:
       +                a.fiat_amount = str(a.fiat_text).split()[0]
       +        else:
       +            if a.btc_text:
       +                a.amount = str(a.btc_text).split()[0]
       +        a.is_fiat = not a.is_fiat
       +
       +    def btc_to_fiat(self, amount_str):
       +        if not amount_str:
       +            return ''
       +        satoshis = self.get_amount(amount_str + ' ' + self.base_unit)
       +        fiat_text = run_hook('format_amount_and_units', satoshis)
       +        return fiat_text if fiat_text else ''
       +
       +    def fiat_to_btc(self, fiat_amount):
       +        if not fiat_amount:
       +            return ''
       +        satoshis = pow(10, 8)
       +        x = run_hook('format_amount_and_units', satoshis)
       +        rate, unit = x.split()
       +        amount = satoshis * Decimal(fiat_amount) / Decimal(rate)
       +        return format_satoshis_plain(amount, self.decimal_point()) + ' ' + self.base_unit
        
            def get_amount(self, amount_str):
                a, u = amount_str.split()
       t@@ -480,29 +481,33 @@ class ElectrumWindow(App):
        
        
            def get_max_amount(self):
       -        from electrum.util import format_satoshis_plain
                inputs = self.wallet.get_spendable_coins(None)
                amount, fee = self.wallet.get_max_amount(self.electrum_config, inputs, None)
                return format_satoshis_plain(amount, self.decimal_point())
        
       -    def update_amount(self, amount, c):
       +    def update_amount(self, label, c):
       +        amount = label.fiat_amount if label.is_fiat else label.amount
                if c == '<':
       -            return amount[:-1]
       -        if c == '.' and amount == '':
       -            return '0.'
       -        if c == '0' and amount == '0':
       -            return '0'
       -        try:
       -            Decimal(amount+c)
       -            amount += c
       -        except:
       -            pass
       -        return amount
       +            amount = amount[:-1]
       +        elif c == '.' and amount == '':
       +            amount = '0.'
       +        elif c == '0' and amount == '0':
       +            amount = '0'
       +        else:
       +            try:
       +                Decimal(amount+c)
       +                amount += c
       +            except:
       +                pass
       +
       +        if label.is_fiat:
       +            label.fiat_amount = amount
       +        else:
       +            label.amount = amount
       +
        
            def format_amount(self, x, is_diff=False, whitespaces=False):
       -        from electrum.util import format_satoshis
       -        return format_satoshis(x, is_diff, self.num_zeros,
       -                               self.decimal_point(), whitespaces)
       +        return format_satoshis(x, is_diff, 0, self.decimal_point(), whitespaces)
        
            @profiler
            def update_wallet(self, *dt):
       t@@ -786,7 +791,7 @@ class ElectrumWindow(App):
                    assert u == self.base_unit
                    popup.ids.a.amount = a
                def cb():
       -            o = popup.ids.a.text
       +            o = popup.ids.a.btc_text
                    label.text = o if o else label.default_text
                    if callback:
                        callback()
   DIR diff --git a/gui/kivy/uix/screens.py b/gui/kivy/uix/screens.py
       t@@ -258,9 +258,6 @@ class ReceiveScreen(CScreen):
                self.update_qr()
        
            @profiler
       -    def update_qrtt(self):
       -        raise
       -
            def update_qr(self):
                from electrum.util import create_URI
                address = self.screen.ids.get('address').text
   DIR diff --git a/gui/kivy/uix/ui_screens/amount.kv b/gui/kivy/uix/ui_screens/amount.kv
       t@@ -17,8 +17,11 @@ Popup:
                        Label:
                            id: a
                            amount: ''
       +                    fiat_amount: ''
                            is_fiat: False
       -                    text: app.get_amount_text(self.amount, self.is_fiat)
       +                    btc_text: app.fiat_to_btc(self.fiat_amount) if self.is_fiat else (self.amount + ' ' + app.base_unit if self.amount else '')
       +                    fiat_text: (self.fiat_amount + ' ' + app.fiat_unit if self.fiat_amount else '') if self.is_fiat else app.btc_to_fiat(self.amount)
       +                    text: (self.fiat_text + ' / ' + self.btc_text if self.is_fiat else self.btc_text + ' / ' + self.fiat_text) if self.btc_text else ''
                            size_hint: 1, 1
        
                    Widget:
       t@@ -75,12 +78,15 @@ Popup:
                            size_hint: 1, None
                            height: '48dp'
                            text: '/'
       -                    on_release: a.is_fiat = not a.is_fiat
       +                    on_release:
       +                        app.toggle_fiat(a)
                        Button:
                            size_hint: 1, None
                            height: '48dp'
                            text: 'Clear'
       -                    on_release: a.amount = ''
       +                    on_release:
       +                        a.amount = ''
       +                        a.fiat_amount = ''
        
                    Widget:
                        size_hint: 1, None
   DIR diff --git a/plugins/exchange_rate/kivy.py b/plugins/exchange_rate/kivy.py
       t@@ -1,3 +1,11 @@
        from exchange_rate import FxPlugin
       +from electrum.plugins import hook
       +
        class Plugin(FxPlugin):
       -    pass
       +    @hook
       +    def load_wallet(self, wallet, window):
       +        self.window = window
       +
       +    def on_quotes(self):
       +        self.print_error("on quotes", self.ccy)
       +        self.window.fiat_unit = self.ccy