Skip to content

Commit 7007402

Browse files
committed
Implement SI-style (thin space) thoudands separator
1 parent 9b4b3cf commit 7007402

10 files changed

+184
-37
lines changed

src/qt/bitcoinamountfield.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,59 @@
1414
#include <QKeyEvent>
1515
#include <qmath.h> // for qPow()
1616

17+
// QDoubleSpinBox that shows SI-style thin space thousands separators
18+
class AmountSpinBox: public QDoubleSpinBox
19+
{
20+
public:
21+
explicit AmountSpinBox(QWidget *parent):
22+
QDoubleSpinBox(parent)
23+
{
24+
}
25+
QString textFromValue(double value) const
26+
{
27+
QStringList parts = QDoubleSpinBox::textFromValue(value).split(".");
28+
QString quotient_str = parts[0];
29+
QString remainder_str;
30+
if(parts.size() > 1)
31+
remainder_str = parts[1];
32+
33+
// Code duplication between here and BitcoinUnits::format
34+
// TODO: Figure out how to share this code
35+
QChar thin_sp(THIN_SP_CP);
36+
int q_size = quotient_str.size();
37+
if (q_size > 4)
38+
for (int i = 3; i < q_size; i += 3)
39+
quotient_str.insert(q_size - i, thin_sp);
40+
41+
int r_size = remainder_str.size();
42+
if (r_size > 4)
43+
for (int i = 3, adj = 0; i < r_size; i += 3, adj++)
44+
remainder_str.insert(i + adj, thin_sp);
45+
46+
if(remainder_str.isEmpty())
47+
return quotient_str;
48+
else
49+
return quotient_str + QString(".") + remainder_str;
50+
}
51+
QValidator::State validate (QString &text, int &pos) const
52+
{
53+
QString s(BitcoinUnits::removeSpaces(text));
54+
return QDoubleSpinBox::validate(s, pos);
55+
}
56+
double valueFromText(const QString& text) const
57+
{
58+
return QDoubleSpinBox::valueFromText(BitcoinUnits::removeSpaces(text));
59+
}
60+
};
61+
1762
BitcoinAmountField::BitcoinAmountField(QWidget *parent) :
1863
QWidget(parent),
1964
amount(0),
2065
currentUnit(-1)
2166
{
2267
nSingleStep = 100000; // satoshis
2368

24-
amount = new QDoubleSpinBox(this);
69+
amount = new AmountSpinBox(this);
2570
amount->setLocale(QLocale::c());
2671
amount->installEventFilter(this);
2772
amount->setMaximumWidth(170);
@@ -52,7 +97,7 @@ void BitcoinAmountField::setText(const QString &text)
5297
if (text.isEmpty())
5398
amount->clear();
5499
else
55-
amount->setValue(text.toDouble());
100+
amount->setValue(BitcoinUnits::removeSpaces(text).toDouble());
56101
}
57102

58103
void BitcoinAmountField::clear()

src/qt/bitcoinunits.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ QString BitcoinUnits::description(int unit)
5050
switch(unit)
5151
{
5252
case BTC: return QString("Bitcoins");
53-
case mBTC: return QString("Milli-Bitcoins (1 / 1,000)");
54-
case uBTC: return QString("Micro-Bitcoins (1 / 1,000,000)");
53+
case mBTC: return QString("Milli-Bitcoins (1 / 1" THIN_SP_UTF8 "000)");
54+
case uBTC: return QString("Micro-Bitcoins (1 / 1" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
5555
default: return QString("???");
5656
}
5757
}
@@ -100,7 +100,7 @@ int BitcoinUnits::decimals(int unit)
100100
}
101101
}
102102

103-
QString BitcoinUnits::format(int unit, qint64 n, bool fPlus)
103+
QString BitcoinUnits::format(int unit, qint64 n, bool fPlus, SeparatorStyle separators, bool fAlign)
104104
{
105105
// Note: not using straight sprintf here because we do NOT want
106106
// localized number formatting.
@@ -119,6 +119,23 @@ QString BitcoinUnits::format(int unit, qint64 n, bool fPlus)
119119
for (int i = remainder_str.size()-1; i>=2 && (remainder_str.at(i) == '0'); --i)
120120
++nTrim;
121121
remainder_str.chop(nTrim);
122+
if (fAlign)
123+
remainder_str.append(QString(QChar(FIGURE_SP_CP)).repeated(nTrim));
124+
125+
// Use SI-stule separators as these are locale indendent and can't be
126+
// confused with the decimal marker. Rule is to use a thin space every
127+
// three digits on *both* sides of the decimal point - but only if there
128+
// are five or more digits
129+
QChar thin_sp(THIN_SP_CP);
130+
int q_size = quotient_str.size();
131+
if (separators == separatorAlways || (separators == separatorStandard && q_size > 4))
132+
for (int i = 3; i < q_size; i += 3)
133+
quotient_str.insert(q_size - i, thin_sp);
134+
135+
int r_size = remainder_str.size();
136+
if (separators == separatorAlways || (separators == separatorStandard && r_size > 4))
137+
for (int i = 3, adj = 0; i < r_size ; i += 3, adj++)
138+
remainder_str.insert(i + adj, thin_sp);
122139

123140
if (n < 0)
124141
quotient_str.insert(0, '-');
@@ -127,17 +144,27 @@ QString BitcoinUnits::format(int unit, qint64 n, bool fPlus)
127144
return quotient_str + QString(".") + remainder_str;
128145
}
129146

