Fix constness of ArgsManager methods
[bitcoinplatinum.git] / src / qt / peertablemodel.cpp
blob42934f8055d0107342d747bb9df09525b8fb66ec
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.
5 #include "peertablemodel.h"
7 #include "clientmodel.h"
8 #include "guiconstants.h"
9 #include "guiutil.h"
11 #include "validation.h" // for cs_main
12 #include "sync.h"
14 #include <QDebug>
15 #include <QList>
16 #include <QTimer>
18 bool NodeLessThan::operator()(const CNodeCombinedStats &left, const CNodeCombinedStats &right) const
20 const CNodeStats *pLeft = &(left.nodeStats);
21 const CNodeStats *pRight = &(right.nodeStats);
23 if (order == Qt::DescendingOrder)
24 std::swap(pLeft, pRight);
26 switch(column)
28 case PeerTableModel::NetNodeId:
29 return pLeft->nodeid < pRight->nodeid;
30 case PeerTableModel::Address:
31 return pLeft->addrName.compare(pRight->addrName) < 0;
32 case PeerTableModel::Subversion:
33 return pLeft->cleanSubVer.compare(pRight->cleanSubVer) < 0;
34 case PeerTableModel::Ping:
35 return pLeft->dMinPing < pRight->dMinPing;
38 return false;
41 // private implementation
42 class PeerTablePriv
44 public:
45 /** Local cache of peer information */
46 QList<CNodeCombinedStats> cachedNodeStats;
47 /** Column to sort nodes by */
48 int sortColumn;
49 /** Order (ascending or descending) to sort nodes by */
50 Qt::SortOrder sortOrder;
51 /** Index of rows by node ID */
52 std::map<NodeId, int> mapNodeRows;
54 /** Pull a full list of peers from vNodes into our cache */
55 void refreshPeers()
58 cachedNodeStats.clear();
59 std::vector<CNodeStats> vstats;
60 if(g_connman)
61 g_connman->GetNodeStats(vstats);
62 #if QT_VERSION >= 0x040700
63 cachedNodeStats.reserve(vstats.size());
64 #endif
65 for (const CNodeStats& nodestats : vstats)
67 CNodeCombinedStats stats;
68 stats.nodeStateStats.nMisbehavior = 0;
69 stats.nodeStateStats.nSyncHeight = -1;
70 stats.nodeStateStats.nCommonHeight = -1;
71 stats.fNodeStateStatsAvailable = false;
72 stats.nodeStats = nodestats;
73 cachedNodeStats.append(stats);
77 // Try to retrieve the CNodeStateStats for each node.
79 TRY_LOCK(cs_main, lockMain);
80 if (lockMain)
82 for (CNodeCombinedStats &stats : cachedNodeStats)
83 stats.fNodeStateStatsAvailable = GetNodeStateStats(stats.nodeStats.nodeid, stats.nodeStateStats);
87 if (sortColumn >= 0)
88 // sort cacheNodeStats (use stable sort to prevent rows jumping around unnecessarily)
89 qStableSort(cachedNodeStats.begin(), cachedNodeStats.end(), NodeLessThan(sortColumn, sortOrder));
91 // build index map
92 mapNodeRows.clear();
93 int row = 0;
94 for (const CNodeCombinedStats& stats : cachedNodeStats)
95 mapNodeRows.insert(std::pair<NodeId, int>(stats.nodeStats.nodeid, row++));
98 int size() const
100 return cachedNodeStats.size();
103 CNodeCombinedStats *index(int idx)
105 if (idx >= 0 && idx < cachedNodeStats.size())
106 return &cachedNodeStats[idx];
108 return 0;
112 PeerTableModel::PeerTableModel(ClientModel *parent) :
113 QAbstractTableModel(parent),
114 clientModel(parent),
115 timer(0)
117 columns << tr("NodeId") << tr("Node/Service") << tr("User Agent") << tr("Ping");
118 priv.reset(new PeerTablePriv());
119 // default to unsorted
120 priv->sortColumn = -1;
122 // set up timer for auto refresh
123 timer = new QTimer(this);
124 connect(timer, SIGNAL(timeout()), SLOT(refresh()));
125 timer->setInterval(MODEL_UPDATE_DELAY);
127 // load initial data
128 refresh();
131 PeerTableModel::~PeerTableModel()
133 // Intentionally left empty
136 void PeerTableModel::startAutoRefresh()
138 timer->start();
141 void PeerTableModel::stopAutoRefresh()
143 timer->stop();
146 int PeerTableModel::rowCount(const QModelIndex &parent) const
148 Q_UNUSED(parent);
149 return priv->size();
152 int PeerTableModel::columnCount(const QModelIndex &parent) const
154 Q_UNUSED(parent);
155 return columns.length();
158 QVariant PeerTableModel::data(const QModelIndex &index, int role) const
160 if(!index.isValid())
161 return QVariant();
163 CNodeCombinedStats *rec = static_cast<CNodeCombinedStats*>(index.internalPointer());
165 if (role == Qt::DisplayRole) {
166 switch(index.column())
168 case NetNodeId:
169 return (qint64)rec->nodeStats.nodeid;
170 case Address:
171 return QString::fromStdString(rec->nodeStats.addrName);
172 case Subversion:
173 return QString::fromStdString(rec->nodeStats.cleanSubVer);
174 case Ping:
175 return GUIUtil::formatPingTime(rec->nodeStats.dMinPing);
177 } else if (role == Qt::TextAlignmentRole) {
178 if (index.column() == Ping)
179 return (QVariant)(Qt::AlignRight | Qt::AlignVCenter);
182 return QVariant();
185 QVariant PeerTableModel::headerData(int section, Qt::Orientation orientation, int role) const
187 if(orientation == Qt::Horizontal)
189 if(role == Qt::DisplayRole && section < columns.size())
191 return columns[section];
194 return QVariant();
197 Qt::ItemFlags PeerTableModel::flags(const QModelIndex &index) const
199 if(!index.isValid())
200 return 0;
202 Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
203 return retval;
206 QModelIndex PeerTableModel::index(int row, int column, const QModelIndex &parent) const
208 Q_UNUSED(parent);
209 CNodeCombinedStats *data = priv->index(row);
211 if (data)
212 return createIndex(row, column, data);
213 return QModelIndex();
216 const CNodeCombinedStats *PeerTableModel::getNodeStats(int idx)
218 return priv->index(idx);
221 void PeerTableModel::refresh()
223 Q_EMIT layoutAboutToBeChanged();
224 priv->refreshPeers();
225 Q_EMIT layoutChanged();
228 int PeerTableModel::getRowByNodeId(NodeId nodeid)
230 std::map<NodeId, int>::iterator it = priv->mapNodeRows.find(nodeid);
231 if (it == priv->mapNodeRows.end())
232 return -1;
234 return it->second;
237 void PeerTableModel::sort(int column, Qt::SortOrder order)
239 priv->sortColumn = column;
240 priv->sortOrder = order;
241 refresh();