1
0

integrate channels_list with existing framework

This commit is contained in:
ThomasV
2018-06-03 10:07:56 +02:00
parent 4fe912f4b3
commit bf6d28e1f0
5 changed files with 100 additions and 316 deletions

76
gui/qt/channels_list.py Normal file
View File

@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtWidgets
from electrum.util import inv_dict, bh2u
from electrum.i18n import _
from .util import MyTreeWidget, SortableTreeWidgetItem
class ChannelsList(MyTreeWidget):
update_rows = QtCore.pyqtSignal(list)
update_single_row = QtCore.pyqtSignal(dict)
def __init__(self, parent):
MyTreeWidget.__init__(self, parent, self.create_menu, [_('Node ID'), _('Capacity'), _('Balance')], 0)
self.main_window = parent
self.update_rows.connect(self.do_update_rows)
self.update_single_row.connect(self.do_update_single_row)
def format_fields(self, chan):
return [bh2u(chan.node_id), self.parent.format_amount(chan.constraints.capacity), self.parent.format_amount(chan.local_state.amount_msat//1000)]
def create_menu(self, position):
menu = QtWidgets.QMenu()
cur = self.currentItem()
def close():
print("closechannel result", self.parent.network.lnworker.close_channel_from_other_thread(cur.di))
menu.addAction(_("Close channel"), close)
menu.exec_(self.viewport().mapToGlobal(position))
@QtCore.pyqtSlot(dict)
def do_update_single_row(self, chan):
items = self.findItems(chan.channel_id, QtCore.Qt.UserRole|QtCore.Qt.MatchContains|QtCore.Qt.MatchRecursive, column=1)
for item in items:
for i, v in enumerate(self.format_fields(chan)):
item.setData(i, QtCore.Qt.DisplayRole, v)
@QtCore.pyqtSlot(list)
def do_update_rows(self, channels):
self.clear()
for chan in channels:
item = SortableTreeWidgetItem(self.format_fields(chan))
item.setData(0, QtCore.Qt.UserRole, chan.channel_id)
self.insertTopLevelItem(0, item)
def get_toolbar(self):
nodeid_inp = QtWidgets.QLineEdit(self)
local_amt_inp = QtWidgets.QLineEdit(self, text='200000')
push_amt_inp = QtWidgets.QLineEdit(self, text='0')
button = QtWidgets.QPushButton(_('Open channel'), self)
button.clicked.connect(lambda: self.main_window.protect(self.open_channel, (nodeid_inp, local_amt_inp, push_amt_inp)))
l=QtWidgets.QVBoxLayout(self)
h=QtWidgets.QGridLayout(self)
nodeid_label = QtWidgets.QLabel(self)
nodeid_label.setText(_("Node ID"))
local_amt_label = QtWidgets.QLabel(self)
local_amt_label.setText("Local amount (sat)")
push_amt_label = QtWidgets.QLabel(self)
push_amt_label.setText("Push amount (sat)")
h.addWidget(nodeid_label, 0, 0)
h.addWidget(local_amt_label, 0, 1)
h.addWidget(push_amt_label, 0, 2)
h.addWidget(nodeid_inp, 1, 0)
h.addWidget(local_amt_inp, 1, 1)
h.addWidget(push_amt_inp, 1, 2)
h.addWidget(button, 1, 3)
h.setColumnStretch(0, 3)
h.setColumnStretch(1, 1)
h.setColumnStretch(2, 1)
h.setColumnStretch(3, 1)
return h
def open_channel(self, nodeIdInput, local_amt_inp, push_amt_inp, password):
node_id = str(nodeIdInput.text())
local_amt = int(local_amt_inp.text())
push_amt = int(push_amt_inp.text())
assert local_amt >= 200000
assert local_amt >= push_amt
obj = self.parent.network.lnworker.open_channel(node_id, local_amt, push_amt, password)

View File

@@ -1,124 +0,0 @@
# -*- coding: utf-8 -*-
import binascii, base64
from PyQt5 import QtCore, QtWidgets
from collections import OrderedDict
import logging
import traceback
# https://api.lightning.community/#listchannels
mapping = {0: "chan_id"}
revMapp = {"chan_id": 0}
datatable = OrderedDict([])
class MyTableRow(QtWidgets.QTreeWidgetItem):
def __init__(self, di):
strs = [str(di[mapping[key]]) for key in range(len(mapping))]
super(MyTableRow, self).__init__(strs)
assert isinstance(di, dict)
self.di = di
def __getitem__(self, idx):
return self.di[idx]
def __setitem__(self, idx, val):
self.di[idx] = val
try:
self.setData(revMapp[idx], QtCore.Qt.DisplayRole, '{0}'.format(val))
except KeyError:
logging.warning("Lightning Channel field %s unknown", idx)
def __str__(self):
return str(self.di)
def addChannelRow(new):
made = MyTableRow(new)
datatable[new["chan_id"]] = made
datatable.move_to_end(new["chan_id"], last=False)
return made
class LightningChannelsList(QtWidgets.QWidget):
update_rows = QtCore.pyqtSignal(dict)
update_single_row = QtCore.pyqtSignal(dict)
def open_channel(self, nodeIdInput, local_amt_inp, push_amt_inp, password):
node_id = str(nodeIdInput.text())
print("creating channel with {}".format(node_id))
local_amt = int(local_amt_inp.text())
push_amt = int(push_amt_inp.text())
assert local_amt >= 200000
assert local_amt >= push_amt
obj = self.lnworker.open_channel(node_id, local_amt, push_amt, password)
def create_menu(self, position):
menu = QtWidgets.QMenu()
cur = self._tv.currentItem()
def close():
print("closechannel result", lnworker.close_channel_from_other_thread(cur.di))
menu.addAction("Close channel", close)
menu.exec_(self._tv.viewport().mapToGlobal(position))
@QtCore.pyqtSlot(dict)
def do_update_single_row(self, new):
try:
obj = datatable[new["chan_id"]]
except KeyError:
print("lightning chan_id {} unknown!".format(new["chan_id"]))
else:
for k, v in new.items():
try:
if obj[k] != v: obj[k] = v
except KeyError:
obj[k] = v
@QtCore.pyqtSlot(dict)
def do_update_rows(self, obj):
self._tv.clear()
for i in obj["channels"]:
self._tv.insertTopLevelItem(0, addChannelRow(i))
def __init__(self, parent, lnworker):
QtWidgets.QWidget.__init__(self, parent)
self.main_window = parent
self.update_rows.connect(self.do_update_rows)
self.update_single_row.connect(self.do_update_single_row)
self.lnworker = lnworker
lnworker.register_callback(self.update_rows.emit, ['channels_updated'])
lnworker.register_callback(self.update_single_row.emit, ['channel_updated'])
self._tv=QtWidgets.QTreeWidget(self)
self._tv.setHeaderLabels([mapping[i] for i in range(len(mapping))])
self._tv.setColumnCount(len(mapping))
self._tv.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self._tv.customContextMenuRequested.connect(self.create_menu)
nodeid_inp = QtWidgets.QLineEdit(self)
local_amt_inp = QtWidgets.QLineEdit(self, text='200000')
push_amt_inp = QtWidgets.QLineEdit(self, text='0')
button = QtWidgets.QPushButton('Open channel', self)
button.clicked.connect(lambda: self.main_window.protect(self.open_channel, (nodeid_inp, local_amt_inp, push_amt_inp)))
l=QtWidgets.QVBoxLayout(self)
h=QtWidgets.QGridLayout(self)
nodeid_label = QtWidgets.QLabel(self)
nodeid_label.setText("Node ID")
local_amt_label = QtWidgets.QLabel(self)
local_amt_label.setText("Local amount (sat)")
push_amt_label = QtWidgets.QLabel(self)
push_amt_label.setText("Push amount (sat)")
h.addWidget(nodeid_label, 0, 0)
h.addWidget(local_amt_label, 0, 1)
h.addWidget(push_amt_label, 0, 2)
h.addWidget(nodeid_inp, 1, 0)
h.addWidget(local_amt_inp, 1, 1)
h.addWidget(push_amt_inp, 1, 2)
h.addWidget(button, 1, 3)
h.setColumnStretch(0, 3)
h.setColumnStretch(1, 1)
h.setColumnStretch(2, 1)
h.setColumnStretch(3, 1)
l.addLayout(h)
l.addWidget(self._tv)
self.resize(2500,1000)
lnworker.on_channels_updated()

View File

@@ -1,153 +0,0 @@
# -*- coding: utf-8 -*-
import base64
import binascii
from PyQt5 import QtCore, QtWidgets
from collections import OrderedDict
import logging
from .qrcodewidget import QRDialog
from PyQt5.QtCore import pyqtSignal, pyqtSlot
mapping = {0: "r_hash", 1: "pay_req", 2: "settled"}
revMapp = {"r_hash": 0, "pay_req": 1, "settled": 2}
datatable = OrderedDict([])
idx = 0
class MyTableRow(QtWidgets.QTreeWidgetItem):
def __init__(self, di):
if "settled" not in di:
di["settled"] = False
strs = [str(di[mapping[key]]) for key in range(len(mapping))]
print(strs)
super(MyTableRow, self).__init__(strs)
assert isinstance(di, dict)
self.di = di
def __getitem__(self, idx):
return self.di[idx]
def __setitem__(self, idx, val):
self.di[idx] = val
try:
self.setData(revMapp[idx], QtCore.Qt.DisplayRole, '{0}'.format(val))
except KeyError:
logging.warning("Lightning Invoice field %s unknown", idx)
def __str__(self):
return str(self.di)
def addInvoiceRow(new):
made = MyTableRow(new)
datatable[new["r_hash"]] = made
datatable.move_to_end(new["r_hash"], last=False)
return made
class LightningInvoiceList(QtWidgets.QWidget):
invoice_added_signal = QtCore.pyqtSignal(dict)
@QtCore.pyqtSlot(dict)
def invoice_added_handler(self, di):
self._tv.insertTopLevelItem(0, addInvoiceRow(invoice))
def clickHandler(self, numInput, treeView, lnworker):
amt = numInput.value()
if amt < 1:
print("value too small")
return
print("creating invoice with value {}".format(amt))
global idx
#obj = {
# "r_hash": binascii.hexlify((int.from_bytes(bytearray.fromhex("9500edb0994b7bc23349193486b25c82097045db641f35fa988c0e849acdec29"), "big")+idx).to_bytes(byteorder="big", length=32)).decode("ascii"),
# "pay_req": "lntb81920n1pdf258s" + str(idx),
# "settled": False
#}
#treeView.insertTopLevelItem(0, addInvoiceRow(obj))
idx += 1
lnworker.add_invoice(amt)
def create_menu(self, position):
menu = QtWidgets.QMenu()
pay_req = self._tv.currentItem()["pay_req"]
cb = QtWidgets.QApplication.instance().clipboard()
def copy():
print(pay_req)
cb.setText(pay_req)
def qr():
d = QRDialog(pay_req, self, "Lightning invoice")
d.exec_()
menu.addAction("Copy payment request", copy)
menu.addAction("Show payment request as QR code", qr)
menu.exec_(self._tv.viewport().mapToGlobal(position))
payment_received_signal = pyqtSignal(dict)
@pyqtSlot(dict)
def paymentReceived(self, new):
try:
obj = datatable[new["r_hash"]]
except KeyError:
print("lightning payment invoice r_hash {} unknown!".format(new["r_hash"]))
else:
for k, v in new.items():
try:
if obj[k] != v: obj[k] = v
except KeyError:
obj[k] = v
def __init__(self, parent, lnworker):
QtWidgets.QWidget.__init__(self, parent)
self.payment_received_signal.connect(self.paymentReceived)
self.invoice_added_signal.connect(self.invoice_added_handler)
#lnworker.subscribe_payment_received_from_other_thread(self.payment_received_signal.emit)
#lnworker.subscribe_invoice_added_from_other_thread(self.invoice_added_signal.emit)
self._tv=QtWidgets.QTreeWidget(self)
self._tv.setHeaderLabels([mapping[i] for i in range(len(mapping))])
self._tv.setColumnCount(len(mapping))
self._tv.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self._tv.customContextMenuRequested.connect(self.create_menu)
class SatoshiCountSpinBox(QtWidgets.QSpinBox):
def keyPressEvent(self2, e):
super(SatoshiCountSpinBox, self2).keyPressEvent(e)
if QtCore.Qt.Key_Return == e.key():
self.clickHandler(self2, self._tv, lnworker)
numInput = SatoshiCountSpinBox(self)
button = QtWidgets.QPushButton('Add invoice', self)
button.clicked.connect(lambda: self.clickHandler(numInput, self._tv, lnworker))
l=QtWidgets.QVBoxLayout(self)
h=QtWidgets.QGridLayout(self)
h.addWidget(numInput, 0, 0)
h.addWidget(button, 0, 1)
#h.addItem(QtWidgets.QSpacerItem(100, 200, QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred), 0, 2)
#h.setSizePolicy(
h.setColumnStretch(0, 1)
h.setColumnStretch(1, 1)
h.setColumnStretch(2, 2)
l.addLayout(h)
l.addWidget(self._tv)
self.resize(2500,1000)
def tick():
key = "9500edb0994b7bc23349193486b25c82097045db641f35fa988c0e849acdec29"
if not key in datatable:
return
row = datatable[key]
row["settled"] = not row["settled"]
print("data changed")
if __name__=="__main__":
from sys import argv, exit
a=QtWidgets.QApplication(argv)
w=LightningInvoiceList()
w.show()
w.raise_()
timer = QtCore.QTimer()
timer.timeout.connect(tick)
timer.start(1000)
exit(a.exec_())