fix for issue #417, adds new column for requested amount
[electrum-nvc.git] / plugins / pointofsale.py
1 import re
2 import platform
3 from decimal import Decimal
4
5 from PyQt4.QtGui import *
6 from PyQt4.QtCore import *
7 import PyQt4.QtCore as QtCore
8 import PyQt4.QtGui as QtGui
9
10 from electrum_gui.qt.qrcodewidget import QRCodeWidget
11
12 from electrum import bmp, pyqrnative, BasePlugin
13 from electrum.i18n import _
14
15
16 if platform.system() == 'Windows':
17     MONOSPACE_FONT = 'Lucida Console'
18 elif platform.system() == 'Darwin':
19     MONOSPACE_FONT = 'Monaco'
20 else:
21     MONOSPACE_FONT = 'monospace'
22
23 column_index = 4
24
25 class QR_Window(QWidget):
26
27     def __init__(self, exchanger):
28         QWidget.__init__(self)
29         self.exchanger = exchanger
30         self.setWindowTitle('Electrum - '+_('Invoice'))
31         self.setMinimumSize(800, 250)
32         self.address = ''
33         self.label = ''
34         self.amount = 0
35         self.setFocusPolicy(QtCore.Qt.NoFocus)
36
37         main_box = QHBoxLayout()
38         
39         self.qrw = QRCodeWidget()
40         main_box.addWidget(self.qrw, 1)
41
42         vbox = QVBoxLayout()
43         main_box.addLayout(vbox)
44
45         self.address_label = QLabel("")
46         #self.address_label.setFont(QFont(MONOSPACE_FONT))
47         vbox.addWidget(self.address_label)
48
49         self.label_label = QLabel("")
50         vbox.addWidget(self.label_label)
51
52         self.amount_label = QLabel("")
53         vbox.addWidget(self.amount_label)
54
55         vbox.addStretch(1)
56         self.setLayout(main_box)
57
58
59     def set_content(self, addr, label, amount, currency):
60         self.address = addr
61         address_text = "<span style='font-size: 18pt'>%s</span>" % addr if addr else ""
62         self.address_label.setText(address_text)
63
64         if currency == 'BTC': currency = None
65         amount_text = ''
66         if amount:
67             if currency:
68                 self.amount = Decimal(amount) / self.exchanger.exchange(1, currency) if currency else amount
69             else:
70                 self.amount = Decimal(amount)
71             self.amount = self.amount.quantize(Decimal('1.0000'))
72
73             if currency:
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)
77
78         self.label = label
79         label_text = "<span style='font-size: 21pt'>%s</span>" % label if label else ""
80         self.label_label.setText(label_text)
81
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)
89             
90         self.qrw.set_addr( msg )
91
92
93
94
95 class Plugin(BasePlugin):
96
97     def fullname(self):
98         return 'Point of Sale'
99
100     def description(self):
101         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.')
102
103     def init(self):
104         self.window = self.gui.main_window
105         self.wallet = self.window.wallet
106
107         self.qr_window = None
108         self.merchant_name = self.config.get('merchant_name', 'Invoice')
109
110         self.window.expert_mode = True
111         self.window.receive_list.setColumnCount(5)
112         self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx'), _('Request')])
113         self.requested_amounts = {}
114         self.toggle_QR_window(True)
115
116     def enable(self):
117         if not self.config.get('use_exchange_rate'):
118             self.gui.main_window.show_message("Please enable exchange rates first!")
119             return False
120
121         return BasePlugin.enable(self)
122
123
124     def load_wallet(self, wallet):
125         self.wallet = wallet
126         self.requested_amounts = self.wallet.storage.get('requested_amounts',{}) 
127
128     def close(self):
129         self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx')])
130         self.window.receive_list.setColumnCount(4)
131         for i,width in enumerate(self.window.column_widths['receive']):
132             self.window.receive_list.setColumnWidth(i, width)
133         self.toggle_QR_window(False)
134     
135
136     def close_main_window(self):
137         if self.qr_window: 
138             self.qr_window.close()
139             self.qr_window = None
140
141     
142     def timer_actions(self):
143         if self.qr_window:
144             self.qr_window.qrw.update_qr()
145
146
147     def toggle_QR_window(self, show):
148         if show and not self.qr_window:
149             self.qr_window = QR_Window(self.gui.exchanger)
150             self.qr_window.setVisible(True)
151             self.qr_window_geometry = self.qr_window.geometry()
152             item = self.window.receive_list.currentItem()
153             if item:
154                 address = str(item.text(1))
155                 label = self.wallet.labels.get(address)
156                 amount, currency = self.requested_amounts.get(address, (None, None))
157                 self.qr_window.set_content( address, label, amount, currency )
158
159         elif show and self.qr_window and not self.qr_window.isVisible():
160             self.qr_window.setVisible(True)
161             self.qr_window.setGeometry(self.qr_window_geometry)
162
163         elif not show and self.qr_window and self.qr_window.isVisible():
164             self.qr_window_geometry = self.qr_window.geometry()
165             self.qr_window.setVisible(False)
166
167
168     
169     def update_receive_item(self, address, item):
170         try:
171             amount, currency = self.requested_amounts.get(address, (None, None))
172         except Exception:
173             print "cannot get requested amount", address, self.requested_amounts.get(address)
174             amount, currency = None, None
175             self.requested_amounts.pop(address)
176
177         amount_str = amount + (' ' + currency if currency else '') if amount is not None  else ''
178         item.setData(column_index,0,amount_str)
179
180
181     
182     def current_item_changed(self, a):
183         if not self.wallet: 
184             return
185         if a is not None and self.qr_window and self.qr_window.isVisible():
186             address = str(a.text(0))
187             label = self.wallet.labels.get(address)
188             try:
189                 amount, currency = self.requested_amounts.get(address, (None, None))
190             except Exception:
191                 amount, currency = None, None
192             self.qr_window.set_content( address, label, amount, currency )
193
194
195     
196     def item_changed(self, item, column):
197         if column != column_index:
198             return
199         address = str( item.text(0) )
200         text = str( item.text(column) )
201         try:
202             seq = self.wallet.get_address_index(address)
203             index = seq[1][1]
204         except Exception:
205             print "cannot get index"
206             return
207
208         text = text.strip().upper()
209         #print text
210         m = re.match('^(\d*(|\.\d*))\s*(|BTC|EUR|USD|GBP|CNY|JPY|RUB|BRL)$', text)
211         if m and m.group(1) and m.group(1)!='.':
212             amount = m.group(1)
213             currency = m.group(3)
214             if not currency:
215                 currency = 'BTC'
216             else:
217                 currency = currency.upper()
218                     
219             self.requested_amounts[address] = (amount, currency)
220             self.wallet.storage.put('requested_amounts', self.requested_amounts, True)
221
222             label = self.wallet.labels.get(address)
223             if label is None:
224                 label = self.merchant_name + ' - %04d'%(index+1)
225                 self.wallet.labels[address] = label
226
227             if self.qr_window:
228                 self.qr_window.set_content( address, label, amount, currency )
229
230         else:
231             item.setText(column,'')
232             if address in self.requested_amounts:
233                 self.requested_amounts.pop(address)
234             
235         self.window.update_receive_item(self.window.receive_list.currentItem())
236
237
238
239
240     def edit_amount(self):
241         l = self.window.receive_list
242         item = l.currentItem()
243         item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
244         l.editItem( item, column_index )
245         item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
246
247     
248     def receive_menu(self, menu):
249         menu.addAction(_("Request amount"), self.edit_amount)
250         menu.addAction(_("Show Invoice"), lambda: self.toggle_QR_window(True))
251
252