simplify gui names
[electrum-nvc.git] / plugins / pointofsale.py
index 228dabc..438e672 100644 (file)
@@ -7,10 +7,10 @@ from PyQt4.QtCore import *
 import PyQt4.QtCore as QtCore
 import PyQt4.QtGui as QtGui
 
-from electrum_gui.qrcodewidget import QRCodeWidget
-from electrum_gui import bmp, pyqrnative
+from electrum_gui.qt.qrcodewidget import QRCodeWidget
 
-from electrum_gui.i18n import _
+from electrum import bmp, pyqrnative, BasePlugin
+from electrum.i18n import _
 
 
 if platform.system() == 'Windows':
@@ -20,6 +20,7 @@ elif platform.system() == 'Darwin':
 else:
     MONOSPACE_FONT = 'monospace'
 
+column_index = 3
 
 class QR_Window(QWidget):
 
@@ -29,7 +30,7 @@ class QR_Window(QWidget):
         self.setWindowTitle('Electrum - '+_('Invoice'))
         self.setMinimumSize(800, 250)
         self.address = ''
-        self.labe = ''
+        self.label = ''
         self.amount = 0
         self.setFocusPolicy(QtCore.Qt.NoFocus)
 
@@ -88,90 +89,110 @@ class QR_Window(QWidget):
             
         self.qrw.set_addr( msg )
 
-            
-
-
-
 
-def get_info():
-    return 'Point of Sale', _('Show QR code window and amounts requested for each address. Add menu item to request amount.')
 
-def init(gui):
-    gui.requested_amounts = gui.config.get('requested_amounts',{}) 
-    gui.qr_window = None
 
+class Plugin(BasePlugin):
 
+    def fullname(self):
+        return 'Point of Sale'
 
-enabled = False
+    def description(self):
+        return _('Show QR code window and amounts requested for each address. Add menu item to request amount.')
 
-def is_enabled():
-    return False
+    def init(self):
+        self.window = self.gui.main_window
 
-def toggle(gui):
-    global enabled
-    enabled = not enabled
-    toggle_QR_window(gui, enabled)
+        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.requested_amounts = {}
+        self.toggle_QR_window(True)
+
+    def load_wallet(self):
+        self.requested_amounts = self.window.wallet.storage.get('requested_amounts',{}) 
+
+    def close(self):
+        self.window.receive_list.setHeaderLabels([ _('Address'), _('Label'), _('Balance'), _('Tx')])
+        self.toggle_QR_window(False)
+    
+
+    def close_main_window(self):
+        if self.qr_window: 
+            self.qr_window.close()
+            self.qr_window = None
+
+    
+    def timer_actions(self):
+        if self.qr_window:
+            self.qr_window.qrw.update_qr()
+
+
+    def toggle_QR_window(self, show):
+        if show and not self.qr_window:
+            self.qr_window = QR_Window(self.gui.exchanger)
+            self.qr_window.setVisible(True)
+            self.qr_window_geometry = self.qr_window.geometry()
+            item = self.window.receive_list.currentItem()
+            if item:
+                address = str(item.text(1))
+                label = self.window.wallet.labels.get(address)
+                amount, currency = self.requested_amounts.get(address, (None, None))
+                self.qr_window.set_content( address, label, amount, currency )
 
-    if enabled:
-        gui.set_hook('item_changed', item_changed)
-        gui.set_hook('current_item_changed', recv_changed)
-        gui.set_hook('receive_menu', receive_menu)
-        gui.set_hook('update_receive_item', update_receive_item)
-        gui.set_hook('timer_actions', timer_actions)
-        gui.set_hook('close_main_window', close_main_window)
-    else:
-        gui.unset_hook('item_changed', item_changed)
-        gui.unset_hook('current_item_changed', recv_changed)
-        gui.unset_hook('receive_menu', receive_menu)
-        gui.unset_hook('update_receive_item', update_receive_item)
-        gui.unset_hook('timer_actions', timer_actions)
-        gui.unset_hook('close_main_window', close_main_window)
-        
+        elif show and self.qr_window and not self.qr_window.isVisible():
+            self.qr_window.setVisible(True)
+            self.qr_window.setGeometry(self.qr_window_geometry)
 
-    return enabled
+        elif not show and self.qr_window and self.qr_window.isVisible():
+            self.qr_window_geometry = self.qr_window.geometry()
+            self.qr_window.setVisible(False)
 
 
-def toggle_QR_window(self, show):
-    if show and not self.qr_window:
-        self.qr_window = QR_Window(self.exchanger)
-        self.qr_window.setVisible(True)
-        self.qr_window_geometry = self.qr_window.geometry()
-        item = self.receive_list.currentItem()
-        if item:
-            address = str(item.text(1))
-            label = self.wallet.labels.get(address)
+    
+    def update_receive_item(self, address, item):
+        try:
             amount, currency = self.requested_amounts.get(address, (None, None))
