POS: request amount in other currencies and convert to BTC

This commit is contained in:
thomasv 2013-01-08 11:04:04 +01:00
parent 6498ff2bca
commit d2a342c22b
2 changed files with 54 additions and 36 deletions

View File

@ -14,10 +14,6 @@ code improvements:
- qrcode and bmp patches are on github (they are incompatible with android) - qrcode and bmp patches are on github (they are incompatible with android)
classic gui :
- in POS mode, request amount in USD, convert to BTC
android: android:
- kivy-based gui - kivy-based gui

View File

@ -202,8 +202,9 @@ class QRCodeWidget(QWidget):
class QR_Window(QWidget): class QR_Window(QWidget):
def __init__(self): def __init__(self, exchanger):
QWidget.__init__(self) QWidget.__init__(self)
self.exchanger = exchanger
self.setWindowTitle('Electrum - Invoice') self.setWindowTitle('Electrum - Invoice')
self.setMinimumSize(800, 250) self.setMinimumSize(800, 250)
self.address = '' self.address = ''
@ -233,13 +234,23 @@ class QR_Window(QWidget):
self.setLayout(main_box) self.setLayout(main_box)
def set_content(self, addr, label, amount): def set_content(self, addr, label, amount, currency):
self.address = addr self.address = addr
address_text = "<span style='font-size: 18pt'>%s</span>" % addr if addr else "" address_text = "<span style='font-size: 18pt'>%s</span>" % addr if addr else ""
self.address_label.setText(address_text) self.address_label.setText(address_text)
self.amount = amount if currency == 'BTC': currency = None
amount_text = "<span style='font-size: 21pt'>%s</span> <span style='font-size: 16pt'>BTC</span> " % format_satoshis(amount) if amount else "" amount_text = ''
if amount:
if currency:
self.amount = Decimal(amount) / self.exchanger.exchange(1, currency) if currency else amount
else:
self.amount = Decimal(amount)
self.amount = self.amount.quantize(Decimal('1.0000'))
if currency:
amount_text += "<span style='font-size: 18pt'>%s %s</span><br/>" % (amount, currency)
amount_text += "<span style='font-size: 21pt'>%s</span> <span style='font-size: 16pt'>BTC</span> " % str(self.amount)
self.amount_label.setText(amount_text) self.amount_label.setText(amount_text)
self.label = label self.label = label
@ -248,7 +259,7 @@ class QR_Window(QWidget):
msg = 'bitcoin:'+self.address msg = 'bitcoin:'+self.address
if self.amount is not None: if self.amount is not None:
msg += '?amount=%s'%(str( Decimal(self.amount) /100000000)) msg += '?amount=%s'%(str( self.amount))
if self.label is not None: if self.label is not None:
msg += '&label=%s'%(self.label) msg += '&label=%s'%(self.label)
elif self.label is not None: elif self.label is not None:
@ -321,7 +332,6 @@ class ElectrumWindow(QMainWindow):
tabs.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) tabs.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.setCentralWidget(tabs) self.setCentralWidget(tabs)
self.create_status_bar() self.create_status_bar()
self.toggle_QR_window(self.receive_tab_mode == 2)
g = self.config.get("winpos-qt",[100, 100, 840, 400]) g = self.config.get("winpos-qt",[100, 100, 840, 400])
self.setGeometry(g[0], g[1], g[2], g[3]) self.setGeometry(g[0], g[1], g[2], g[3])
@ -339,6 +349,7 @@ class ElectrumWindow(QMainWindow):
self.history_list.setFocus(True) self.history_list.setFocus(True)
self.exchanger = exchange_rate.Exchanger(self) self.exchanger = exchange_rate.Exchanger(self)
self.toggle_QR_window(self.receive_tab_mode == 2)
self.connect(self, SIGNAL("refresh_balance()"), self.update_wallet) self.connect(self, SIGNAL("refresh_balance()"), self.update_wallet)
# dark magic fix by flatfly; https://bitcointalk.org/index.php?topic=73651.msg959913#msg959913 # dark magic fix by flatfly; https://bitcointalk.org/index.php?topic=73651.msg959913#msg959913
@ -536,34 +547,38 @@ class ElectrumWindow(QMainWindow):
self.recv_changed(item) self.recv_changed(item)
if column == 3: if column == 3:
address = unicode( item.text(column_addr) ) address = str( item.text(column_addr) )
text = unicode( item.text(3) ) text = str( item.text(3) )
try: try:
index = self.wallet.addresses.index(address) index = self.wallet.addresses.index(address)
except: except:
return return
try: text = text.strip().upper()
amount = int( Decimal(text) * 100000000 ) m = re.match('^(\d+(|\.\d*))\s*(|BTC|EUR|USD|GBP|CNY|JPY|RUB|BRL)$', text)
item.setText(3,format_satoshis(amount,False, self.wallet.num_zeros)) if m:
except: amount = m.group(1)
amount = self.wallet.requested_amounts.get(address) currency = m.group(3)
if amount: if not currency:
item.setText(3,format_satoshis(amount,False, self.wallet.num_zeros)) currency = 'BTC'
else: else:
item.setText(3,"") currency = currency.upper()
return self.wallet.requested_amounts[address] = (amount, currency)
self.wallet.requested_amounts[address] = amount label = self.wallet.labels.get(address)
if label is None:
label = self.merchant_name + ' - %04d'%(index+1)
self.wallet.labels[address] = label
label = self.wallet.labels.get(address) if self.qr_window:
if label is None: self.qr_window.set_content( address, label, amount, currency )
label = self.merchant_name + ' - %04d'%(index+1)
self.wallet.labels[address] = label
else:
item.setText(3,'')
if address in self.wallet.requested_amounts:
self.wallet.requested_amounts.pop(address)
self.update_receive_item(self.receive_list.currentItem()) self.update_receive_item(self.receive_list.currentItem())
if self.qr_window:
self.qr_window.set_content( address, label, amount )
def recv_changed(self, a): def recv_changed(self, a):
@ -571,8 +586,11 @@ class ElectrumWindow(QMainWindow):
if a is not None and self.qr_window and self.qr_window.isVisible(): if a is not None and self.qr_window and self.qr_window.isVisible():
address = str(a.text(1)) address = str(a.text(1))
label = self.wallet.labels.get(address) label = self.wallet.labels.get(address)
amount = self.wallet.requested_amounts.get(address) try:
self.qr_window.set_content( address, label, amount ) amount, currency = self.wallet.requested_amounts.get(address, (None, None))
except:
amount, currency = None, None
self.qr_window.set_content( address, label, amount, currency )
def update_history_tab(self): def update_history_tab(self):
@ -1017,10 +1035,14 @@ class ElectrumWindow(QMainWindow):
label = self.wallet.labels.get(address,'') label = self.wallet.labels.get(address,'')
item.setData(2,0,label) item.setData(2,0,label)
amount = self.wallet.requested_amounts.get(address,None) try:
amount_str = format_satoshis( amount, False, self.wallet.num_zeros ) if amount is not None else "" amount, currency = self.wallet.requested_amounts.get(address, (None, None))
except:
amount, currency = None, None
amount_str = amount + (' ' + currency if currency else '') if amount is not None else ''
item.setData(3,0,amount_str) item.setData(3,0,amount_str)
c, u = self.wallet.get_addr_balance(address) c, u = self.wallet.get_addr_balance(address)
balance = format_satoshis( c + u, False, self.wallet.num_zeros ) balance = format_satoshis( c + u, False, self.wallet.num_zeros )
item.setData(4,0,balance) item.setData(4,0,balance)
@ -1362,15 +1384,15 @@ class ElectrumWindow(QMainWindow):
def toggle_QR_window(self, show): def toggle_QR_window(self, show):
if show and not self.qr_window: if show and not self.qr_window:
self.qr_window = QR_Window() self.qr_window = QR_Window(self.exchanger)
self.qr_window.setVisible(True) self.qr_window.setVisible(True)
self.qr_window_geometry = self.qr_window.geometry() self.qr_window_geometry = self.qr_window.geometry()
item = self.receive_list.currentItem() item = self.receive_list.currentItem()
if item: if item:
address = str(item.text(1)) address = str(item.text(1))
label = self.wallet.labels.get(address) label = self.wallet.labels.get(address)
amount = self.wallet.requested_amounts.get(address) amount, currency = self.wallet.requested_amounts.get(address, (None, None))
self.qr_window.set_content( address, label, amount ) self.qr_window.set_content( address, label, amount, currency )
elif show and self.qr_window and not self.qr_window.isVisible(): elif show and self.qr_window and not self.qr_window.isVisible():
self.qr_window.setVisible(True) self.qr_window.setVisible(True)