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