130-
QString BitcoinUnits::formatWithUnit(int unit, qint64 amount, bool plussign)
147+
QString BitcoinUnits::formatWithUnit(int unit, qint64 amount, bool plussign, SeparatorStyle separators, bool fAlign)
131148
{
132-
return format(unit, amount, plussign) + QString(" ") + name(unit);
149+
return format(unit, amount, plussign, separators, fAlign) + QString(" ") + name(unit);
133150
}
134151

152+
QString BitcoinUnits::formatHtmlWithUnit(int unit, qint64 amount, bool plussign, SeparatorStyle separators)
153+
{
154+
QString str(formatWithUnit(unit, amount, plussign, separators));
155+
str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML));
156+
return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
157+
}
158+
159+
135160
bool BitcoinUnits::parse(int unit, const QString &value, qint64 *val_out)
136161
{
137162
if(!valid(unit) || value.isEmpty())
138163
return false; // Refuse to parse invalid unit or empty string
139164
int num_decimals = decimals(unit);
140-
QStringList parts = value.split(".");
165+
166+
// Ignore spaces and thin spaces when parsing
167+
QStringList parts = removeSpaces(value).split(".");
141168

142169
if(parts.size() > 2)
143170
{

src/qt/bitcoinunits.h

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,37 @@
88
#include <QAbstractListModel>
99
#include <QString>
1010

11+
// U+2009 THIN SPACE = UTF-8 E2 80 89
12+
#define REAL_THIN_SP_CP 0x2009
13+
#define REAL_THIN_SP_UTF8 "\xE2\x80\x89"
14+
#define REAL_THIN_SP_HTML "&thinsp;"
15+
16+
// U+200A HAIR SPACE = UTF-8 E2 80 8A
17+
#define HAIR_SP_CP 0x200A
18+
#define HAIR_SP_UTF8 "\xE2\x80\x8A"
19+
#define HAIR_SP_HTML "&#8202;"
20+
21+
// U+2006 SIX-PER-EM SPACE = UTF-8 E2 80 86
22+
#define SIXPEREM_SP_CP 0x2006
23+
#define SIXPEREM_SP_UTF8 "\xE2\x80\x86"
24+
#define SIXPEREM_SP_HTML "&#8198;"
25+
26+
// U+2007 FIGURE SPACE = UTF-8 E2 80 87
27+
#define FIGURE_SP_CP 0x2007
28+
#define FIGURE_SP_UTF8 "\xE2\x80\x87"
29+
#define FIGURE_SP_HTML "&#8199;"
30+
31+
// QMessageBox seems to have a bug whereby it doesn't display thin/hair spaces
32+
// correctly. Workaround is to display a space in a small font. If you
33+
// change this, please test that it doesn't cause the parent span to start
34+
// wrapping.
35+
#define HTML_HACK_SP "<span style='white-space: nowrap; font-size: 6pt'> </span>"
36+
37+
// Define THIN_SP_* variables to be our preferred type of thin space
38+
#define THIN_SP_CP REAL_THIN_SP_CP
39+
#define THIN_SP_UTF8 REAL_THIN_SP_UTF8
40+
#define THIN_SP_HTML HTML_HACK_SP
41+
1142
/** Bitcoin unit definitions. Encapsulates parsing and formatting
1243
and serves as list model for drop-down selection boxes.
1344
*/
@@ -28,6 +59,13 @@ class BitcoinUnits: public QAbstractListModel
2859
uBTC
2960
};
3061

62+
enum SeparatorStyle
63+
{
64+
separatorNever,
65+
separatorStandard,
66+
separatorAlways
67+
};
68+
3169
//! @name Static API
3270
//! Unit conversion and formatting
3371
///@{
@@ -49,9 +87,10 @@ class BitcoinUnits: public QAbstractListModel
4987
//! Number of decimals left
5088
static int decimals(int unit);
5189
//! Format as string
52-
static QString format(int unit, qint64 amount, bool plussign=false);
90+
static QString format(int unit, qint64 amount, bool plussign=false, SeparatorStyle separators=separatorStandard, bool fAlign=false);
5391
//! Format as string (with unit)
54-
static QString formatWithUnit(int unit, qint64 amount, bool plussign=false);
92+
static QString formatWithUnit(int unit, qint64 amount, bool plussign=false, SeparatorStyle separators=separatorStandard, bool fAlign=false);
93+
static QString formatHtmlWithUnit(int unit, qint64 amount, bool plussign=false, SeparatorStyle separators=separatorStandard);
5594
//! Parse string to coin amount
5695
static bool parse(int unit, const QString &value, qint64 *val_out);
5796
///@}
@@ -67,6 +106,16 @@ class BitcoinUnits: public QAbstractListModel
67106
QVariant data(const QModelIndex &index, int role) const;
68107
///@}
69108

109+
static QString removeSpaces(QString text)
110+
{
111+
text.remove(' ');
112+
text.remove(QChar(THIN_SP_CP));
113+
#if (THIN_SP_CP != REAL_THIN_SP_CP)
114+
text.remove(QChar(REAL_THIN_SP_CP));
115+
#endif
116+
return text;
117+
}
118+
70119
private:
71120
QList<BitcoinUnits::Unit> unitlist;
72121
};

