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.

131 lines
3.5 KiB

6 years ago
#include "balancestablemodel.h"
#include "addressbook.h"
#include "settings.h"
6 years ago
6 years ago
BalancesTableModel::BalancesTableModel(QObject *parent)
6 years ago
: QAbstractTableModel(parent) {
}
void BalancesTableModel::setNewData(const QMap<QString, qint64> balances,
const QList<UnspentOutput> outputs)
6 years ago
{
loading = false;
int currentRows = rowCount(QModelIndex());
// Copy over the utxos for our use
delete utxos;
utxos = new QList<UnspentOutput>();
6 years ago
// This is a QList deep copy.
*utxos = outputs;
6 years ago
// Process the address balances into a list
delete modeldata;
modeldata = new QList<std::tuple<QString, qint64>>();
std::for_each(balances.keyBegin(), balances.keyEnd(), [=] (auto keyIt) {
if (balances.value(keyIt) > 0)
modeldata->push_back(std::make_tuple(keyIt, balances.value(keyIt)));
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() {
6 years ago
delete modeldata;
delete utxos;
6 years ago
}
int BalancesTableModel::rowCount(const QModelIndex&) const
{
6 years ago
if (modeldata == nullptr) {
if (loading)
return 1;
else
return 0;
}
return modeldata->size();
6 years ago
}
int BalancesTableModel::columnCount(const QModelIndex&) const
{
6 years ago
return 2;
6 years ago
}
QVariant BalancesTableModel::data(const QModelIndex &index, int role) const
{
6 years ago
if (loading) {
if (role == Qt::DisplayRole)
return "Loading...";
else
return QVariant();
}
6 years ago
if (role == Qt::TextAlignmentRole && index.column() == 1) return QVariant(Qt::AlignRight | Qt::AlignVCenter);
6 years ago
if (role == Qt::ForegroundRole) {
// If any of the UTXOs for this address has zero confirmations, paint it in red
6 years ago
const auto& addr = std::get<0>(modeldata->at(index.row()));
6 years ago
for (auto utxo : *utxos) {
if (utxo.address == addr && !utxo.spendable) {
6 years ago
QBrush b;
b.setColor(Qt::red);
return b;
}
}
// Else, just return the default brush
6 years ago
QBrush b;
6 years ago
b.setColor(Qt::black);
return b;
}
if (role == Qt::DisplayRole) {
switch (index.column()) {
case 0: return AddressBook::addLabelToAddress(std::get<0>(modeldata->at(index.row())));
5 years ago
case 1: return Settings::gethushDisplayFormat(std::get<1>(modeldata->at(index.row())));
6 years ago
}
}
if(role == Qt::ToolTipRole) {
switch (index.column()) {
case 0: return AddressBook::addLabelToAddress(std::get<0>(modeldata->at(index.row())));
5 years ago
case 1: return Settings::getUSDFromhushAmount(std::get<1>(modeldata->at(index.row())));
6 years ago
}
}
return QVariant();
6 years ago
}
QVariant BalancesTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
6 years ago
if (role == Qt::TextAlignmentRole && section == 1) {
6 years ago
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
6 years ago
}
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();
6 years ago
}