3 from decimal import Decimal
5 from PyQt4.QtGui import *
6 from PyQt4.QtCore import *
7 import PyQt4.QtCore as QtCore
8 import PyQt4.QtGui as QtGui
10 from electrum_gui.qrcodewidget import QRCodeWidget
11 from electrum_gui import bmp, pyqrnative
13 from electrum_gui.i18n import _
16 if platform.system() == 'Windows':
17 MONOSPACE_FONT = 'Lucida Console'
18 elif platform.system() == 'Darwin':
19 MONOSPACE_FONT = 'Monaco'
21 MONOSPACE_FONT = 'monospace'
25 class QR_Window(QWidget):
27 def __init__(self, exchanger):
28 QWidget.__init__(self)
29 self.exchanger = exchanger
30 self.setWindowTitle('Electrum - '+_('Invoice'))
31 self.setMinimumSize(800, 250)
35 self.setFocusPolicy(QtCore.Qt.NoFocus)
37 main_box = QHBoxLayout()
39 self.qrw = QRCodeWidget()
40 main_box.addWidget(self.qrw, 1)
43 main_box.addLayout(vbox)
45 self.address_label = QLabel("")
46 #self.address_label.setFont(QFont(MONOSPACE_FONT))
47 vbox.addWidget(self.address_label)
49 self.label_label = QLabel("")
50 vbox.addWidget(self.label_label)
52 self.amount_label = QLabel("")
53 vbox.addWidget(self.amount_label)
56 self.setLayout(main_box)
59 def set_content(self, addr, label, amount, currency):
61 address_text = "<span style='font-size: 18pt'>%s</span>" % addr if addr else ""
62 self.address_label.setText(address_text)
64 if currency == 'BTC': currency = None
68 self.amount = Decimal(amount) / self.exchanger.exchange(1, currency) if currency else amount
70 self.amount = Decimal(amount)
71 self.amount = self.amount.quantize(Decimal('1.0000'))
74 amount_text += "<span style='font-size: 18pt'>%s %s</span><br/>" % (amount, currency)
75 amount_text += "<span style='font-size: 21pt'>%s</span> <span style='font-size: 16pt'>BTC</span> " % str(self.amount)
76 self.amount_label.setText(amount_text)
79 label_text = "<span style='font-size: 21pt'>%s</span>" % label if label else ""
80 self.label_label.setText(label_text)
82 msg = 'bitcoin:'+self.address
83 if self.amount is not None:
84 msg += '?amount=%s'%(str( self.amount))
85 if self.label is not None:
86 msg += '&label=%s'%(self.label)
87 elif self.label is not None:
88 msg += '?label=%s'%(self.label)
90 self.qrw.set_addr( msg )
98 return 'Point of Sale', _('Show QR code window and amounts requested for each address. Add menu item to request amount.')
101 gui.requested_amounts = gui.config.get('requested_amounts',{})
113 enabled = not enabled
114 toggle_QR_window(gui, enabled)
117 gui.expert_mode = True
118 gui.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Request')])
120 gui.set_hook('item_changed', item_changed)
121 gui.set_hook('current_item_changed', recv_changed)
122 gui.set_hook('receive_menu', receive_menu)
123 gui.set_hook('update_receive_item', update_receive_item)
124 gui.set_hook('timer_actions', timer_actions)
125 gui.set_hook('close_main_window', close_main_window)
127 gui.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx')])
128 gui.unset_hook('item_changed', item_changed)
129 gui.unset_hook('current_item_changed', recv_changed)
130 gui.unset_hook('receive_menu', receive_menu)
131 gui.unset_hook('update_receive_item', update_receive_item)
132 gui.unset_hook('timer_actions', timer_actions)
133 gui.unset_hook('close_main_window', close_main_window)
139 def toggle_QR_window(self, show):
140 if show and not self.qr_window:
141 self.qr_window = QR_Window(self.exchanger)
142 self.qr_window.setVisible(True)
143 self.qr_window_geometry = self.qr_window.geometry()
144 item = self.receive_list.currentItem()
146 address = str(item.text(1))
147 label = self.wallet.labels.get(address)
148 amount, currency = self.requested_amounts.get(address, (None, None))
149 self.qr_window.set_content( address, label, amount, currency )
151 elif show and self.qr_window and not self.qr_window.isVisible():
152 self.qr_window.setVisible(True)
153 self.qr_window.setGeometry(self.qr_window_geometry)
155 elif not show and self.qr_window and self.qr_window.isVisible():
156 self.qr_window_geometry = self.qr_window.geometry()
157 self.qr_window.setVisible(False)
159 #self.print_button.setHidden(self.qr_window is None or not self.qr_window.isVisible())
160 self.receive_list.setColumnHidden(2, self.qr_window is None or not self.qr_window.isVisible())
161 #self.receive_list.setColumnWidth(1, 200)
165 def item_changed(self, item, column):
166 if column == column_index:
167 address = str( item.text(0) )
168 text = str( item.text(column) )
170 seq = self.wallet.get_address_index(address)
173 print "cannot get index"
176 text = text.strip().upper()
178 m = re.match('^(\d+(|\.\d*))\s*(|BTC|EUR|USD|GBP|CNY|JPY|RUB|BRL)$', text)
181 currency = m.group(3)
185 currency = currency.upper()
187 self.requested_amounts[address] = (amount, currency)
188 self.wallet.config.set_key('requested_amounts', self.requested_amounts, True)
190 label = self.wallet.labels.get(address)
192 label = self.merchant_name + ' - %04d'%(index+1)
193 self.wallet.labels[address] = label
196 self.qr_window.set_content( address, label, amount, currency )
199 item.setText(column,'')
200 if address in self.requested_amounts:
201 self.requested_amounts.pop(address)
203 self.update_receive_item(self.receive_list.currentItem())
206 def recv_changed(self, a):
207 if a is not None and self.qr_window and self.qr_window.isVisible():
208 address = str(a.text(0))
209 label = self.wallet.labels.get(address)
211 amount, currency = self.requested_amounts.get(address, (None, None))
213 amount, currency = None, None
214 self.qr_window.set_content( address, label, amount, currency )
218 def edit_amount(self):
219 l = self.receive_list
220 item = l.currentItem()
221 item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
222 l.editItem( item, column_index )
223 item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
225 def receive_menu(self, menu):
226 menu.addAction(_("Request amount"), lambda: edit_amount(self))
229 def update_receive_item(self, address, item):
231 amount, currency = self.requested_amounts.get(address, (None, None))
233 print "cannot get requested amount", address, self.requested_amounts.get(address)
234 amount, currency = None, None
235 self.requested_amounts.pop(address)
237 amount_str = amount + (' ' + currency if currency else '') if amount is not None else ''
238 item.setData(column_index,0,amount_str)
241 def close_main_window(self):
243 self.qr_window.close()
244 self.qr_window = None
247 def timer_actions(self):
249 self.qr_window.qrw.update_qr()