GUI: Abstract asset+amount formatting into new GUIUtil::formatAssetAmount

This commit is contained in:
Luke Dashjr 2018-12-16 04:20:46 +00:00 committed by Gregory Sanders
parent 89275ebf21
commit 988ed5effc
3 changed files with 27 additions and 16 deletions

View file

@ -11,6 +11,7 @@
#include <asset.h>
#include <base58.h>
#include "assetsdir.h"
#include <chainparams.h>
#include <primitives/transaction.h>
#include <key_io.h>
@ -773,6 +774,26 @@ QString boostPathToQString(const fs::path &path)
return QString::fromStdString(path.string(utf8));
}
QString formatAssetAmount(const CAsset& asset, const CAmount& amount, const int bitcoin_unit, BitcoinUnits::SeparatorStyle separators)
{
if (asset == Params().GetConsensus().pegged_asset) {
return BitcoinUnits::formatWithUnit(bitcoin_unit, amount, false, separators);
}
qlonglong whole = amount / 100000000;
qlonglong fraction = amount % 100000000;
QString str = QString("%1").arg(whole);
if (fraction) {
str += QString(".%1").arg(fraction, 8, 10, QLatin1Char('0'));
}
std::string asset_label = gAssetsDir.GetLabel(asset);
if (asset_label.empty()) {
asset_label = asset.GetHex();
}
str += QString(" ") + QString::fromStdString(asset_label);
return str;
}
QString formatDurationStr(int secs)
{
QStringList strList;

View file

@ -7,6 +7,8 @@
#include <amount.h>
#include <fs.h>
#include "bitcoinunits.h"
#include <asset.h>
#include <QEvent>
#include <QHeaderView>
@ -187,6 +189,9 @@ namespace GUIUtil
/* Convert OS specific boost path to QString through UTF-8 */
QString boostPathToQString(const fs::path &path);
/* Format an amount of assets in a user-friendly style */
QString formatAssetAmount(const CAsset&, const CAmount&, int bitcoin_unit, BitcoinUnits::SeparatorStyle);
/* Convert seconds into a QString with days, hours, mins, secs */
QString formatDurationStr(int secs);

View file

@ -429,22 +429,7 @@ QVariant TransactionTableModel::addressColor(const TransactionRecord *wtx) const
QString TransactionTableModel::formatTxAmount(const TransactionRecord *wtx, bool showUnconfirmed, BitcoinUnits::SeparatorStyle separators) const
{
QString str;
if (wtx->asset == ::policyAsset) {
str = BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), wtx->amount, false, separators);
} else {
qlonglong whole = wtx->amount / 100000000;
qlonglong fraction = wtx->amount % 100000000;
str = QString("%1").arg(whole);
if (fraction) {
str += QString(".%1").arg(fraction, 8, 10, QLatin1Char('0'));
}
std::string asset_label = gAssetsDir.GetLabel(wtx->asset);
if (asset_label.empty()) {
asset_label = wtx->asset.GetHex();
}
str += QString(" ") + QString::fromStdString(asset_label);
}
QString str = GUIUtil::formatAssetAmount(wtx->asset, wtx->amount, walletModel->getOptionsModel()->getDisplayUnit(), separators);
if(showUnconfirmed)
{
if(!wtx->status.countsForBalance)