commit 607664e663dbe490b1a09957afdb59f33bae57c1
parent 851db130ea0ce779680d09c95967e6c55ac390a6
Author: Neil Booth <kyuupichan@gmail.com>
Date: Sat, 29 Aug 2015 21:22:08 +0900
Fix contact editing.
This fixes some bugs in contact editing:
- a changed address is now checked for validity. Shows
error if invalid and restores prior value
- the changes are saved, before they were dropped
- adding a new contact switches to the contacts tab,
it used to switch to the address tab
As an enhancement, the contact name, as well as its address,
can be edited and updated.
Finally, the platform edit key can also be used to edit,
in adition to double-clicking. This is typically the F2 key.
Diffstat:
2 files changed, 29 insertions(+), 21 deletions(-)
diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py
@@ -1507,7 +1507,8 @@ class ElectrumWindow(QMainWindow):
return self.create_list_tab(l)
def create_contacts_tab(self):
- l = MyTreeWidget(self, self.create_contact_menu, [_('Key'), _('Value'), _('Type')], 1)
+ l = MyTreeWidget(self, self.create_contact_menu, [_('Name'), _('Address'), _('Type')], 1, [0, 1])
+ l.item_edited = self.contact_edited
self.contacts_list = l
return self.create_list_tab(l)
@@ -1645,6 +1646,22 @@ class ElectrumWindow(QMainWindow):
self.payto_e.setText(addr)
self.amount_e.setFocus()
+ def contact_edited(self, item, column, prior):
+ if column == 0: # Remove old contact if renamed
+ self.contacts.pop(prior)
+ self.set_contact(unicode(item.text(0)), unicode(item.text(1)))
+
+ def set_contact(self, label, address):
+ if not is_valid(address):
+ QMessageBox.warning(self, _('Error'), _('Invalid Address'), _('OK'))
+ self.update_contacts_tab() # Displays original unchanged value
+ return False
+ self.contacts[label] = ('address', address)
+ self.update_contacts_tab()
+ self.update_history_tab()
+ self.update_completions()
+ return True
+
def delete_contact(self, x):
if not self.question(_("Do you want to remove")+" %s "%x +_("from your list of contacts?")):
return
@@ -1936,19 +1953,8 @@ class ElectrumWindow(QMainWindow):
if not d.exec_():
return
- address = str(line1.text())
- label = unicode(line2.text())
-
- if not is_valid(address):
- QMessageBox.warning(self, _('Error'), _('Invalid Address'), _('OK'))
- return
-
- self.contacts[label] = ('address', address)
-
- self.update_contacts_tab()
- self.update_history_tab()
- self.update_completions()
- self.tabs.setCurrentIndex(3)
+ if self.set_contact(unicode(line2.text()), str(line1.text())):
+ self.tabs.setCurrentIndex(4)
@protected
diff --git a/gui/qt/util.py b/gui/qt/util.py
@@ -289,13 +289,10 @@ class EditableItem(QTreeWidgetItem):
self.setFlags(self.flags() | Qt.ItemIsEditable)
class EditableItemDelegate(QStyledItemDelegate):
- def __init__(self, parent, editable_columns):
- QStyledItemDelegate.__init__(self, parent)
- self.editable_columns = editable_columns
-
def createEditor(self, parent, option, index):
- if index.column() not in self.editable_columns:
+ if index.column() not in self.parent().editable_columns:
return None
+ self.parent().prior_text = unicode(index.data().toString())
return QStyledItemDelegate.createEditor(self, parent, option, index)
class MyTreeWidget(QTreeWidget):
@@ -317,10 +314,11 @@ class MyTreeWidget(QTreeWidget):
# Control which columns are editable
if editable_columns is None:
editable_columns = [stretch_column]
+ self.editable_columns = editable_columns
self.setEditTriggers(QAbstractItemView.DoubleClicked |
QAbstractItemView.EditKeyPressed)
- self.setItemDelegate(EditableItemDelegate(self, editable_columns))
- self.itemChanged.connect(self.item_edited)
+ self.setItemDelegate(EditableItemDelegate(self))
+ self.itemChanged.connect(self.item_changed)
# stretch
for i in range(len(headers)):
@@ -340,6 +338,10 @@ class MyTreeWidget(QTreeWidget):
break
self.emit(SIGNAL('customContextMenuRequested(const QPoint&)'), QPoint(50, i*5 + j - 1))
+ def item_changed(self, item, column):
+ '''Called only when the text actually changes'''
+ self.item_edited(item, column, self.prior_text)
+
def item_edited(self, item, column, prior):
'''Called only when the text actually changes'''
key = str(item.data(0, Qt.UserRole).toString())