Skip to content

Commit

Permalink
Merge 1184c07 into 9350709
Browse files Browse the repository at this point in the history
  • Loading branch information
ysangkok committed Dec 3, 2018
2 parents 9350709 + 1184c07 commit bf99ca4
Show file tree
Hide file tree
Showing 10 changed files with 572 additions and 403 deletions.
3 changes: 2 additions & 1 deletion electrum/contacts.py
Expand Up @@ -65,8 +65,9 @@ def __setitem__(self, key, value):

def pop(self, key):
if key in self.keys():
dict.pop(self, key)
res = dict.pop(self, key)
self.save()
return res

def resolve(self, k):
if bitcoin.is_address(k):
Expand Down
82 changes: 42 additions & 40 deletions electrum/gui/qt/address_list.py
Expand Up @@ -31,13 +31,11 @@

from .util import *


class AddressList(MyTreeWidget):
class AddressList(MyTreeView):
filter_columns = [0, 1, 2, 3] # Type, Address, Label, Balance

def __init__(self, parent=None):
MyTreeWidget.__init__(self, parent, self.create_menu, [], 2)
self.refresh_headers()
super().__init__(parent, self.create_menu, 2)
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setSortingEnabled(True)
self.show_change = 0
Expand All @@ -50,6 +48,8 @@ def __init__(self, parent=None):
self.used_button.currentIndexChanged.connect(self.toggle_used)
for t in [_('All'), _('Unused'), _('Funded'), _('Used')]:
self.used_button.addItem(t)
self.setModel(QStandardItemModel(self))
self.update()

def get_toolbar_buttons(self):
return QLabel(_("Filter:")), self.change_button, self.used_button
Expand Down Expand Up @@ -82,18 +82,19 @@ def toggle_used(self, state):
self.show_used = state
self.update()

def on_update(self):
def update(self):
self.wallet = self.parent.wallet
item = self.currentItem()
current_address = item.data(0, Qt.UserRole) if item else None
current_address = self.current_item_user_role(col=2)
if self.show_change == 1:
addr_list = self.wallet.get_receiving_addresses()
elif self.show_change == 2:
addr_list = self.wallet.get_change_addresses()
else:
addr_list = self.wallet.get_addresses()
self.clear()
self.model().clear()
self.refresh_headers()
fx = self.parent.fx
set_address = None
for address in addr_list:
num = self.wallet.get_address_history_len(address)
label = self.wallet.labels.get(address, '')
Expand All @@ -111,61 +112,66 @@ def on_update(self):
if fx and fx.get_fiat_address_config():
rate = fx.exchange_rate()
fiat_balance = fx.value_str(balance, rate)
address_item = SortableTreeWidgetItem(['', address, label, balance_text, fiat_balance, "%d"%num])
labels = ['', address, label, balance_text, fiat_balance, "%d"%num]
address_item = [QStandardItem(e) for e in labels]
else:
address_item = SortableTreeWidgetItem(['', address, label, balance_text, "%d"%num])
labels = ['', address, label, balance_text, "%d"%num]
address_item = [QStandardItem(e) for e in labels]
# align text and set fonts
for i in range(address_item.columnCount()):
address_item.setTextAlignment(i, Qt.AlignVCenter)
for i, item in enumerate(address_item):
item.setTextAlignment(Qt.AlignVCenter)
if i not in (0, 2):
address_item.setFont(i, QFont(MONOSPACE_FONT))
item.setFont(QFont(MONOSPACE_FONT))
item.setEditable(i in self.editable_columns)
if fx and fx.get_fiat_address_config():
address_item.setTextAlignment(4, Qt.AlignRight | Qt.AlignVCenter)
address_item[4].setTextAlignment(Qt.AlignRight | Qt.AlignVCenter)
# setup column 0
if self.wallet.is_change(address):
address_item.setText(0, _('change'))
address_item.setBackground(0, ColorScheme.YELLOW.as_color(True))
address_item[0].setText(_('change'))
address_item[0].setBackground(ColorScheme.YELLOW.as_color(True))
else:
address_item.setText(0, _('receiving'))
address_item.setBackground(0, ColorScheme.GREEN.as_color(True))
address_item.setData(0, Qt.UserRole, address) # column 0; independent from address column
address_item[0].setText(_('receiving'))
address_item[0].setBackground(ColorScheme.GREEN.as_color(True))
address_item[2].setData(address, Qt.UserRole)
# setup column 1
if self.wallet.is_frozen(address):
address_item.setBackground(1, ColorScheme.BLUE.as_color(True))
address_item[1].setBackground(ColorScheme.BLUE.as_color(True))
if self.wallet.is_beyond_limit(address):
address_item.setBackground(1, ColorScheme.RED.as_color(True))
address_item[1].setBackground(ColorScheme.RED.as_color(True))
# add item
self.addChild(address_item)
count = self.model().rowCount()
self.model().insertRow(count, address_item)
address_idx = self.model().index(count, 2)
if address == current_address:
self.setCurrentItem(address_item)
set_address = QPersistentModelIndex(address_idx)
self.set_current_idx(set_address)

