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