Merge bitcoin-core/gui#161: Add PeerTableModel::StatsRole to prevent data layer violation

b3e9bcaac8 qt, refactor: Drop no longer used PeerTableModel::getNodeStats function (Hennadii Stepanov)
49c604077c qt: Use PeerTableModel::StatsRole (Hennadii Stepanov)
35007edf9c qt: Add PeerTableModel::StatsRole (Hennadii Stepanov)

Pull request description:

  This PR allows to access to the `CNodeCombinedStats` instance directly from any view object.

  The `PeerTableModel::getNodeStats` member function removed as a kind of layer violation.

  No behavior changes.

  Also other pulls (bugfixes) are based on this one: #18 and #164.

ACKs for top commit:
  jonatack:
    Tested re-ACK b3e9bcaac8 per `git range-diff ae8f797 4c05fe0 b3e9bca`
  promag:
    Code review ACK b3e9bcaac8.
  jonasschnelli:
    utACK b3e9bcaac8

Tree-SHA512: 6ba50d5dd2c0373655d491ce8b130c47d598da2db5ff4b00633f447404c7e70f8562ead53ddf166e851384d9632ff9146a770c99845c2cdd3ff7250677e4c130
pull/826/head
Jonas Schnelli 4 years ago
commit 616eace02a
No known key found for this signature in database
GPG Key ID: 1EB776BB03C7922D

@ -185,6 +185,11 @@ QVariant PeerTableModel::data(const QModelIndex &index, int role) const
default: default:
return QVariant(); return QVariant();
} }
} else if (role == StatsRole) {
switch (index.column()) {
case NetNodeId: return QVariant::fromValue(rec);
default: return QVariant();
}
} }
return QVariant(); return QVariant();
@ -220,11 +225,6 @@ QModelIndex PeerTableModel::index(int row, int column, const QModelIndex &parent
return QModelIndex(); return QModelIndex();
} }
const CNodeCombinedStats *PeerTableModel::getNodeStats(int idx)
{
return priv->index(idx);
}
void PeerTableModel::refresh() void PeerTableModel::refresh()
{ {
Q_EMIT layoutAboutToBeChanged(); Q_EMIT layoutAboutToBeChanged();

@ -28,6 +28,7 @@ struct CNodeCombinedStats {
CNodeStateStats nodeStateStats; CNodeStateStats nodeStateStats;
bool fNodeStateStatsAvailable; bool fNodeStateStatsAvailable;
}; };
Q_DECLARE_METATYPE(CNodeCombinedStats*)
class NodeLessThan class NodeLessThan
{ {
@ -52,7 +53,6 @@ class PeerTableModel : public QAbstractTableModel
public: public:
explicit PeerTableModel(interfaces::Node& node, QObject* parent); explicit PeerTableModel(interfaces::Node& node, QObject* parent);
~PeerTableModel(); ~PeerTableModel();
const CNodeCombinedStats *getNodeStats(int idx);
int getRowByNodeId(NodeId nodeid); int getRowByNodeId(NodeId nodeid);
void startAutoRefresh(); void startAutoRefresh();
void stopAutoRefresh(); void stopAutoRefresh();
@ -67,6 +67,10 @@ public:
Subversion = 6 Subversion = 6
}; };
enum {
StatsRole = Qt::UserRole,
};
/** @name Methods overridden from QAbstractTableModel /** @name Methods overridden from QAbstractTableModel
@{*/ @{*/
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;

@ -1022,11 +1022,9 @@ void RPCConsole::updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
void RPCConsole::peerLayoutAboutToChange() void RPCConsole::peerLayoutAboutToChange()
{ {
QModelIndexList selected = ui->peerWidget->selectionModel()->selectedIndexes();
cachedNodeids.clear(); cachedNodeids.clear();
for(int i = 0; i < selected.size(); i++) for (const QModelIndex& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
{ const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(selected.at(i).row());
cachedNodeids.append(stats->nodeStats.nodeid); cachedNodeids.append(stats->nodeStats.nodeid);
} }
} }
@ -1085,15 +1083,13 @@ void RPCConsole::peerLayoutChanged()
void RPCConsole::updateDetailWidget() void RPCConsole::updateDetailWidget()
{ {
QModelIndexList selected_rows; const QList<QModelIndex> selected_peers = GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId);
auto selection_model = ui->peerWidget->selectionModel(); if (!clientModel || !clientModel->getPeerTableModel() || selected_peers.size() != 1) {
if (selection_model) selected_rows = selection_model->selectedRows();
if (!clientModel || !clientModel->getPeerTableModel() || selected_rows.size() != 1) {
ui->detailWidget->hide(); ui->detailWidget->hide();
ui->peerHeading->setText(tr("Select a peer to view detailed information.")); ui->peerHeading->setText(tr("Select a peer to view detailed information."));
return; return;
} }
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(selected_rows.first().row()); const auto stats = selected_peers.first().data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
// update the detail ui with latest node information // update the detail ui with latest node information
QString peerAddrDetails(QString::fromStdString(stats->nodeStats.addrName) + " "); QString peerAddrDetails(QString::fromStdString(stats->nodeStats.addrName) + " ");
peerAddrDetails += tr("(peer id: %1)").arg(QString::number(stats->nodeStats.nodeid)); peerAddrDetails += tr("(peer id: %1)").arg(QString::number(stats->nodeStats.nodeid));
@ -1206,19 +1202,9 @@ void RPCConsole::banSelectedNode(int bantime)
if (!clientModel) if (!clientModel)
return; return;
// Get selected peer addresses for (const QModelIndex& peer : GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId)) {
QList<QModelIndex> nodes = GUIUtil::getEntryData(ui->peerWidget, PeerTableModel::NetNodeId);
for(int i = 0; i < nodes.count(); i++)
{
// Get currently selected peer address
NodeId id = nodes.at(i).data().toLongLong();
// Get currently selected peer address
int detailNodeRow = clientModel->getPeerTableModel()->getRowByNodeId(id);
if (detailNodeRow < 0) return;
// Find possible nodes, ban it and clear the selected node // Find possible nodes, ban it and clear the selected node
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(detailNodeRow); const auto stats = peer.data(PeerTableModel::StatsRole).value<CNodeCombinedStats*>();
if (stats) { if (stats) {
m_node.ban(stats->nodeStats.addr, bantime); m_node.ban(stats->nodeStats.addr, bantime);
m_node.disconnectByAddress(stats->nodeStats.addr); m_node.disconnectByAddress(stats->nodeStats.addr);

Loading…
Cancel
Save