qt: Drop PeerTablePriv class

This commit does not change behavior.
This commit is contained in:
Hennadii Stepanov 2021-02-22 09:55:10 +02:00
parent efb7e5aa96
commit 1b66f6e556
No known key found for this signature in database
GPG Key ID: 410108112E7EA81F
2 changed files with 20 additions and 49 deletions

View File

@ -14,52 +14,11 @@
#include <QList> #include <QList>
#include <QTimer> #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) : PeerTableModel::PeerTableModel(interfaces::Node& node, QObject* parent) :
QAbstractTableModel(parent), QAbstractTableModel(parent),
m_node(node), m_node(node),
timer(nullptr) timer(nullptr)
{ {
priv.reset(new PeerTablePriv());
// set up timer for auto refresh // set up timer for auto refresh
timer = new QTimer(this); timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &PeerTableModel::refresh); connect(timer, &QTimer::timeout, this, &PeerTableModel::refresh);
@ -89,7 +48,7 @@ int PeerTableModel::rowCount(const QModelIndex& parent) const
if (parent.isValid()) { if (parent.isValid()) {
return 0; return 0;
} }
return priv->size(); return m_peers_data.size();
} }
int PeerTableModel::columnCount(const QModelIndex& parent) const int PeerTableModel::columnCount(const QModelIndex& parent) const
@ -175,16 +134,26 @@ Qt::ItemFlags PeerTableModel::flags(const QModelIndex &index) const
QModelIndex PeerTableModel::index(int row, int column, const QModelIndex& parent) const QModelIndex PeerTableModel::index(int row, int column, const QModelIndex& parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent);
CNodeCombinedStats *data = priv->index(row);
if (data) if (0 <= row && row < rowCount() && 0 <= column && column < columnCount()) {
return createIndex(row, column, data); return createIndex(row, column, const_cast<CNodeCombinedStats*>(&m_peers_data[row]));
}
return QModelIndex(); return QModelIndex();
} }
void PeerTableModel::refresh() 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) {
const CNodeCombinedStats stats{std::get<0>(node_stats), std::get<2>(node_stats), std::get<1>(node_stats)};
new_peers_data.append(stats);
}
Q_EMIT layoutAboutToBeChanged(); Q_EMIT layoutAboutToBeChanged();
priv->refreshPeers(m_node); m_peers_data.swap(new_peers_data);
Q_EMIT layoutChanged(); Q_EMIT layoutChanged();
} }

View File

@ -8,10 +8,11 @@
#include <net_processing.h> // For CNodeStateStats #include <net_processing.h> // For CNodeStateStats
#include <net.h> #include <net.h>
#include <memory>
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QList>
#include <QModelIndex>
#include <QStringList> #include <QStringList>
#include <QVariant>
class PeerTablePriv; class PeerTablePriv;
@ -73,6 +74,8 @@ public Q_SLOTS:
void refresh(); void refresh();
private: private:
//! Internal peer data structure.
QList<CNodeCombinedStats> m_peers_data{};
interfaces::Node& m_node; interfaces::Node& m_node;
const QStringList columns{ const QStringList columns{
/*: Title of Peers Table column which contains a /*: Title of Peers Table column which contains a
@ -99,7 +102,6 @@ private:
/*: Title of Peers Table column which contains the peer's /*: Title of Peers Table column which contains the peer's
User Agent string. */ User Agent string. */
tr("User Agent")}; tr("User Agent")};
std::unique_ptr<PeerTablePriv> priv;
QTimer *timer; QTimer *timer;
}; };