qrscanner: use win.show_qr_code
[electrum-nvc.git] / plugins / pointofsale.py
index 45c5d48..27f4ccd 100644 (file)
@@ -1,13 +1,14 @@
 import re
 import platform
 from decimal import Decimal
+from urllib import quote
 
 from PyQt4.QtGui import *
 from PyQt4.QtCore import *
 import PyQt4.QtCore as QtCore
 import PyQt4.QtGui as QtGui
 
-from electrum_gui.gui_classic.qrcodewidget import QRCodeWidget
+from electrum_gui.qt.qrcodewidget import QRCodeWidget
 
 from electrum import bmp, pyqrnative, BasePlugin
 from electrum.i18n import _
@@ -20,7 +21,7 @@ elif platform.system() == 'Darwin':
 else:
     MONOSPACE_FONT = 'monospace'
 
-column_index = 3
+column_index = 4
 
 class QR_Window(QWidget):
 
@@ -65,7 +66,10 @@ class QR_Window(QWidget):
         amount_text = ''
         if amount:
             if currency:
-                self.amount = Decimal(amount) / self.exchanger.exchange(1, currency) if currency else amount
+                try:
+                    self.amount = Decimal(amount) / self.exchanger.exchange(1, currency) if currency else amount
+                except Exception:
+                    self.amount = None
             else:
                 self.amount = Decimal(amount)
             self.amount = self.amount.quantize(Decimal('1.0000'))
@@ -73,6 +77,9 @@ class QR_Window(QWidget):
             if currency:
                 amount_text += "<span style='font-size: 18pt'>%s %s</span><br/>" % (amount, currency)
             amount_text += "<span style='font-size: 21pt'>%s</span> <span style='font-size: 16pt'>BTC</span> " % str(self.amount) 
+        else:
+            self.amount = None
+            
         self.amount_label.setText(amount_text)
 
         self.label = label
@@ -83,9 +90,11 @@ class QR_Window(QWidget):
         if self.amount is not None:
             msg += '?amount=%s'%(str( self.amount))
             if self.label is not None:
-                msg += '&label=%s'%(self.label)
+                encoded_label = quote(self.label)
+                msg += '&label=%s'%(encoded_label)
         elif self.label is not None:
-            msg += '?label=%s'%(self.label)
+            encoded_label = quote(self.label)
+            msg += '?label=%s'%(encoded_label)
             
         self.qrw.set_addr( msg )
 
@@ -98,24 +107,38 @@ class Plugin(BasePlugin):
         return 'Point of Sale'
 
     def description(self):
-        return _('Show QR code window and amounts requested for each address. Add menu item to request amount.')
+        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.')
 
     def init(self):
         self.window = self.gui.main_window
+        self.wallet = self.window.wallet
 
         self.qr_window = None
         self.merchant_name = self.config.get('merchant_name', 'Invoice')
 
         self.window.expert_mode = True
-        self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Request')])
+        self.window.receive_list.setColumnCount(5)
+        self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx'), _('Request')])
         self.requested_amounts = {}
         self.toggle_QR_window(True)
 
-    def load_wallet(self):
-        self.requested_amounts = self.window.wallet.storage.get('requested_amounts',{}) 
+    def enable(self):
+        if not self.config.get('use_exchange_rate'):
+            self.gui.main_window.show_message("Please enable exchange rates first!")
+            return False
+
+        return BasePlugin.enable(self)
+
+
+    def load_wallet(self, wallet):
+        self.wallet = wallet
+        self.requested_amounts = self.wallet.storage.get('requested_amounts',{}) 
 
     def close(self):
         self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx')])
+        self.window.receive_list.setColumnCount(4)
+        for i,width in enumerate(self.window.column_widths['receive']):
+            self.window.receive_list.setColumnWidth(i, width)
         self.toggle_QR_window(False)
     
 
@@ -138,7 +161,7 @@ class Plugin(BasePlugin):
             item = self.window.receive_list.currentItem()
             if item:
                 address = str(item.text(1))
-                label = self.window.wallet.labels.get(address)
+                label = self.wallet.labels.get(address)
                 amount, currency = self.requested_amounts.get(address, (None, None))
                 self.qr_window.set_content( address, label, amount, currency )
 
@@ -155,7 +178,7 @@ class Plugin(BasePlugin):
     def update_receive_item(self, address, item):
         try:
             amount, currency = self.requested_amounts.get(address, (None, None))
-        except:
+        except Exception:
             print "cannot get requested amount", address, self.requested_amounts.get(address)
             amount, currency = None, None
             self.requested_amounts.pop(address)
@@ -166,12 +189,14 @@ class Plugin(BasePlugin):
 
     
     def current_item_changed(self, a):
+        if not self.wallet: 
+            return
         if a is not None and self.qr_window and self.qr_window.isVisible():
             address = str(a.text(0))
-            label = self.window.wallet.labels.get(address)
+            label = self.wallet.labels.get(address)
             try:
                 amount, currency = self.requested_amounts.get(address, (None, None))
-            except:
+            except Exception:
                 amount, currency = None, None
             self.qr_window.set_content( address, label, amount, currency )
 
@@ -183,14 +208,14 @@ class Plugin(BasePlugin):
         address = str( item.text(0) )
         text = str( item.text(column) )
         try:
-            seq = self.window.wallet.get_address_index(address)
+            seq = self.wallet.get_address_index(address)
             index = seq[1][1]
-        except:
+        except Exception:
             print "cannot get index"
             return
 
         text = text.strip().upper()
-        print text
+        #print text
         m = re.match('^(\d*(|\.\d*))\s*(|BTC|EUR|USD|GBP|CNY|JPY|RUB|BRL)$', text)
         if m and m.group(1) and m.group(1)!='.':
             amount = m.group(1)
@@ -201,12 +226,12 @@ class Plugin(BasePlugin):
                 currency = currency.upper()
                     
             self.requested_amounts[address] = (amount, currency)
-            self.window.wallet.storage.put('requested_amounts', self.requested_amounts, True)
+            self.wallet.storage.put('requested_amounts', self.requested_amounts, True)
 
-            label = self.window.wallet.labels.get(address)
+            label = self.wallet.labels.get(address)
             if label is None:
                 label = self.merchant_name + ' - %04d'%(index+1)
-                self.window.wallet.labels[address] = label
+                self.wallet.labels[address] = label
 
             if self.qr_window:
                 self.qr_window.set_content( address, label, amount, currency )
@@ -229,7 +254,8 @@ class Plugin(BasePlugin):
         item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
 
     
-    def receive_menu(self, menu):
+    def receive_menu(self, menu, addr):
         menu.addAction(_("Request amount"), self.edit_amount)
+        menu.addAction(_("Show Invoice"), lambda: self.toggle_QR_window(True))