Skip to content

Commit

Permalink
qt: Add BitcoinUnits::formatWithPrivacy() function
Browse files Browse the repository at this point in the history
Summary:
> This PR allows to hide/reveal values on the Overviewpage by checking/unchecking Menu->Settings-> Mask Values

This is a backport of [[bitcoin/bitcoin#16432 | core#16432]] [1/2]
bitcoin/bitcoin@73d8ef7

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D9919
  • Loading branch information
hebasto authored and PiRK committed Aug 24, 2021
1 parent fc59a38 commit 357df3a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/qt/bitcoinunits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <QStringList>

#include <cassert>

// clang-format off
using unitNameMap =
std::map<
Expand Down Expand Up @@ -104,7 +106,7 @@ int BitcoinUnits::decimals(int unit) {
}

QString BitcoinUnits::format(int unit, const Amount nIn, bool fPlus,
SeparatorStyle separators) {
SeparatorStyle separators, bool justify) {
// Note: not using straight sprintf here because we do NOT want
// localized number formatting.
if (!valid(unit)) {
Expand All @@ -117,6 +119,9 @@ QString BitcoinUnits::format(int unit, const Amount nIn, bool fPlus,
qint64 n_abs = (n > 0 ? n : -n);
qint64 quotient = n_abs / coin;
QString quotient_str = QString::number(quotient);
if (justify) {
quotient_str = quotient_str.rightJustified(16 - num_decimals, ' ');
}

// Use SI-style thin space separators as these are locale independent and
// can't be confused with the decimal marker.
Expand Down Expand Up @@ -167,6 +172,20 @@ QString BitcoinUnits::formatHtmlWithUnit(int unit, const Amount amount,
return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
}

QString BitcoinUnits::formatWithPrivacy(int unit, const Amount &amount,
SeparatorStyle separators,
bool privacy) {
assert(amount >= Amount::zero());
QString value;
if (privacy) {
value = format(unit, Amount::zero(), false, separators, true)
.replace('0', '#');
} else {
value = format(unit, amount, false, separators, true);
}
return value + QString(" ") + shortName(unit);
}

bool BitcoinUnits::parse(int unit, const QString &value, Amount *val_out) {
if (!valid(unit) || value.isEmpty()) {
// Refuse to parse invalid unit or empty string
Expand Down
7 changes: 6 additions & 1 deletion src/qt/bitcoinunits.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class BitcoinUnits : public QAbstractListModel {
static int decimals(int unit);
//! Format as string
static QString format(int unit, const Amount amount, bool plussign = false,
SeparatorStyle separators = separatorStandard);
SeparatorStyle separators = separatorStandard,
bool justify = false);
//! Format as string (with unit)
static QString
formatWithUnit(int unit, const Amount amount, bool plussign = false,
Expand All @@ -72,6 +73,10 @@ class BitcoinUnits : public QAbstractListModel {
static QString
formatHtmlWithUnit(int unit, const Amount amount, bool plussign = false,
SeparatorStyle separators = separatorStandard);
//! Format as string (with unit) of fixed length to preserve privacy, if it
//! is set.
static QString formatWithPrivacy(int unit, const Amount &amount,
SeparatorStyle separators, bool privacy);
//! Parse string to coin amount
static bool parse(int unit, const QString &value, Amount *val_out);
//! Gets title for amount column including current display unit if
Expand Down

0 comments on commit 357df3a

Please sign in to comment.