-WIP-electrum-btcp/gui/qt/history_list.py

201 lines
7.8 KiB
Python
Raw Normal View History

2015-04-04 11:59:57 -07:00
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2015 Thomas Voegtlin
#
2016-02-23 02:36:42 -08:00
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
2015-04-04 11:59:57 -07:00
#
2016-02-23 02:36:42 -08:00
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
2015-04-04 11:59:57 -07:00
#
2016-02-23 02:36:42 -08:00
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2015-04-04 11:59:57 -07:00
import webbrowser
2017-01-22 10:25:24 -08:00
from .util import *
2015-04-04 11:59:57 -07:00
from electrum.i18n import _
from electrum.util import block_explorer_URL
from electrum.util import timestamp_to_datetime, profiler
2015-04-04 11:59:57 -07:00
TX_ICONS = [
"warning.png",
"warning.png",
"warning.png",
"unconfirmed.png",
"unconfirmed.png",
2018-01-23 14:50:02 -08:00
"offline_tx.png",
"clock1.png",
"clock2.png",
"clock3.png",
"clock4.png",
"clock5.png",
"confirmed.png",
]
class HistoryList(MyTreeWidget):
2017-02-16 08:10:02 -08:00
filter_columns = [2, 3, 4] # Date, Description, Amount
2015-04-04 11:59:57 -07:00
def __init__(self, parent=None):
MyTreeWidget.__init__(self, parent, self.create_menu, [], 3)
self.refresh_headers()
self.setColumnHidden(1, True)
2015-04-04 11:59:57 -07:00
def refresh_headers(self):
headers = ['', '', _('Date'), _('Description') , _('Amount'), _('Balance')]
2017-03-05 12:10:30 -08:00
fx = self.parent.fx
if fx and fx.show_history():
headers.extend(['%s '%fx.ccy + _('Amount'), '%s '%fx.ccy + _('Balance')])
self.update_headers(headers)
def get_domain(self):
'''Replaced in address_dialog.py'''
return self.wallet.get_addresses()
@profiler
def on_update(self):
2015-04-04 11:59:57 -07:00
self.wallet = self.parent.wallet
h = self.wallet.get_history(self.get_domain())
2015-04-04 11:59:57 -07:00
item = self.currentItem()
2017-01-30 01:36:56 -08:00
current_tx = item.data(0, Qt.UserRole) if item else None
2015-04-04 11:59:57 -07:00
self.clear()
fx = self.parent.fx
2017-03-05 12:10:30 -08:00
if fx: fx.history_used_spot = False
2016-05-28 06:51:00 -07:00
for h_item in h:
tx_hash, height, conf, timestamp, value, balance = h_item
status, status_str = self.wallet.get_tx_status(tx_hash, height, conf, timestamp)
has_invoice = self.wallet.invoices.paid.get(tx_hash)
icon = QIcon(":icons/" + TX_ICONS[status])
2015-04-04 11:59:57 -07:00
v_str = self.parent.format_amount(value, True, whitespaces=True)
balance_str = self.parent.format_amount(balance, whitespaces=True)
2015-12-15 03:52:30 -08:00
label = self.wallet.get_label(tx_hash)
entry = ['', tx_hash, status_str, label, v_str, balance_str]
2017-03-05 12:10:30 -08:00
if fx and fx.show_history():
date = timestamp_to_datetime(time.time() if conf <= 0 else timestamp)
for amount in [value, balance]:
text = fx.historical_value_str(amount, date)
entry.append(text)
item = QTreeWidgetItem(entry)
item.setIcon(0, icon)
item.setToolTip(0, str(conf) + " confirmation" + ("s" if conf != 1 else ""))
if has_invoice:
item.setIcon(3, QIcon(":icons/seal"))
2015-09-05 00:11:48 -07:00
for i in range(len(entry)):
if i>3:
item.setTextAlignment(i, Qt.AlignRight)
2015-09-05 00:24:53 -07:00
if i!=2:
item.setFont(i, QFont(MONOSPACE_FONT))
2017-09-02 21:25:50 -07:00
if value and value < 0:
item.setForeground(3, QBrush(QColor("#BC1E1E")))
item.setForeground(4, QBrush(QColor("#BC1E1E")))
2015-04-04 11:59:57 -07:00
if tx_hash:
item.setData(0, Qt.UserRole, tx_hash)
self.insertTopLevelItem(0, item)
if current_tx == tx_hash:
self.setCurrentItem(item)
def on_doubleclick(self, item, column):
if self.permit_edit(item, column):
super(HistoryList, self).on_doubleclick(item, column)
else:
2017-08-27 20:27:15 -07:00
tx_hash = item.data(0, Qt.UserRole)
tx = self.wallet.transactions.get(tx_hash)
self.parent.show_transaction(tx)
2016-10-10 01:32:47 -07:00
def update_labels(self):
root = self.invisibleRootItem()
child_count = root.childCount()
for i in range(child_count):
item = root.child(i)
2017-01-30 01:36:56 -08:00
txid = item.data(0, Qt.UserRole)
2016-10-10 01:32:47 -07:00
label = self.wallet.get_label(txid)
item.setText(3, label)
def update_item(self, tx_hash, height, conf, timestamp):
status, status_str = self.wallet.get_tx_status(tx_hash, height, conf, timestamp)
icon = QIcon(":icons/" + TX_ICONS[status])
items = self.findItems(tx_hash, Qt.UserRole|Qt.MatchContains|Qt.MatchRecursive, column=1)
if items:
item = items[0]
item.setIcon(0, icon)
2016-05-30 10:52:22 -07:00
item.setText(2, status_str)
2015-04-04 11:59:57 -07:00
def create_menu(self, position):
self.selectedIndexes()
item = self.currentItem()
if not item:
return
column = self.currentColumn()
2017-01-30 01:36:56 -08:00
tx_hash = item.data(0, Qt.UserRole)
2015-04-04 11:59:57 -07:00
if not tx_hash:
return
if column is 0:
column_title = "ID"
column_data = tx_hash
else:
column_title = self.headerItem().text(column)
column_data = item.text(column)
tx_URL = block_explorer_URL(self.config, 'tx', tx_hash)
height, conf, timestamp = self.wallet.get_tx_height(tx_hash)
2016-05-20 01:38:48 -07:00
tx = self.wallet.transactions.get(tx_hash)
is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(tx)
is_unconfirmed = height <= 0
pr_key = self.wallet.invoices.paid.get(tx_hash)
2015-04-04 11:59:57 -07:00
menu = QMenu()
if height == -2:
menu.addAction(_("Remove"), lambda: self.remove_local_tx(tx_hash))
menu.addAction(_("Copy %s")%column_title, lambda: self.parent.app.clipboard().setText(column_data))
if column in self.editable_columns:
menu.addAction(_("Edit %s")%column_title, lambda: self.editItem(item, column))
2016-05-20 01:38:48 -07:00
menu.addAction(_("Details"), lambda: self.parent.show_transaction(tx))
if is_unconfirmed and tx:
rbf = is_mine and not tx.is_final()
if rbf:
menu.addAction(_("Increase fee"), lambda: self.parent.bump_fee_dialog(tx))
else:
child_tx = self.wallet.cpfp(tx, 0)
if child_tx:
menu.addAction(_("Child pays for parent"), lambda: self.parent.cpfp(tx, child_tx))
if pr_key:
menu.addAction(QIcon(":icons/seal"), _("View invoice"), lambda: self.parent.show_invoice(pr_key))
2016-05-20 01:38:48 -07:00
if tx_URL:
menu.addAction(_("View on block explorer"), lambda: webbrowser.open(tx_URL))
2015-04-04 11:59:57 -07:00
menu.exec_(self.viewport().mapToGlobal(position))
def remove_local_tx(self, tx_hash):
answer = QMessageBox.question(self.parent,
_("Please confirm"),
_("Are you sure you want to remove this transaction?"),
QMessageBox.Yes,
QMessageBox.No)
if answer == QMessageBox.No:
return
self.wallet.remove_transaction(tx_hash)
root = self.invisibleRootItem()
child_count = root.childCount()
for i in range(child_count):
item = root.child(i)
if item.data(0, Qt.UserRole) == tx_hash:
root.removeChild(item)
return