listmodels self-initialize,
lazy-load listmodels in QEWallet, process channel updates in qechannellistmodel
This commit is contained in:
@@ -169,5 +169,4 @@ Pane {
|
||||
}
|
||||
}
|
||||
|
||||
Component.onCompleted: Daemon.currentWallet.addressModel.init_model()
|
||||
}
|
||||
|
||||
@@ -102,5 +102,4 @@ Pane {
|
||||
|
||||
}
|
||||
|
||||
Component.onCompleted: Daemon.currentWallet.channelModel.init_model()
|
||||
}
|
||||
|
||||
@@ -10,9 +10,7 @@ class QEAddressListModel(QAbstractListModel):
|
||||
def __init__(self, wallet, parent=None):
|
||||
super().__init__(parent)
|
||||
self.wallet = wallet
|
||||
self.receive_addresses = []
|
||||
self.change_addresses = []
|
||||
|
||||
self.init_model()
|
||||
|
||||
_logger = get_logger(__name__)
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject
|
||||
from PyQt5.QtCore import Qt, QAbstractListModel, QModelIndex
|
||||
|
||||
from electrum.logging import get_logger
|
||||
from electrum.util import Satoshis, TxMinedInfo
|
||||
from electrum.util import Satoshis, register_callback
|
||||
|
||||
from .qetypes import QEAmount
|
||||
|
||||
@@ -12,7 +12,16 @@ class QEChannelListModel(QAbstractListModel):
|
||||
def __init__(self, wallet, parent=None):
|
||||
super().__init__(parent)
|
||||
self.wallet = wallet
|
||||
self.channels = []
|
||||
self.init_model()
|
||||
|
||||
interests = ['channel', 'channels_updated', 'gossip_peers',
|
||||
'ln_gossip_sync_progress', 'unknown_channels',
|
||||
'channel_db', 'gossip_db_loaded']
|
||||
# To avoid leaking references to "self" that prevent the
|
||||
# window from being GC-ed when closed, callbacks should be
|
||||
# methods of this class only, and specifically not be
|
||||
# partials, lambdas or methods of subobjects. Hence...
|
||||
register_callback(self.on_network, interests)
|
||||
|
||||
_logger = get_logger(__name__)
|
||||
|
||||
@@ -48,6 +57,7 @@ class QEChannelListModel(QAbstractListModel):
|
||||
def channel_to_model(self, lnc):
|
||||
lnworker = self.wallet.lnworker
|
||||
item = {}
|
||||
item['channel_id'] = lnc.channel_id
|
||||
item['node_alias'] = lnworker.get_node_alias(lnc.node_id) or lnc.node_id.hex()
|
||||
item['short_cid'] = lnc.short_id_for_GUI()
|
||||
item['state'] = lnc.get_state_for_GUI()
|
||||
@@ -57,6 +67,7 @@ class QEChannelListModel(QAbstractListModel):
|
||||
|
||||
@pyqtSlot()
|
||||
def init_model(self):
|
||||
self._logger.debug('init_model')
|
||||
if not self.wallet.lnworker:
|
||||
self._logger.warning('lnworker should be defined')
|
||||
return
|
||||
@@ -73,3 +84,31 @@ class QEChannelListModel(QAbstractListModel):
|
||||
self.beginInsertRows(QModelIndex(), 0, len(channels) - 1)
|
||||
self.channels = channels
|
||||
self.endInsertRows()
|
||||
|
||||
def on_network(self, event, *args):
|
||||
if event == 'channel':
|
||||
wallet, channel = args
|
||||
if wallet == self.wallet:
|
||||
self.on_channel_updated(channel)
|
||||
elif event == 'channels_updated':
|
||||
wallet, = args
|
||||
if wallet == self.wallet:
|
||||
self.init_model() # TODO: remove/add less crude than full re-init
|
||||
else:
|
||||
self._logger.debug('unhandled event %s: %s' % (event, repr(args)))
|
||||
|
||||
def on_channel_updated(self, channel):
|
||||
i = 0
|
||||
for c in self.channels:
|
||||
if c['channel_id'] == channel.channel_id:
|
||||
self.do_update(i,channel)
|
||||
break
|
||||
i = i + 1
|
||||
|
||||
def do_update(self, modelindex, channel):
|
||||
modelitem = self.channels[modelindex]
|
||||
self._logger.debug(repr(modelitem))
|
||||
modelitem.update(self.channel_to_model(channel))
|
||||
|
||||
mi = self.createIndex(modelindex, 0)
|
||||
self.dataChanged.emit(mi, mi, self._ROLE_KEYS)
|
||||
|
||||
@@ -15,7 +15,7 @@ class QEAbstractInvoiceListModel(QAbstractListModel):
|
||||
def __init__(self, wallet, parent=None):
|
||||
super().__init__(parent)
|
||||
self.wallet = wallet
|
||||
self.invoices = []
|
||||
self.init_model()
|
||||
|
||||
# define listmodel rolemap
|
||||
_ROLE_NAMES=('key','is_lightning','timestamp','date','message','amount','status','status_str','address','expiration','type')
|
||||
|
||||
@@ -12,7 +12,7 @@ class QETransactionListModel(QAbstractListModel):
|
||||
def __init__(self, wallet, parent=None):
|
||||
super().__init__(parent)
|
||||
self.wallet = wallet
|
||||
self.tx_history = []
|
||||
self.init_model()
|
||||
|
||||
_logger = get_logger(__name__)
|
||||
|
||||
|
||||
@@ -57,16 +57,12 @@ class QEWallet(QObject):
|
||||
super().__init__(parent)
|
||||
self.wallet = wallet
|
||||
|
||||
self._historyModel = QETransactionListModel(wallet)
|
||||
self._addressModel = QEAddressListModel(wallet)
|
||||
self._requestModel = QERequestListModel(wallet)
|
||||
self._invoiceModel = QEInvoiceListModel(wallet)
|
||||
self._historyModel = None
|
||||
self._addressModel = None
|
||||
self._requestModel = None
|
||||
self._invoiceModel = None
|
||||
self._channelModel = None
|
||||
|
||||
self._historyModel.init_model()
|
||||
self._requestModel.init_model()
|
||||
self._invoiceModel.init_model()
|
||||
|
||||
self.tx_notification_queue = queue.Queue()
|
||||
self.tx_notification_last_time = 0
|
||||
|
||||
@@ -125,6 +121,9 @@ class QEWallet(QObject):
|
||||
if wallet == self.wallet:
|
||||
self._logger.debug('wallet %s updated' % str(wallet))
|
||||
self.balanceChanged.emit()
|
||||
elif event in ['channel','channels_updated']:
|
||||
# TODO update balance/can-spend etc
|
||||
pass
|
||||
else:
|
||||
self._logger.debug('unhandled event: %s %s' % (event, str(args)))
|
||||
|
||||
@@ -177,21 +176,29 @@ class QEWallet(QObject):
|
||||
historyModelChanged = pyqtSignal()
|
||||
@pyqtProperty(QETransactionListModel, notify=historyModelChanged)
|
||||
def historyModel(self):
|
||||
if self._historyModel is None:
|
||||
self._historyModel = QETransactionListModel(self.wallet)
|
||||
return self._historyModel
|
||||
|
||||
addressModelChanged = pyqtSignal()
|
||||
@pyqtProperty(QEAddressListModel, notify=addressModelChanged)
|
||||
def addressModel(self):
|
||||
if self._addressModel is None:
|
||||
self._addressModel = QEAddressListModel(self.wallet)
|
||||
return self._addressModel
|
||||
|
||||
requestModelChanged = pyqtSignal()
|
||||
@pyqtProperty(QERequestListModel, notify=requestModelChanged)
|
||||
def requestModel(self):
|
||||
if self._requestModel is None:
|
||||
self._requestModel = QERequestListModel(self.wallet)
|
||||
return self._requestModel
|
||||
|
||||
invoiceModelChanged = pyqtSignal()
|
||||
@pyqtProperty(QEInvoiceListModel, notify=invoiceModelChanged)
|
||||
def invoiceModel(self):
|
||||
if self._invoiceModel is None:
|
||||
self._invoiceModel = QEInvoiceListModel(self.wallet)
|
||||
return self._invoiceModel
|
||||
|
||||
channelModelChanged = pyqtSignal()
|
||||
|
||||
Reference in New Issue
Block a user