def create_menu(self, position):
from electrum.wallet import Multisig_Wallet
is_multisig = isinstance(self.wallet, Multisig_Wallet)
can_delete = self.wallet.can_delete_address()
selected = self.selectedItems()
selected = self.selected_in_column(1)
multi_select = len(selected) > 1
addrs = [item.text(1) for item in selected]
if not addrs:
return
addrs = [self.model().itemFromIndex(item).text() for item in selected]
if not multi_select:
item = self.itemAt(position)
col = self.currentColumn()
idx = self.indexAt(position)
col = idx.column()
item = self.model().itemFromIndex(idx)
if not item:
return
addr = addrs[0]
if not is_address(addr):
item.setExpanded(not item.isExpanded())
return

menu = QMenu()
if not multi_select:
column_title = self.headerItem().text(col)
copy_text = item.text(col)
addr_column_title = self.model().horizontalHeaderItem(2).text()
addr_idx = idx.sibling(idx.row(), 2)

column_title = self.model().horizontalHeaderItem(col).text()
copy_text = self.model().itemFromIndex(idx).text()
menu.addAction(_("Copy {}").format(column_title), lambda: self.parent.app.clipboard().setText(copy_text))
menu.addAction(_('Details'), lambda: self.parent.show_address(addr))
if col in self.editable_columns:
menu.addAction(_("Edit {}").format(column_title), lambda: self.editItem(item, col))
persistent = QPersistentModelIndex(addr_idx)
menu.addAction(_("Edit {}").format(addr_column_title), lambda p=persistent: self.edit(QModelIndex(p)))
menu.addAction(_("Request payment"), lambda: self.parent.receive_at(addr))
if self.wallet.can_export():
menu.addAction(_("Private key"), lambda: self.parent.show_private_key(addr))
Expand All @@ -189,7 +195,3 @@ def create_menu(self, position):

run_hook('receive_menu', menu, addrs, self.wallet)
menu.exec_(self.viewport().mapToGlobal(position))

def on_permit_edit(self, item, column):
# labels for headings, e.g. "receiving" or "used" should not be editable
return item.childCount() == 0
80 changes: 47 additions & 33 deletions electrum/gui/qt/contact_list.py
Expand Up @@ -34,67 +34,81 @@
from electrum.util import block_explorer_URL
from electrum.plugin import run_hook

from .util import MyTreeWidget, import_meta_gui, export_meta_gui
from .util import MyTreeView, import_meta_gui, export_meta_gui


class ContactList(MyTreeWidget):
class ContactList(MyTreeView):
filter_columns = [0, 1] # Key, Value

def __init__(self, parent):
MyTreeWidget.__init__(self, parent, self.create_menu, [_('Name'), _('Address')], 0, [0])
super().__init__(parent, self.create_menu, stretch_column=0, editable_columns=[0])
self.setModel(QStandardItemModel(self))
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setSortingEnabled(True)
self.update()

def on_permit_edit(self, item, column):
# openalias items shouldn't be editable
return item.text(1) != "openalias"
def on_edited(self, idx, user_role, text):
_type, prior_name = self.parent.contacts.pop(user_role)

