define frozen signal for exchange rate plugin
[electrum-nvc.git] / gui / qt / main_window.py
index f6a92b3..473705f 100644 (file)
@@ -35,14 +35,12 @@ from electrum.plugins import run_hook
 
 import icons_rc
 
-from electrum.wallet import format_satoshis
+from electrum.util import format_satoshis
 from electrum import Transaction
 from electrum import mnemonic
 from electrum import util, bitcoin, commands, Interface, Wallet
 from electrum import SimpleConfig, Wallet, WalletStorage
 
-from electrum import bmp, pyqrnative
-
 from amountedit import AmountEdit, BTCAmountEdit, MyLineEdit
 from network_dialog import NetworkDialog
 from qrcodewidget import QRCodeWidget, QRDialog
@@ -231,6 +229,8 @@ class ElectrumWindow(QMainWindow):
         self.update_buttons_on_seed()
         self.update_console()
 
+        self.clear_receive_tab()
+        self.update_receive_tab()
         run_hook('load_wallet', wallet)
 
 
@@ -433,7 +433,6 @@ class ElectrumWindow(QMainWindow):
             self.update_wallet()
             self.need_update.clear()
 
-        self.receive_qr.update_qr()
         run_hook('timer_actions')
 
     def format_amount(self, x, is_diff=False, whitespaces=False):
@@ -500,9 +499,8 @@ class ElectrumWindow(QMainWindow):
         for i,width in enumerate(self.column_widths['history']):
             l.setColumnWidth(i, width)
         l.setHeaderLabels( [ '', _('Date'), _('Description') , _('Amount'), _('Balance')] )
-        self.connect(l, SIGNAL('itemDoubleClicked(QTreeWidgetItem*, int)'), self.tx_label_clicked)
-        self.connect(l, SIGNAL('itemChanged(QTreeWidgetItem*, int)'), self.tx_label_changed)
-
+        l.itemDoubleClicked.connect(self.tx_label_clicked)
+        l.itemChanged.connect(self.tx_label_changed)
         l.customContextMenuRequested.connect(self.create_history_menu)
         return l
 
@@ -559,7 +557,7 @@ class ElectrumWindow(QMainWindow):
 
 
     def edit_label(self, is_recv):
-        l = self.receive_list if is_recv else self.contacts_list
+        l = self.address_list if is_recv else self.contacts_list
         item = l.currentItem()
         item.setFlags(Qt.ItemIsEditable|Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsDragEnabled)
         l.editItem( item, 1 )
@@ -680,37 +678,49 @@ class ElectrumWindow(QMainWindow):
         grid.addWidget(self.receive_amount_e, 2, 1, 1, 2)
         self.receive_amount_e.textChanged.connect(self.update_receive_qr)
 
-        save_button = QPushButton(_('Save'))
-        save_button.clicked.connect(self.save_payment_request)
-        grid.addWidget(save_button, 3, 1)
-        clear_button = QPushButton(_('Clear'))
+        self.save_request_button = QPushButton(_('Save'))
+        self.save_request_button.clicked.connect(self.save_payment_request)
+        grid.addWidget(self.save_request_button, 3, 1)
+        clear_button = QPushButton(_('New'))
         clear_button.clicked.connect(self.clear_receive_tab)
         grid.addWidget(clear_button, 3, 2)
         grid.setRowStretch(4, 1)
 
-        self.receive_qr = QRCodeWidget()
+        self.receive_qr = QRCodeWidget(fixedSize=200)
         grid.addWidget(self.receive_qr, 0, 4, 5, 2)
 
-        self.receive_requests_label = QLabel(_('Pending requests'))
+        grid.setRowStretch(5, 1)
+
+        self.receive_requests_label = QLabel(_('Saved Requests'))
         self.receive_list = MyTreeWidget(self)
         self.receive_list.customContextMenuRequested.connect(self.receive_list_menu)
-        self.receive_list.setHeaderLabels( [_('Address'), _('Message'), _('Amount'), _('Status')] )
-        self.receive_list.setColumnWidth(0, 320)
+        self.receive_list.currentItemChanged.connect(self.receive_item_changed)
+        self.receive_list.itemClicked.connect(self.receive_item_changed)
+        self.receive_list.setHeaderLabels( [_('Address'), _('Message'), _('Amount')] )
+        self.receive_list.setColumnWidth(0, 340)
         h = self.receive_list.header()
         h.setStretchLastSection(False)
         h.setResizeMode(1, QHeaderView.Stretch)
 
-        grid.addWidget(self.receive_requests_label, 5, 0)
-        grid.addWidget(self.receive_list, 6, 0, 1, 6)
-
-        grid.setRowStretch(7, 1)
+        grid.addWidget(self.receive_requests_label, 6, 0)
+        grid.addWidget(self.receive_list, 7, 0, 1, 6)
         return w
 
