1 // Copyright (c) 2011-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include <qt/bitcoinunits.h>
6
7 #include <QStringList>
8
9 #include <cassert>
10
11 static constexpr auto MAX_DIGITS_BTC = 16;
12
BitcoinUnits(QObject * parent)13 BitcoinUnits::BitcoinUnits(QObject *parent):
14 QAbstractListModel(parent),
15 unitlist(availableUnits())
16 {
17 }
18
availableUnits()19 QList<BitcoinUnits::Unit> BitcoinUnits::availableUnits()
20 {
21 QList<BitcoinUnits::Unit> unitlist;
22 unitlist.append(BTC);
23 unitlist.append(mBTC);
24 unitlist.append(uBTC);
25 unitlist.append(SAT);
26 return unitlist;
27 }
28
valid(int unit)29 bool BitcoinUnits::valid(int unit)
30 {
31 switch(unit)
32 {
33 case BTC:
34 case mBTC:
35 case uBTC:
36 case SAT:
37 return true;
38 default:
39 return false;
40 }
41 }
42
longName(int unit)43 QString BitcoinUnits::longName(int unit)
44 {
45 switch(unit)
46 {
47 case BTC: return QString("BTC");
48 case mBTC: return QString("mBTC");
49 case uBTC: return QString::fromUtf8("µBTC (bits)");
50 case SAT: return QString("Satoshi (sat)");
51 default: return QString("???");
52 }
53 }
54
shortName(int unit)55 QString BitcoinUnits::shortName(int unit)
56 {
57 switch(unit)
58 {
59 case uBTC: return QString::fromUtf8("bits");
60 case SAT: return QString("sat");
61 default: return longName(unit);
62 }
63 }
64
description(int unit)65 QString BitcoinUnits::description(int unit)
66 {
67 switch(unit)
68 {
69 case BTC: return QString("Bitcoins");
70 case mBTC: return QString("Milli-Bitcoins (1 / 1" THIN_SP_UTF8 "000)");
71 case uBTC: return QString("Micro-Bitcoins (bits) (1 / 1" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
72 case SAT: return QString("Satoshi (sat) (1 / 100" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
73 default: return QString("???");
74 }
75 }
76
factor(int unit)77 qint64 BitcoinUnits::factor(int unit)
78 {
79 switch(unit)
80 {
81 case BTC: return 100000000;
82 case mBTC: return 100000;
83 case uBTC: return 100;
84 case SAT: return 1;
85 default: return 100000000;
86 }
87 }
88
decimals(int unit)89 int BitcoinUnits::decimals(int unit)
90 {
91 switch(unit)
92 {
93 case BTC: return 8;
94 case mBTC: return 5;
95 case uBTC: return 2;
96 case SAT: return 0;
97 default: return 0;
98 }
99 }
100
format(int unit,const CAmount & nIn,bool fPlus,SeparatorStyle separators,bool justify)101 QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators, bool justify)
102 {
103 // Note: not using straight sprintf here because we do NOT want
104 // localized number formatting.
105 if(!valid(unit))
106 return QString(); // Refuse to format invalid unit
107 qint64 n = (qint64)nIn;
108 qint64 coin = factor(unit);
109 int num_decimals = decimals(unit);
110 qint64 n_abs = (n > 0 ? n : -n);
111 qint64 quotient = n_abs / coin;
112 QString quotient_str = QString::number(quotient);
113 if (justify) {
114 quotient_str = quotient_str.rightJustified(MAX_DIGITS_BTC - num_decimals, ' ');
115 }
116
117 // Use SI-style thin space separators as these are locale independent and can't be
118 // confused with the decimal marker.
119 QChar thin_sp(THIN_SP_CP);
120 int q_size = quotient_str.size();
121 if (separators == SeparatorStyle::ALWAYS || (separators == SeparatorStyle::STANDARD && q_size > 4))
122 for (int i = 3; i < q_size; i += 3)
123 quotient_str.insert(q_size - i, thin_sp);
124
125 if (n < 0)
126 quotient_str.insert(0, '-');
127 else if (fPlus && n > 0)
128 quotient_str.insert(0, '+');
129
130 if (num_decimals > 0) {
131 qint64 remainder = n_abs % coin;
132 QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
133 return quotient_str + QString(".") + remainder_str;
134 } else {
135 return quotient_str;
136 }
137 }
138
139
140 // NOTE: Using formatWithUnit in an HTML context risks wrapping
141 // quantities at the thousands separator. More subtly, it also results
142 // in a standard space rather than a thin space, due to a bug in Qt's
143 // XML whitespace canonicalisation
144 //
145 // Please take care to use formatHtmlWithUnit instead, when
146 // appropriate.
147
formatWithUnit(int unit,const CAmount & amount,bool plussign,SeparatorStyle separators)148 QString BitcoinUnits::formatWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
149 {
150 return format(unit, amount, plussign, separators) + QString(" ") + shortName(unit);
151 }
152
formatHtmlWithUnit(int unit,const CAmount & amount,bool plussign,SeparatorStyle separators)153 QString BitcoinUnits::formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
154 {
155 QString str(formatWithUnit(unit, amount, plussign, separators));
156 str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML));
157 return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
158 }
159
formatWithPrivacy(int unit,const CAmount & amount,SeparatorStyle separators,bool privacy)160 QString BitcoinUnits::formatWithPrivacy(int unit, const CAmount& amount, SeparatorStyle separators, bool privacy)
161 {
162 assert(amount >= 0);
163 QString value;
164 if (privacy) {
165 value = format(unit, 0, false, separators, true).replace('0', '#');
166 } else {
167 value = format(unit, amount, false, separators, true);
168 }
169 return value + QString(" ") + shortName(unit);
170 }
171
parse(int unit,const QString & value,CAmount * val_out)172 bool BitcoinUnits::parse(int unit, const QString &value, CAmount *val_out)
173 {
174 if(!valid(unit) || value.isEmpty())
175 return false; // Refuse to parse invalid unit or empty string
176 int num_decimals = decimals(unit);
177
178 // Ignore spaces and thin spaces when parsing
179 QStringList parts = removeSpaces(value).split(".");
180
181 if(parts.size() > 2)
182 {
183 return false; // More than one dot
184 }
185 QString whole = parts[0];
186 QString decimals;
187
188 if(parts.size() > 1)
189 {
190 decimals = parts[1];
191 }
192 if(decimals.size() > num_decimals)
193 {
194 return false; // Exceeds max precision
195 }
196 bool ok = false;
197 QString str = whole + decimals.leftJustified(num_decimals, '0');
198
199 if(str.size() > 18)
200 {
201 return false; // Longer numbers will exceed 63 bits
202 }
203 CAmount retvalue(str.toLongLong(&ok));
204 if(val_out)
205 {
206 *val_out = retvalue;
207 }
208 return ok;
209 }
210
getAmountColumnTitle(int unit)211 QString BitcoinUnits::getAmountColumnTitle(int unit)
212 {
213 QString amountTitle = QObject::tr("Amount");
214 if (BitcoinUnits::valid(unit))
215 {
216 amountTitle += " ("+BitcoinUnits::shortName(unit) + ")";
217 }
218 return amountTitle;
219 }
220
rowCount(const QModelIndex & parent) const221 int BitcoinUnits::rowCount(const QModelIndex &parent) const
222 {
223 Q_UNUSED(parent);
224 return unitlist.size();
225 }
226
data(const QModelIndex & index,int role) const227 QVariant BitcoinUnits::data(const QModelIndex &index, int role) const
228 {
229 int row = index.row();
230 if(row >= 0 && row < unitlist.size())
231 {
232 Unit unit = unitlist.at(row);
233 switch(role)
234 {
235 case Qt::EditRole:
236 case Qt::DisplayRole:
237 return QVariant(longName(unit));
238 case Qt::ToolTipRole:
239 return QVariant(description(unit));
240 case UnitRole:
241 return QVariant(static_cast<int>(unit));
242 }
243 }
244 return QVariant();
245 }
246
maxMoney()247 CAmount BitcoinUnits::maxMoney()
248 {
249 return MAX_MONEY;
250 }
251