Bitcoin ABC 0.32.4
P2P Digital Currency
peertablemodel.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2016 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <qt/peertablemodel.h>
6
7#include <qt/guiconstants.h>
8#include <qt/guiutil.h>
9
10#include <interfaces/node.h>
11
12#include <utility>
13
14#include <QDebug>
15#include <QList>
16#include <QTimer>
17
19 const CNodeCombinedStats &right) const {
20 const CNodeStats *pLeft = &(left.nodeStats);
21 const CNodeStats *pRight = &(right.nodeStats);
22
23 if (order == Qt::DescendingOrder) {
24 std::swap(pLeft, pRight);
25 }
26
27 switch (column) {
29 return pLeft->nodeid < pRight->nodeid;
31 return pLeft->m_addr_name.compare(pRight->m_addr_name) < 0;
33 return pLeft->m_network < pRight->m_network;
35 return pLeft->m_min_ping_time < pRight->m_min_ping_time;
37 return pLeft->nSendBytes < pRight->nSendBytes;
39 return pLeft->nRecvBytes < pRight->nRecvBytes;
41 return pLeft->cleanSubVer.compare(pRight->cleanSubVer) < 0;
42 }
43
44 return false;
45}
46
47// private implementation
49public:
51 QList<CNodeCombinedStats> cachedNodeStats;
53 int sortColumn{-1};
55 Qt::SortOrder sortOrder;
57 std::map<NodeId, int> mapNodeRows;
58
61 {
62 cachedNodeStats.clear();
63
65 node.getNodesStats(nodes_stats);
66 cachedNodeStats.reserve(nodes_stats.size());
67 for (const auto &node_stats : nodes_stats) {
69 stats.nodeStats = std::get<0>(node_stats);
70 stats.fNodeStateStatsAvailable = std::get<1>(node_stats);
71 stats.nodeStateStats = std::get<2>(node_stats);
72 cachedNodeStats.append(stats);
73 }
74 }
75
76 if (sortColumn >= 0) {
77 // sort cacheNodeStats (use stable sort to prevent rows jumping
78 // around unnecessarily)
79 std::stable_sort(cachedNodeStats.begin(), cachedNodeStats.end(),
81 }
82
83 // build index map
84 mapNodeRows.clear();
85 int row = 0;
86 for (const CNodeCombinedStats &stats : cachedNodeStats) {
87 mapNodeRows.insert(
88 std::pair<NodeId, int>(stats.nodeStats.nodeid, row++));
89 }
90 }
91
92 int size() const { return cachedNodeStats.size(); }
93
95 if (idx >= 0 && idx < cachedNodeStats.size()) {
96 return &cachedNodeStats[idx];
97 }
98
99 return nullptr;
100 }
101};
102
104 : QAbstractTableModel(parent), m_node(node), timer(nullptr) {
105 priv.reset(new PeerTablePriv());
106
107 // set up timer for auto refresh
108 timer = new QTimer(this);
109 connect(timer, &QTimer::timeout, this, &PeerTableModel::refresh);
110 timer->setInterval(MODEL_UPDATE_DELAY);
111
112 // load initial data
113 refresh();
114}
115
117
119 timer->start();
120}
121
123 timer->stop();
124}
125
126int PeerTableModel::rowCount(const QModelIndex &parent) const {
127 Q_UNUSED(parent);
128 return priv->size();
129}
130
131int PeerTableModel::columnCount(const QModelIndex &parent) const {
132 Q_UNUSED(parent);
133 return columns.length();
134}
135
136QVariant PeerTableModel::data(const QModelIndex &index, int role) const {
137 if (!index.isValid()) {
138 return QVariant();
139 }
140
141 CNodeCombinedStats *rec =
142 static_cast<CNodeCombinedStats *>(index.internalPointer());
143
144 if (role == Qt::DisplayRole) {
145 switch (index.column()) {
146 case NetNodeId:
147 return (qint64)rec->nodeStats.nodeid;
148 case Address:
149 // prepend to peer address down-arrow symbol for inbound
150 // connection and up-arrow for outbound connection
151 return QString(rec->nodeStats.fInbound ? "↓ " : "↑ ") +
152 QString::fromStdString(rec->nodeStats.m_addr_name);
153 case Network:
155 case Ping:
157 case Sent:
159 case Received:
161 case Subversion:
162 return QString::fromStdString(rec->nodeStats.cleanSubVer);
163 }
164 } else if (role == Qt::TextAlignmentRole) {
165 switch (index.column()) {
166 case Network:
167 return QVariant(Qt::AlignCenter);
168 case Ping:
169 case Sent:
170 case Received:
171 return QVariant(Qt::AlignRight | Qt::AlignVCenter);
172 default:
173 return QVariant();
174 }
175 }
176
177 return QVariant();
178}
179
180QVariant PeerTableModel::headerData(int section, Qt::Orientation orientation,
181 int role) const {
182 if (orientation == Qt::Horizontal) {
183 if (role == Qt::DisplayRole && section < columns.size()) {
184 return columns[section];
185 }
186 }
187 return QVariant();
188}
189
190Qt::ItemFlags PeerTableModel::flags(const QModelIndex &index) const {
191 if (!index.isValid()) {
192 return Qt::NoItemFlags;
193 }
194
195 Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
196 return retval;
197}
198
199QModelIndex PeerTableModel::index(int row, int column,
200 const QModelIndex &parent) const {
201 Q_UNUSED(parent);
202 CNodeCombinedStats *data = priv->index(row);
203
204 if (data) {
205 return createIndex(row, column, data);
206 }
207 return QModelIndex();
208}
209
211 return priv->index(idx);
212}
213
215 Q_EMIT layoutAboutToBeChanged();
216 priv->refreshPeers(m_node);
217 Q_EMIT layoutChanged();
218}
219
221 std::map<NodeId, int>::iterator it = priv->mapNodeRows.find(nodeid);
222 if (it == priv->mapNodeRows.end()) {
223 return -1;
224 }
225
226 return it->second;
227}
228
229void PeerTableModel::sort(int column, Qt::SortOrder order) {
230 priv->sortColumn = column;
231 priv->sortOrder = order;
232 refresh();
233}
bool operator()(const CNodeCombinedStats &left, const CNodeCombinedStats &right) const
Qt::SortOrder order
QVariant data(const QModelIndex &index, int role) const override
int columnCount(const QModelIndex &parent) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
interfaces::Node & m_node
QModelIndex index(int row, int column, const QModelIndex &parent) const override
void sort(int column, Qt::SortOrder order) override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
const CNodeCombinedStats * getNodeStats(int idx)
const QStringList columns
PeerTableModel(interfaces::Node &node, QObject *parent)
int getRowByNodeId(NodeId nodeid)
int rowCount(const QModelIndex &parent) const override
std::unique_ptr< PeerTablePriv > priv
int sortColumn
Column to sort nodes by (default to unsorted)
void refreshPeers(interfaces::Node &node)
Pull a full list of peers from vNodes into our cache.
QList< CNodeCombinedStats > cachedNodeStats
Local cache of peer information.
CNodeCombinedStats * index(int idx)
int size() const
Qt::SortOrder sortOrder
Order (ascending or descending) to sort nodes by.
std::map< NodeId, int > mapNodeRows
Index of rows by node ID.
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:59
std::vector< std::tuple< CNodeStats, bool, CNodeStateStats > > NodesStats
Get stats for connected nodes.
Definition: node.h:125
int64_t NodeId
Definition: eviction.h:16
static constexpr auto MODEL_UPDATE_DELAY
A delay between model updates.
Definition: guiconstants.h:14
QString NetworkToQString(Network net)
Convert enum Network to QString.
Definition: guiutil.cpp:795
QString formatBytes(uint64_t bytes)
Definition: guiutil.cpp:899
QString formatPingTime(std::chrono::microseconds ping_time)
Format a CNodeStats.m_last_ping_time into a user-readable string or display N/A, if 0.
Definition: guiutil.cpp:857
Definition: init.h:31
NodeContext & m_node
Definition: interfaces.cpp:822
CNodeStateStats nodeStateStats
CNodeStats nodeStats
POD that contains various stats about a node.
Definition: net.h:213
uint64_t nRecvBytes
Definition: net.h:233
bool fInbound
Definition: net.h:225
uint64_t nSendBytes
Definition: net.h:231
std::chrono::microseconds m_min_ping_time
Definition: net.h:237
std::string m_addr_name
Definition: net.h:222
Network m_network
Definition: net.h:245
NodeId nodeid
Definition: net.h:214
std::string cleanSubVer
Definition: net.h:224