3 from decimal import Decimal
4 from urllib import quote
6 from PyQt4.QtGui import *
7 from PyQt4.QtCore import *
8 import PyQt4.QtCore as QtCore
9 import PyQt4.QtGui as QtGui
11 from electrum_gui.qt.qrcodewidget import QRCodeWidget
13 from electrum import bmp, pyqrnative, BasePlugin
14 from electrum.i18n import _
17 if platform.system() == 'Windows':
18 MONOSPACE_FONT = 'Lucida Console'
19 elif platform.system() == 'Darwin':
20 MONOSPACE_FONT = 'Monaco'
22 MONOSPACE_FONT = 'monospace'
26 class QR_Window(QWidget):
28 def __init__(self, exchanger):
29 QWidget.__init__(self)
30 self.exchanger = exchanger
31 self.setWindowTitle('Electrum - '+_('Invoice'))
32 self.setMinimumSize(800, 250)
36 self.setFocusPolicy(QtCore.Qt.NoFocus)
38 main_box = QHBoxLayout()
40 self.qrw = QRCodeWidget()
41 main_box.addWidget(self.qrw, 1)
44 main_box.addLayout(vbox)
46 self.address_label = QLabel("")
47 #self.address_label.setFont(QFont(MONOSPACE_FONT))
48 vbox.addWidget(self.address_label)
50 self.label_label = QLabel("")
51 vbox.addWidget(self.label_label)
53 self.amount_label = QLabel("")
54 vbox.addWidget(self.amount_label)
57 self.setLayout(main_box)
60 def set_content(self, addr, label, amount, currency):
62 address_text = "<span style='font-size: 18pt'>%s</span>" % addr if addr else ""
63 self.address_label.setText(address_text)
65 if currency == 'BTC': currency = None
70 self.amount = Decimal(amount) / self.exchanger.exchange(1, currency) if currency else amount
74 self.amount = Decimal(amount)
75 self.amount = self.amount.quantize(Decimal('1.0000'))
78 amount_text += "<span style='font-size: 18pt'>%s %s</span><br/>" % (amount, currency)
79 amount_text += "<span style='font-size: 21pt'>%s</span> <span style='font-size: 16pt'>BTC</span> " % str(self.amount)
83 self.amount_label.setText(amount_text)
86 label_text = "<span style='font-size: 21pt'>%s</span>" % label if label else ""
87 self.label_label.setText(label_text)
89 msg = 'bitcoin:'+self.address
90 if self.amount is not None:
91 msg += '?amount=%s'%(str( self.amount))
92 if self.label is not None:
93 encoded_label = quote(self.label)
94 msg += '&label=%s'%(encoded_label)
95 elif self.label is not None:
96 encoded_label = quote(self.label)
97 msg += '?label=%s'%(encoded_label)
99 self.qrw.set_addr( msg )
104 class Plugin(BasePlugin):
107 return 'Point of Sale'
109 def description(self):
110 return _('Show QR code window and amounts requested for each address. Add menu item to request amount.')+_(' Note: This requires the exchange rate plugin to be installed.')
113 self.window = self.gui.main_window
114 self.wallet = self.window.wallet
116 self.qr_window = None
117 self.merchant_name = self.config.get('merchant_name', 'Invoice')
119 self.window.expert_mode = True
120 self.window.receive_list.setColumnCount(5)
121 self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx'), _('Request')])
122 self.requested_amounts = {}
123 self.toggle_QR_window(True)
126 if not self.config.get('use_exchange_rate'):
127 self.gui.main_window.show_message("Please enable exchange rates first!")
130 return BasePlugin.enable(self)
133 def load_wallet(self, wallet):
135 self.requested_amounts = self.wallet.storage.get('requested_amounts',{})
138 self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx')])
139 self.window.receive_list.setColumnCount(4)
140 for i,width in enumerate(self.window.column_widths['receive']):
141 self.window.receive_list.setColumnWidth(i, width)
142 self.toggle_QR_window(False)
145 def close_main_window(self):
147 self.qr_window.close()
148 self.qr_window = None
151 def timer_actions(self):
153 self.qr_window.qrw.update_qr()
156 def toggle_QR_window(self, show):
157 if show and not self.qr_window:
158 self.qr_window = QR_Window(self.gui.exchanger)
159 self.qr_window.setVisible(True)
160 self.qr_window_geometry = self.qr_window.geometry()
161 item = self.window.receive_list.currentItem()
163 address = str(item.text(1))
164 label = self.wallet.labels.get(address)
165 amount, currency = self.requested_amounts.get(address, (None, None))
166 self.qr_window.set_content( address, label, amount, currency )
168 elif show and self.qr_window and not self.qr_window.isVisible():
169 self.qr_window.setVisible(True)
170 self.qr_window.setGeometry(self.qr_window_geometry)
172 elif not show and self.qr_window and self.qr_window.isVisible():
173 self.qr_window_geometry = self.qr_window.geometry()
174 self.qr_window.setVisible(False)
178 def update_receive_item(self, address, item):
180 amount, currency = self.requested_amounts.get(address, (None, None))
182 print "cannot get requested amount", address, self.requested_amounts.get(address)
183 amount, currency = None, None
184 self.requested_amounts.pop(address)
186 amount_str = amount + (' ' + currency if currency else '') if amount is not None else ''
187 item.setData(column_index,0,amount_str)
191 def current_item_changed(self, a):
194 if a is not None and self.qr_window and self.qr_window.isVisible():
195 address = str(a.text(0))
196 label = self.wallet.labels.get(address)
198 amount, currency = self.requested_amounts.get(address, (None, None))
200 amount, currency = None, None
201 self.qr_window.set_content( address, label, amount, currency )
205 def item_changed(self, item, column):
206 if column != column_index:
208 address = str( item.text(0) )
209 text = str( item.text(column) )
211 seq = self.wallet.get_address_index(address)
214 print "cannot get index"
217 text = text.strip().upper()
219 m = re.match('^(\d*(|\.\d*))\s*(|BTC|EUR|USD|GBP|CNY|JPY|RUB|BRL)$', text)
220 if m and m.group(1) and m.group(1)!='.':
222 currency = m.group(3)
226 currency = currency.upper()
228 self.requested_amounts[address] = (amount, currency)
229 self.wallet.storage.put('requested_amounts', self.requested_amounts, True)
231 label = self.wallet.labels.get(address)
233 label = self.merchant_name + ' - %04d'%(index+1)
234 self.wallet.labels[address] = label
237 self.qr_window.set_content( address, label, amount, currency )
240 item.setText(column,'')
241 if address in self.requested_amounts:
242 self.requested_amounts.pop(address)
244 self.window.update_receive_item(self.window.receive_list.currentItem())
249 def edit_amount(self):
250 l = self.window.receive_list
251 item = l.currentItem()
252 item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
253 l.editItem( item, column_index )
254 item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
257 def receive_menu(self, menu, addr):
258 menu.addAction(_("Request amount"), self.edit_amount)
259 menu.addAction(_("Show Invoice"), lambda: self.toggle_QR_window(True))