generalize plugins to all guis
[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.gui_classic.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 = 3
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.')
102
103     def init(self):
104         self.window = self.gui.main_window
105
106         self.qr_window = None
107         self.merchant_name = self.config.get('merchant_name', 'Invoice')
108
109         self.window.expert_mode = True
110         self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Request')])
111         self.requested_amounts = {}
112         self.toggle_QR_window(True)
113
114     def load_wallet(self):
115         self.requested_amounts = self.window.wallet.storage.get('requested_amounts',{}) 
116
117     def close(self):
118         self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx')])
119         self.toggle_QR_window(False)
120     
121
122     def close_main_window(self):
123         if self.qr_window: 
124             self.qr_window.close()
125             self.qr_window = None
126
127     
128     def timer_actions(self):
129         if self.qr_window:
130             self.qr_window.qrw.update_qr()
131
132
133     def toggle_QR_window(self, show):
134         if show and not self.qr_window:
135             self.qr_window = QR_Window(self.gui.exchanger)
136             self.qr_window.setVisible(True)
137             self.qr_window_geometry = self.qr_window.geometry()
138             item = self.window.receive_list.currentItem()
139             if item:
140                 address = str(item.text(1))
141                 label = self.window.wallet.labels.get(address)
142                 amount, currency = self.requested_amounts.get(address, (None, None))
143                 self.qr_window.set_content( address, label, amount, currency )
144
145         elif show and self.qr_window and not self.qr_window.isVisible():
146             self.qr_window.setVisible(True)
147             self.qr_window.setGeometry(self.qr_window_geometry)
148
149         elif not show and self.qr_window and self.qr_window.isVisible():
150             self.qr_window_geometry = self.qr_window.geometry()
151             self.qr_window.setVisible(False)
152
153
154     
155     def update_receive_item(self, address, item):
156         try:
157             amount, currency = self.requested_amounts.get(address, (None, None))
158         except:
159             print "cannot get requested amount", address, self.requested_amounts.get(address)
160             amount, currency = None, None
161             self.requested_amounts.pop(address)
162
163         amount_str = amount + (' ' + currency if currency else '') if amount is not None  else ''
164         item.setData(column_index,0,amount_str)
165
166
167     
168     def current_item_changed(self, a):
169         if a is not None and self.qr_window and self.qr_window.isVisible():
170             address = str(a.text(0))
171             label = self.window.wallet.labels.get(address)
172             try:
173                 amount, currency = self.requested_amounts.get(address, (None, None))
174             except:
175                 amount, currency = None, None
176             self.qr_window.set_content( address, label, amount, currency )
177
178
179     
180     def item_changed(self, item, column):
181         if column != column_index:
182             return
183         address = str( item.text(0) )
184         text = str( item.text(column) )
185         try:
186             seq = self.window.wallet.get_address_index(address)
187             index = seq[1][1]
188         except:
189             print "cannot get index"
190             return
191
192         text = text.strip().upper()
193         print text
194         m = re.match('^(\d*(|\.\d*))\s*(|BTC|EUR|USD|GBP|CNY|JPY|RUB|BRL)$', text)
195         if m and m.group(1) and m.group(1)!='.':
196             amount = m.group(1)
197             currency = m.group(3)
198             if not currency:
199                 currency = 'BTC'
200             else:
201                 currency = currency.upper()
202                     
203             self.requested_amounts[address] = (amount, currency)
204             self.window.wallet.storage.put('requested_amounts', self.requested_amounts, True)
205
206             label = self.window.wallet.labels.get(address)
207             if label is None:
208                 label = self.merchant_name + ' - %04d'%(index+1)
209                 self.window.wallet.labels[address] = label
210
211             if self.qr_window:
212                 self.qr_window.set_content( address, label, amount, currency )
213
214         else:
215             item.setText(column,'')
216             if address in self.requested_amounts:
217                 self.requested_amounts.pop(address)
218             
219         self.window.update_receive_item(self.window.receive_list.currentItem())
220
221
222
223
224     def edit_amount(self):
225         l = self.window.receive_list
226         item = l.currentItem()
227         item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
228         l.editItem( item, column_index )
229         item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
230
231     
232     def receive_menu(self, menu):
233         menu.addAction(_("Request amount"), self.edit_amount)
234
235