Hush lite wallet https://faq.hush.is/sdl
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.

114 lines
2.9 KiB

6 years ago
#include "balancestablemodel.h"
BalancesTableModel::BalancesTableModel(QObject *parent)
6 years ago
: QAbstractTableModel(parent)
{
}
void BalancesTableModel::setNewData(const QMap<QString, double>* balances,
const QList<UnspentOutput>* outputs)
6 years ago
{
int currentRows = rowCount(QModelIndex());
6 years ago
// Copy over the utxos for our use
delete utxos;
6 years ago
utxos = new QList<UnspentOutput>();
// This is a QList deep copy.
*utxos = *outputs;
// Process the address balances into a list
delete modeldata;
6 years ago
modeldata = new QList<std::tuple<QString, QString>>();
std::for_each(balances->constKeyValueBegin(), balances->constKeyValueEnd(), [=] (auto it) {
modeldata->push_back(std::make_tuple(it.first, QString::number(it.second, 'g', 8)));
6 years ago
});
// And then update the data
dataChanged(index(0, 0), index(modeldata->size()-1, columnCount(index(0,0))-1));
// Change the layout only if the number of rows changed
if (modeldata && modeldata->size() != currentRows)
layoutChanged();
6 years ago
}
BalancesTableModel::~BalancesTableModel() {
delete modeldata;
delete utxos;
}
int BalancesTableModel::rowCount(const QModelIndex&) const
{
if (modeldata == nullptr) return 0;
6 years ago
return modeldata->size();
}
int BalancesTableModel::columnCount(const QModelIndex&) const
{
return 2;
}
QVariant BalancesTableModel::data(const QModelIndex &index, int role) const
{
auto fnSplitAddressForWrap = [=] (const QString& a) -> QString {
if (!a.startsWith("z")) return a;
auto half = a.length() / 2;
auto splitted = a.left(half) + "\n" + a.right(a.length() - half);
return splitted;
};
6 years ago
if (role == Qt::TextAlignmentRole && index.column() == 1) return QVariant(Qt::AlignRight | Qt::AlignVCenter);
if (role == Qt::ForegroundRole) {
// If any of the UTXOs for this address has zero confirmations, paint it in red
const auto& addr = std::get<0>(modeldata->at(index.row()));
for (auto utxo : *utxos) {
if (utxo.address == addr && utxo.confirmations == 0) {
QBrush b;
b.setColor(Qt::red);
return b;
}
}
// Else, just return the default brush
QBrush b;
b.setColor(Qt::black);
return b;
}
if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
switch (index.column()) {
case 0: return fnSplitAddressForWrap(std::get<0>(modeldata->at(index.row())));
6 years ago
case 1: return QVariant(std::get<1>(modeldata->at(index.row())) % " ZEC");
}
}
return QVariant();
}
QVariant BalancesTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::TextAlignmentRole && section == 1) {
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
}
if (role == Qt::FontRole) {
QFont f;
f.setBold(true);
return f;
}
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0: return tr("Address");
case 1: return tr("Amount");
default: return QVariant();
}
}
return QVariant();
}