commit c59f1b1edb7860c4d45220091a565104e61648a9
parent 6c18fa1844d8e3d285f6b1b324fe9e27b113268d
Author: ThomasV <thomasv@gitorious>
Date: Thu, 5 Jun 2014 12:40:07 +0200
handle units in AmountEdit
Diffstat:
2 files changed, 36 insertions(+), 18 deletions(-)
diff --git a/gui/qt/amountedit.py b/gui/qt/amountedit.py
@@ -3,27 +3,44 @@
from PyQt4.QtCore import *
from PyQt4.QtGui import *
+from decimal import Decimal
class AmountEdit(QLineEdit):
- def __init__(self, text_getter, is_int = False, parent=None):
+ def __init__(self, decimal_point, is_int = False, parent=None):
QLineEdit.__init__(self, parent)
- self.text_getter = text_getter
+ self.decimal_point = decimal_point
self.textChanged.connect(self.numbify)
self.is_int = is_int
self.is_shortcut = False
+ def base_unit(self):
+ p = self.decimal_point()
+ assert p in [5,8]
+ return "BTC" if p == 8 else "mBTC"
+
+ def get_amount(self):
+ x = unicode( self.text() )
+ if x in['.', '']:
+ return None
+ p = pow(10, self.decimal_point())
+ return int( p * Decimal(x) )
+
+ def setAmount(self, amount):
+ p = pow(10, self.decimal_point())
+ x = amount / Decimal(p)
+ self.setText(str(x))
def paintEvent(self, event):
QLineEdit.paintEvent(self, event)
- if self.text_getter:
+ if self.decimal_point:
panel = QStyleOptionFrameV2()
self.initStyleOption(panel)
textRect = self.style().subElementRect(QStyle.SE_LineEditContents, panel, self)
textRect.adjust(2, 0, -10, 0)
painter = QPainter(self)
painter.setPen(self.palette().brush(QPalette.Disabled, QPalette.Text).color())
- painter.drawText(textRect, Qt.AlignRight | Qt.AlignVCenter, self.text_getter())
+ painter.drawText(textRect, Qt.AlignRight | Qt.AlignVCenter, self.base_unit())
def numbify(self):
@@ -41,3 +58,5 @@ class AmountEdit(QLineEdit):
s = s[:p] + '.' + s[p:p+8]
self.setText(s)
self.setCursorPosition(pos)
+
+
diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py
@@ -417,10 +417,10 @@ class ElectrumWindow(QMainWindow):
def format_amount(self, x, is_diff=False, whitespaces=False):
return format_satoshis(x, is_diff, self.num_zeros, self.decimal_point, whitespaces)
- def read_amount(self, x):
- if x in['.', '']: return None
- p = pow(10, self.decimal_point)
- return int( p * Decimal(x) )
+
+ def get_decimal_point(self):
+ return self.decimal_point
+
def base_unit(self):
assert self.decimal_point in [5,8]
@@ -671,7 +671,7 @@ class ElectrumWindow(QMainWindow):
grid.addWidget(self.from_list, 3, 1, 1, 3)
self.set_pay_from([])
- self.amount_e = AmountEdit(self.base_unit)
+ self.amount_e = AmountEdit(self.get_decimal_point)
self.amount_help = HelpButton(_('Amount to be sent.') + '\n\n' \
+ _('The amount will be displayed in red if you do not have enough funds in your wallet. Note that if you have frozen some of your addresses, the available funds will be lower than your total balance.') \
+ '\n\n' + _('Keyboard shortcut: type "!" to send all your coins.'))
@@ -679,7 +679,7 @@ class ElectrumWindow(QMainWindow):
grid.addWidget(self.amount_e, 4, 1, 1, 2)
grid.addWidget(self.amount_help, 4, 3)
- self.fee_e = AmountEdit(self.base_unit)
+ self.fee_e = AmountEdit(self.get_decimal_point)
grid.addWidget(QLabel(_('Fee')), 5, 0)
grid.addWidget(self.fee_e, 5, 1, 1, 2)
grid.addWidget(HelpButton(
@@ -716,8 +716,8 @@ class ElectrumWindow(QMainWindow):
self.fee_e.setText( self.format_amount( fee ) )
return
- amount = self.read_amount(str(self.amount_e.text()))
- fee = self.read_amount(str(self.fee_e.text()))
+ amount = self.amount_e.get_amount()
+ fee = self.fee_e.get_amount()
if not is_fee: fee = None
if amount is None:
@@ -791,9 +791,9 @@ class ElectrumWindow(QMainWindow):
if not is_valid(to_address):
QMessageBox.warning(self, _('Error'), _('Invalid Bitcoin Address') + ':\n' + to_address, _('OK'))
return
-
+
try:
- amount = self.read_amount(unicode( self.amount_e.text()))
+ amount = self.amount_e.get_amount()
except Exception:
QMessageBox.warning(self, _('Error'), _('Invalid Amount'), _('OK'))
return
@@ -801,7 +801,7 @@ class ElectrumWindow(QMainWindow):
outputs = [(to_address, amount)]
try:
- fee = self.read_amount(unicode( self.fee_e.text()))
+ fee = self.fee_e.get_amount()
except Exception:
QMessageBox.warning(self, _('Error'), _('Invalid Fee'), _('OK'))
return
@@ -2191,7 +2191,7 @@ class ElectrumWindow(QMainWindow):
fee_label = QLabel(_('Transaction fee') + ':')
grid.addWidget(fee_label, 2, 0)
- fee_e = AmountEdit(self.base_unit)
+ fee_e = AmountEdit(self.get_decimal_point)
fee_e.setText(self.format_amount(self.wallet.fee).strip())
grid.addWidget(fee_e, 2, 1)
msg = _('Fee per kilobyte of transaction.') + ' ' \
@@ -2240,9 +2240,8 @@ class ElectrumWindow(QMainWindow):
# run the dialog
if not d.exec_(): return
- fee = unicode(fee_e.text())
try:
- fee = self.read_amount(fee)
+ fee = self.fee_e.get_amount()
except Exception:
QMessageBox.warning(self, _('Error'), _('Invalid value') +': %s'%fee, _('OK'))
return