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