def on_edited(self, item, column, prior):
if column == 0: # Remove old contact if renamed
self.parent.contacts.pop(prior)
self.parent.set_contact(item.text(0), item.text(1))
# TODO when min Qt >= 5.11, use siblingAtColumn
col_1_sibling = idx.sibling(idx.row(), 1)
col_1_item = self.model().itemFromIndex(col_1_sibling)

self.parent.set_contact(text, col_1_item.text())

def import_contacts(self):
import_meta_gui(self.parent, _('contacts'), self.parent.contacts.import_file, self.on_update)
import_meta_gui(self.parent, _('contacts'), self.parent.contacts.import_file, self.update)

def export_contacts(self):
export_meta_gui(self.parent, _('contacts'), self.parent.contacts.export_file)

def create_menu(self, position):
menu = QMenu()
selected = self.selectedItems()
if not selected:
selected = self.selected_in_column(0)
selected_keys = []
for idx in selected:
sel_key = self.model().itemFromIndex(idx).data(Qt.UserRole)
selected_keys.append(sel_key)
idx = self.indexAt(position)
if not selected or not idx.isValid():
menu.addAction(_("New contact"), lambda: self.parent.new_contact_dialog())
menu.addAction(_("Import file"), lambda: self.import_contacts())
menu.addAction(_("Export file"), lambda: self.export_contacts())
else:
names = [item.text(0) for item in selected]
keys = [item.text(1) for item in selected]
column = self.currentColumn()
column_title = self.headerItem().text(column)
column_data = '\n'.join([item.text(column) for item in selected])
column = idx.column()
column_title = self.model().horizontalHeaderItem(column).text()
column_data = '\n'.join(self.model().itemFromIndex(idx).text() for idx in selected)
menu.addAction(_("Copy {}").format(column_title), lambda: self.parent.app.clipboard().setText(column_data))
if column in self.editable_columns:
item = self.currentItem()
menu.addAction(_("Edit {}").format(column_title), lambda: self.editItem(item, column))
menu.addAction(_("Pay to"), lambda: self.parent.payto_contacts(keys))
menu.addAction(_("Delete"), lambda: self.parent.delete_contacts(keys))
URLs = [block_explorer_URL(self.config, 'addr', key) for key in filter(is_address, keys)]
item = self.model().itemFromIndex(idx)
if item.isEditable():
# would not be editable if openalias
persistent = QPersistentModelIndex(idx)
menu.addAction(_("Edit {}").format(column_title), lambda p=persistent: self.edit(QModelIndex(p)))
menu.addAction(_("Pay to"), lambda: self.parent.payto_contacts(selected_keys))
menu.addAction(_("Delete"), lambda: self.parent.delete_contacts(selected_keys))
URLs = [block_explorer_URL(self.config, 'addr', key) for key in filter(is_address, selected_keys)]
if URLs:
menu.addAction(_("View on block explorer"), lambda: map(webbrowser.open, URLs))

run_hook('create_contact_menu', menu, selected)
run_hook('create_contact_menu', menu, selected_keys)
menu.exec_(self.viewport().mapToGlobal(position))

def on_update(self):
item = self.currentItem()
current_key = item.data(0, Qt.UserRole) if item else None
self.clear()
def update(self):
current_key = self.current_item_user_role(col=0)
self.model().clear()
self.update_headers([_('Name'), _('Address')])
set_current = None
for key in sorted(self.parent.contacts.keys()):
_type, name = self.parent.contacts[key]
item = QTreeWidgetItem([name, key])
item.setData(0, Qt.UserRole, key)
self.addTopLevelItem(item)
contact_type, name = self.parent.contacts[key]
items = [QStandardItem(x) for x in (name, key)]
items[0].setEditable(contact_type != 'openalias')
items[1].setEditable(False)
items[0].setData(key, Qt.UserRole)
row_count = self.model().rowCount()
self.model().insertRow(row_count, items)
if key == current_key:
self.setCurrentItem(item)
idx = self.model().index(row_count, 0)
set_current = QPersistentModelIndex(idx)
self.set_current_idx(set_current)
run_hook('update_contacts_tab', self)

0 comments on commit bf99ca4

Please sign in to comment.