+    def receive_item_changed(self, item):
+        if item is None:
+            return
+        addr = str(item.text(0))
+        amount, message = self.receive_requests[addr]
+        self.receive_address_e.setText(addr)
+        self.receive_message_e.setText(message)
+        self.receive_amount_e.setAmount(amount)
+
+
     def receive_list_delete(self, item):
         addr = str(item.text(0))
         self.receive_requests.pop(addr)
         self.update_receive_tab()
-        self.redraw_from_list()
+        self.clear_receive_tab()
 
     def receive_list_menu(self, position):
         item = self.receive_list.itemAt(position)
@@ -731,29 +741,25 @@ class ElectrumWindow(QMainWindow):
         self.update_receive_tab()
 
     def clear_receive_tab(self):
-        self.receive_amount_e.setAmount(None)
-        self.receive_message_e.setText("")
-
-    def receive_at(self, addr):
-        if not bitcoin.is_address(addr):
-            return
-        self.tabs.setCurrentIndex(2)
-        self.receive_address_e.setText(addr)
-
-    def update_receive_tab(self):
         self.receive_requests = self.wallet.storage.get('receive_requests',{}) 
-
         domain = self.wallet.get_account_addresses(self.current_account, include_change=False)
         for addr in domain:
             if not self.wallet.address_is_old(addr) and addr not in self.receive_requests.keys():
                 break
         else:
             addr = ""
-
         self.receive_address_e.setText(addr)
-        self.receive_message_e.setText("")
+        self.receive_message_e.setText('')
         self.receive_amount_e.setAmount(None)
 
+    def receive_at(self, addr):
+        if not bitcoin.is_address(addr):
+            return
+        self.tabs.setCurrentIndex(2)
+        self.receive_address_e.setText(addr)
+
+    def update_receive_tab(self):
+        self.receive_requests = self.wallet.storage.get('receive_requests',{}) 
         b = len(self.receive_requests) > 0
         self.receive_list.setVisible(b)
         self.receive_requests_label.setVisible(b)
@@ -761,26 +767,29 @@ class ElectrumWindow(QMainWindow):
         self.receive_list.clear()
         for address, v in self.receive_requests.items():
             amount, message = v
-            item = QTreeWidgetItem( [ address, message, self.format_amount(amount) if amount else "", ""] )
+            item = QTreeWidgetItem( [ address, message, self.format_amount(amount) if amount else ""] )
+            item.setFont(0, QFont(MONOSPACE_FONT))
             self.receive_list.addTopLevelItem(item)
 
 
     def update_receive_qr(self):
         import urlparse, urllib
         addr = str(self.receive_address_e.text())
+        amount = self.receive_amount_e.get_amount()
+        message = unicode(self.receive_message_e.text()).encode('utf8')
+        self.save_request_button.setEnabled((amount is not None) or (message != ""))
         if addr:
             query = []
-            amount = self.receive_amount_e.get_amount()
             if amount:
                 query.append('amount=%s'%format_satoshis(amount))
-            message = unicode(self.receive_message_e.text()).encode('utf8')
             if message:
                 query.append('message=%s'%urllib.quote(message))
             p = urlparse.ParseResult(scheme='bitcoin', netloc='', path=addr, params='', query='&'.join(query), fragment='')
             url = urlparse.urlunparse(p)
         else:
             url = ""
-        self.receive_qr.set_addr(url)
+        self.receive_qr.setData(url)
+        run_hook('update_receive_qr', addr, amount, message, url)
 
 
     def create_send_tab(self):
@@ -863,6 +872,7 @@ class ElectrumWindow(QMainWindow):
                 fee = self.wallet.estimated_fee(inputs, 1)
                 amount = total - fee
                 self.amount_e.setAmount(amount)
+                self.amount_e.textEdited.emit("")
                 self.fee_e.setAmount(fee)
                 return
 
@@ -1015,7 +1025,7 @@ class ElectrumWindow(QMainWindow):
         def sign_thread():
             time.sleep(0.1)
             keypairs = {}
-            self.wallet.add_keypairs_from_wallet(tx, keypairs, password)
+            self.wallet.add_keypairs(tx, keypairs, password)
             self.wallet.sign_transaction(tx, keypairs, password)
             return tx, fee, label
 
@@ -1223,9 +1233,9 @@ class ElectrumWindow(QMainWindow):
         l.setContextMenuPolicy(Qt.CustomContextMenu)
         l.customContextMenuRequested.connect(self.create_receive_menu)
         l.setSelectionMode(QAbstractItemView.ExtendedSelection)
