History: add balance column

This commit is contained in:
tobtoht 2023-07-15 01:55:02 +02:00
parent 7f914ed682
commit 9ec53dc7d5
No known key found for this signature in database
GPG key ID: E45B10DD027D2472
7 changed files with 40 additions and 10 deletions

2
monero

@ -1 +1 @@
Subproject commit 9e4a4d305c0a24f294d9f75d33065381b6fba520
Subproject commit ecca08f4c0b1e5a012cdd20110b015c540bce0c6

View file

@ -64,13 +64,22 @@ void TransactionHistory::refresh(quint32 accountIndex)
m_locked = false;
m_minutesToUnlock = 0;
quint64 balance = 0;
m_pimpl->refresh();
for (const auto i : m_pimpl->getAll()) {
if (i->subaddrAccount() != accountIndex) {
continue;
}
m_tinfo.append(new TransactionInfo(i, this));
if (i->direction() == Monero::TransactionInfo::Direction_In) {
balance += i->amount();
}
else if (i->direction() == Monero::TransactionInfo::Direction_Out) {
balance -= i->amount() + i->fee();
}
m_tinfo.append(new TransactionInfo(i, balance, this));
const TransactionInfo *ti = m_tinfo.back();
// looking for transactions timestamp scope
@ -159,7 +168,7 @@ bool TransactionHistory::writeCSV(const QString &path) {
});
for (const auto &tx : transactions) {
TransactionInfo info(tx, this);
TransactionInfo info(tx, 0, this);
// collect column data
QDateTime timeStamp = info.timestamp();

View file

@ -37,6 +37,11 @@ quint64 TransactionInfo::balanceDelta() const
return m_amount;
}
quint64 TransactionInfo::balance() const
{
return m_balance;
}
double TransactionInfo::amount() const
{
// there's no unsigned uint64 for JS, so better use double
@ -167,7 +172,7 @@ QString TransactionInfo::rings_formatted() const
return rings;
}
TransactionInfo::TransactionInfo(const Monero::TransactionInfo *pimpl, QObject *parent)
TransactionInfo::TransactionInfo(const Monero::TransactionInfo *pimpl, quint64 balance, QObject *parent)
: QObject(parent)
, m_amount(pimpl->amount())
, m_blockHeight(pimpl->blockHeight())
@ -184,6 +189,7 @@ TransactionInfo::TransactionInfo(const Monero::TransactionInfo *pimpl, QObject *
, m_timestamp(QDateTime::fromSecsSinceEpoch(pimpl->timestamp()))
, m_unlockTime(pimpl->unlockTime())
, m_coinbase(pimpl->isCoinbase())
, m_balance(balance)
{
for (auto const &t: pimpl->transfers())
{

View file

@ -53,6 +53,7 @@ public:
bool isFailed() const;
bool isCoinbase() const;
quint64 balanceDelta() const;
quint64 balance() const;
double amount() const;
quint64 atomicAmount() const;
QString displayAmount() const;
@ -80,7 +81,7 @@ public:
QString rings_formatted() const;
private:
explicit TransactionInfo(const Monero::TransactionInfo *pimpl, QObject *parent = nullptr);
explicit TransactionInfo(const Monero::TransactionInfo *pimpl, quint64 balance, QObject *parent = nullptr);
private:
friend class TransactionHistory;
mutable QList<Transfer*> m_transfers;
@ -102,6 +103,7 @@ private:
QDateTime m_timestamp;
quint64 m_unlockTime;
bool m_coinbase;
quint64 m_balance;
};
#endif // TRANSACTIONINFO_H

View file

@ -181,6 +181,7 @@ void HistoryView::resetViewToDefaults()
header()->showSection(TransactionHistoryModel::Description);
header()->showSection(TransactionHistoryModel::Amount);
header()->showSection(TransactionHistoryModel::FiatAmount);
header()->hideSection(TransactionHistoryModel::Balance);
// Reset column order to logical indices
for (int i = 0; i < header()->count(); ++i) {

View file

@ -67,7 +67,7 @@ QVariant TransactionHistoryModel::data(const QModelIndex &index, int role) const
bool found = m_transactionHistory->transaction(index.row(), [this, &index, &result, &role](const TransactionInfo &tInfo) {
if(role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole) {
result = parseTransactionInfo(tInfo, index.column(), role);
result = parseTransactionInfo(tInfo, index.row(), index.column(), role);
}
else if (role == Qt::TextAlignmentRole) {
switch (index.column()) {
@ -139,14 +139,14 @@ QVariant TransactionHistoryModel::data(const QModelIndex &index, int role) const
return result;
}
QVariant TransactionHistoryModel::parseTransactionInfo(const TransactionInfo &tInfo, int column, int role) const
QVariant TransactionHistoryModel::parseTransactionInfo(const TransactionInfo &tInfo, int row, int column, int role) const
{
switch (column)
{
case Column::Date:
{
if (role == Qt::UserRole) {
return tInfo.timestamp();
return row;
}
return tInfo.timestamp().toString(QString("%1 %2 ").arg(config()->get(Config::dateFormat).toString(),
config()->get(Config::timeFormat).toString()));
@ -158,7 +158,7 @@ QVariant TransactionHistoryModel::parseTransactionInfo(const TransactionInfo &tI
if (role == Qt::UserRole) {
return tInfo.balanceDelta();
}
QString amount = QString::number(tInfo.balanceDelta() / constants::cdiv, 'f', config()->get(Config::amountPrecision).toInt());
auto amount = this->formatAmount(tInfo.balanceDelta());
amount = (tInfo.direction() == TransactionInfo::Direction_Out) ? "-" + amount : "+" + amount;
return amount;
}
@ -188,6 +188,10 @@ QVariant TransactionHistoryModel::parseTransactionInfo(const TransactionInfo &tI
double fiat_rounded = ceil(Utils::roundSignificant(usd_amount, 3) * 100.0) / 100.0;
return QString("%1").arg(Utils::amountToCurrencyString(fiat_rounded, preferredFiatCurrency));
}
case Column::Balance:
{
return this->formatAmount(tInfo.balance());
}
default:
{
qCritical() << "Unimplemented role";
@ -196,6 +200,10 @@ QVariant TransactionHistoryModel::parseTransactionInfo(const TransactionInfo &tI
}
}
QString TransactionHistoryModel::formatAmount(quint64 amount) const {
return QString::number(amount / constants::cdiv, 'f', config()->get(Config::amountPrecision).toInt());
}
QVariant TransactionHistoryModel::headerData(int section, Qt::Orientation orientation, int role) const {
Q_UNUSED(orientation)
if (role != Qt::DisplayRole) {
@ -213,6 +221,8 @@ QVariant TransactionHistoryModel::headerData(int section, Qt::Orientation orient
return QString("Txid");
case Column::FiatAmount:
return QString("Fiat");
case Column::Balance:
return QString("Balance");
default:
return QVariant();
}

View file

@ -27,6 +27,7 @@ public:
Description,
Amount,
FiatAmount,
Balance,
COUNT
};
@ -47,7 +48,8 @@ signals:
void transactionHistoryChanged();
private:
QVariant parseTransactionInfo(const TransactionInfo &tInfo, int column, int role) const;
QVariant parseTransactionInfo(const TransactionInfo &tInfo, int row, int column, int role) const;
QString formatAmount(quint64 amount) const;
TransactionHistory * m_transactionHistory;
};