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'
24 class QR_Window(QWidget):
26 def __init__(self, exchanger):
27 QWidget.__init__(self)
28 self.exchanger = exchanger
29 self.setWindowTitle('Electrum - '+_('Invoice'))
30 self.setMinimumSize(800, 250)
34 self.setFocusPolicy(QtCore.Qt.NoFocus)
36 main_box = QHBoxLayout()
38 self.qrw = QRCodeWidget()
39 main_box.addWidget(self.qrw, 1)
42 main_box.addLayout(vbox)
44 self.address_label = QLabel("")
45 #self.address_label.setFont(QFont(MONOSPACE_FONT))
46 vbox.addWidget(self.address_label)
48 self.label_label = QLabel("")
49 vbox.addWidget(self.label_label)
51 self.amount_label = QLabel("")
52 vbox.addWidget(self.amount_label)
55 self.setLayout(main_box)
58 def set_content(self, addr, label, amount, currency):
60 address_text = "<span style='font-size: 18pt'>%s</span>" % addr if addr else ""
61 self.address_label.setText(address_text)
63 if currency == 'BTC': currency = None
67 self.amount = Decimal(amount) / self.exchanger.exchange(1, currency) if currency else amount
69 self.amount = Decimal(amount)
70 self.amount = self.amount.quantize(Decimal('1.0000'))
73 amount_text += "<span style='font-size: 18pt'>%s %s</span><br/>" % (amount, currency)
74 amount_text += "<span style='font-size: 21pt'>%s</span> <span style='font-size: 16pt'>BTC</span> " % str(self.amount)
75 self.amount_label.setText(amount_text)
78 label_text = "<span style='font-size: 21pt'>%s</span>" % label if label else ""
79 self.label_label.setText(label_text)
81 msg = 'bitcoin:'+self.address
82 if self.amount is not None:
83 msg += '?amount=%s'%(str( self.amount))
84 if self.label is not None:
85 msg += '&label=%s'%(self.label)
86 elif self.label is not None:
87 msg += '?label=%s'%(self.label)
89 self.qrw.set_addr( msg )
97 return 'Point of Sale', _('Show QR code window and amounts requested for each address. Add menu item to request amount.')
100 gui.requested_amounts = gui.config.get('requested_amounts',{})
112 enabled = not enabled
113 toggle_QR_window(gui, enabled)
116 gui.set_hook('item_changed', item_changed)
117 gui.set_hook('current_item_changed', recv_changed)
118 gui.set_hook('receive_menu', receive_menu)
119 gui.set_hook('update_receive_item', update_receive_item)
120 gui.set_hook('timer_actions', timer_actions)
121 gui.set_hook('close_main_window', close_main_window)
123 gui.unset_hook('item_changed', item_changed)
124 gui.unset_hook('current_item_changed', recv_changed)
125 gui.unset_hook('receive_menu', receive_menu)
126 gui.unset_hook('update_receive_item', update_receive_item)
127 gui.unset_hook('timer_actions', timer_actions)
128 gui.unset_hook('close_main_window', close_main_window)
134 def toggle_QR_window(self, show):
135 if show and not self.qr_window:
136 self.qr_window = QR_Window(self.exchanger)
137 self.qr_window.setVisible(True)
138 self.qr_window_geometry = self.qr_window.geometry()
139 item = self.receive_list.currentItem()
141 address = str(item.text(1))
142 label = self.wallet.labels.get(address)
143 amount, currency = self.requested_amounts.get(address, (None, None))
144 self.qr_window.set_content( address, label, amount, currency )
146 elif show and self.qr_window and not self.qr_window.isVisible():
147 self.qr_window.setVisible(True)
148 self.qr_window.setGeometry(self.qr_window_geometry)
150 elif not show and self.qr_window and self.qr_window.isVisible():
151 self.qr_window_geometry = self.qr_window.geometry()
152 self.qr_window.setVisible(False)
154 #self.print_button.setHidden(self.qr_window is None or not self.qr_window.isVisible())
155 self.receive_list.setColumnHidden(2, self.qr_window is None or not self.qr_window.isVisible())
156 #self.receive_list.setColumnWidth(1, 200)
160 def item_changed(self, item, column):
162 address = str( item.text(0) )
163 text = str( item.text(column) )
165 seq = self.wallet.get_address_index(address)
168 print "cannot get index"
171 text = text.strip().upper()
173 m = re.match('^(\d+(|\.\d*))\s*(|BTC|EUR|USD|GBP|CNY|JPY|RUB|BRL)$', text)
176 currency = m.group(3)
180 currency = currency.upper()
182 self.requested_amounts[address] = (amount, currency)
183 self.wallet.config.set_key('requested_amounts', self.requested_amounts, True)
185 label = self.wallet.labels.get(address)
187 label = self.merchant_name + ' - %04d'%(index+1)
188 self.wallet.labels[address] = label
191 self.qr_window.set_content( address, label, amount, currency )
194 item.setText(column,'')
195 if address in self.requested_amounts:
196 self.requested_amounts.pop(address)
198 self.update_receive_item(self.receive_list.currentItem())
201 def recv_changed(self, a):
202 if a is not None and self.qr_window and self.qr_window.isVisible():
203 address = str(a.text(0))
204 label = self.wallet.labels.get(address)
206 amount, currency = self.requested_amounts.get(address, (None, None))
208 amount, currency = None, None
209 self.qr_window.set_content( address, label, amount, currency )
213 def edit_amount(self):
214 l = self.receive_list
215 item = l.currentItem()
216 item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
217 l.editItem( item, 4 )
218 item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
220 def receive_menu(self, menu):
221 menu.addAction(_("Request amount"), lambda: edit_amount(self))
224 def update_receive_item(self, address, item):
226 amount, currency = self.requested_amounts.get(address, (None, None))
228 print "cannot get requested amount", address, self.requested_amounts.get(address)
229 amount, currency = None, None
231 amount_str = amount + (' ' + currency if currency else '') if amount is not None else ''
232 item.setData(4,0,amount_str)
235 def close_main_window(self):
237 self.qr_window.close()
238 self.qr_window = None
241 def timer_actions(self):
243 self.qr_window.qrw.update_qr()