tformat_amount_and_units: general method to display amounts and fiat values. fixes #1078 - electrum - Electrum Bitcoin wallet
 (HTM) git clone https://git.parazyd.org/electrum
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
       ---
 (DIR) commit 483cf2286f03c5b11412a51c19de79e9e6ed3cc2
 (DIR) parent 7c4b7493401328f08a36f0f6299223e43b099c82
 (HTM) Author: ThomasV <thomasv@electrum.org>
       Date:   Fri, 11 Sep 2015 16:46:37 +0200
       
       format_amount_and_units: general method to display amounts and fiat values. fixes #1078
       
       Diffstat:
         M gui/qt/main_window.py               |      23 +++++++++++++++--------
         M plugins/exchange_rate.py            |       8 ++++++--
       
       2 files changed, 21 insertions(+), 10 deletions(-)
       ---
 (DIR) diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py
       t@@ -432,8 +432,8 @@ class ElectrumWindow(QMainWindow, PrintError):
                            is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(tx)
                            if(v > 0):
                                total_amount += v
       -                self.notify(_("%(txs)s new transactions received. Total amount received in the new transactions %(amount)s %(unit)s") \
       -                            % { 'txs' : tx_amount, 'amount' : self.format_amount(total_amount), 'unit' : self.base_unit()})
       +                self.notify(_("%(txs)s new transactions received. Total amount received in the new transactions %(amount)s") \
       +                            % { 'txs' : tx_amount, 'amount' : self.format_amount_and_units(total_amount)})
                        self.tx_notifications = []
                    else:
                      for tx in self.tx_notifications:
       t@@ -441,7 +441,7 @@ class ElectrumWindow(QMainWindow, PrintError):
                              self.tx_notifications.remove(tx)
                              is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(tx)
                              if(v > 0):
       -                          self.notify(_("New transaction received. %(amount)s %(unit)s") % { 'amount' : self.format_amount(v), 'unit' : self.base_unit()})
       +                          self.notify(_("New transaction received. %(amount)s") % { 'amount' : self.format_amount_and_units(v)})
        
            def notify(self, message):
                if self.tray:
       t@@ -483,6 +483,13 @@ class ElectrumWindow(QMainWindow, PrintError):
            def format_amount(self, x, is_diff=False, whitespaces=False):
                return format_satoshis(x, is_diff, self.num_zeros, self.decimal_point, whitespaces)
        
       +    def format_amount_and_units(self, amount):
       +        text = self.format_amount(amount) + ' '+ self.base_unit()
       +        x = run_hook('format_amount_and_units', amount)
       +        if x:
       +            text += ''.join(x)
       +        return text
       +
            def get_decimal_point(self):
                return self.decimal_point
        
       t@@ -518,7 +525,7 @@ class ElectrumWindow(QMainWindow, PrintError):
                        icon = QIcon(":icons/status_lagging.png")
                    else:
                        c, u, x = self.wallet.get_account_balance(self.current_account)
       -                text =  _("Balance" ) + ": %s "%(self.format_amount(c)) + self.base_unit()
       +                text =  _("Balance" ) + ": %s "%(self.format_amount_and_units(c))
                        if u:
                            text +=  " [%s unconfirmed]"%(self.format_amount(u, True).strip())
                        if x:
       t@@ -1170,6 +1177,7 @@ class ElectrumWindow(QMainWindow, PrintError):
                if not r:
                    return
                outputs, fee, tx_desc, coins = r
       +        amount = sum(map(lambda x:x[2], outputs))
                try:
                    tx = self.wallet.make_unsigned_transaction(coins, outputs, self.config, fee)
                except NotEnoughFunds:
       t@@ -1188,11 +1196,10 @@ class ElectrumWindow(QMainWindow, PrintError):
                    self.show_transaction(tx, tx_desc)
                    return
                # confirmation dialog
       -        amount = sum(map(lambda x:x[2], outputs))
                confirm_amount = self.config.get('confirm_amount', COIN)
                msg = [
       -            _("Amount to be sent") + ": %s"%(self.format_amount(amount) + ' '+ self.base_unit()),
       -            _("Transaction fee") + ": %s"%(self.format_amount(fee) + ' '+ self.base_unit()),
       +            _("Amount to be sent") + ": " + self.format_amount_and_units(amount),
       +            _("Transaction fee") + ": " + self.format_amount_and_units(fee),
                ]
                if self.wallet.use_encryption:
                    msg.append(_("Enter your password to proceed"))
       t@@ -1248,7 +1255,7 @@ class ElectrumWindow(QMainWindow, PrintError):
                if tx.get_fee() >= confirm_fee:
                    msg = '\n'.join([
                        _("The fee for this transaction seems unusually high."),
       -                _("Are you really sure you want to pay %(fee)s in fees?")%{ 'fee' : self.format_amount(fee) + ' '+ self.base_unit()}
       +                _("Are you really sure you want to pay %(fee)s in fees?")%{ 'fee' : self.format_amount_and_units(fee)}
                    ])
                    if not self.question(msg):
                        return
 (DIR) diff --git a/plugins/exchange_rate.py b/plugins/exchange_rate.py
       t@@ -394,14 +394,18 @@ class Plugin(BasePlugin, ThreadJob):
                    return Decimal(rate)
        
            @hook
       +    def format_amount_and_units(self, btc_balance):
       +        rate = self.exchange_rate()
       +        return '' if rate is None else " (%s %s)" % (self.value_str(btc_balance, rate), self.ccy)
       +
       +    @hook
            def get_fiat_status_text(self, btc_balance, result):
                # return status as:   (1.23 USD)    1 BTC~123.45 USD
                rate = self.exchange_rate()
                if rate is None:
                    text = _("  (No FX rate available)")
                else:
       -            text =  "  (%s %s)    1 BTC~%s %s" % (self.value_str(btc_balance, rate), self.ccy,
       -                                                  self.value_str(COIN, rate), self.ccy)
       +            text =  "1 BTC~%s %s" % (self.value_str(COIN, rate), self.ccy)
                result['text'] = text
        
            def get_historical_rates(self):