tcontact_list.py - electrum - Electrum Bitcoin wallet
HTML git clone https://git.parazyd.org/electrum
DIR Log
DIR Files
DIR Refs
DIR Submodules
---
tcontact_list.py (5457B)
---
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2015 Thomas Voegtlin
5 #
6 # Permission is hereby granted, free of charge, to any person
7 # obtaining a copy of this software and associated documentation files
8 # (the "Software"), to deal in the Software without restriction,
9 # including without limitation the rights to use, copy, modify, merge,
10 # publish, distribute, sublicense, and/or sell copies of the Software,
11 # and to permit persons to whom the Software is furnished to do so,
12 # subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 # SOFTWARE.
25
26 from enum import IntEnum
27
28 from PyQt5.QtGui import QStandardItemModel, QStandardItem
29 from PyQt5.QtCore import Qt, QPersistentModelIndex, QModelIndex
30 from PyQt5.QtWidgets import (QAbstractItemView, QMenu)
31
32 from electrum.i18n import _
33 from electrum.bitcoin import is_address
34 from electrum.util import block_explorer_URL
35 from electrum.plugin import run_hook
36
37 from .util import MyTreeView, webopen
38
39
40 class ContactList(MyTreeView):
41
42 class Columns(IntEnum):
43 NAME = 0
44 ADDRESS = 1
45
46 headers = {
47 Columns.NAME: _('Name'),
48 Columns.ADDRESS: _('Address'),
49 }
50 filter_columns = [Columns.NAME, Columns.ADDRESS]
51
52 def __init__(self, parent):
53 super().__init__(parent, self.create_menu,
54 stretch_column=self.Columns.NAME,
55 editable_columns=[self.Columns.NAME])
56 self.setModel(QStandardItemModel(self))
57 self.setSelectionMode(QAbstractItemView.ExtendedSelection)
58 self.setSortingEnabled(True)
59 self.update()
60
61 def on_edited(self, idx, user_role, text):
62 _type, prior_name = self.parent.contacts.pop(user_role)
63 self.parent.set_contact(text, user_role)
64 self.update()
65
66 def create_menu(self, position):
67 menu = QMenu()
68 idx = self.indexAt(position)
69 column = idx.column() or self.Columns.NAME
70 selected_keys = []
71 for s_idx in self.selected_in_column(self.Columns.NAME):
72 sel_key = self.model().itemFromIndex(s_idx).data(Qt.UserRole)
73 selected_keys.append(sel_key)
74 if not selected_keys or not idx.isValid():
75 menu.addAction(_("New contact"), lambda: self.parent.new_contact_dialog())
76 menu.addAction(_("Import file"), lambda: self.parent.import_contacts())
77 menu.addAction(_("Export file"), lambda: self.parent.export_contacts())
78 else:
79 column_title = self.model().horizontalHeaderItem(column).text()
80 column_data = '\n'.join(self.model().itemFromIndex(s_idx).text()
81 for s_idx in self.selected_in_column(column))
82 menu.addAction(_("Copy {}").format(column_title), lambda: self.place_text_on_clipboard(column_data, title=column_title))
83 if column in self.editable_columns:
84 item = self.model().itemFromIndex(idx)
85 if item.isEditable():
86 # would not be editable if openalias
87 persistent = QPersistentModelIndex(idx)
88 menu.addAction(_("Edit {}").format(column_title), lambda p=persistent: self.edit(QModelIndex(p)))
89 menu.addAction(_("Pay to"), lambda: self.parent.payto_contacts(selected_keys))
90 menu.addAction(_("Delete"), lambda: self.parent.delete_contacts(selected_keys))
91 URLs = [block_explorer_URL(self.config, 'addr', key) for key in filter(is_address, selected_keys)]
92 if URLs:
93 menu.addAction(_("View on block explorer"), lambda: [webopen(u) for u in URLs])
94
95 run_hook('create_contact_menu', menu, selected_keys)
96 menu.exec_(self.viewport().mapToGlobal(position))
97
98 def update(self):
99 if self.maybe_defer_update():
100 return
101 current_key = self.current_item_user_role(col=self.Columns.NAME)
102 self.model().clear()
103 self.update_headers(self.__class__.headers)
104 set_current = None
105 for key in sorted(self.parent.contacts.keys()):
106 contact_type, name = self.parent.contacts[key]
107 items = [QStandardItem(x) for x in (name, key)]
108 items[self.Columns.NAME].setEditable(contact_type != 'openalias')
109 items[self.Columns.ADDRESS].setEditable(False)
110 items[self.Columns.NAME].setData(key, Qt.UserRole)
111 row_count = self.model().rowCount()
112 self.model().insertRow(row_count, items)
113 if key == current_key:
114 idx = self.model().index(row_count, self.Columns.NAME)
115 set_current = QPersistentModelIndex(idx)
116 self.set_current_idx(set_current)
117 # FIXME refresh loses sort order; so set "default" here:
118 self.sortByColumn(self.Columns.NAME, Qt.AscendingOrder)
119 self.filter()
120 run_hook('update_contacts_tab', self)