fix self.amount in POS plugin
[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         else:
77             self.amount = None
78             
79         self.amount_label.setText(amount_text)
80
81         self.label = label
82         label_text = "<span style='font-size: 21pt'>%s</span>" % label if label else ""
83         self.label_label.setText(label_text)
84
85         msg = 'bitcoin:'+self.address
86         if self.amount is not None:
87             msg += '?amount=%s'%(str( self.amount))
88             if self.label is not None:
89                 msg += '&label=%s'%(self.label)
90         elif self.label is not None:
91             msg += '?label=%s'%(self.label)
92             
93         self.qrw.set_addr( msg )
94
95
96
97
98 class Plugin(BasePlugin):
99
100     def fullname(self):
101         return 'Point of Sale'
102
103     def description(self):
104         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.')
105
106     def init(self):
107         self.window = self.gui.main_window
108         self.wallet = self.window.wallet
109
110         self.qr_window = None
111         self.merchant_name = self.config.get('merchant_name', 'Invoice')
112
113         self.window.expert_mode = True
114         self.window.receive_list.setColumnCount(5)
115         self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx'), _('Request')])
116         self.requested_amounts = {}
117         self.toggle_QR_window(True)
118
119     def enable(self):
120         if not self.config.get('use_exchange_rate'):
121             self.gui.main_window.show_message("Please enable exchange rates first!")
122             return False
123
124         return BasePlugin.enable(self)
125
126
127     def load_wallet(self, wallet):
128         self.wallet = wallet
129         self.requested_amounts = self.wallet.storage.get('requested_amounts',{}) 
130
131     def close(self):
132         self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx')])
133         self.window.receive_list.setColumnCount(4)
134         for i,width in enumerate(self.window.column_widths['receive']):
135             self.window.receive_list.setColumnWidth(i, width)
136         self.toggle_QR_window(False)
137     
138
139     def close_main_window(self):
140         if self.qr_window: 
141             self.qr_window.close()
142             self.qr_window = None
143
144     
145     def timer_actions(self):
146         if self.qr_window:
147             self.qr_window.qrw.update_qr()
148
149
150     def toggle_QR_window(self, show):
151         if show and not self.qr_window:
152             self.qr_window = QR_Window(self.gui.exchanger)
153             self.qr_window.setVisible(True)
154             self.qr_window_geometry = self.qr_window.geometry()
155             item = self.window.receive_list.currentItem()
156             if item:
157                 address = str(item.text(1))
158                 label = self.wallet.labels.get(address)
159                 amount, currency = self.requested_amounts.get(address, (None, None))
160                 self.qr_window.set_content( address, label, amount, currency )
161
162         elif show and self.qr_window and not self.qr_window.isVisible():
163             self.qr_window.setVisible(True)
164             self.qr_window.setGeometry(self.qr_window_geometry)
165
166         elif not show and self.qr_window and self.qr_window.isVisible():
167             self.qr_window_geometry = self.qr_window.geometry()
168             self.qr_window.setVisible(False)
169
170
171     
172     def update_receive_item(self, address, item):
173         try:
174             amount, currency = self.requested_amounts.get(address, (None, None))
175         except Exception:
176             print "cannot get requested amount", address, self.requested_amounts.get(address)
177             amount, currency = None, None
178             self.requested_amounts.pop(address)
179
180         amount_str = amount + (' ' + currency if currency else '') if amount is not None  else ''
181         item.setData(column_index,0,amount_str)
182
183
184     
185     def current_item_changed(self, a):
186         if not self.wallet: 
187             return
188         if a is not None and self.qr_window and self.qr_window.isVisible():
189             address = str(a.text(0))
190             label = self.wallet.labels.get(address)
191             try:
192                 amount, currency = self.requested_amounts.get(address, (None, None))
193             except Exception:
194                 amount, currency = None, None
195             self.qr_window.set_content( address, label, amount, currency )
196
197
198     
199     def item_changed(self, item, column):
200         if column != column_index:
201             return
202         address = str( item.text(0) )
203         text = str( item.text(column) )
204         try:
205             seq = self.wallet.get_address_index(address)
206             index = seq[1][1]
207         except Exception:
208             print "cannot get index"
209             return
210
211         text = text.strip().upper()
212         #print text
213         m = re.match('^(\d*(|\.\d*))\s*(|BTC|EUR|USD|GBP|CNY|JPY|RUB|BRL)$', text)
214         if m and m.group(1) and m.group(1)!='.':
215             amount = m.group(1)
216             currency = m.group(3)
217             if not currency:
218                 currency = 'BTC'
219             else:
220                 currency = currency.upper()
221                     
222             self.requested_amounts[address] = (amount, currency)
223             self.wallet.storage.put('requested_amounts', self.requested_amounts, True)
224
225             label = self.wallet.labels.get(address)
226             if label is None:
227                 label = self.merchant_name + ' - %04d'%(index+1)
228                 self.wallet.labels[address] = label
229
230             if self.qr_window:
231                 self.qr_window.set_content( address, label, amount, currency )
232
233         else:
234             item.setText(column,'')
235             if address in self.requested_amounts:
236                 self.requested_amounts.pop(address)
237             
238         self.window.update_receive_item(self.window.receive_list.currentItem())
239
240
241
242
243     def edit_amount(self):
244         l = self.window.receive_list
245         item = l.currentItem()
246         item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
247         l.editItem( item, column_index )
248         item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
249
250     
251     def receive_menu(self, menu):
252         menu.addAction(_("Request amount"), self.edit_amount)
253         menu.addAction(_("Show Invoice"), lambda: self.toggle_QR_window(True))
254
255