|
|
@ -1,11 +1,12 @@ |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
from decimal import Decimal |
|
|
|
|
|
|
|
from PyQt5.QtCore import * |
|
|
|
from PyQt5.QtGui import * |
|
|
|
from PyQt5.QtWidgets import (QLineEdit, QStyle, QStyleOptionFrame) |
|
|
|
|
|
|
|
from decimal import Decimal |
|
|
|
from electrum.util import format_satoshis_plain, decimal_point_to_base_unit_name |
|
|
|
from electrum.util import format_satoshis_plain, decimal_point_to_base_unit_name, FEERATE_PRECISION |
|
|
|
|
|
|
|
|
|
|
|
class MyLineEdit(QLineEdit): |
|
|
@ -19,7 +20,7 @@ class MyLineEdit(QLineEdit): |
|
|
|
class AmountEdit(MyLineEdit): |
|
|
|
shortcut = pyqtSignal() |
|
|
|
|
|
|
|
def __init__(self, base_unit, is_int = False, parent=None): |
|
|
|
def __init__(self, base_unit, is_int=False, parent=None): |
|
|
|
QLineEdit.__init__(self, parent) |
|
|
|
# This seems sufficient for hundred-BTC amounts with 8 decimals |
|
|
|
self.setFixedWidth(140) |
|
|
@ -28,10 +29,14 @@ class AmountEdit(MyLineEdit): |
|
|
|
self.is_int = is_int |
|
|
|
self.is_shortcut = False |
|
|
|
self.help_palette = QPalette() |
|
|
|
self.extra_precision = 0 |
|
|
|
|
|
|
|
def decimal_point(self): |
|
|
|
return 8 |
|
|
|
|
|
|
|
def max_precision(self): |
|
|
|
return self.decimal_point() + self.extra_precision |
|
|
|
|
|
|
|
def numbify(self): |
|
|
|
text = self.text().strip() |
|
|
|
if text == '!': |
|
|
@ -45,7 +50,7 @@ class AmountEdit(MyLineEdit): |
|
|
|
if '.' in s: |
|
|
|
p = s.find('.') |
|
|
|
s = s.replace('.','') |
|
|
|
s = s[:p] + '.' + s[p:p+self.decimal_point()] |
|
|
|
s = s[:p] + '.' + s[p:p+self.max_precision()] |
|
|
|
self.setText(s) |
|
|
|
# setText sets Modified to False. Instead we want to remember |
|
|
|
# if updates were because of user modification. |
|
|
@ -75,7 +80,7 @@ class AmountEdit(MyLineEdit): |
|
|
|
|
|
|
|
class BTCAmountEdit(AmountEdit): |
|
|
|
|
|
|
|
def __init__(self, decimal_point, is_int = False, parent=None): |
|
|
|
def __init__(self, decimal_point, is_int=False, parent=None): |
|
|
|
AmountEdit.__init__(self, self._base_unit, is_int, parent) |
|
|
|
self.decimal_point = decimal_point |
|
|
|
|
|
|
@ -87,8 +92,15 @@ class BTCAmountEdit(AmountEdit): |
|
|
|
x = Decimal(str(self.text())) |
|
|
|
except: |
|
|
|
return None |
|
|
|
p = pow(10, self.decimal_point()) |
|
|
|
return int( p * x ) |
|
|
|
# scale it to max allowed precision, make it an int |
|
|
|
power = pow(10, self.max_precision()) |
|
|
|
max_prec_amount = int(power * x) |
|
|
|
# if the max precision is simply what unit conversion allows, just return |
|
|
|
if self.max_precision() == self.decimal_point(): |
|
|
|
return max_prec_amount |
|
|
|
# otherwise, scale it back to the expected unit |
|
|
|
amount = Decimal(max_prec_amount) / pow(10, self.max_precision()-self.decimal_point()) |
|
|
|
return Decimal(amount) if not self.is_int else int(amount) |
|
|
|
|
|
|
|
def setAmount(self, amount): |
|
|
|
if amount is None: |
|
|
@ -98,6 +110,11 @@ class BTCAmountEdit(AmountEdit): |
|
|
|
|
|
|
|
|
|
|
|
class FeerateEdit(BTCAmountEdit): |
|
|
|
|
|
|
|
def __init__(self, decimal_point, is_int=False, parent=None): |
|
|
|
super().__init__(decimal_point, is_int, parent) |
|
|
|
self.extra_precision = FEERATE_PRECISION |
|
|
|
|
|
|
|
def _base_unit(self): |
|
|
|
return 'sat/byte' |
|
|
|
|
|
|
|