Skip to content

Commit

Permalink
qt: Drop PeerTablePriv class
Browse files Browse the repository at this point in the history
This commit does not change behavior.
  • Loading branch information
hebasto committed Jan 27, 2021
1 parent 2aef7c7 commit 3746548
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 49 deletions.
64 changes: 18 additions & 46 deletions src/qt/peertablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,11 @@
#include <QList>
#include <QTimer>

// private implementation
class PeerTablePriv
{
public:
/** Local cache of peer information */
QList<CNodeCombinedStats> cachedNodeStats;

/** Pull a full list of peers from vNodes into our cache */
void refreshPeers(interfaces::Node& node)
{
cachedNodeStats.clear();

interfaces::Node::NodesStats nodes_stats;
node.getNodesStats(nodes_stats);
cachedNodeStats.reserve(nodes_stats.size());
for (const auto& node_stats : nodes_stats)
{
CNodeCombinedStats stats;
stats.nodeStats = std::get<0>(node_stats);
stats.fNodeStateStatsAvailable = std::get<1>(node_stats);
stats.nodeStateStats = std::get<2>(node_stats);
cachedNodeStats.append(stats);
}
}

int size() const
{
return cachedNodeStats.size();
}

CNodeCombinedStats *index(int idx)
{
if (idx >= 0 && idx < cachedNodeStats.size())
return &cachedNodeStats[idx];

return nullptr;
}
};

PeerTableModel::PeerTableModel(interfaces::Node& node, QObject* parent) :
QAbstractTableModel(parent),
m_node(node),
timer(nullptr)
{
priv.reset(new PeerTablePriv());

// set up timer for auto refresh
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &PeerTableModel::refresh);
Expand Down Expand Up @@ -89,7 +48,7 @@ int PeerTableModel::rowCount(const QModelIndex& parent) const
if (parent.isValid()) {
return 0;
}
return priv->size();
return m_peers_data.size();
}

int PeerTableModel::columnCount(const QModelIndex& parent) const
Expand Down Expand Up @@ -167,16 +126,29 @@ Qt::ItemFlags PeerTableModel::flags(const QModelIndex &index) const
QModelIndex PeerTableModel::index(int row, int column, const QModelIndex& parent) const
{
Q_UNUSED(parent);
CNodeCombinedStats *data = priv->index(row);

if (data)
return createIndex(row, column, data);
if (0 <= row && row < rowCount() && 0 <= column && column < columnCount()) {
return createIndex(row, column, const_cast<CNodeCombinedStats*>(&m_peers_data[row]));
}

return QModelIndex();
}

void PeerTableModel::refresh()
{
interfaces::Node::NodesStats nodes_stats;
m_node.getNodesStats(nodes_stats);
decltype(m_peers_data) new_peers_data;
new_peers_data.reserve(nodes_stats.size());
for (const auto& node_stats : nodes_stats) {
CNodeCombinedStats stats;
stats.nodeStats = std::get<0>(node_stats);
stats.fNodeStateStatsAvailable = std::get<1>(node_stats);
stats.nodeStateStats = std::get<2>(node_stats);
new_peers_data.append(stats);
}

Q_EMIT layoutAboutToBeChanged();
priv->refreshPeers(m_node);
m_peers_data.swap(new_peers_data);
Q_EMIT layoutChanged();
}
8 changes: 5 additions & 3 deletions src/qt/peertablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#include <net_processing.h> // For CNodeStateStats
#include <net.h>

#include <memory>

#include <QAbstractTableModel>
#include <QList>
#include <QModelIndex>
#include <QStringList>
#include <QVariant>

class PeerTablePriv;

Expand Down Expand Up @@ -72,9 +73,10 @@ public Q_SLOTS:
void refresh();

private:
//! Internal peer data structure.
QList<CNodeCombinedStats> m_peers_data{};
interfaces::Node& m_node;
const QStringList columns{tr("Peer Id"), tr("Address"), tr("Network"), tr("Ping"), tr("Sent"), tr("Received"), tr("User Agent")};
std::unique_ptr<PeerTablePriv> priv;
QTimer *timer;
};

Expand Down

0 comments on commit 3746548

Please sign in to comment.