src/qt/overviewpage.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class TxViewDelegate : public QAbstractItemDelegate
7272
foreground = option.palette.color(QPalette::Text);
7373
}
7474
painter->setPen(foreground);
75-
QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true);
75+
QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true, BitcoinUnits::separatorAlways, true);
7676
if(!confirmed)
7777
{
7878
amountText = QString("[") + amountText + QString("]");
@@ -141,10 +141,10 @@ void OverviewPage::setBalance(qint64 balance, qint64 unconfirmedBalance, qint64
141141
currentBalance = balance;
142142
currentUnconfirmedBalance = unconfirmedBalance;
143143
currentImmatureBalance = immatureBalance;
144-
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
145-
ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, unconfirmedBalance));
146-
ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, immatureBalance));
147-
ui->labelTotal->setText(BitcoinUnits::formatWithUnit(unit, balance + unconfirmedBalance + immatureBalance));
144+
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance, false, BitcoinUnits::separatorAlways, true));
145+
ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, unconfirmedBalance, false, BitcoinUnits::separatorAlways, true));
146+
ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, immatureBalance, false, BitcoinUnits::separatorAlways, true));
147+
ui->labelTotal->setText(BitcoinUnits::formatWithUnit(unit, balance + unconfirmedBalance + immatureBalance, false, BitcoinUnits::separatorAlways, true));
148148

149149
// only show immature (newly mined) balance if it's non-zero, so as not to complicate things
150150
// for the non-mining users

src/qt/sendcoinsdialog.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void SendCoinsDialog::on_sendButton_clicked()
142142
foreach(const SendCoinsRecipient &rcp, recipients)
143143
{
144144
// generate bold amount string
145-
QString amount = "<b>" + BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), rcp.amount);
145+
QString amount = "<b>" + BitcoinUnits::formatHtmlWithUnit(model->getOptionsModel()->getDisplayUnit(), rcp.amount);
146146
amount.append("</b>");
147147
// generate monospace address string
148148
QString address = "<span style='font-family: monospace;'>" + rcp.address;
@@ -210,7 +210,7 @@ void SendCoinsDialog::on_sendButton_clicked()
210210
{
211211
// append fee string if a fee is required
212212
questionString.append("<hr /><span style='color:#aa0000;'>");
213-
questionString.append(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), txFee));
213+
questionString.append(BitcoinUnits::formatHtmlWithUnit(model->getOptionsModel()->getDisplayUnit(), txFee));
214214
questionString.append("</span> ");
215215
questionString.append(tr("added as transaction fee"));
216216
}
@@ -222,10 +222,10 @@ void SendCoinsDialog::on_sendButton_clicked()
222222
foreach(BitcoinUnits::Unit u, BitcoinUnits::availableUnits())
223223
{
224224
if(u != model->getOptionsModel()->getDisplayUnit())
225-
alternativeUnits.append(BitcoinUnits::formatWithUnit(u, totalAmount));
225+
alternativeUnits.append(BitcoinUnits::formatHtmlWithUnit(u, totalAmount));
226226
}
227227
questionString.append(tr("Total Amount %1 (= %2)")
228-
.arg(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), totalAmount))
228+
.arg(BitcoinUnits::formatHtmlWithUnit(model->getOptionsModel()->getDisplayUnit(), totalAmount))
229229
.arg(alternativeUnits.join(" " + tr("or") + " ")));
230230