-        self.connect(l, SIGNAL('itemDoubleClicked(QTreeWidgetItem*, int)'), lambda a, b: self.address_label_clicked(a,b,l,0,1))
-        self.connect(l, SIGNAL('itemChanged(QTreeWidgetItem*, int)'), lambda a,b: self.address_label_changed(a,b,l,0,1))
-        self.connect(l, SIGNAL('currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)'), lambda a,b: self.current_item_changed(a))
+        l.itemDoubleClicked.connect(lambda a, b: self.address_label_clicked(a,b,l,0,1))
+        l.itemChanged.connect(lambda a,b: self.address_label_changed(a,b,l,0,1))
+        l.currentItemChanged.connect(lambda a,b: self.current_item_changed(a))
         self.address_list = l
         return w
 
@@ -1254,14 +1264,15 @@ class ElectrumWindow(QMainWindow):
         l.customContextMenuRequested.connect(self.create_contact_menu)
         for i,width in enumerate(self.column_widths['contacts']):
             l.setColumnWidth(i, width)
-        self.connect(l, SIGNAL('itemDoubleClicked(QTreeWidgetItem*, int)'), lambda a, b: self.address_label_clicked(a,b,l,0,1))
-        self.connect(l, SIGNAL('itemChanged(QTreeWidgetItem*, int)'), lambda a,b: self.address_label_changed(a,b,l,0,1))
+        l.itemDoubleClicked.connect(lambda a, b: self.address_label_clicked(a,b,l,0,1))
+        l.itemChanged.connect(lambda a,b: self.address_label_changed(a,b,l,0,1))
         self.contacts_list = l
         return w
 
 
     def create_invoices_tab(self):
         l, w = self.create_list_tab([_('Requestor'), _('Memo'),_('Amount'), _('Status')])
+        l.setColumnWidth(0, 150)
         h = l.header()
         h.setStretchLastSection(False)
         h.setResizeMode(1, QHeaderView.Stretch)
@@ -1362,7 +1373,9 @@ class ElectrumWindow(QMainWindow):
         if any(addr in self.wallet.frozen_addresses for addr in addrs):
             menu.addAction(_("Unfreeze"), lambda: self.set_addrs_frozen(addrs, False))
 
-        if any(addr not in self.wallet.frozen_addresses for addr in addrs):
+        def can_send(addr):
+            return addr not in self.wallet.frozen_addresses and self.wallet.get_addr_balance(addr) != (0, 0)
+        if any(can_send(addr) for addr in addrs):
             menu.addAction(_("Send From"), lambda: self.send_from_addresses(addrs))
 
         run_hook('receive_menu', menu, addrs)
@@ -2033,24 +2046,29 @@ class ElectrumWindow(QMainWindow):
         "json or raw hexadecimal"
         try:
             txt.decode('hex')
-            tx = Transaction(txt)
-            return tx
-        except Exception:
-            pass
+            is_hex = True
+        except:
+            is_hex = False
+
+        if is_hex:
+            try:
+                return Transaction(txt)
+            except:
+                traceback.print_exc(file=sys.stdout)
+                QMessageBox.critical(None, _("Unable to parse transaction"), _("Electrum was unable to parse your transaction"))
+                return
 
         try:
             tx_dict = json.loads(str(txt))
             assert "hex" in tx_dict.keys()
             tx = Transaction(tx_dict["hex"])
-            if tx_dict.has_key("input_info"):
-                input_info = json.loads(tx_dict['input_info'])
-                tx.add_input_info(input_info)
+            #if tx_dict.has_key("input_info"):
+            #    input_info = json.loads(tx_dict['input_info'])
+            #    tx.add_input_info(input_info)
             return tx
         except Exception:
             traceback.print_exc(file=sys.stdout)
-            pass
-
-        QMessageBox.critical(None, _("Unable to parse transaction"), _("Electrum was unable to parse your transaction"))
+            QMessageBox.critical(None, _("Unable to parse transaction"), _("Electrum was unable to parse your transaction"))
 
 
 
@@ -2068,8 +2086,12 @@ class ElectrumWindow(QMainWindow):
 
 
     @protected
-    def sign_raw_transaction(self, tx, input_info, password):
-        self.wallet.signrawtransaction(tx, input_info, [], password)
+    def sign_raw_transaction(self, tx, password):
+        try:
+            self.wallet.signrawtransaction(tx, [], password)
+        except Exception as e:
+            traceback.print_exc(file=sys.stdout)
+            QMessageBox.warning(self, _("Error"), str(e))
 
     def do_process_from_text(self):
         text = text_dialog(self, _('Input raw transaction'), _("Transaction:"), _("Load transaction"))