You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wowlet/src/model/NodeModel.cpp

103 lines
2.8 KiB

// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#include "NodeModel.h"
#include <utils/nodes.h>
NodeModel::NodeModel(int nodeSource, QObject *parent)
: QAbstractTableModel(parent)
, m_nodeSource(nodeSource)
, m_offline(QIcon(":/assets/images/expired_icon.png"))
, m_online(QIcon(":/assets/images/confirmed_icon.png"))
{
}
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)
return QBrush(QColor("#A9DEF9"));
else if (node.isActive)
return QBrush(QColor("#78BC61"));
}
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);
return FeatherNode();
}
return m_nodes.at(row);
}