parse payto text
This commit is contained in:
@@ -41,3 +41,7 @@ class AmountEdit(QLineEdit):
|
|||||||
s = s[:p] + '.' + s[p:p+8]
|
s = s[:p] + '.' + s[p:p+8]
|
||||||
self.setText(s)
|
self.setText(s)
|
||||||
self.setCursorPosition(pos)
|
self.setCursorPosition(pos)
|
||||||
|
|
||||||
|
|
||||||
|
def setAmount(self, amount):
|
||||||
|
self.setText(self.format_amount(self.wallet.fee).strip())
|
||||||
|
|||||||
@@ -644,7 +644,8 @@ class ElectrumWindow(QMainWindow):
|
|||||||
grid.setRowStretch(8, 1)
|
grid.setRowStretch(8, 1)
|
||||||
|
|
||||||
from paytoedit import PayToEdit
|
from paytoedit import PayToEdit
|
||||||
self.payto_e = PayToEdit()
|
self.amount_e = AmountEdit(self.base_unit)
|
||||||
|
self.payto_e = PayToEdit(self.amount_e)
|
||||||
self.payto_help = HelpButton(_('Recipient of the funds.') + '\n\n' + _('You may enter a Bitcoin address, a label from your list of contacts (a list of completions will be proposed), or an alias (email-like address that forwards to a Bitcoin address)'))
|
self.payto_help = HelpButton(_('Recipient of the funds.') + '\n\n' + _('You may enter a Bitcoin address, a label from your list of contacts (a list of completions will be proposed), or an alias (email-like address that forwards to a Bitcoin address)'))
|
||||||
grid.addWidget(QLabel(_('Pay to')), 1, 0)
|
grid.addWidget(QLabel(_('Pay to')), 1, 0)
|
||||||
grid.addWidget(self.payto_e, 1, 1, 1, 3)
|
grid.addWidget(self.payto_e, 1, 1, 1, 3)
|
||||||
@@ -672,7 +673,6 @@ class ElectrumWindow(QMainWindow):
|
|||||||
grid.addWidget(self.from_list, 3, 1, 1, 3)
|
grid.addWidget(self.from_list, 3, 1, 1, 3)
|
||||||
self.set_pay_from([])
|
self.set_pay_from([])
|
||||||
|
|
||||||
self.amount_e = AmountEdit(self.base_unit)
|
|
||||||
self.amount_help = HelpButton(_('Amount to be sent.') + '\n\n' \
|
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.') \
|
+ _('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.'))
|
+ '\n\n' + _('Keyboard shortcut: type "!" to send all your coins.'))
|
||||||
|
|||||||
@@ -19,11 +19,18 @@
|
|||||||
from PyQt4.QtCore import *
|
from PyQt4.QtCore import *
|
||||||
from PyQt4.QtGui import *
|
from PyQt4.QtGui import *
|
||||||
|
|
||||||
|
import re
|
||||||
|
from decimal import Decimal
|
||||||
|
from electrum import bitcoin
|
||||||
|
|
||||||
|
RE_ADDRESS = '[1-9A-HJ-NP-Za-km-z]{26,}'
|
||||||
|
RE_ALIAS = '(.*?)\s*\<([1-9A-HJ-NP-Za-km-z]{26,})\>'
|
||||||
|
|
||||||
class PayToEdit(QTextEdit):
|
class PayToEdit(QTextEdit):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, amount_edit):
|
||||||
QTextEdit.__init__(self, *args, **kwargs)
|
QTextEdit.__init__(self)
|
||||||
|
self.amount_edit = amount_edit
|
||||||
self.document().contentsChanged.connect(self.update_size)
|
self.document().contentsChanged.connect(self.update_size)
|
||||||
self.heightMin = 0
|
self.heightMin = 0
|
||||||
self.heightMax = 150
|
self.heightMax = 150
|
||||||
@@ -33,6 +40,57 @@ class PayToEdit(QTextEdit):
|
|||||||
self.c = None
|
self.c = None
|
||||||
|
|
||||||
|
|
||||||
|
def lock_amount(self):
|
||||||
|
e = self.amount_edit
|
||||||
|
e.setReadOnly(True)
|
||||||
|
e.setFrame(False)
|
||||||
|
|
||||||
|
def unlock_amount(self):
|
||||||
|
e = self.amount_edit
|
||||||
|
e.setReadOnly(False)
|
||||||
|
e.setFrame(True)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_line(self, line):
|
||||||
|
recipient, amount = line.split(',')
|
||||||
|
amount = Decimal(amount.strip())
|
||||||
|
recipient = recipient.strip()
|
||||||
|
m = re.match(RE_ALIAS, recipient)
|
||||||
|
to_address = m.group(2) if m else recipient
|
||||||
|
assert bitcoin.is_address(to_address)
|
||||||
|
return to_address, amount
|
||||||
|
|
||||||
|
|
||||||
|
def check_text(self):
|
||||||
|
# filter out empty lines
|
||||||
|
lines = filter( lambda x: x, self.lines())
|
||||||
|
outputs = []
|
||||||
|
total = 0
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
try:
|
||||||
|
to_address, amount = self.parse_line(line)
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
outputs.append((to_address, amount))
|
||||||
|
total += amount
|
||||||
|
|
||||||
|
self.outputs = outputs
|
||||||
|
|
||||||
|
self.amount_edit.setText(str(total) if total else "")
|
||||||
|
if total or len(lines)>1:
|
||||||
|
self.lock_amount()
|
||||||
|
else:
|
||||||
|
self.unlock_amount()
|
||||||
|
|
||||||
|
def lines(self):
|
||||||
|
return str(self.toPlainText()).split('\n')
|
||||||
|
|
||||||
|
|
||||||
|
def is_multiline(self):
|
||||||
|
return len(self.lines()) > 1
|
||||||
|
|
||||||
|
|
||||||
def update_size(self):
|
def update_size(self):
|
||||||
docHeight = self.document().size().height()
|
docHeight = self.document().size().height()
|
||||||
if self.heightMin <= docHeight <= self.heightMax:
|
if self.heightMin <= docHeight <= self.heightMax:
|
||||||
@@ -56,6 +114,7 @@ class PayToEdit(QTextEdit):
|
|||||||
tc.movePosition(QTextCursor.EndOfWord)
|
tc.movePosition(QTextCursor.EndOfWord)
|
||||||
tc.insertText(completion.right(extra))
|
tc.insertText(completion.right(extra))
|
||||||
self.setTextCursor(tc)
|
self.setTextCursor(tc)
|
||||||
|
self.check_text()
|
||||||
|
|
||||||
|
|
||||||
def textUnderCursor(self):
|
def textUnderCursor(self):
|
||||||
@@ -70,10 +129,20 @@ class PayToEdit(QTextEdit):
|
|||||||
e.ignore()
|
e.ignore()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if e.key() in [Qt.Key_Tab]:
|
||||||
|
e.ignore()
|
||||||
|
return
|
||||||
|
|
||||||
|
if e.key() in [Qt.Key_Down, Qt.Key_Up] and not self.is_multiline():
|
||||||
|
e.ignore()
|
||||||
|
return
|
||||||
|
|
||||||
isShortcut = (e.modifiers() and Qt.ControlModifier) and e.key() == Qt.Key_E
|
isShortcut = (e.modifiers() and Qt.ControlModifier) and e.key() == Qt.Key_E
|
||||||
|
|
||||||
if not self.c or not isShortcut:
|
if not self.c or not isShortcut:
|
||||||
QTextEdit.keyPressEvent(self, e)
|
QTextEdit.keyPressEvent(self, e)
|
||||||
|
self.check_text()
|
||||||
|
|
||||||
|
|
||||||
ctrlOrShift = e.modifiers() and (Qt.ControlModifier or Qt.ShiftModifier)
|
ctrlOrShift = e.modifiers() and (Qt.ControlModifier or Qt.ShiftModifier)
|
||||||
if self.c is None or (ctrlOrShift and e.text().isEmpty()):
|
if self.c is None or (ctrlOrShift and e.text().isEmpty()):
|
||||||
|
|||||||
Reference in New Issue
Block a user