From d69ed7a20412e9c4323e2321dfc186b3ea42deed Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Wed, 1 Jun 2022 16:57:07 +0200 Subject: [PATCH] initial channel list model and delegate --- electrum/gui/qml/components/Channels.qml | 30 +------ .../components/controls/ChannelDelegate.qml | 80 +++++++++++++++++++ electrum/gui/qml/qechannellistmodel.py | 38 +++++++-- electrum/gui/qml/qetransactionlistmodel.py | 2 - 4 files changed, 112 insertions(+), 38 deletions(-) create mode 100644 electrum/gui/qml/components/controls/ChannelDelegate.qml diff --git a/electrum/gui/qml/components/Channels.qml b/electrum/gui/qml/components/Channels.qml index 1939c6736..f4a88547f 100644 --- a/electrum/gui/qml/components/Channels.qml +++ b/electrum/gui/qml/components/Channels.qml @@ -89,36 +89,10 @@ Pane { Layout.preferredWidth: parent.width Layout.fillHeight: true clip: true - model: 3 //Daemon.currentWallet.channelsModel + model: Daemon.currentWallet.channelModel - delegate: ItemDelegate { - width: ListView.view.width - height: row.height + delegate: ChannelDelegate { highlighted: ListView.isCurrentItem - - font.pixelSize: constants.fontSizeMedium // set default font size for child controls - - RowLayout { - id: row - spacing: 10 - x: constants.paddingSmall - width: parent.width - 2 * constants.paddingSmall - - Image { - id: walleticon - source: "../../icons/lightning.png" - fillMode: Image.PreserveAspectFit - Layout.preferredWidth: constants.iconSizeLarge - Layout.preferredHeight: constants.iconSizeLarge - } - - Label { - font.pixelSize: constants.fontSizeLarge - text: index - Layout.fillWidth: true - } - - } } ScrollIndicator.vertical: ScrollIndicator { } diff --git a/electrum/gui/qml/components/controls/ChannelDelegate.qml b/electrum/gui/qml/components/controls/ChannelDelegate.qml new file mode 100644 index 000000000..a9d46da4b --- /dev/null +++ b/electrum/gui/qml/components/controls/ChannelDelegate.qml @@ -0,0 +1,80 @@ +import QtQuick 2.6 +import QtQuick.Controls 2.0 +import QtQuick.Layouts 1.0 +import QtQuick.Controls.Material 2.0 + +ItemDelegate { + id: root + height: item.height + width: ListView.view.width + + font.pixelSize: constants.fontSizeSmall // set default font size for child controls + + GridLayout { + id: item + + anchors { + left: parent.left + right: parent.right + leftMargin: constants.paddingSmall + rightMargin: constants.paddingSmall + } + + columns: 2 + + Rectangle { + Layout.columnSpan: 2 + Layout.fillWidth: true + Layout.preferredHeight: constants.paddingTiny + color: 'transparent' + } + + Image { + id: walleticon + source: "../../../icons/lightning.png" + fillMode: Image.PreserveAspectFit + Layout.rowSpan: 2 + Layout.preferredWidth: constants.iconSizeLarge + Layout.preferredHeight: constants.iconSizeLarge + } + + RowLayout { + Layout.fillWidth: true + Label { + Layout.fillWidth: true + text: model.node_alias + elide: Text.ElideRight + wrapMode: Text.Wrap + maximumLineCount: 2 + } + + Label { + text: model.state + } + } + + RowLayout { + Layout.fillWidth: true + Label { + Layout.fillWidth: true + text: model.short_cid + } + + Label { + text: Config.formatSats(model.capacity) + } + + Label { + text: Config.baseUnit + } + } + + Rectangle { + Layout.columnSpan: 2 + Layout.fillWidth: true + Layout.preferredHeight: constants.paddingTiny + color: 'transparent' + } + + } +} diff --git a/electrum/gui/qml/qechannellistmodel.py b/electrum/gui/qml/qechannellistmodel.py index 531ca0203..27910378c 100644 --- a/electrum/gui/qml/qechannellistmodel.py +++ b/electrum/gui/qml/qechannellistmodel.py @@ -19,35 +19,57 @@ class QEChannelListModel(QAbstractListModel): # define listmodel rolemap _ROLE_NAMES=('cid','state','initiator','capacity','can_send','can_receive', 'l_csv_delat','r_csv_delay','send_frozen','receive_frozen', - 'type','node_id','funding_tx') + 'type','node_id','node_alias','short_cid','funding_tx') _ROLE_KEYS = range(Qt.UserRole, Qt.UserRole + len(_ROLE_NAMES)) _ROLE_MAP = dict(zip(_ROLE_KEYS, [bytearray(x.encode()) for x in _ROLE_NAMES])) _ROLE_RMAP = dict(zip(_ROLE_NAMES, _ROLE_KEYS)) def rowCount(self, index): - return len(self.tx_history) + return len(self.channels) def roleNames(self): return self._ROLE_MAP def data(self, index, role): - tx = self.tx_history[index.row()] + tx = self.channels[index.row()] role_index = role - Qt.UserRole value = tx[self._ROLE_NAMES[role_index]] if isinstance(value, (bool, list, int, str, QEAmount)) or value is None: return value if isinstance(value, Satoshis): return value.value - if isinstance(value, QEAmount): - return value return str(value) + def clear(self): + self.beginResetModel() + self.channels = [] + self.endResetModel() + + def channel_to_model(self, lnc): + lnworker = self.wallet.lnworker + item = {} + 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() + item['capacity'] = QEAmount(amount_sat=lnc.get_capacity()) + self._logger.debug(repr(item)) + return item + @pyqtSlot() def init_model(self): if not self.wallet.lnworker: self._logger.warning('lnworker should be defined') return - channels = self.wallet.lnworker.channels - self._logger.debug(repr(channels)) - #channels = list(lnworker.channels.values()) if lnworker else [] + channels = [] + + lnchannels = self.wallet.lnworker.channels + for channel in lnchannels.values(): + self._logger.debug(repr(channel)) + item = self.channel_to_model(channel) + channels.append(item) + + self.clear() + self.beginInsertRows(QModelIndex(), 0, len(channels) - 1) + self.channels = channels + self.endInsertRows() diff --git a/electrum/gui/qml/qetransactionlistmodel.py b/electrum/gui/qml/qetransactionlistmodel.py index 71d474afd..85574fce2 100644 --- a/electrum/gui/qml/qetransactionlistmodel.py +++ b/electrum/gui/qml/qetransactionlistmodel.py @@ -38,8 +38,6 @@ class QETransactionListModel(QAbstractListModel): return value if isinstance(value, Satoshis): return value.value - if isinstance(value, QEAmount): - return value return str(value) def clear(self):