231231
QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),

src/qt/transactiondesc.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, int vout, int u
138138
nUnmatured += wallet->GetCredit(txout);
139139
strHTML += "<b>" + tr("Credit") + ":</b> ";
140140
if (wtx.IsInMainChain())
141-
strHTML += BitcoinUnits::formatWithUnit(unit, nUnmatured)+ " (" + tr("matures in %n more block(s)", "", wtx.GetBlocksToMaturity()) + ")";
141+
strHTML += BitcoinUnits::formatHtmlWithUnit(unit, nUnmatured)+ " (" + tr("matures in %n more block(s)", "", wtx.GetBlocksToMaturity()) + ")";
142142
else
143143
strHTML += "(" + tr("not accepted") + ")";
144144
strHTML += "<br>";
@@ -148,7 +148,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, int vout, int u
148148
//
149149
// Credit
150150
//
151-
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatWithUnit(unit, nNet) + "<br>";
151+
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, nNet) + "<br>";
152152
}
153153
else
154154
{
@@ -184,21 +184,21 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, int vout, int u
184184
}
185185
}
186186

187-
strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatWithUnit(unit, -txout.nValue) + "<br>";
187+
strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -txout.nValue) + "<br>";
188188
}
189189

190190
if (fAllToMe)
191191
{
192192
// Payment to self
193193
int64_t nChange = wtx.GetChange();
194194
int64_t nValue = nCredit - nChange;
195-
strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatWithUnit(unit, -nValue) + "<br>";
196-
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatWithUnit(unit, nValue) + "<br>";
195+
strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -nValue) + "<br>";
196+
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, nValue) + "<br>";
197197
}
198198

199199
int64_t nTxFee = nDebit - wtx.GetValueOut();
200200
if (nTxFee > 0)
201-
strHTML += "<b>" + tr("Transaction fee") + ":</b> " + BitcoinUnits::formatWithUnit(unit, -nTxFee) + "<br>";
201+
strHTML += "<b>" + tr("Transaction fee") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -nTxFee) + "<br>";
202202
}
203203
else
204204
{
@@ -207,14 +207,14 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, int vout, int u
207207
//
208208
BOOST_FOREACH(const CTxIn& txin, wtx.vin)
209209
if (wallet->IsMine(txin))
210-
strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatWithUnit(unit, -wallet->GetDebit(txin)) + "<br>";
210+
strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -wallet->GetDebit(txin)) + "<br>";
211211
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
212212
if (wallet->IsMine(txout))
213-
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatWithUnit(unit, wallet->GetCredit(txout)) + "<br>";
213+
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, wallet->GetCredit(txout)) + "<br>";
214214
}
215215
}
216216

217-
strHTML += "<b>" + tr("Net amount") + ":</b> " + BitcoinUnits::formatWithUnit(unit, nNet, true) + "<br>";
217+
strHTML += "<b>" + tr("Net amount") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, nNet, true) + "<br>";
218218

219219
//
220220
// Message
@@ -260,10 +260,10 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, int vout, int u
260260
strHTML += "<hr><br>" + tr("Debug information") + "<br><br>";
261261
BOOST_FOREACH(const CTxIn& txin, wtx.vin)
262262
if(wallet->IsMine(txin))
263-
strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatWithUnit(unit, -wallet->GetDebit(txin)) + "<br>";
263+
strHTML += "<b>" + tr("Debit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, -wallet->GetDebit(txin)) + "<br>";
264264
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
265265
if(wallet->IsMine(txout))
266-
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatWithUnit(unit, wallet->GetCredit(txout)) + "<br>";
266+
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatHtmlWithUnit(unit, wallet->GetCredit(txout)) + "<br>";
267267

268268
strHTML += "<br><b>" + tr("Transaction") + ":</b><br>";
269269
strHTML += GUIUtil::HtmlEscape(wtx.ToString(), true);
@@ -289,7 +289,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, int vout, int u
289289
strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + " ";
290290
strHTML += QString::fromStdString(CBitcoinAddress(address).ToString());
291291
}
292-
strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatWithUnit(unit, vout.nValue);
292+
strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatHtmlWithUnit(unit, vout.nValue);
293293
strHTML = strHTML + " IsMine=" + (wallet->IsMine(vout) ? tr("true") : tr("false")) + "</li>";
294294
}
295295
}

0 commit comments

Comments
 (0)