update// reformated code in balancestablemodel.cpp

This commit is contained in:
Strider
2020-04-08 14:34:09 +02:00
parent afcb3a14f1
commit eb2adaa81c

View File

@@ -3,9 +3,8 @@
#include "settings.h" #include "settings.h"
#include "camount.h" #include "camount.h"
BalancesTableModel::BalancesTableModel(QObject *parent) BalancesTableModel::BalancesTableModel(QObject *parent): QAbstractTableModel(parent)
: QAbstractTableModel(parent) { {}
}
void BalancesTableModel::setNewData(const QList<QString> zaddrs, const QList<QString> taddrs, void BalancesTableModel::setNewData(const QList<QString> zaddrs, const QList<QString> taddrs,
const QMap<QString, CAmount> balances, const QList<UnspentOutput> outputs) const QMap<QString, CAmount> balances, const QList<UnspentOutput> outputs)
@@ -29,21 +28,19 @@ void BalancesTableModel::setNewData(const QList<QString> zaddrs, const QList<QSt
modeldata = new QList<std::tuple<QString, CAmount>>(); modeldata = new QList<std::tuple<QString, CAmount>>();
std::for_each(balances.keyBegin(), balances.keyEnd(), [=, &anyz, &anyt] (auto keyIt) { std::for_each(balances.keyBegin(), balances.keyEnd(), [=, &anyz, &anyt] (auto keyIt) {
modeldata->push_back(std::make_tuple(keyIt, balances.value(keyIt))); modeldata->push_back(std::make_tuple(keyIt, balances.value(keyIt)));
if (Settings::isZAddress(keyIt)) { if (Settings::isZAddress(keyIt))
anyz = true; anyz = true;
} else if (Settings::isTAddress(keyIt)) {
else if (Settings::isTAddress(keyIt))
anyt = true; anyt = true;
}
}); });
// Add all addresses that have no balances, if there are no existing addresses // Add all addresses that have no balances, if there are no existing addresses
if (!anyz && zaddrs.length() > 0) { if (!anyz && zaddrs.length() > 0)
modeldata->push_back(std::make_tuple(zaddrs[0], CAmount::fromqint64(0))); modeldata->push_back(std::make_tuple(zaddrs[0], CAmount::fromqint64(0)));
}
if (!anyt && taddrs.length() > 0) { if (!anyt && taddrs.length() > 0)
modeldata->push_back(std::make_tuple(taddrs[0], CAmount::fromqint64(0))); modeldata->push_back(std::make_tuple(taddrs[0], CAmount::fromqint64(0)));
}
// And then update the data // And then update the data
dataChanged(index(0, 0), index(modeldata->size()-1, columnCount(index(0,0))-1)); dataChanged(index(0, 0), index(modeldata->size()-1, columnCount(index(0,0))-1));
@@ -53,19 +50,17 @@ void BalancesTableModel::setNewData(const QList<QString> zaddrs, const QList<QSt
layoutChanged(); layoutChanged();
} }
BalancesTableModel::~BalancesTableModel() { BalancesTableModel::~BalancesTableModel()
{
delete modeldata; delete modeldata;
delete unspentOutputs; delete unspentOutputs;
} }
int BalancesTableModel::rowCount(const QModelIndex&) const int BalancesTableModel::rowCount(const QModelIndex&) const
{ {
if (modeldata == nullptr) { if (modeldata == nullptr)
if (loading) return (loading) ? 1: 0;
return 1;
else
return 0;
}
return modeldata->size(); return modeldata->size();
} }
@@ -76,25 +71,31 @@ int BalancesTableModel::columnCount(const QModelIndex&) const
QVariant BalancesTableModel::data(const QModelIndex &index, int role) const QVariant BalancesTableModel::data(const QModelIndex &index, int role) const
{ {
if (loading) { if (loading)
if (role == Qt::DisplayRole) return (role == Qt::DisplayRole) ? "Loading..." : QVariant();
/*if (role == Qt::DisplayRole)
return "Loading..."; return "Loading...";
else else
return QVariant(); return QVariant();*/
}
if (role == Qt::TextAlignmentRole && index.column() == 1) return QVariant(Qt::AlignRight | Qt::AlignVCenter); if (role == Qt::TextAlignmentRole && index.column() == 1)
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
if (role == Qt::ForegroundRole) { if (role == Qt::ForegroundRole) {
// If any of the UTXOs for this address has zero confirmations, paint it in red // 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())); const auto& addr = std::get<0>(modeldata->at(index.row()));
for (auto unconfirmedOutput : *unspentOutputs) { for (auto unconfirmedOutput : *unspentOutputs)
if (unconfirmedOutput.address == addr && {
(!unconfirmedOutput.spendable || unconfirmedOutput.pending)) { if (
unconfirmedOutput.address == addr &&
(!unconfirmedOutput.spendable || unconfirmedOutput.pending)
)
{
QBrush b; QBrush b;
b.setColor(Qt::red); b.setColor(Qt::red);
return b; return b;
} }
} }
// Else, just return the default brush // Else, just return the default brush
@@ -103,17 +104,21 @@ QVariant BalancesTableModel::data(const QModelIndex &index, int role) const
return b; return b;
} }
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole)
switch (index.column()) { {
case 0: return AddressBook::addLabelToAddress(std::get<0>(modeldata->at(index.row()))); switch (index.column())
case 1: return std::get<1>(modeldata->at(index.row())).toDecimalhushString(); {
case 0: return AddressBook::addLabelToAddress(std::get<0>(modeldata->at(index.row())));
case 1: return std::get<1>(modeldata->at(index.row())).toDecimalhushString();
} }
} }
if(role == Qt::ToolTipRole) { if(role == Qt::ToolTipRole)
switch (index.column()) { {
case 0: return AddressBook::addLabelToAddress(std::get<0>(modeldata->at(index.row()))); switch (index.column())
case 1: return std::get<1>(modeldata->at(index.row())).toDecimalhushString(); {
case 0: return AddressBook::addLabelToAddress(std::get<0>(modeldata->at(index.row())));
case 1: return std::get<1>(modeldata->at(index.row())).toDecimalhushString();
} }
} }
@@ -123,11 +128,13 @@ QVariant BalancesTableModel::data(const QModelIndex &index, int role) const
QVariant BalancesTableModel::headerData(int section, Qt::Orientation orientation, int role) const QVariant BalancesTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{ {
if (role == Qt::TextAlignmentRole && section == 1) { if (role == Qt::TextAlignmentRole && section == 1)
{
return QVariant(Qt::AlignRight | Qt::AlignVCenter); return QVariant(Qt::AlignRight | Qt::AlignVCenter);
} }
if (role == Qt::FontRole) { if (role == Qt::FontRole)
{
QFont f; QFont f;
f.setBold(true); f.setBold(true);
return f; return f;
@@ -136,13 +143,16 @@ QVariant BalancesTableModel::headerData(int section, Qt::Orientation orientation
if (role != Qt::DisplayRole) if (role != Qt::DisplayRole)
return QVariant(); return QVariant();
if (orientation == Qt::Horizontal) { if (orientation == Qt::Horizontal)
switch (section) { {
case 0: return tr("Address"); switch (section)
case 1: return tr("Amount"); {
default: return QVariant(); case 0: return tr("Address");
case 1: return tr("Amount");
default: return QVariant();
} }
} }
return QVariant(); return QVariant();
} }