diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py index d91b4a33f..58861796e 100644 --- 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 index 3ad944b8f..82b38cb7d 100644 --- 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())