|
14 | 14 | #include <QList>
|
15 | 15 | #include <QTimer>
|
16 | 16 |
|
17 |
| -// private implementation |
18 |
| -class PeerTablePriv |
19 |
| -{ |
20 |
| -public: |
21 |
| - /** Local cache of peer information */ |
22 |
| - QList<CNodeCombinedStats> cachedNodeStats; |
23 |
| - |
24 |
| - /** Pull a full list of peers from vNodes into our cache */ |
25 |
| - void refreshPeers(interfaces::Node& node) |
26 |
| - { |
27 |
| - cachedNodeStats.clear(); |
28 |
| - |
29 |
| - interfaces::Node::NodesStats nodes_stats; |
30 |
| - node.getNodesStats(nodes_stats); |
31 |
| - cachedNodeStats.reserve(nodes_stats.size()); |
32 |
| - for (const auto& node_stats : nodes_stats) |
33 |
| - { |
34 |
| - CNodeCombinedStats stats; |
35 |
| - stats.nodeStats = std::get<0>(node_stats); |
36 |
| - stats.fNodeStateStatsAvailable = std::get<1>(node_stats); |
37 |
| - stats.nodeStateStats = std::get<2>(node_stats); |
38 |
| - cachedNodeStats.append(stats); |
39 |
| - } |
40 |
| - } |
41 |
| - |
42 |
| - int size() const |
43 |
| - { |
44 |
| - return cachedNodeStats.size(); |
45 |
| - } |
46 |
| - |
47 |
| - CNodeCombinedStats *index(int idx) |
48 |
| - { |
49 |
| - if (idx >= 0 && idx < cachedNodeStats.size()) |
50 |
| - return &cachedNodeStats[idx]; |
51 |
| - |
52 |
| - return nullptr; |
53 |
| - } |
54 |
| -}; |
55 |
| - |
56 | 17 | PeerTableModel::PeerTableModel(interfaces::Node& node, QObject* parent) :
|
57 | 18 | QAbstractTableModel(parent),
|
58 | 19 | m_node(node),
|
59 | 20 | timer(nullptr)
|
60 | 21 | {
|
61 |
| - priv.reset(new PeerTablePriv()); |
62 |
| - |
63 | 22 | // set up timer for auto refresh
|
64 | 23 | timer = new QTimer(this);
|
65 | 24 | connect(timer, &QTimer::timeout, this, &PeerTableModel::refresh);
|
@@ -89,7 +48,7 @@ int PeerTableModel::rowCount(const QModelIndex& parent) const
|
89 | 48 | if (parent.isValid()) {
|
90 | 49 | return 0;
|
91 | 50 | }
|
92 |
| - return priv->size(); |
| 51 | + return m_peers_data.size(); |
93 | 52 | }
|
94 | 53 |
|
95 | 54 | int PeerTableModel::columnCount(const QModelIndex& parent) const
|
@@ -175,16 +134,26 @@ Qt::ItemFlags PeerTableModel::flags(const QModelIndex &index) const
|
175 | 134 | QModelIndex PeerTableModel::index(int row, int column, const QModelIndex& parent) const
|
176 | 135 | {
|
177 | 136 | Q_UNUSED(parent);
|
178 |
| - CNodeCombinedStats *data = priv->index(row); |
179 | 137 |
|
180 |
| - if (data) |
181 |
| - return createIndex(row, column, data); |
| 138 | + if (0 <= row && row < rowCount() && 0 <= column && column < columnCount()) { |
| 139 | + return createIndex(row, column, const_cast<CNodeCombinedStats*>(&m_peers_data[row])); |
| 140 | + } |
| 141 | + |
182 | 142 | return QModelIndex();
|
183 | 143 | }
|
184 | 144 |
|
185 | 145 | void PeerTableModel::refresh()
|
186 | 146 | {
|
| 147 | + interfaces::Node::NodesStats nodes_stats; |
| 148 | + m_node.getNodesStats(nodes_stats); |
| 149 | + decltype(m_peers_data) new_peers_data; |
| 150 | + new_peers_data.reserve(nodes_stats.size()); |
| 151 | + for (const auto& node_stats : nodes_stats) { |
| 152 | + const CNodeCombinedStats stats{std::get<0>(node_stats), std::get<2>(node_stats), std::get<1>(node_stats)}; |
| 153 | + new_peers_data.append(stats); |
| 154 | + } |
| 155 | + |
187 | 156 | Q_EMIT layoutAboutToBeChanged();
|
188 |
| - priv->refreshPeers(m_node); |
| 157 | + m_peers_data.swap(new_peers_data); |
189 | 158 | Q_EMIT layoutChanged();
|
190 | 159 | }
|
0 commit comments