2020-10-07 10:36:04 +00:00
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-12-26 19:56:06 +00:00
|
|
|
// Copyright (c) 2020-2021, The Monero Project.
|
2020-10-07 10:36:04 +00:00
|
|
|
|
|
|
|
#include "NodeModel.h"
|
|
|
|
#include <utils/nodes.h>
|
|
|
|
|
2020-12-24 13:34:37 +00:00
|
|
|
NodeModel::NodeModel(int nodeSource, QObject *parent)
|
2020-10-07 10:36:04 +00:00
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
, m_nodeSource(nodeSource)
|
2020-10-13 13:45:52 +00:00
|
|
|
, m_offline(QIcon(":/assets/images/expired_icon.png"))
|
|
|
|
, m_online(QIcon(":/assets/images/confirmed_icon.png"))
|
2020-10-07 10:36:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeModel::clear() {
|
|
|
|
beginResetModel();
|
|
|
|
m_nodes.clear();
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NodeModel::updateNodes(const QList<FeatherNode> nodes) {
|
|
|
|
beginResetModel();
|
|
|
|
m_nodes.clear();
|
|
|
|
m_nodes = nodes;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
int NodeModel::rowCount(const QModelIndex &parent) const{
|
|
|
|
if (parent.isValid())
|
|
|
|
return 0;
|
|
|
|
return m_nodes.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int NodeModel::columnCount(const QModelIndex &parent) const {
|
|
|
|
if (parent.isValid())
|
|
|
|
return 0;
|
|
|
|
return m_nodeSource == NodeSource::websocket ? 2 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant NodeModel::data(const QModelIndex &index, int role) const {
|
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_nodes.count())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
FeatherNode node = m_nodes.at(index.row());
|
|
|
|
|
|
|
|
if(role == Qt::DisplayRole) {
|
|
|
|
switch(index.column()) {
|
|
|
|
case NodeModel::URL:
|
|
|
|
return node.full;
|
|
|
|
case NodeModel::Height:
|
|
|
|
if(node.online)
|
|
|
|
return node.height == 0 ? QString("-") : QString::number(node.height);
|
|
|
|
return QString("-");
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (role == Qt::DecorationRole) {
|
|
|
|
switch (index.column()) {
|
|
|
|
case NodeModel::URL: {
|
|
|
|
if(m_nodeSource == NodeSource::websocket)
|
|
|
|
return QVariant(node.online ? m_online : m_offline);
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(role == Qt::BackgroundRole) {
|
|
|
|
if (node.isConnecting)
|
2020-11-23 16:57:38 +00:00
|
|
|
return QBrush(QColor("#A9DEF9"));
|
2020-10-07 10:36:04 +00:00
|
|
|
else if (node.isActive)
|
2020-11-23 16:57:38 +00:00
|
|
|
return QBrush(QColor("#78BC61"));
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant NodeModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
|
|
|
if (role != Qt::DisplayRole) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
if (orientation == Qt::Horizontal) {
|
|
|
|
switch(section) {
|
|
|
|
case NodeModel::URL:
|
|
|
|
return QString("Node");
|
|
|
|
case NodeModel::Height:
|
|
|
|
return QString("Height");
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
FeatherNode NodeModel::node(int row) {
|
|
|
|
if (row < 0 || row >= m_nodes.size()) {
|
|
|
|
qCritical("%s: no reddit post for index %d", __FUNCTION__, row);
|
2020-11-14 21:42:41 +00:00
|
|
|
return FeatherNode();
|
2020-10-07 10:36:04 +00:00
|
|
|
}
|
|
|
|
return m_nodes.at(row);
|
|
|
|
}
|