-            self.qr_window.set_content( address, label, amount, currency )
-
-    elif show and self.qr_window and not self.qr_window.isVisible():
-        self.qr_window.setVisible(True)
-        self.qr_window.setGeometry(self.qr_window_geometry)
+        except:
+            print "cannot get requested amount", address, self.requested_amounts.get(address)
+            amount, currency = None, None
+            self.requested_amounts.pop(address)
 
-    elif not show and self.qr_window and self.qr_window.isVisible():
-        self.qr_window_geometry = self.qr_window.geometry()
-        self.qr_window.setVisible(False)
+        amount_str = amount + (' ' + currency if currency else '') if amount is not None  else ''
+        item.setData(column_index,0,amount_str)
 
-    #self.print_button.setHidden(self.qr_window is None or not self.qr_window.isVisible())
-    self.receive_list.setColumnHidden(2, self.qr_window is None or not self.qr_window.isVisible())
-    #self.receive_list.setColumnWidth(1, 200)
 
+    
+    def current_item_changed(self, a):
+        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)
+            try:
+                amount, currency = self.requested_amounts.get(address, (None, None))
+            except:
+                amount, currency = None, None
+            self.qr_window.set_content( address, label, amount, currency )
 
 
-def item_changed(self, item, column):
-    if column == 4:
+    
+    def item_changed(self, item, column):
+        if column != column_index:
+            return
         address = str( item.text(0) )
         text = str( item.text(column) )
         try:
-            seq = self.wallet.get_address_index(address)
-            index = seq[-1]
+            seq = self.window.wallet.get_address_index(address)
+            index = seq[1][1]
         except:
             print "cannot get index"
             return
 
         text = text.strip().upper()
         print text
-        m = re.match('^(\d+(|\.\d*))\s*(|BTC|EUR|USD|GBP|CNY|JPY|RUB|BRL)$', text)
-        if m:
+        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)
             currency = m.group(3)
             if not currency:
@@ -180,12 +201,12 @@ def item_changed(self, item, column):
                 currency = currency.upper()
                     
             self.requested_amounts[address] = (amount, currency)
-            self.wallet.config.set_key('requested_amounts', self.requested_amounts, True)
+            self.window.wallet.storage.put('requested_amounts', self.requested_amounts, True)
 
-            label = self.wallet.labels.get(address)
+            label = self.window.wallet.labels.get(address)
             if label is None:
                 label = self.merchant_name + ' - %04d'%(index+1)
-                self.wallet.labels[address] = label
+                self.window.wallet.labels[address] = label
 
             if self.qr_window:
                 self.qr_window.set_content( address, label, amount, currency )
@@ -195,49 +216,20 @@ def item_changed(self, item, column):
             if address in self.requested_amounts:
                 self.requested_amounts.pop(address)
             
-        self.update_receive_item(self.receive_list.currentItem())
-
-
-def recv_changed(self, a):
-    if a is not None and self.qr_window and self.qr_window.isVisible():
-        address = str(a.text(0))
-        label = self.wallet.labels.get(address)
-        try:
-            amount, currency = self.requested_amounts.get(address, (None, None))
-        except:
-            amount, currency = None, None
-        self.qr_window.set_content( address, label, amount, currency )
-
-
+        self.window.update_receive_item(self.window.receive_list.currentItem())
 
-def edit_amount(self):
-    l = self.receive_list
-    item = l.currentItem()
-    item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
-    l.editItem( item, 4 )
-    item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
 
-def receive_menu(self, menu):
-    menu.addAction(_("Request amount"), lambda: edit_amount(self))
 
 
-def update_receive_item(self, address, item):
-    try:
-        amount, currency = self.requested_amounts.get(address, (None, None))
-    except:
-        print "cannot get requested amount", address, self.requested_amounts.get(address)
-        amount, currency = None, None
-            
-    amount_str = amount + (' ' + currency if currency else '') if amount is not None  else ''
-    item.setData(4,0,amount_str)
-
+    def edit_amount(self):
+        l = self.window.receive_list
+        item = l.currentItem()
+        item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
+        l.editItem( item, column_index )
+        item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
 
-def close_main_window(self):
-    if self.qr_window: 
-        self.qr_window.close()
-        self.qr_window = None
+    
+    def receive_menu(self, menu):
+        menu.addAction(_("Request amount"), self.edit_amount)
 
 
-def timer_actions(self):
-    if self.qr_window:
-        self.qr_window.qrw.update_qr()